testing correct sync bahavior
This commit is contained in:
@@ -147,7 +147,7 @@ func TestHandlerRefreshUnauthorized(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandlerRefreshOK(t *testing.T) {
|
func TestHandlerRefreshSyncError(t *testing.T) {
|
||||||
|
|
||||||
ts, _ := newTestServer(t, mirrorOK)
|
ts, _ := newTestServer(t, mirrorOK)
|
||||||
|
|
||||||
@@ -160,7 +160,7 @@ func TestHandlerRefreshOK(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("POST failed: %v", err)
|
t.Fatalf("POST failed: %v", err)
|
||||||
}
|
}
|
||||||
if resp.StatusCode != http.StatusNoContent {
|
if resp.StatusCode != http.StatusInternalServerError {
|
||||||
t.Errorf("expected %d got %d", http.StatusNoContent, resp.StatusCode)
|
t.Errorf("expected %d got %d", http.StatusNoContent, resp.StatusCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,8 +44,8 @@ func (r *RepoSync) Sync() error {
|
|||||||
// create map of pkgname to filenames from current db
|
// create map of pkgname to filenames from current db
|
||||||
cachedPkgs, err := r.buildMap(repo)
|
cachedPkgs, err := r.buildMap(repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// pass through to cover initialization of repos
|
||||||
slog.Warn("failed to read current db", "err", err)
|
slog.Warn("failed to read current db", "err", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// call cache db fetch
|
// call cache db fetch
|
||||||
|
|||||||
@@ -15,15 +15,39 @@ import (
|
|||||||
// create a CacheClient to mock cache.Fetch and cache.FetchDB to fulfill
|
// create a CacheClient to mock cache.Fetch and cache.FetchDB to fulfill
|
||||||
// expected interface in RepoSync struct
|
// expected interface in RepoSync struct
|
||||||
type mockCache struct {
|
type mockCache struct {
|
||||||
fetched []string
|
fetched []string
|
||||||
|
fetchDBCalled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockCache) FetchDB() error { return nil }
|
func (m *mockCache) FetchDB() error {
|
||||||
|
m.fetchDBCalled = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
func (m *mockCache) Fetch(relPath string) (*cache.CacheFile, error) {
|
func (m *mockCache) Fetch(relPath string) (*cache.CacheFile, error) {
|
||||||
m.fetched = append(m.fetched, relPath)
|
m.fetched = append(m.fetched, relPath)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newTestRepo(t *testing.T, repoPath string, rs *RepoSync) {
|
||||||
|
// create newTestRepo with db file
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
src, err := os.Open(filepath.Join("testdata", "pre.core.db.tar.gz"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to open src db for copy: %v", err)
|
||||||
|
}
|
||||||
|
defer src.Close()
|
||||||
|
rs.root.MkdirAll(repoPath, 0750)
|
||||||
|
dst, err := rs.root.Create(filepath.Join(repoPath, "core.db.tar.gz"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to open dst db for copy: %s", err)
|
||||||
|
}
|
||||||
|
defer dst.Close()
|
||||||
|
if _, err := io.Copy(dst, src); err != nil {
|
||||||
|
t.Fatalf("failed to copy db: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseDesc(t *testing.T) {
|
func TestParseDesc(t *testing.T) {
|
||||||
t.Run("parse good desc file", func(t *testing.T) {
|
t.Run("parse good desc file", func(t *testing.T) {
|
||||||
r := strings.NewReader("%NAME%\nlinux\n\n\n%FAKEKEY%\nFAKEINFO\n\n%FILENAME%\nlinux-7.0.9\n")
|
r := strings.NewReader("%NAME%\nlinux\n\n\n%FAKEKEY%\nFAKEINFO\n\n%FILENAME%\nlinux-7.0.9\n")
|
||||||
@@ -56,7 +80,7 @@ func TestCachedFiles(t *testing.T) {
|
|||||||
t.Fatalf("failed to create RepoMaint: %v", err)
|
t.Fatalf("failed to create RepoMaint: %v", err)
|
||||||
}
|
}
|
||||||
repoPath := filepath.Join("core", repoArch)
|
repoPath := filepath.Join("core", repoArch)
|
||||||
rs.root.MkdirAll(repoPath, 0750)
|
newTestRepo(t, repoPath, rs)
|
||||||
for _, f := range testFiles {
|
for _, f := range testFiles {
|
||||||
rs.root.WriteFile(filepath.Join(repoPath, f), []byte{}, 0644)
|
rs.root.WriteFile(filepath.Join(repoPath, f), []byte{}, 0644)
|
||||||
}
|
}
|
||||||
@@ -84,23 +108,10 @@ func TestBuildMap(t *testing.T) {
|
|||||||
t.Fatalf("failed to create RepoMaint: %v", err)
|
t.Fatalf("failed to create RepoMaint: %v", err)
|
||||||
}
|
}
|
||||||
repoPath := filepath.Join(rs.repos[0], repoArch)
|
repoPath := filepath.Join(rs.repos[0], repoArch)
|
||||||
rs.root.MkdirAll(repoPath, 0750)
|
newTestRepo(t, repoPath, rs)
|
||||||
for _, f := range testFiles {
|
for _, f := range testFiles {
|
||||||
rs.root.WriteFile(filepath.Join(repoPath, f), []byte{}, 0644)
|
rs.root.WriteFile(filepath.Join(repoPath, f), []byte{}, 0644)
|
||||||
}
|
}
|
||||||
src, err := os.Open(filepath.Join("testdata", "pre.core.db.tar.gz"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to open src db for copy: %v", err)
|
|
||||||
}
|
|
||||||
defer src.Close()
|
|
||||||
dst, err := rs.root.Create(filepath.Join(repoPath, "core.db.tar.gz"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to open dst db for copy: %s", err)
|
|
||||||
}
|
|
||||||
defer dst.Close()
|
|
||||||
if _, err := io.Copy(dst, src); err != nil {
|
|
||||||
t.Fatalf("failed to copy db: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// run tests
|
// run tests
|
||||||
|
|
||||||
@@ -134,21 +145,7 @@ func TestUpdatablePkgs(t *testing.T) {
|
|||||||
t.Fatalf("failed to create RepoMaint: %v", err)
|
t.Fatalf("failed to create RepoMaint: %v", err)
|
||||||
}
|
}
|
||||||
repoPath := filepath.Join(rs.repos[0], repoArch)
|
repoPath := filepath.Join(rs.repos[0], repoArch)
|
||||||
rs.root.MkdirAll(repoPath, 0750)
|
newTestRepo(t, repoPath, rs)
|
||||||
|
|
||||||
src, err := os.Open(filepath.Join("testdata", "pre.core.db.tar.gz"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to open src db for copy: %v", err)
|
|
||||||
}
|
|
||||||
defer src.Close()
|
|
||||||
dst, err := rs.root.Create(filepath.Join(repoPath, "core.db.tar.gz"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to open dst db for copy: %s", err)
|
|
||||||
}
|
|
||||||
defer dst.Close()
|
|
||||||
if _, err := io.Copy(dst, src); err != nil {
|
|
||||||
t.Fatalf("failed to copy db: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
files, err := rs.updatablePkgs(repo, testMap)
|
files, err := rs.updatablePkgs(repo, testMap)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -157,3 +154,22 @@ func TestUpdatablePkgs(t *testing.T) {
|
|||||||
assert.NotContains(t, files, unexpectedFileName)
|
assert.NotContains(t, files, unexpectedFileName)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSyncNoDB(t *testing.T) {
|
||||||
|
// can't create db and test initialization behavior without
|
||||||
|
// going out of scope. We test fall through to FetchDB and
|
||||||
|
// second attempt to read db file fails
|
||||||
|
c := &mockCache{
|
||||||
|
fetched: make([]string, 5),
|
||||||
|
}
|
||||||
|
rs, err := NewRepoSync(c, t.TempDir(), []string{"core"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create RepoSync: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = rs.Sync()
|
||||||
|
// err here is correct behavior as no db exists
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.True(t, c.fetchDBCalled)
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user