Repository navigation
Expand file tree
/
Copy pathpull_test.go
More file actions
416 lines (385 loc) · 18.9 KB
/
Copy pathpull_test.go
File metadata and controls
416 lines (385 loc) · 18.9 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package pull
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"path"
"strconv"
"testing"
"github.com/github/codeql-action-sync/internal/cachedirectory"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/github/codeql-action-sync/test"
"github.com/google/go-github/v32/github"
)
const initialActionRepository = "./pull_test/codeql-action-initial.git"
const modifiedActionRepository = "./pull_test/codeql-action-modified.git"
const releaseSomeCodeQLVersionOnMainContent = "This isn't really a CodeQL bundle!"
var releaseSomeCodeQLVersionOnMain = github.RepositoryRelease{
TagName: github.String("some-codeql-version-on-main"),
Name: github.String("some-codeql-version-on-main"),
Assets: []*github.ReleaseAsset{
&github.ReleaseAsset{
ID: github.Int64(1),
Name: github.String("codeql-bundle.tar.gz"),
Size: github.Int(len(releaseSomeCodeQLVersionOnMainContent)),
},
},
}
const releaseSomeCodeQLVersionOnV1AndV2Content = "This isn't a CodeQL bundle either, but it's a different not-bundle."
var releaseSomeCodeQLVersionOnV1AndV2 = github.RepositoryRelease{
TagName: github.String("some-codeql-version-on-v1-and-v2"),
Name: github.String("some-codeql-version-on-v1-and-v2"),
Assets: []*github.ReleaseAsset{
&github.ReleaseAsset{
ID: github.Int64(2),
Name: github.String("codeql-bundle.tar.gz"),
Size: github.Int(len(releaseSomeCodeQLVersionOnV1AndV2Content)),
},
},
}
func getTestPullService(t *testing.T, temporaryDirectory string, gitCloneURL string, githubURL string) pullService {
cacheDirectory := cachedirectory.NewCacheDirectory(temporaryDirectory)
var githubDotComClient *github.Client
if githubURL != "" {
client, err := github.NewEnterpriseClient(githubURL+"/api/v3", githubURL+"/api/uploads", &http.Client{})
githubDotComClient = client
require.NoError(t, err)
} else {
githubDotComClient = nil
}
return pullService{
ctx: context.Background(),
cacheDirectory: cacheDirectory,
gitCloneURL: gitCloneURL,
githubDotComClient: githubDotComClient,
}
}
func getTestPullServiceWithAssets(t *testing.T, names ...string) pullService {
t.Helper()
router, githubURL := test.GetTestHTTPServer(t)
assets := releaseAssets(names...)
contents := map[int]string{}
for index, asset := range assets {
id := index + 1
contents[id] = asset.GetName()
asset.ID = github.Int64(int64(id))
asset.Size = github.Int(len(contents[id]))
}
for _, tag := range []string{"some-codeql-version-on-main", "some-codeql-version-on-v1-and-v2"} {
release := github.RepositoryRelease{TagName: github.String(tag), Assets: assets}
router.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/"+tag, func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, release, response)
}).Methods("GET")
}
router.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/{id:[0-9]+}", func(response http.ResponseWriter, request *http.Request) {
id, err := strconv.Atoi(mux.Vars(request)["id"])
require.NoError(t, err)
content, exists := contents[id]
require.True(t, exists)
test.ServeHTTPResponseFromString(t, content, response)
}).Methods("GET").Headers("accept", "application/octet-stream")
service := getTestPullService(t, test.CreateTemporaryDirectory(t), initialActionRepository, githubURL)
require.NoError(t, service.pullGit(true))
return service
}
func checkExpectedReferencesInCache(t *testing.T, cacheDirectory cachedirectory.CacheDirectory, expectedReferences []string) {
localRepository, err := git.PlainOpen(cacheDirectory.GitPath())
require.NoError(t, err)
referenceIterator, err := localRepository.References()
require.NoError(t, err)
actualReferences := []string{}
err = referenceIterator.ForEach(func(reference *plumbing.Reference) error {
referenceString := reference.String()
if referenceString != "ref: refs/heads/master HEAD" {
actualReferences = append(actualReferences, referenceString)
}
return nil
})
require.NoError(t, err)
require.ElementsMatch(t, expectedReferences, actualReferences)
}
func TestPullGitFresh(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, "")
err := pullService.pullGit(true)
require.NoError(t, err)
test.CheckExpectedReferencesInRepository(t, pullService.cacheDirectory.GitPath(), []string{
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/v1",
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
// It is expected that we still pull these even though they don't match the expected pattern. We just ignore them later on.
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning",
})
}
func TestPullGitNotFreshReturnsErrorIfNoCache(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, "")
err := pullService.pullGit(false)
require.Error(t, err)
}
func TestPullGitNotFreshNoChanges(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, "")
err := pullService.pullGit(true)
require.NoError(t, err)
err = pullService.pullGit(false)
require.NoError(t, err)
}
func TestPullGitNotFreshWithChanges(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, "")
err := pullService.pullGit(true)
require.NoError(t, err)
pullService = getTestPullService(t, temporaryDirectory, modifiedActionRepository, "")
err = pullService.pullGit(false)
require.NoError(t, err)
test.CheckExpectedReferencesInRepository(t, pullService.cacheDirectory.GitPath(), []string{
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/v1",
"33d42021633d74bcd0bf9c95e3d3159131a5faa7 refs/heads/v3", // v3 was force-pushed, and should have been force-pulled too.
"42d077b4730d1ba413f7bb7e0fa7c98653fb0c78 refs/heads/v4", // v4 is a new branch.
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning/because-it-now-has-this-extra-bit",
})
}
func TestFindRelevantReleases(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, "")
err := pullService.pullGit(true)
require.NoError(t, err)
relevantReleases, err := pullService.findRelevantReleases()
require.NoError(t, err)
require.ElementsMatch(t, []string{
"some-codeql-version-on-main",
"some-codeql-version-on-v1-and-v2",
// v3 intentionally matches the patten for a release branch but has no configuration so it should be ignored with a warning.
}, relevantReleases)
}
func TestPullReleases(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
githubTestServer, githubURL := test.GetTestHTTPServer(t)
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnMain, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/1", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnMainContent, response)
}).Methods("GET").Headers("accept", "application/octet-stream")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-v1-and-v2", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnV1AndV2, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/2", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnV1AndV2Content, response)
}).Methods("GET").Headers("accept", "application/octet-stream")
pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, githubURL)
err := pullService.pullGit(true)
require.NoError(t, err)
err = pullService.pullReleases()
require.NoError(t, err)
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnMainContent, pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz"))
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnV1AndV2Content, pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"))
// If we pull again, we should only download assets where the size mismatches.
err = ioutil.WriteFile(pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"), []byte("Some nonsense."), 0644)
require.NoError(t, err)
githubTestServer, githubURL = test.GetTestHTTPServer(t)
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnMain, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-v1-and-v2", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnV1AndV2, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/2", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnV1AndV2Content, response)
}).Methods("GET").Headers("accept", "application/octet-stream")
pullService = getTestPullService(t, temporaryDirectory, initialActionRepository, githubURL)
err = pullService.pullReleases()
require.NoError(t, err)
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnMainContent, pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz"))
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnV1AndV2Content, pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"))
}
func TestPullReleasesFiltersAndPrunesCache(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
githubTestServer, githubURL := test.GetTestHTTPServer(t)
contents := map[int]string{
10: "linux zstd",
13: "version metadata",
14: "linux proxy",
20: "other linux zstd",
}
mainRelease := github.RepositoryRelease{
TagName: github.String("some-codeql-version-on-main"),
Assets: []*github.ReleaseAsset{
{ID: github.Int64(10), Name: github.String("codeql-bundle-linux64.tar.zst"), Size: github.Int(len(contents[10]))},
{ID: github.Int64(11), Name: github.String("codeql-bundle-linux64.tar.gz"), Size: github.Int(1)},
{ID: github.Int64(12), Name: github.String("codeql-bundle.tar.zst"), Size: github.Int(1)},
{ID: github.Int64(13), Name: github.String("cli-version-2.27.2.txt"), Size: github.Int(len(contents[13]))},
{ID: github.Int64(14), Name: github.String("update-job-proxy-linux64.tar.gz"), Size: github.Int(len(contents[14]))},
{ID: github.Int64(15), Name: github.String("codeql-bundle-win64.tar.zst"), Size: github.Int(1)},
},
}
otherRelease := github.RepositoryRelease{
TagName: github.String("some-codeql-version-on-v1-and-v2"),
Assets: []*github.ReleaseAsset{
{ID: github.Int64(20), Name: github.String("codeql-bundle-linux64.tar.zst"), Size: github.Int(len(contents[20]))},
},
}
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, mainRelease, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-v1-and-v2", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, otherRelease, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/{id:[0-9]+}", func(response http.ResponseWriter, request *http.Request) {
id, err := strconv.Atoi(mux.Vars(request)["id"])
require.NoError(t, err)
content, expected := contents[id]
require.True(t, expected, "asset %d should have been filtered", id)
test.ServeHTTPResponseFromString(t, content, response)
}).Methods("GET").Headers("accept", "application/octet-stream")
pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, githubURL)
pullService.assetFilter, _ = newReleaseAssetFilter([]string{"linux64"}, nil, "tar.zst")
err := pullService.pullGit(true)
require.NoError(t, err)
staleAssetsPath := pullService.cacheDirectory.AssetsPath("some-codeql-version-on-main")
require.NoError(t, os.MkdirAll(staleAssetsPath, 0755))
staleAssetPath := path.Join(staleAssetsPath, "codeql-bundle-osx64.tar.gz")
require.NoError(t, ioutil.WriteFile(staleAssetPath, []byte("stale"), 0644))
err = pullService.pullReleases()
require.NoError(t, err)
require.NoFileExists(t, staleAssetPath)
test.RequireFileHasContent(t, contents[10], pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle-linux64.tar.zst"))
test.RequireFileHasContent(t, contents[13], pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "cli-version-2.27.2.txt"))
test.RequireFileHasContent(t, contents[14], pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "update-job-proxy-linux64.tar.gz"))
require.NoFileExists(t, pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle-linux64.tar.gz"))
require.NoFileExists(t, pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.zst"))
require.NoFileExists(t, pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle-win64.tar.zst"))
test.RequireFileHasContent(t, contents[20], pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle-linux64.tar.zst"))
}
func TestPullReleasesFiltersHistoricalCache(t *testing.T) {
names := []string{
"codeql-bundle-linux64.tar.gz",
"codeql-bundle-linux64.tar.gz.checksum.txt",
"codeql-bundle-linux64.tar.zst",
"codeql-bundle-linux64.tar.zst.checksum.txt",
"codeql-bundle-win64.tar.gz",
"codeql-bundle-win64.tar.zst",
"codeql-bundle.tar.gz",
"codeql-bundle.tar.zst",
"update-job-proxy-linux64.tar.gz",
"update-job-proxy-win64.tar.gz",
"cli-version.txt",
"future-release.tar.gz",
}
cases := []struct {
name string
includePlatforms []string
excludePlatforms []string
bundleArchiveFormat string
expectedNames []string
}{
{
name: "included platform and gzip",
includePlatforms: []string{"linux64"},
bundleArchiveFormat: "tar.gz",
expectedNames: []string{
"codeql-bundle-linux64.tar.gz",
"codeql-bundle-linux64.tar.gz.checksum.txt",
"update-job-proxy-linux64.tar.gz",
"cli-version.txt",
"future-release.tar.gz",
},
},
{
name: "excluded platform and zstd",
excludePlatforms: []string{"win64"},
bundleArchiveFormat: "tar.zst",
expectedNames: []string{
"codeql-bundle-linux64.tar.zst",
"codeql-bundle-linux64.tar.zst.checksum.txt",
"update-job-proxy-linux64.tar.gz",
"cli-version.txt",
"future-release.tar.gz",
},
},
{
name: "format only",
bundleArchiveFormat: "tar.gz",
expectedNames: []string{
"codeql-bundle-linux64.tar.gz",
"codeql-bundle-linux64.tar.gz.checksum.txt",
"codeql-bundle-win64.tar.gz",
"codeql-bundle.tar.gz",
"update-job-proxy-linux64.tar.gz",
"update-job-proxy-win64.tar.gz",
"cli-version.txt",
"future-release.tar.gz",
},
},
{
name: "no filters preserve history",
expectedNames: names,
},
}
for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
service := getTestPullServiceWithAssets(t, names...)
var err error
service.assetFilter, err = newReleaseAssetFilter(testCase.includePlatforms, testCase.excludePlatforms, testCase.bundleArchiveFormat)
require.NoError(t, err)
historicalTag := "an-ignored-tag-too"
require.NoError(t, os.MkdirAll(service.cacheDirectory.AssetsPath(historicalTag), 0755))
metadata, err := json.Marshal(github.RepositoryRelease{TagName: github.String(historicalTag)})
require.NoError(t, err)
require.NoError(t, ioutil.WriteFile(service.cacheDirectory.MetadataPath(historicalTag), metadata, 0644))
for _, name := range names {
require.NoError(t, ioutil.WriteFile(service.cacheDirectory.AssetPath(historicalTag, name), []byte("historical "+name), 0644))
}
require.NoError(t, service.pullReleases())
cachedAssets, err := ioutil.ReadDir(service.cacheDirectory.AssetsPath(historicalTag))
require.NoError(t, err)
actualNames := []string{}
for _, asset := range cachedAssets {
actualNames = append(actualNames, asset.Name())
test.RequireFileHasContent(t, "historical "+asset.Name(), service.cacheDirectory.AssetPath(historicalTag, asset.Name()))
}
require.ElementsMatch(t, testCase.expectedNames, actualNames)
test.RequireFileHasContent(t, string(metadata), service.cacheDirectory.MetadataPath(historicalTag))
})
}
}
func TestPullReleasesWarnsWhenRestrictingBundleFormats(t *testing.T) {
for _, format := range []string{"tar.gz", ""} {
t.Run("format="+format, func(t *testing.T) {
gzipName := "codeql-bundle-linux64.tar.gz"
zstdName := "codeql-bundle-linux64.tar.zst"
service := getTestPullServiceWithAssets(t, gzipName, zstdName)
var err error
service.assetFilter, err = newReleaseAssetFilter([]string{"linux64"}, nil, format)
require.NoError(t, err)
var logged bytes.Buffer
previousOutput := log.StandardLogger().Out
log.SetOutput(&logged)
t.Cleanup(func() { log.SetOutput(previousOutput) })
require.NoError(t, service.pullReleases())
test.RequireFileHasContent(t, gzipName, service.cacheDirectory.AssetPath("some-codeql-version-on-main", gzipName))
if format != "" {
require.NoFileExists(t, service.cacheDirectory.AssetPath("some-codeql-version-on-main", zstdName))
require.Contains(t, logged.String(), "--bundle-archive-format")
require.Contains(t, logged.String(), "tools input with an explicit URL")
require.Contains(t, logged.String(), "GitHub.com")
} else {
test.RequireFileHasContent(t, zstdName, service.cacheDirectory.AssetPath("some-codeql-version-on-main", zstdName))
require.NotContains(t, logged.String(), "--bundle-archive-format")
}
})
}
}