@@ -28,13 +28,101 @@ import (
2828 "os"
2929 "path"
3030 "runtime"
31+ "strings"
32+ "testing"
3133
3234 . "github.com/onsi/ginkgo/v2"
3335 . "github.com/onsi/gomega"
3436 "github.com/onsi/gomega/ghttp"
3537 "sigs.k8s.io/yaml"
3638)
3739
40+ func TestInterpretKubernetesVersion (t * testing.T ) {
41+ t .Parallel ()
42+
43+ testCases := []struct {
44+ name string
45+ inputs []string
46+
47+ expectExact bool
48+ expectSeriesMajor uint
49+ expectSeriesMinor uint
50+ }{
51+ {
52+ name : `SemVer and "v" prefix are exact` ,
53+ inputs : []string {
54+ "1.2.3" , "v1.2.3" , "v1.30.2" , "v1.31.0-beta.0" , "v1.33.0-alpha.2" ,
55+ },
56+ expectExact : true ,
57+ },
58+ {
59+ name : "empty string is not a version" ,
60+ inputs : []string {"" },
61+ },
62+ {
63+ name : "leading zeroes are not a version" ,
64+ inputs : []string {
65+ "01.2.0" , "00001.2.3" , "1.2.03" , "v01.02.0003" ,
66+ },
67+ },
68+ {
69+ name : "weird stuff is not a version" ,
70+ inputs : []string {
71+ "asdf" , "version" , "vegeta4" , "the.1" , "2ne1" , "=7.8.9" , "10.x" , "*" ,
72+ "0.0001" , "1.00002" , "v1.2anything" , "1.2.x" , "1.2.z" , "1.2.*" ,
73+ },
74+ },
75+ {
76+ name : "one number is not a version" ,
77+ inputs : []string {
78+ "1" , "v1" , "v001" , "1." , "v1." , "1.x" ,
79+ },
80+ },
81+ {
82+ name : "two numbers are a release series" ,
83+ inputs : []string {"0.1" , "v0.1" },
84+
85+ expectSeriesMajor : 0 ,
86+ expectSeriesMinor : 1 ,
87+ },
88+ {
89+ name : "two numbers are a release series" ,
90+ inputs : []string {"1.2" , "v1.2" },
91+
92+ expectSeriesMajor : 1 ,
93+ expectSeriesMinor : 2 ,
94+ },
95+ }
96+
97+ for _ , tc := range testCases {
98+ t .Run (tc .name , func (t * testing.T ) {
99+ for _ , input := range tc .inputs {
100+ exact , major , minor := interpretKubernetesVersion (input )
101+
102+ if tc .expectExact {
103+ if expected := strings .TrimPrefix (input , "v" ); exact != expected {
104+ t .Errorf ("expected canonical %q for %q, got %q" , expected , input , exact )
105+ }
106+ if major != 0 || minor != 0 {
107+ t .Errorf ("expected no release series for %q, got (%v, %v)" , input , major , minor )
108+ }
109+ continue
110+ }
111+
112+ if major != tc .expectSeriesMajor {
113+ t .Errorf ("expected major %v for %q, got %v" , tc .expectSeriesMajor , input , major )
114+ }
115+ if minor != tc .expectSeriesMinor {
116+ t .Errorf ("expected minor %v for %q, got %v" , tc .expectSeriesMinor , input , minor )
117+ }
118+ if exact != "" {
119+ t .Errorf ("expected no canonical version for %q, got %q" , input , exact )
120+ }
121+ }
122+ })
123+ }
124+ }
125+
38126var _ = Describe ("Test download binaries" , func () {
39127 var downloadDirectory string
40128 var server * ghttp.Server
@@ -68,11 +156,11 @@ var _ = Describe("Test download binaries", func() {
68156 Expect (actualFiles ).To (ConsistOf ("some-file" ))
69157 })
70158
71- It ("should download v1.32.0 binaries" , func (ctx SpecContext ) {
159+ It ("should download binaries of an exact version " , func (ctx SpecContext ) {
72160 apiServerPath , etcdPath , kubectlPath , err := downloadBinaryAssets (ctx , downloadDirectory , "v1.31.0" , fmt .Sprintf ("http://%s/%s" , server .Addr (), "envtest-releases.yaml" ))
73161 Expect (err ).ToNot (HaveOccurred ())
74162
75- // Verify latest stable version (v1.32 .0) was downloaded
163+ // Verify exact version (v1.31 .0) was downloaded
76164 versionDownloadDirectory := path .Join (downloadDirectory , fmt .Sprintf ("1.31.0-%s-%s" , runtime .GOOS , runtime .GOARCH ))
77165 Expect (apiServerPath ).To (Equal (path .Join (versionDownloadDirectory , "kube-apiserver" )))
78166 Expect (etcdPath ).To (Equal (path .Join (versionDownloadDirectory , "etcd" )))
@@ -86,6 +174,38 @@ var _ = Describe("Test download binaries", func() {
86174 }
87175 Expect (actualFiles ).To (ConsistOf ("some-file" ))
88176 })
177+
178+ It ("should download binaries of latest stable version of a release series" , func (ctx SpecContext ) {
179+ apiServerPath , etcdPath , kubectlPath , err := downloadBinaryAssets (ctx , downloadDirectory , "1.31" , fmt .Sprintf ("http://%s/%s" , server .Addr (), "envtest-releases.yaml" ))
180+ Expect (err ).ToNot (HaveOccurred ())
181+
182+ // Verify stable version (v1.31.4) was downloaded
183+ versionDownloadDirectory := path .Join (downloadDirectory , fmt .Sprintf ("1.31.4-%s-%s" , runtime .GOOS , runtime .GOARCH ))
184+ Expect (apiServerPath ).To (Equal (path .Join (versionDownloadDirectory , "kube-apiserver" )))
185+ Expect (etcdPath ).To (Equal (path .Join (versionDownloadDirectory , "etcd" )))
186+ Expect (kubectlPath ).To (Equal (path .Join (versionDownloadDirectory , "kubectl" )))
187+
188+ dirEntries , err := os .ReadDir (versionDownloadDirectory )
189+ Expect (err ).ToNot (HaveOccurred ())
190+ var actualFiles []string
191+ for _ , e := range dirEntries {
192+ actualFiles = append (actualFiles , e .Name ())
193+ }
194+ Expect (actualFiles ).To (ConsistOf ("some-file" ))
195+ })
196+
197+ It ("should error when the asset version is not a version" , func (ctx SpecContext ) {
198+ _ , _ , _ , err := downloadBinaryAssets (ctx , downloadDirectory , "wonky" , fmt .Sprintf ("http://%s/%s" , server .Addr (), "envtest-releases.yaml" ))
199+ Expect (err ).To (MatchError ("failed to find envtest binaries for version wonky" ))
200+ })
201+
202+ It ("should error when the asset version is not in the index" , func (ctx SpecContext ) {
203+ _ , _ , _ , err := downloadBinaryAssets (ctx , downloadDirectory , "v1.5.0" , fmt .Sprintf ("http://%s/%s" , server .Addr (), "envtest-releases.yaml" ))
204+ Expect (err ).To (MatchError ("failed to find envtest binaries for version v1.5.0" ))
205+
206+ _ , _ , _ , err = downloadBinaryAssets (ctx , downloadDirectory , "v1.5" , fmt .Sprintf ("http://%s/%s" , server .Addr (), "envtest-releases.yaml" ))
207+ Expect (err ).To (MatchError ("failed to find latest stable version from index: index does not have 1.5 stable versions" ))
208+ })
89209})
90210
91211var (
@@ -100,6 +220,15 @@ var (
100220 "envtest-v1.32.0-linux-s390x.tar.gz" : {},
101221 "envtest-v1.32.0-windows-amd64.tar.gz" : {},
102222 },
223+ "v1.31.4" : map [string ]archive {
224+ "envtest-v1.31.4-darwin-amd64.tar.gz" : {},
225+ "envtest-v1.31.4-darwin-arm64.tar.gz" : {},
226+ "envtest-v1.31.4-linux-amd64.tar.gz" : {},
227+ "envtest-v1.31.4-linux-arm64.tar.gz" : {},
228+ "envtest-v1.31.4-linux-ppc64le.tar.gz" : {},
229+ "envtest-v1.31.4-linux-s390x.tar.gz" : {},
230+ "envtest-v1.31.4-windows-amd64.tar.gz" : {},
231+ },
103232 "v1.31.0" : map [string ]archive {
104233 "envtest-v1.31.0-darwin-amd64.tar.gz" : {},
105234 "envtest-v1.31.0-darwin-arm64.tar.gz" : {},
0 commit comments