forked from bazelbuild/bazelisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bazelisk_version_test.go
320 lines (267 loc) · 8.64 KB
/
bazelisk_version_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"testing"
"github.com/bazelbuild/bazelisk/core"
"github.com/bazelbuild/bazelisk/httputil"
"github.com/bazelbuild/bazelisk/repositories"
"github.com/bazelbuild/bazelisk/versions"
)
var (
transport = &fakeTransport{responses: make(map[string]*http.Response)}
tmpDir = ""
)
func init() {
httputil.DefaultTransport = transport
}
func TestMain(m *testing.M) {
var err error
tmpDir, err = ioutil.TempDir("", "version_test")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)
code := m.Run()
os.Exit(code)
}
func TestResolveLatestRcVersion(t *testing.T) {
s := setUp(t)
s.AddVersion("4.0.0", false)
s.AddVersion("10.0.0", false)
s.AddVersion("11.0.0", true)
s.AddVersion("11.11.0", false, 1, 2)
s.Finish()
gcs := &repositories.GCSRepo{}
repos := core.CreateRepositories(nil, gcs, nil, nil, nil, false)
version, _, err := repos.ResolveVersion(tmpDir, versions.BazelUpstream, "last_rc")
if err != nil {
t.Fatalf("Version resolution failed unexpectedly: %v", err)
}
expectedRC := "11.11.0rc2"
if version != expectedRC {
t.Fatalf("Expected version %s, but got %s", expectedRC, version)
}
}
func TestResolveLatestRcVersion_WithFullRelease(t *testing.T) {
s := setUp(t)
s.AddVersion("4.0.0", true, 1, 2, 3)
s.Finish()
gcs := &repositories.GCSRepo{}
repos := core.CreateRepositories(nil, gcs, nil, nil, nil, false)
version, _, err := repos.ResolveVersion(tmpDir, versions.BazelUpstream, "last_rc")
if err != nil {
t.Fatalf("Version resolution failed unexpectedly: %v", err)
}
expectedRC := "4.0.0rc3"
if version != expectedRC {
t.Fatalf("Expected version %s, but got %s", expectedRC, version)
}
}
func TestResolveLatestVersion_TwoLatestVersionsDoNotHaveAReleaseYet(t *testing.T) {
s := setUp(t)
s.AddVersion("4.0.0", true)
s.AddVersion("5.0.0", false)
s.AddVersion("6.0.0", false)
s.Finish()
gcs := &repositories.GCSRepo{}
repos := core.CreateRepositories(gcs, nil, nil, nil, nil, false)
version, _, err := repos.ResolveVersion(tmpDir, versions.BazelUpstream, "latest")
if err != nil {
t.Fatalf("Version resolution failed unexpectedly: %v", err)
}
expectedVersion := "4.0.0"
if version != expectedVersion {
t.Fatalf("Expected version %s, but got %s", expectedVersion, version)
}
}
func TestResolveLatestVersion_FilterReleaseCandidates(t *testing.T) {
s := setUp(t)
s.AddVersion("3.0.0", true)
s.AddVersion("4.0.0", false)
s.AddVersion("5.0.0", false)
s.AddVersion("6.0.0", true)
s.Finish()
gcs := &repositories.GCSRepo{}
repos := core.CreateRepositories(gcs, nil, nil, nil, nil, false)
version, _, err := repos.ResolveVersion(tmpDir, versions.BazelUpstream, "latest-1")
if err != nil {
t.Fatalf("Version resolution failed unexpectedly: %v", err)
}
expectedVersion := "3.0.0"
if version != expectedVersion {
t.Fatalf("Expected version %s, but got %s", expectedVersion, version)
}
}
func TestResolveLatestVersion_ShouldFailIfNotEnoughReleases(t *testing.T) {
s := setUp(t)
s.AddVersion("3.0.0", true)
s.AddVersion("4.0.0", false)
s.Finish()
gcs := &repositories.GCSRepo{}
repos := core.CreateRepositories(gcs, nil, nil, nil, nil, false)
_, _, err := repos.ResolveVersion(tmpDir, versions.BazelUpstream, "latest-1")
if err == nil {
t.Fatal("Expected ResolveVersion() to fail.")
}
expectedError := "unable to determine latest version: requested 2 latest releases, but only found 1"
if err.Error() != expectedError {
t.Fatalf("Expected error message '%s', but got '%v'", expectedError, err)
}
}
func TestResolveLatestVersion_GCSIsDown(t *testing.T) {
setUp(t).WithError().Finish()
transport.AddResponse("https://www.googleapis.com/storage/v1/b/bazel/o?delimiter=/", 500, "")
gcs := &repositories.GCSRepo{}
repos := core.CreateRepositories(gcs, nil, nil, nil, nil, false)
_, _, err := repos.ResolveVersion(tmpDir, versions.BazelUpstream, "latest")
if err == nil {
t.Fatal("Expected resolveLatestVersion() to fail.")
}
expectedPrefix := "unable to determine latest version: could not list Bazel versions in GCS bucket"
if !strings.HasPrefix(err.Error(), expectedPrefix) {
t.Fatalf("Expected error message that starts with '%s', but got '%v'", expectedPrefix, err)
}
}
func TestResolveLatestVersion_GitHubIsDown(t *testing.T) {
transport.AddResponse("https://api.github.com/repos/bazelbuild/bazel/releases", 500, "")
gh := repositories.CreateGitHubRepo("test_token")
repos := core.CreateRepositories(nil, nil, gh, nil, nil, false)
_, _, err := repos.ResolveVersion(tmpDir, "some_fork", "latest")
if err == nil {
t.Fatal("Expected resolveLatestVersion() to fail.")
}
expectedPrefix := "unable to determine latest version: unable to dermine 'some_fork' releases: could not download list of Bazel releases from github.com/some_fork"
if !strings.HasPrefix(err.Error(), expectedPrefix) {
t.Fatalf("Expected error message that starts with '%s', but got '%v'", expectedPrefix, err)
}
}
func TestAcceptRollingReleaseName(t *testing.T) {
gh := repositories.CreateGitHubRepo("test_token")
repos := core.CreateRepositories(nil, nil, nil, nil, gh, false)
version := "10.0.0-pre.20201103.4"
resolvedVersion, _, err := repos.ResolveVersion(tmpDir, "", version)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if resolvedVersion != version {
t.Fatalf("Expected version '%s', but got '%s'", version, resolvedVersion)
}
}
func TestResolveLatestRollingRelease(t *testing.T) {
text := `
[
{
"tag_name": "4.0.0",
"prerelease": false
},
{
"tag_name": "5.0.0-pre.20210319",
"prerelease": true
},
{
"tag_name": "5.0.0-pre.20210322.4",
"prerelease": true
},
{
"tag_name": "5.0.0",
"prerelease": false
}
]
`
transport.AddResponse("https://api.github.com/repos/bazelbuild/bazel/releases", 200, text)
gh := repositories.CreateGitHubRepo("test_token")
repos := core.CreateRepositories(nil, nil, nil, nil, gh, false)
version, _, err := repos.ResolveVersion(tmpDir, "", "rolling")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
want := "5.0.0-pre.20210322.4"
if version != want {
t.Fatalf("Expected version '%s', but got '%s'", want, version)
}
}
type gcsSetup struct {
baseURL string
versionPrefixes []string
status int
test *testing.T
}
func (g *gcsSetup) AddVersion(version string, hasRelease bool, rcs ...int) *gcsSetup {
g.versionPrefixes = append(g.versionPrefixes, fmt.Sprintf("%s/", version))
prefixes := make([]string, 0)
for _, rc := range rcs {
prefix := fmt.Sprintf("%s/rc%d/", version, rc)
prefixes = append(prefixes, prefix)
g.addURL(prefix, false)
}
// The /release/ URLs have to exist, even if there is no release. In this case GCS returns no items, though.
releasePrefix := fmt.Sprintf("%s/release/", version)
g.addURL(releasePrefix, hasRelease)
if hasRelease {
prefixes = append(prefixes, releasePrefix)
}
g.addURL(fmt.Sprintf("%s/", version), false, prefixes...)
return g
}
func (g *gcsSetup) addURL(prefix string, containsItem bool, childPrefixes ...string) {
items := make([]interface{}, 0)
if containsItem {
items = append(items, "this_is_a_release")
}
resp := buildGCSResponseOrFail(g.test, childPrefixes, items)
transport.AddResponse(fmt.Sprintf("%s&prefix=%s", g.baseURL, prefix), 200, resp)
}
func setUp(t *testing.T) *gcsSetup {
return &gcsSetup{
baseURL: "https://www.googleapis.com/storage/v1/b/bazel/o?delimiter=/",
status: 200,
versionPrefixes: make([]string, 0),
test: t,
}
}
func (g *gcsSetup) WithError() *gcsSetup {
g.status = 500
return g
}
func (g *gcsSetup) Finish() {
// TODO: sort and deduplicate versionPrefixes
listBody := buildGCSResponseOrFail(g.test, g.versionPrefixes, []interface{}{})
transport.AddResponse(g.baseURL, g.status, listBody)
}
type fakeTransport struct {
responses map[string]*http.Response
}
func (ft *fakeTransport) AddResponse(url string, status int, body string) {
ft.responses[url] = ft.createResponse(status, body)
}
func (ft *fakeTransport) createResponse(status int, body string) *http.Response {
return &http.Response{
StatusCode: status,
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
}
}
func (ft *fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if resp, ok := ft.responses[req.URL.String()]; ok {
return resp, nil
}
return ft.createResponse(http.StatusNotFound, ""), nil
}
func buildGCSResponseOrFail(t *testing.T, prefixes []string, items []interface{}) string {
r := &repositories.GcsListResponse{
Prefixes: prefixes,
Items: items,
}
byteValue, err := json.Marshal(r)
if err != nil {
t.Fatalf("Could not build GCS json response: %v", err)
return ""
}
return string(byteValue)
}