Skip to content

Commit bd4b21d

Browse files
committed
envtest: search the assets index for latest of a release series
Signed-off-by: Chris Bandy <[email protected]>
1 parent ea7a2e2 commit bd4b21d

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

pkg/envtest/binaries.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ func downloadBinaryAssets(ctx context.Context, binaryAssetsDirectory, binaryAsse
125125
}
126126

127127
var binaryAssetsIndex *index
128-
if binaryAssetsVersion == "" {
128+
if binaryAssetsVersion == "" || strings.Count(binaryAssetsVersion, ".") < 2 {
129129
var err error
130130
binaryAssetsIndex, err = getIndex(ctx, binaryAssetsIndexURL)
131131
if err != nil {
132132
return "", "", "", err
133133
}
134134

135-
binaryAssetsVersion, err = latestStableVersionFromIndex(binaryAssetsIndex)
135+
binaryAssetsVersion, err = latestStableVersionFromIndex(binaryAssetsIndex, binaryAssetsVersion)
136136
if err != nil {
137137
return "", "", "", err
138138
}
@@ -252,13 +252,19 @@ func downloadBinaryAssetsArchive(ctx context.Context, index *index, version stri
252252
return readBody(resp, out, archiveName, archive.Hash)
253253
}
254254

255-
func latestStableVersionFromIndex(index *index) (string, error) {
255+
func latestStableVersionFromIndex(index *index, prefix string) (string, error) {
256256
if len(index.Releases) == 0 {
257257
return "", fmt.Errorf("failed to find latest stable version from index: index is empty")
258258
}
259259

260+
prefix = strings.TrimPrefix(prefix, "v")
260261
parsedVersions := []semver.Version{}
261262
for releaseVersion := range index.Releases {
263+
// Filter on prefix.
264+
if !strings.HasPrefix(strings.TrimPrefix(releaseVersion, "v"), prefix) {
265+
continue
266+
}
267+
262268
v, err := semver.ParseTolerant(releaseVersion)
263269
if err != nil {
264270
return "", fmt.Errorf("failed to parse version %q: %w", releaseVersion, err)

pkg/envtest/binaries_test.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ var _ = Describe("Test download binaries", func() {
6868
Expect(actualFiles).To(ConsistOf("some-file"))
6969
})
7070

71-
It("should download v1.32.0 binaries", func(ctx SpecContext) {
71+
It("should download binaries of an exact version", func(ctx SpecContext) {
7272
apiServerPath, etcdPath, kubectlPath, err := downloadBinaryAssets(ctx, downloadDirectory, "v1.31.0", fmt.Sprintf("http://%s/%s", server.Addr(), "envtest-releases.yaml"))
7373
Expect(err).ToNot(HaveOccurred())
7474

75-
// Verify latest stable version (v1.32.0) was downloaded
75+
// Verify exact version (v1.31.0) was downloaded
7676
versionDownloadDirectory := path.Join(downloadDirectory, fmt.Sprintf("1.31.0-%s-%s", runtime.GOOS, runtime.GOARCH))
7777
Expect(apiServerPath).To(Equal(path.Join(versionDownloadDirectory, "kube-apiserver")))
7878
Expect(etcdPath).To(Equal(path.Join(versionDownloadDirectory, "etcd")))
@@ -86,6 +86,25 @@ var _ = Describe("Test download binaries", func() {
8686
}
8787
Expect(actualFiles).To(ConsistOf("some-file"))
8888
})
89+
90+
It("should download binaries of latest stable version of a release series", func(ctx SpecContext) {
91+
apiServerPath, etcdPath, kubectlPath, err := downloadBinaryAssets(ctx, downloadDirectory, "1.31", fmt.Sprintf("http://%s/%s", server.Addr(), "envtest-releases.yaml"))
92+
Expect(err).ToNot(HaveOccurred())
93+
94+
// Verify stable version (v1.31.4) was downloaded
95+
versionDownloadDirectory := path.Join(downloadDirectory, fmt.Sprintf("1.31.4-%s-%s", runtime.GOOS, runtime.GOARCH))
96+
Expect(apiServerPath).To(Equal(path.Join(versionDownloadDirectory, "kube-apiserver")))
97+
Expect(etcdPath).To(Equal(path.Join(versionDownloadDirectory, "etcd")))
98+
Expect(kubectlPath).To(Equal(path.Join(versionDownloadDirectory, "kubectl")))
99+
100+
dirEntries, err := os.ReadDir(versionDownloadDirectory)
101+
Expect(err).ToNot(HaveOccurred())
102+
var actualFiles []string
103+
for _, e := range dirEntries {
104+
actualFiles = append(actualFiles, e.Name())
105+
}
106+
Expect(actualFiles).To(ConsistOf("some-file"))
107+
})
89108
})
90109

91110
var (
@@ -100,6 +119,15 @@ var (
100119
"envtest-v1.32.0-linux-s390x.tar.gz": {},
101120
"envtest-v1.32.0-windows-amd64.tar.gz": {},
102121
},
122+
"v1.31.4": map[string]archive{
123+
"envtest-v1.31.4-darwin-amd64.tar.gz": {},
124+
"envtest-v1.31.4-darwin-arm64.tar.gz": {},
125+
"envtest-v1.31.4-linux-amd64.tar.gz": {},
126+
"envtest-v1.31.4-linux-arm64.tar.gz": {},
127+
"envtest-v1.31.4-linux-ppc64le.tar.gz": {},
128+
"envtest-v1.31.4-linux-s390x.tar.gz": {},
129+
"envtest-v1.31.4-windows-amd64.tar.gz": {},
130+
},
103131
"v1.31.0": map[string]archive{
104132
"envtest-v1.31.0-darwin-amd64.tar.gz": {},
105133
"envtest-v1.31.0-darwin-arm64.tar.gz": {},

0 commit comments

Comments
 (0)