-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* run the just built binary against this project * move eval calls into 'script' section to fail build if eval fails * fix test bug due to stale test db files having expired TTL cache items. Also add new test of TTL expiration. * be certain the TTL expires by waiting * add myself to the list of the usual suspects.
- Loading branch information
Showing
4 changed files
with
75 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ package ossindex | |
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/dgraph-io/badger" | ||
"github.com/sonatype-nexus-community/nancy/types" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
|
@@ -29,6 +30,7 @@ import ( | |
"path" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
const purl = "pkg:github/BurntSushi/[email protected]" | ||
|
@@ -165,20 +167,7 @@ func TestAuditPackages_ErrorBadResponseBody(t *testing.T) { | |
|
||
func TestAuditPackages_NewPackage(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodPost, r.Method) | ||
assert.Equal(t, "/", r.URL.EscapedPath()) | ||
|
||
w.WriteHeader(http.StatusOK) | ||
|
||
coordinates := []types.Coordinate{ | ||
{ | ||
Coordinates: "pkg:github/burntsushi/[email protected]", | ||
Reference: "https://ossindex.sonatype.org/component/pkg:github/burntsushi/[email protected]", | ||
Vulnerabilities: []types.Vulnerability{}, | ||
}, | ||
} | ||
jsonCoordinates, _ := json.Marshal(coordinates) | ||
_, _ = w.Write(jsonCoordinates) | ||
verifyClientCallAndWriteValidPackageResponse(t, r, w) | ||
})) | ||
defer ts.Close() | ||
ossIndexUrl = ts.URL | ||
|
@@ -192,6 +181,21 @@ func TestAuditPackages_NewPackage(t *testing.T) { | |
assert.Nil(t, err) | ||
} | ||
|
||
func verifyClientCallAndWriteValidPackageResponse(t *testing.T, r *http.Request, w http.ResponseWriter) { | ||
assert.Equal(t, http.MethodPost, r.Method) | ||
assert.Equal(t, "/", r.URL.EscapedPath()) | ||
w.WriteHeader(http.StatusOK) | ||
coordinates := []types.Coordinate{ | ||
{ | ||
Coordinates: "pkg:github/burntsushi/[email protected]", | ||
Reference: "https://ossindex.sonatype.org/component/pkg:github/burntsushi/[email protected]", | ||
Vulnerabilities: []types.Vulnerability{}, | ||
}, | ||
} | ||
jsonCoordinates, _ := json.Marshal(coordinates) | ||
_, _ = w.Write(jsonCoordinates) | ||
} | ||
|
||
// File copies a single file from src to dst | ||
func copyFile(src, dst string) error { | ||
var err error | ||
|
@@ -257,7 +261,7 @@ func copyDir(src string, dst string) error { | |
|
||
func TestAuditPackages_SinglePackage_Cached(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
t.Errorf("No call should occur with nil package. called: %v", r) | ||
t.Errorf("No call should occur with previously cached package. called: %v", r) | ||
})) | ||
defer ts.Close() | ||
ossIndexUrl = ts.URL | ||
|
@@ -268,6 +272,50 @@ func TestAuditPackages_SinglePackage_Cached(t *testing.T) { | |
// put test db cache dir in expected location | ||
cacheValueDir := getDatabaseDirectory() + "/" + dbValueDirName | ||
assert.Nil(t, copyDir("testdata/golang", cacheValueDir)) | ||
// need to re-set the cached package to avoid test failures due to expiration of the TTL for the cached item | ||
db, err := openDb(getDatabaseDirectory()) | ||
assert.Nil(t, err) | ||
assert.Nil(t, db.Update(func(txn *badger.Txn) error { | ||
var coordJson, _ = json.Marshal(expectedCoordinate) | ||
err := txn.SetWithTTL([]byte(strings.ToLower(lowerCasePurl)), []byte(coordJson), time.Hour*12) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
})) | ||
assert.Nil(t, db.Close()) | ||
|
||
coordinates, err := AuditPackages([]string{purl}) | ||
assert.Equal(t, []types.Coordinate{expectedCoordinate}, coordinates) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestAuditPackages_SinglePackage_Cached_WithExpiredTTL(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
verifyClientCallAndWriteValidPackageResponse(t, r, w) | ||
})) | ||
defer ts.Close() | ||
ossIndexUrl = ts.URL | ||
|
||
teardownTestCase := setupTestCaseMoveCacheDb(t) | ||
defer teardownTestCase(t) | ||
|
||
// put test db cache dir in expected location | ||
cacheValueDir := getDatabaseDirectory() + "/" + dbValueDirName | ||
assert.Nil(t, copyDir("testdata/golang", cacheValueDir)) | ||
// need to re-set the cached package with short TTL for the cached item to ensure it expires before we read it | ||
db, err := openDb(getDatabaseDirectory()) | ||
assert.Nil(t, err) | ||
assert.Nil(t, db.Update(func(txn *badger.Txn) error { | ||
var coordJson, _ = json.Marshal(expectedCoordinate) | ||
err := txn.SetWithTTL([]byte(strings.ToLower(lowerCasePurl)), []byte(coordJson), time.Second*1) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
})) | ||
assert.Nil(t, db.Close()) | ||
time.Sleep(2 * time.Second) | ||
|
||
coordinates, err := AuditPackages([]string{purl}) | ||
assert.Equal(t, []types.Coordinate{expectedCoordinate}, coordinates) | ||
|