Skip to content

Commit

Permalink
Refactor pversion_test to use a test table
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuatcasey committed Aug 27, 2024
1 parent 94809ee commit f7f25a8
Showing 1 changed file with 110 additions and 103 deletions.
213 changes: 110 additions & 103 deletions internal/pversion/version_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 the Pinniped contributors. All Rights Reserved.
// Copyright 2023-2024 the Pinniped contributors. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package pversion
Expand All @@ -20,108 +20,115 @@ func TestGet(t *testing.T) {
readBuildInfo = debug.ReadBuildInfo
})

t.Run("when readBuildInfo() returns not ok", func(t *testing.T) {
gitVersion = ""
readBuildInfo = func() (info *debug.BuildInfo, ok bool) {
return nil, false
}
tests := []struct {
name string
gitVersion string
readBuildInfo func() (info *debug.BuildInfo, ok bool)
wantInfo apimachineryversion.Info
}{
{
name: "when readBuildInfo() returns not ok",
gitVersion: "",
readBuildInfo: func() (info *debug.BuildInfo, ok bool) {
return nil, false
},
wantInfo: apimachineryversion.Info{
Major: "0",
Minor: "0",
GitVersion: "v0.0.0",
GitTreeState: "dirty",
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
},
},
{
name: "when readBuildInfo() returns ok",
gitVersion: "9.8.7",
readBuildInfo: func() (info *debug.BuildInfo, ok bool) {
buildInfo := debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "revision-value"},
{Key: "vcs.time", Value: "time-value"},
{Key: "vcs.modified", Value: "anything but 'true'"},
{Key: "other", Value: "ignored"},
},
}
return &buildInfo, true
},
wantInfo: apimachineryversion.Info{
Major: "9",
Minor: "8",
GitVersion: "9.8.7",
GitCommit: "revision-value",
GitTreeState: "dirty",
BuildDate: "time-value",
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
},
},
{
name: "when readBuildInfo() returns ok but gitVersion is not provided",
gitVersion: "",
readBuildInfo: func() (info *debug.BuildInfo, ok bool) {
buildInfo := debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "384850953501b7d66d466b4ca4d13a81bc54a7c3"},
{Key: "vcs.time", Value: "time-value"},
{Key: "vcs.modified", Value: "anything but 'true'"},
{Key: "other", Value: "ignored"},
},
}
return &buildInfo, true
},
wantInfo: apimachineryversion.Info{
Major: "0",
Minor: "0",
GitVersion: "v0.0.0-38485095-dirty",
GitCommit: "384850953501b7d66d466b4ca4d13a81bc54a7c3",
GitTreeState: "dirty",
BuildDate: "time-value",
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
},
},
{
name: "when gitVersion is complex",
gitVersion: "v1.2.3-abc123",
readBuildInfo: func() (info *debug.BuildInfo, ok bool) {
buildInfo := debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "abc123"},
{Key: "vcs.time", Value: "time-value"},
{Key: "vcs.modified", Value: "false"},
{Key: "other", Value: "ignored"},
},
}
return &buildInfo, true
},
wantInfo: apimachineryversion.Info{
Major: "1",
Minor: "2",
GitVersion: "v1.2.3-abc123",
GitCommit: "abc123",
GitTreeState: "clean",
BuildDate: "time-value",
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
},
},
}

info := apimachineryversion.Info{
Major: "0",
Minor: "0",
GitVersion: "v0.0.0",
GitTreeState: "dirty",
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
require.Equal(t, info, Get())
})

t.Run("when readBuildInfo() returns ok", func(t *testing.T) {
gitVersion = "9.8.7"
readBuildInfo = func() (info *debug.BuildInfo, ok bool) {
buildInfo := debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "revision-value"},
{Key: "vcs.time", Value: "time-value"},
{Key: "vcs.modified", Value: "anything but 'true'"},
{Key: "other", Value: "ignored"},
},
}
return &buildInfo, true
}

expected := apimachineryversion.Info{
Major: "9",
Minor: "8",
GitVersion: "9.8.7",
GitCommit: "revision-value",
GitTreeState: "dirty",
BuildDate: "time-value",
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}

require.Equal(t, expected, Get())
})

t.Run("when readBuildInfo() returns ok but gitVersion is not provided", func(t *testing.T) {
gitVersion = ""
readBuildInfo = func() (info *debug.BuildInfo, ok bool) {
buildInfo := debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "384850953501b7d66d466b4ca4d13a81bc54a7c3"},
{Key: "vcs.time", Value: "time-value"},
{Key: "vcs.modified", Value: "anything but 'true'"},
{Key: "other", Value: "ignored"},
},
}
return &buildInfo, true
}
// These tests cannot be done in Parallel due to side effects
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
gitVersion = test.gitVersion
readBuildInfo = test.readBuildInfo

expected := apimachineryversion.Info{
Major: "0",
Minor: "0",
GitVersion: "v0.0.0-38485095-dirty",
GitCommit: "384850953501b7d66d466b4ca4d13a81bc54a7c3",
GitTreeState: "dirty",
BuildDate: "time-value",
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}

require.Equal(t, expected, Get())
})

t.Run("when gitVersion is complex", func(t *testing.T) {
gitVersion = "v1.2.3-abc123"
readBuildInfo = func() (info *debug.BuildInfo, ok bool) {
buildInfo := debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "abc123"},
{Key: "vcs.time", Value: "time-value"},
{Key: "vcs.modified", Value: "false"},
{Key: "other", Value: "ignored"},
},
}
return &buildInfo, true
}

expected := apimachineryversion.Info{
Major: "1",
Minor: "2",
GitVersion: "v1.2.3-abc123",
GitCommit: "abc123",
GitTreeState: "clean",
BuildDate: "time-value",
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}

require.Equal(t, expected, Get())
})
require.Equal(t, test.wantInfo, Get())
})
}
}

0 comments on commit f7f25a8

Please sign in to comment.