Skip to content

Commit 1c16978

Browse files
GODRIVER-3493 Simplify docker image lookup
1 parent 2803830 commit 1c16978

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

internal/test/compilecheck/compile_check_test.go

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ import (
2323
"golang.org/x/mod/semver"
2424
)
2525

26+
// This module is outside of the go workspace since testcontainers requires a
27+
// version of klauspost/compress not supported by the driver. Use GOWORK=off
28+
// when running these tests.
29+
2630
const minSupportedVersion = "1.18"
2731

2832
func TestCompileCheck(t *testing.T) {
2933
cwd, err := os.Getwd()
3034
require.NoError(t, err)
3135

32-
internalDir := filepath.Dir(filepath.Dir(filepath.Dir(cwd)))
36+
rootDir := filepath.Dir(filepath.Dir(filepath.Dir(cwd)))
3337

3438
versions, err := getDockerGolangImages()
3539
require.NoError(t, err)
@@ -45,7 +49,7 @@ func TestCompileCheck(t *testing.T) {
4549
Image: image,
4650
Cmd: []string{"tail", "-f", "/dev/null"},
4751
Mounts: []testcontainers.ContainerMount{
48-
testcontainers.BindMount(internalDir, "/workspace"),
52+
testcontainers.BindMount(rootDir, "/workspace"),
4953
},
5054
WorkingDir: "/workspace",
5155
Env: map[string]string{
@@ -79,9 +83,11 @@ func TestCompileCheck(t *testing.T) {
7983
}
8084

8185
func getDockerGolangImages() ([]string, error) {
82-
url := "https://hub.docker.com/v2/repositories/library/golang/tags?page_size=100"
86+
var url = "https://hub.docker.com/v2/repositories/library/golang/tags?page_size=100"
8387

8488
versionSet := map[string]bool{}
89+
versions := []string{}
90+
8591
for url != "" {
8692
resp, err := http.Get(url)
8793
if err != nil {
@@ -101,7 +107,6 @@ func getDockerGolangImages() ([]string, error) {
101107
return nil, err
102108
}
103109

104-
finished := false
105110
for _, tag := range data.Results {
106111
// Skip tags that don't start with a digit (e.g. alpine, buster, etc)
107112
if len(tag.Name) == 0 || tag.Name[0] < '0' || tag.Name[0] > '9' {
@@ -111,42 +116,20 @@ func getDockerGolangImages() ([]string, error) {
111116
// Extract the base version (e.g. 1.18.1 from 1.18.1-alpine)
112117
base := strings.Split(tag.Name, "-")[0]
113118

114-
// If its not at least three characters, do nothing.
115-
if len(base) < 3 {
116-
continue
117-
}
118-
119-
// Skip release candidates.
120-
if strings.Contains(base, "rc") {
121-
continue
122-
}
123-
124-
// Only take major versions.
125-
if strings.Count(base, ".") > 1 {
119+
// Reduce versions to MajorMinor.
120+
baseMajMin := semver.MajorMinor("v" + base)
121+
if !semver.IsValid(baseMajMin) || versionSet[baseMajMin] {
126122
continue
127123
}
128124

129-
//if compareVersions(base, minSupportedVersion) >= 0 {
130-
if semver.Compare("v"+base, "v"+minSupportedVersion) >= 0 {
131-
versionSet[base] = true
132-
133-
continue
134-
} else {
135-
finished = true
125+
if semver.Compare(baseMajMin, "v"+minSupportedVersion) >= 0 {
126+
versionSet[baseMajMin] = true
127+
versions = append(versions, baseMajMin[1:])
136128
}
137129
}
138130

139-
if finished {
140-
break
141-
}
142-
143131
url = data.Next
144132
}
145133

146-
versions := []string{}
147-
for v := range versionSet {
148-
versions = append(versions, v)
149-
}
150-
151134
return versions, nil
152135
}

0 commit comments

Comments
 (0)