testing correct sync bahavior

This commit is contained in:
2026-05-24 04:16:15 -06:00
parent b02747493a
commit 7ee5736704
3 changed files with 51 additions and 35 deletions
+2 -2
View File
@@ -147,7 +147,7 @@ func TestHandlerRefreshUnauthorized(t *testing.T) {
}
}
func TestHandlerRefreshOK(t *testing.T) {
func TestHandlerRefreshSyncError(t *testing.T) {
ts, _ := newTestServer(t, mirrorOK)
@@ -160,7 +160,7 @@ func TestHandlerRefreshOK(t *testing.T) {
if err != nil {
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)
}
}
+1 -1
View File
@@ -44,8 +44,8 @@ func (r *RepoSync) Sync() error {
// create map of pkgname to filenames from current db
cachedPkgs, err := r.buildMap(repo)
if err != nil {
// pass through to cover initialization of repos
slog.Warn("failed to read current db", "err", err)
return err
}
// call cache db fetch
+48 -32
View File
@@ -15,15 +15,39 @@ import (
// create a CacheClient to mock cache.Fetch and cache.FetchDB to fulfill
// expected interface in RepoSync 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) {
m.fetched = append(m.fetched, relPath)
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) {
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")
@@ -56,7 +80,7 @@ func TestCachedFiles(t *testing.T) {
t.Fatalf("failed to create RepoMaint: %v", err)
}
repoPath := filepath.Join("core", repoArch)
rs.root.MkdirAll(repoPath, 0750)
newTestRepo(t, repoPath, rs)
for _, f := range testFiles {
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)
}
repoPath := filepath.Join(rs.repos[0], repoArch)
rs.root.MkdirAll(repoPath, 0750)
newTestRepo(t, repoPath, rs)
for _, f := range testFiles {
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
@@ -134,21 +145,7 @@ func TestUpdatablePkgs(t *testing.T) {
t.Fatalf("failed to create RepoMaint: %v", err)
}
repoPath := filepath.Join(rs.repos[0], repoArch)
rs.root.MkdirAll(repoPath, 0750)
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)
}
newTestRepo(t, repoPath, rs)
files, err := rs.updatablePkgs(repo, testMap)
require.NoError(t, err)
@@ -157,3 +154,22 @@ func TestUpdatablePkgs(t *testing.T) {
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)
}