Skip to content

Commit

Permalink
test: automatically cleanup test zip server (#834)
Browse files Browse the repository at this point in the history
When doing #832 I leaned `t.Cleanup` is a thing so let's use that
instead here
  • Loading branch information
G-Rath authored Mar 4, 2024
1 parent 0e3a11b commit f999707
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions internal/local/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ func cacheWriteBad(t *testing.T, storedAt string, contents string) {
}
}

func createZipServer(t *testing.T, handler http.HandlerFunc) (*httptest.Server, func()) {
func createZipServer(t *testing.T, handler http.HandlerFunc) *httptest.Server {
t.Helper()

ts := httptest.NewServer(handler)

return ts, ts.Close
t.Cleanup(ts.Close)

return ts
}

func computeCRC32CHash(t *testing.T, data []byte) string {
Expand Down Expand Up @@ -139,10 +141,9 @@ func TestNewZippedDB_Offline_WithoutCache(t *testing.T) {

testDir := testutility.CreateTestDir(t)

ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
t.Errorf("a server request was made when running offline")
})
defer cleanupTestServer()

_, err := local.NewZippedDB(testDir, "my-db", ts.URL, true)

Expand All @@ -164,10 +165,9 @@ func TestNewZippedDB_Offline_WithCache(t *testing.T) {

testDir := testutility.CreateTestDir(t)

ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
t.Errorf("a server request was made when running offline")
})
defer cleanupTestServer()

cacheWrite(t, determineStoredAtPath(testDir, "my-db"), zipOSVs(t, map[string]models.Vulnerability{
"GHSA-1.json": {ID: "GHSA-1"},
Expand All @@ -191,10 +191,9 @@ func TestNewZippedDB_BadZip(t *testing.T) {

testDir := testutility.CreateTestDir(t)

ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("this is not a zip"))
})
defer cleanupTestServer()

_, err := local.NewZippedDB(testDir, "my-db", ts.URL, false)

Expand Down Expand Up @@ -228,7 +227,7 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {

testDir := testutility.CreateTestDir(t)

ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = writeOSVsZip(t, w, map[string]models.Vulnerability{
"GHSA-1.json": {ID: "GHSA-1"},
"GHSA-2.json": {ID: "GHSA-2"},
Expand All @@ -237,7 +236,6 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
"GHSA-5.json": {ID: "GHSA-5"},
})
})
defer cleanupTestServer()

db, err := local.NewZippedDB(testDir, "my-db", ts.URL, false)

Expand All @@ -261,7 +259,7 @@ func TestNewZippedDB_Online_WithoutCacheAndNoHashHeader(t *testing.T) {

testDir := testutility.CreateTestDir(t)

ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(zipOSVs(t, map[string]models.Vulnerability{
"GHSA-1.json": {ID: "GHSA-1"},
"GHSA-2.json": {ID: "GHSA-2"},
Expand All @@ -270,7 +268,6 @@ func TestNewZippedDB_Online_WithoutCacheAndNoHashHeader(t *testing.T) {
"GHSA-5.json": {ID: "GHSA-5"},
}))
})
defer cleanupTestServer()

db, err := local.NewZippedDB(testDir, "my-db", ts.URL, false)

Expand Down Expand Up @@ -298,7 +295,7 @@ func TestNewZippedDB_Online_WithSameCache(t *testing.T) {
"GHSA-3.json": {ID: "GHSA-3"},
})

ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodHead {
t.Errorf("unexpected %s request", r.Method)
}
Expand All @@ -307,7 +304,6 @@ func TestNewZippedDB_Online_WithSameCache(t *testing.T) {

_, _ = w.Write(cache)
})
defer cleanupTestServer()

cacheWrite(t, determineStoredAtPath(testDir, "my-db"), cache)

Expand All @@ -333,7 +329,7 @@ func TestNewZippedDB_Online_WithDifferentCache(t *testing.T) {

testDir := testutility.CreateTestDir(t)

ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = writeOSVsZip(t, w, map[string]models.Vulnerability{
"GHSA-1.json": {ID: "GHSA-1"},
"GHSA-2.json": {ID: "GHSA-2"},
Expand All @@ -342,7 +338,6 @@ func TestNewZippedDB_Online_WithDifferentCache(t *testing.T) {
"GHSA-5.json": {ID: "GHSA-5"},
})
})
defer cleanupTestServer()

cacheWrite(t, determineStoredAtPath(testDir, "my-db"), zipOSVs(t, map[string]models.Vulnerability{
"GHSA-1.json": {ID: "GHSA-1"},
Expand All @@ -364,7 +359,7 @@ func TestNewZippedDB_Online_WithCacheButNoHashHeader(t *testing.T) {

testDir := testutility.CreateTestDir(t)

ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(zipOSVs(t, map[string]models.Vulnerability{
"GHSA-1.json": {ID: "GHSA-1"},
"GHSA-2.json": {ID: "GHSA-2"},
Expand All @@ -373,7 +368,6 @@ func TestNewZippedDB_Online_WithCacheButNoHashHeader(t *testing.T) {
"GHSA-5.json": {ID: "GHSA-5"},
}))
})
defer cleanupTestServer()

cacheWrite(t, determineStoredAtPath(testDir, "my-db"), zipOSVs(t, map[string]models.Vulnerability{
"GHSA-1.json": {ID: "GHSA-1"},
Expand All @@ -399,14 +393,13 @@ func TestNewZippedDB_Online_WithBadCache(t *testing.T) {

testDir := testutility.CreateTestDir(t)

ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = writeOSVsZip(t, w, map[string]models.Vulnerability{
"GHSA-1.json": {ID: "GHSA-1"},
"GHSA-2.json": {ID: "GHSA-2"},
"GHSA-3.json": {ID: "GHSA-3"},
})
})
defer cleanupTestServer()

cacheWriteBad(t, determineStoredAtPath(testDir, "my-db"), "this is not json!")

Expand All @@ -426,7 +419,7 @@ func TestNewZippedDB_FileChecks(t *testing.T) {

testDir := testutility.CreateTestDir(t)

ts, cleanupTestServer := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = writeOSVsZip(t, w, map[string]models.Vulnerability{
"file.json": {ID: "GHSA-1234"},
// only files with .json suffix should be loaded
Expand All @@ -435,7 +428,6 @@ func TestNewZippedDB_FileChecks(t *testing.T) {
"advisory-database-main/advisories/unreviewed/file.json": {ID: "GHSA-4321"},
})
})
defer cleanupTestServer()

db, err := local.NewZippedDB(testDir, "my-db", ts.URL, false)

Expand Down

0 comments on commit f999707

Please sign in to comment.