@@ -28,13 +28,93 @@ import (
28
28
"os"
29
29
"path"
30
30
"runtime"
31
+ "strings"
32
+ "testing"
31
33
32
34
. "github.com/onsi/ginkgo/v2"
33
35
. "github.com/onsi/gomega"
34
36
"github.com/onsi/gomega/ghttp"
35
37
"sigs.k8s.io/yaml"
36
38
)
37
39
40
+ func TestInterpretKubernetesVersion (t * testing.T ) {
41
+ t .Parallel ()
42
+
43
+ testCases := []struct {
44
+ name string
45
+ in []string
46
+ exact bool
47
+ major uint
48
+ minor uint
49
+ }{
50
+ {
51
+ name : `SemVer and "v" prefix are exact` ,
52
+ exact : true ,
53
+ in : []string {
54
+ "1.2.3" , "v1.2.3" , "v1.30.2" , "v1.31.0-beta.0" , "v1.33.0-alpha.2" ,
55
+ },
56
+ },
57
+ {
58
+ name : "leading zeroes are not a version" ,
59
+ in : []string {
60
+ "01.2.0" , "00001.2.3" , "1.2.03" , "v01.02.0003" ,
61
+ },
62
+ },
63
+ {
64
+ name : "weird stuff is not a version" ,
65
+ in : []string {
66
+ "asdf" , "version" , "vegeta4" , "the.1" , "2ne1" , "=7.8.9" , "10.x" , "*" ,
67
+ "0.0001" , "1.00002" , "v1.2anything" , "1.2.x" , "1.2.z" , "1.2.*" ,
68
+ },
69
+ },
70
+ {
71
+ name : "one number is not a version" ,
72
+ in : []string {
73
+ "1" , "v1" , "v001" , "1." , "v1." , "1.x" ,
74
+ },
75
+ },
76
+ {
77
+ name : "two numbers are a release series" ,
78
+ major : 0 , minor : 1 ,
79
+ in : []string {"0.1" , "v0.1" },
80
+ },
81
+ {
82
+ name : "two numbers are a release series" ,
83
+ major : 1 , minor : 2 ,
84
+ in : []string {"1.2" , "v1.2" },
85
+ },
86
+ }
87
+
88
+ for _ , tc := range testCases {
89
+ t .Run (tc .name , func (t * testing.T ) {
90
+ for _ , input := range tc .in {
91
+ exact , major , minor := interpretKubernetesVersion (input )
92
+
93
+ if tc .exact {
94
+ if expected := strings .TrimPrefix (input , "v" ); exact != expected {
95
+ t .Errorf ("expected canonical %q for %q, got %q" , expected , input , exact )
96
+ }
97
+ if major != 0 || minor != 0 {
98
+ t .Errorf ("expected no release series for %q, got (%v, %v)" , input , major , minor )
99
+ }
100
+ }
101
+
102
+ if ! tc .exact {
103
+ if major != tc .major {
104
+ t .Errorf ("expected major %v for %q, got %v" , tc .major , input , major )
105
+ }
106
+ if minor != tc .minor {
107
+ t .Errorf ("expected minor %v for %q, got %v" , tc .minor , input , minor )
108
+ }
109
+ if exact != "" {
110
+ t .Errorf ("expected no canonical version for %q, got %q" , input , exact )
111
+ }
112
+ }
113
+ }
114
+ })
115
+ }
116
+ }
117
+
38
118
var _ = Describe ("Test download binaries" , func () {
39
119
var downloadDirectory string
40
120
var server * ghttp.Server
@@ -68,11 +148,11 @@ var _ = Describe("Test download binaries", func() {
68
148
Expect (actualFiles ).To (ConsistOf ("some-file" ))
69
149
})
70
150
71
- It ("should download v1.32.0 binaries" , func (ctx SpecContext ) {
151
+ It ("should download binaries of an exact version " , func (ctx SpecContext ) {
72
152
apiServerPath , etcdPath , kubectlPath , err := downloadBinaryAssets (ctx , downloadDirectory , "v1.31.0" , fmt .Sprintf ("http://%s/%s" , server .Addr (), "envtest-releases.yaml" ))
73
153
Expect (err ).ToNot (HaveOccurred ())
74
154
75
- // Verify latest stable version (v1.32 .0) was downloaded
155
+ // Verify exact version (v1.31 .0) was downloaded
76
156
versionDownloadDirectory := path .Join (downloadDirectory , fmt .Sprintf ("1.31.0-%s-%s" , runtime .GOOS , runtime .GOARCH ))
77
157
Expect (apiServerPath ).To (Equal (path .Join (versionDownloadDirectory , "kube-apiserver" )))
78
158
Expect (etcdPath ).To (Equal (path .Join (versionDownloadDirectory , "etcd" )))
@@ -86,6 +166,25 @@ var _ = Describe("Test download binaries", func() {
86
166
}
87
167
Expect (actualFiles ).To (ConsistOf ("some-file" ))
88
168
})
169
+
170
+ It ("should download binaries of latest stable version of a release series" , func (ctx SpecContext ) {
171
+ apiServerPath , etcdPath , kubectlPath , err := downloadBinaryAssets (ctx , downloadDirectory , "1.31" , fmt .Sprintf ("http://%s/%s" , server .Addr (), "envtest-releases.yaml" ))
172
+ Expect (err ).ToNot (HaveOccurred ())
173
+
174
+ // Verify stable version (v1.31.4) was downloaded
175
+ versionDownloadDirectory := path .Join (downloadDirectory , fmt .Sprintf ("1.31.4-%s-%s" , runtime .GOOS , runtime .GOARCH ))
176
+ Expect (apiServerPath ).To (Equal (path .Join (versionDownloadDirectory , "kube-apiserver" )))
177
+ Expect (etcdPath ).To (Equal (path .Join (versionDownloadDirectory , "etcd" )))
178
+ Expect (kubectlPath ).To (Equal (path .Join (versionDownloadDirectory , "kubectl" )))
179
+
180
+ dirEntries , err := os .ReadDir (versionDownloadDirectory )
181
+ Expect (err ).ToNot (HaveOccurred ())
182
+ var actualFiles []string
183
+ for _ , e := range dirEntries {
184
+ actualFiles = append (actualFiles , e .Name ())
185
+ }
186
+ Expect (actualFiles ).To (ConsistOf ("some-file" ))
187
+ })
89
188
})
90
189
91
190
var (
@@ -100,6 +199,15 @@ var (
100
199
"envtest-v1.32.0-linux-s390x.tar.gz" : {},
101
200
"envtest-v1.32.0-windows-amd64.tar.gz" : {},
102
201
},
202
+ "v1.31.4" : map [string ]archive {
203
+ "envtest-v1.31.4-darwin-amd64.tar.gz" : {},
204
+ "envtest-v1.31.4-darwin-arm64.tar.gz" : {},
205
+ "envtest-v1.31.4-linux-amd64.tar.gz" : {},
206
+ "envtest-v1.31.4-linux-arm64.tar.gz" : {},
207
+ "envtest-v1.31.4-linux-ppc64le.tar.gz" : {},
208
+ "envtest-v1.31.4-linux-s390x.tar.gz" : {},
209
+ "envtest-v1.31.4-windows-amd64.tar.gz" : {},
210
+ },
103
211
"v1.31.0" : map [string ]archive {
104
212
"envtest-v1.31.0-darwin-amd64.tar.gz" : {},
105
213
"envtest-v1.31.0-darwin-arm64.tar.gz" : {},
0 commit comments