- module path is now gitea.home.hrajfrisbee.cz/kacerr/gatewayapi-certificates-operator - internal/version resolves the commit from ldflags or Go build info; --version prints it and the first log line carries it - --metrics-bind-address is passed to the manager instead of being dropped - Dockerfile takes GIT_COMMIT, honours TARGETARCH, labels the image revision Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package version
|
|
|
|
import (
|
|
"runtime/debug"
|
|
"testing"
|
|
)
|
|
|
|
func buildInfoWith(settings ...debug.BuildSetting) func() (*debug.BuildInfo, bool) {
|
|
return func() (*debug.BuildInfo, bool) {
|
|
return &debug.BuildInfo{Settings: settings}, true
|
|
}
|
|
}
|
|
|
|
func TestResolve_precedenceAndFallback(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
noBuildInfo := func() (*debug.BuildInfo, bool) { return nil, false }
|
|
|
|
tests := []struct {
|
|
name string
|
|
ldflagsCommit string
|
|
readBuildInfo func() (*debug.BuildInfo, bool)
|
|
want string
|
|
}{
|
|
{
|
|
name: "ldflags stamp wins over build info",
|
|
ldflagsCommit: "abc123def456-dirty",
|
|
readBuildInfo: buildInfoWith(debug.BuildSetting{Key: "vcs.revision", Value: "ffffffffffffffffffffffffffffffffffffffff"}),
|
|
want: "abc123def456-dirty",
|
|
},
|
|
{
|
|
name: "no stamp, no build info",
|
|
readBuildInfo: noBuildInfo,
|
|
want: "unknown",
|
|
},
|
|
{
|
|
name: "build info without vcs settings",
|
|
readBuildInfo: buildInfoWith(),
|
|
want: "unknown",
|
|
},
|
|
{
|
|
name: "full revision truncated to 12 chars",
|
|
readBuildInfo: buildInfoWith(
|
|
debug.BuildSetting{Key: "vcs.revision", Value: "0123456789abcdef0123456789abcdef01234567"},
|
|
debug.BuildSetting{Key: "vcs.modified", Value: "false"},
|
|
),
|
|
want: "0123456789ab",
|
|
},
|
|
{
|
|
name: "modified tree gets dirty suffix",
|
|
readBuildInfo: buildInfoWith(
|
|
debug.BuildSetting{Key: "vcs.revision", Value: "0123456789abcdef0123456789abcdef01234567"},
|
|
debug.BuildSetting{Key: "vcs.modified", Value: "true"},
|
|
),
|
|
want: "0123456789ab-dirty",
|
|
},
|
|
{
|
|
name: "short revision kept as-is",
|
|
readBuildInfo: buildInfoWith(
|
|
debug.BuildSetting{Key: "vcs.revision", Value: "abc123"},
|
|
),
|
|
want: "abc123",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
if got := resolve(tc.ldflagsCommit, tc.readBuildInfo); got != tc.want {
|
|
t.Errorf("resolve() = %q, want %q", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|