|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "runtime" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestGetVersion(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + inputGitCommit string |
| 14 | + inputGitTag string |
| 15 | + inputTreeState string |
| 16 | + inputVersion string |
| 17 | + expected string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Official release with tag and clean state", |
| 21 | + inputGitCommit: "abcdef123456", |
| 22 | + inputGitTag: "v1.2.3", |
| 23 | + inputTreeState: "clean", |
| 24 | + inputVersion: "1.2.3", |
| 25 | + expected: "v1.2.3", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "Dirty state with commit", |
| 29 | + inputGitCommit: "deadbeefcafebabe", |
| 30 | + inputGitTag: "", |
| 31 | + inputTreeState: "dirty", |
| 32 | + inputVersion: "2.0.1", |
| 33 | + expected: "v2.0.1+deadbee.dirty", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "Clean state with commit, no tag", |
| 37 | + inputGitCommit: "cafebabedeadbeef", |
| 38 | + inputGitTag: "", |
| 39 | + inputTreeState: "clean", |
| 40 | + inputVersion: "2.1.0", |
| 41 | + expected: "v2.1.0+cafebab", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Missing commit and tag", |
| 45 | + inputGitCommit: "", |
| 46 | + inputGitTag: "", |
| 47 | + inputTreeState: "clean", |
| 48 | + inputVersion: "3.1.0", |
| 49 | + expected: "v3.1.0+unknown", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "Short commit", |
| 53 | + inputGitCommit: "abc", |
| 54 | + inputGitTag: "", |
| 55 | + inputTreeState: "clean", |
| 56 | + inputVersion: "4.0.0", |
| 57 | + expected: "v4.0.0+unknown", |
| 58 | + }, |
| 59 | + } |
| 60 | + for _, tt := range tests { |
| 61 | + gitCommit = tt.inputGitCommit |
| 62 | + gitTag = tt.inputGitTag |
| 63 | + gitTreeState = tt.inputTreeState |
| 64 | + version = tt.inputVersion |
| 65 | + |
| 66 | + buildDate = "2025-06-26" |
| 67 | + kubectlVersion = "v1.30.0" |
| 68 | + extraBuildInfo = "test-build" |
| 69 | + |
| 70 | + got := GetVersion() |
| 71 | + assert.Equal(t, tt.expected, got.Version) |
| 72 | + assert.Equal(t, buildDate, got.BuildDate) |
| 73 | + assert.Equal(t, tt.inputGitCommit, got.GitCommit) |
| 74 | + assert.Equal(t, tt.inputGitTag, got.GitTag) |
| 75 | + assert.Equal(t, tt.inputTreeState, got.GitTreeState) |
| 76 | + assert.Equal(t, runtime.Version(), got.GoVersion) |
| 77 | + assert.Equal(t, runtime.Compiler, got.Compiler) |
| 78 | + assert.Equal(t, runtime.GOOS+"/"+runtime.GOARCH, got.Platform) |
| 79 | + assert.Equal(t, kubectlVersion, got.KubectlVersion) |
| 80 | + assert.Equal(t, extraBuildInfo, got.ExtraBuildInfo) |
| 81 | + } |
| 82 | +} |
0 commit comments