File: provenance_test.go

package info (click to toggle)
golang-k8s-sigs-kustomize-api 0.19.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,732 kB
  • sloc: makefile: 206; sh: 67
file content (104 lines) | stat: -rw-r--r-- 2,784 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2022 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package provenance_test

import (
	"fmt"
	"runtime/debug"
	"testing"

	"github.com/stretchr/testify/assert"
	"sigs.k8s.io/kustomize/api/provenance"
)

const expectedBuildDateFromLdFlag = "2023-01-31T23:38:41Z"
const expectedVersionFromLdFlag = "(test)"

func TestGetProvenance(t *testing.T) {
	p := provenance.GetProvenance()
	// These are set by ldflags in our Makefile
	assert.Equal(t, expectedVersionFromLdFlag, p.Version)
	assert.Equal(t, expectedBuildDateFromLdFlag, p.BuildDate)
	// This comes from BuildInfo, which is not set during go test: https://github.com/golang/go/issues/33976
	assert.Equal(t, "unknown", p.GitCommit)

	// These are set properly during go test
	assert.NotEmpty(t, p.GoArch)
	assert.NotEmpty(t, p.GoOs)
	assert.Contains(t, p.GoVersion, "go1.")
}

func TestProvenance_Short(t *testing.T) {
	p := provenance.GetProvenance()
	// The version not set during go test, so this comes from an ldflag: https://github.com/golang/go/issues/33976
	assert.Equal(t, fmt.Sprintf("{%s  %s   }", expectedVersionFromLdFlag, expectedBuildDateFromLdFlag), p.Short())

	p.Version = "kustomize/v4.11.12"
	assert.Equal(t, fmt.Sprintf("{kustomize/v4.11.12  %s   }", expectedBuildDateFromLdFlag), p.Short())
}

func TestProvenance_Semver(t *testing.T) {
	p := provenance.GetProvenance()
	// The version not set during go test
	assert.Equal(t, "(test)", p.Semver())

	p.Version = "kustomize/v4.11.12"
	assert.Equal(t, "v4.11.12", p.Semver())
}

func mockModule(version string) debug.Module {
	return debug.Module{
		Path:    "sigs.k8s.io/kustomize/kustomize/v5",
		Version: version,
		Replace: nil,
	}
}

func TestGetMostRecentTag(t *testing.T) {
	tests := []struct {
		name        string
		module      debug.Module
		isError     bool
		expectedTag string
	}{
		{
			name:        "Standard version",
			module:      mockModule("v1.2.3"),
			expectedTag: "v1.2.3",
		},
		{
			name:        "Pseudo-version with patch increment",
			module:      mockModule("v0.0.0-20210101010101-abcdefabcdef"),
			expectedTag: "v0.0.0",
		},
		{
			name:    "Invalid semver string",
			module:  mockModule("invalid-version"),
			isError: true,
		},
		{
			name:        "Valid semver with patch increment and pre-release info",
			module:      mockModule("v1.2.3-0.20210101010101-abcdefabcdef"),
			expectedTag: "v1.2.2",
		},
		{
			name:        "Valid semver no patch increment",
			module:      mockModule("v1.2.0"),
			expectedTag: "v1.2.0",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tag, err := provenance.GetMostRecentTag(tt.module)
			if err != nil {
				if !tt.isError {
					assert.NoError(t, err)
				}
			} else {
				assert.Equal(t, tt.expectedTag, tag)
			}
		})
	}
}