66 "regexp"
77 "runtime"
88 "testing"
9-
10- "golang.org/x/mod/modfile"
119)
1210
1311func TestDockerfileVersionsMatchGoMod (t * testing.T ) {
@@ -22,48 +20,15 @@ func TestDockerfileVersionsMatchGoMod(t *testing.T) {
2220 }
2321 dockerfile := string (dockerfileBytes )
2422
25- gotGoVersion := mustMatch1 (t , dockerfile , `(?m)^ARG GO_VERSION=([^\s]+)\s*$` , "Dockerfile ARG GO_VERSION" )
26- gotHelmVersion := mustMatch1 (t , dockerfile , `(?m)^ENV HELM_VERSION=([^\s]+)\s*$` , "Dockerfile ENV HELM_VERSION" )
27-
28- goModPath := filepath .Join (rootDir , "go.mod" )
29- goModBytes , err := os .ReadFile (goModPath )
30- if err != nil {
31- t .Fatalf ("read go.mod: %v" , err )
32- }
33-
34- parsed , err := modfile .Parse (goModPath , goModBytes , nil )
35- if err != nil {
36- t .Fatalf ("parse go.mod: %v" , err )
23+ // Go and Helm versions are extracted directly from go.mod at Docker build
24+ // time, so there are no hardcoded values to drift. Verify the Dockerfile
25+ // does NOT contain stale hardcoded versions.
26+ if regexp .MustCompile (`(?m)^ARG GO_VERSION=` ).FindStringIndex (dockerfile ) != nil {
27+ t .Fatalf ("Dockerfile should not hardcode ARG GO_VERSION; the Go version is derived from go.mod at build time" )
3728 }
38- if parsed .Go == nil || parsed .Go .Version == "" {
39- t .Fatalf ("go.mod is missing a go version directive" )
40- }
41-
42- wantGoVersion := parsed .Go .Version
43- wantHelmVersion := requireVersion (t , parsed , "helm.sh/helm/v3" )
44- wantKindVersion := requireVersion (t , parsed , "sigs.k8s.io/kind" )
45-
46- makefilePath := filepath .Join (rootDir , "Makefile" )
47- makefileBytes , err := os .ReadFile (makefilePath )
48- if err != nil {
49- t .Fatalf ("read Makefile: %v" , err )
29+ if regexp .MustCompile (`(?m)^ENV HELM_VERSION=` ).FindStringIndex (dockerfile ) != nil {
30+ t .Fatalf ("Dockerfile should not hardcode ENV HELM_VERSION; the Helm version is derived from go.mod at build time" )
5031 }
51- makefile := string (makefileBytes )
52- gotKindVersion := mustMatch1 (t , makefile , `(?m)^KIND_VERSION\s*\?=\s*([^\s]+)\s*$` , "Makefile KIND_VERSION" )
53-
54- t .Run ("go" , func (t * testing.T ) {
55- t .Parallel ()
56- if gotGoVersion != wantGoVersion {
57- t .Fatalf ("GO_VERSION drift detected: Dockerfile has %q, go.mod has %q" , gotGoVersion , wantGoVersion )
58- }
59- })
60-
61- t .Run ("helm" , func (t * testing.T ) {
62- t .Parallel ()
63- if gotHelmVersion != wantHelmVersion {
64- t .Fatalf ("HELM_VERSION drift detected: Dockerfile has %q, go.mod helm.sh/helm/v3 is %q" , gotHelmVersion , wantHelmVersion )
65- }
66- })
6732
6833 t .Run ("kind" , func (t * testing.T ) {
6934 t .Parallel ()
@@ -77,8 +42,16 @@ func TestDockerfileVersionsMatchGoMod(t *testing.T) {
7742 t .Fatalf ("KIND_VERSION drift risk detected: Dockerfile should not download kind via curl" )
7843 }
7944
80- if gotKindVersion != wantKindVersion {
81- t .Fatalf ("KIND_VERSION drift detected: Makefile has %q, go.mod sigs.k8s.io/kind is %q" , gotKindVersion , wantKindVersion )
45+ // KIND_VERSION in the Makefile is derived from go.mod at make time,
46+ // so there is no hardcoded literal to drift. Verify it stays dynamic.
47+ makefilePath := filepath .Join (rootDir , "Makefile" )
48+ makefileBytes , err := os .ReadFile (makefilePath )
49+ if err != nil {
50+ t .Fatalf ("read Makefile: %v" , err )
51+ }
52+ makefile := string (makefileBytes )
53+ if regexp .MustCompile (`(?m)^KIND_VERSION\s*\?=\s*v[\d.]+\s*$` ).FindStringIndex (makefile ) != nil {
54+ t .Fatalf ("KIND_VERSION drift risk detected: Makefile should derive KIND_VERSION from go.mod, not hardcode it" )
8255 }
8356 })
8457}
@@ -113,27 +86,3 @@ func fileExists(path string) bool {
11386 _ , err := os .Stat (path )
11487 return err == nil
11588}
116-
117- func mustMatch1 (t * testing.T , content , pattern , label string ) string {
118- t .Helper ()
119-
120- re := regexp .MustCompile (pattern )
121- m := re .FindStringSubmatch (content )
122- if len (m ) != 2 {
123- t .Fatalf ("%s not found (pattern %q)" , label , pattern )
124- }
125- return m [1 ]
126- }
127-
128- func requireVersion (t * testing.T , mf * modfile.File , modulePath string ) string {
129- t .Helper ()
130-
131- for _ , req := range mf .Require {
132- if req .Mod .Path == modulePath {
133- return req .Mod .Version
134- }
135- }
136-
137- t .Fatalf ("go.mod is missing required module %q" , modulePath )
138- return ""
139- }
0 commit comments