From 4c959081f3ac0097371d45fe01229ae4380738d8 Mon Sep 17 00:00:00 2001 From: garethgeorge Date: Wed, 15 Nov 2023 23:50:50 -0800 Subject: [PATCH] chore: fix test coverage --- internal/config/config_test.go | 10 ++--- internal/database/oplog/oplog_test.go | 42 ++++++++++++++++++- .../serializationutil/serializationutil.go | 3 ++ .../serializationutil_test.go | 4 +- 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 601aa5a0..9ec29887 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -35,7 +35,7 @@ func TestConfig(t *testing.T) { { name: "default config", config: NewDefaultConfig(), - store: &CachingValidatingStore{ConfigStore: &YamlFileStore{Path: dir + "/default-config.yaml"}}, + store: &CachingValidatingStore{ConfigStore: &JsonFileStore{Path: dir + "/default-config.json"}}, }, { name: "simple valid config", @@ -43,14 +43,14 @@ func TestConfig(t *testing.T) { Repos: []*v1.Repo{testRepo}, Plans: []*v1.Plan{testPlan}, }, - store: &CachingValidatingStore{ConfigStore: &YamlFileStore{Path: dir + "/valid-config.yaml"}}, + store: &CachingValidatingStore{ConfigStore: &JsonFileStore{Path: dir + "/valid-config.json"}}, }, { name: "plan references non-existent repo", config: &v1.Config{ Plans: []*v1.Plan{testPlan}, }, - store: &CachingValidatingStore{ConfigStore: &YamlFileStore{Path: dir + "/invalid-config.yaml"}}, + store: &CachingValidatingStore{ConfigStore: &JsonFileStore{Path: dir + "/invalid-config.json"}}, wantErr: true, wantErrContains: "repo \"test-repo\" not found", }, @@ -62,7 +62,7 @@ func TestConfig(t *testing.T) { testRepo, }, }, - store: &CachingValidatingStore{ConfigStore: &YamlFileStore{Path: dir + "/invalid-config2.yaml"}}, + store: &CachingValidatingStore{ConfigStore: &JsonFileStore{Path: dir + "/invalid-config2.json"}}, wantErr: true, wantErrContains: "repo test-repo: duplicate id", }, @@ -81,7 +81,7 @@ func TestConfig(t *testing.T) { }, }, }, - store: &CachingValidatingStore{ConfigStore: &YamlFileStore{Path: dir + "/invalid-config3.yaml"}}, + store: &CachingValidatingStore{ConfigStore: &JsonFileStore{Path: dir + "/invalid-config3.json"}}, wantErr: true, wantErrContains: "invalid cron \"bad cron\"", }, diff --git a/internal/database/oplog/oplog_test.go b/internal/database/oplog/oplog_test.go index f9d2993a..4cde8949 100644 --- a/internal/database/oplog/oplog_test.go +++ b/internal/database/oplog/oplog_test.go @@ -32,16 +32,50 @@ func TestAddOperation(t *testing.T) { wantErr bool }{ { - name: "basic operation", + name: "no operation", op: &v1.Operation{ Id: 0, }, + wantErr: true, + }, + { + name: "basic backup operation", + op: &v1.Operation{ + Id: 0, + Op: &v1.Operation_OperationBackup{}, + }, + wantErr: false, + }, + { + name: "basic snapshot operation", + op: &v1.Operation{ + Id: 0, + Op: &v1.Operation_OperationIndexSnapshot{ + OperationIndexSnapshot: &v1.OperationIndexSnapshot{ + Snapshot: &v1.ResticSnapshot{ + Id: "test", + }, + }, + }, + }, wantErr: false, }, + { + name: "basic snapshot operation with no snapshot", + op: &v1.Operation{ + Id: 0, + Op: &v1.Operation_OperationIndexSnapshot{ + OperationIndexSnapshot: &v1.OperationIndexSnapshot{ + }, + }, + }, + wantErr: true, + }, { name: "operation with ID", op: &v1.Operation{ Id: 1, + Op: &v1.Operation_OperationBackup{}, }, wantErr: true, }, @@ -50,6 +84,7 @@ func TestAddOperation(t *testing.T) { op: &v1.Operation{ Id: 0, RepoId: "testrepo", + Op: &v1.Operation_OperationBackup{}, }, }, { @@ -57,6 +92,7 @@ func TestAddOperation(t *testing.T) { op: &v1.Operation{ Id: 0, PlanId: "testplan", + Op: &v1.Operation_OperationBackup{}, }, }, } @@ -88,16 +124,19 @@ func TestListOperation(t *testing.T) { PlanId: "plan1", RepoId: "repo1", DisplayMessage: "op1", + Op: &v1.Operation_OperationBackup{}, }, { PlanId: "plan1", RepoId: "repo2", DisplayMessage: "op2", + Op: &v1.Operation_OperationBackup{}, }, { PlanId: "plan2", RepoId: "repo2", DisplayMessage: "op3", + Op: &v1.Operation_OperationBackup{}, }, } @@ -175,6 +214,7 @@ func TestBigIO(t *testing.T) { if err := log.Add(&v1.Operation{ PlanId: "plan1", RepoId: "repo1", + Op: &v1.Operation_OperationBackup{}, }); err != nil { t.Fatalf("error adding operation: %s", err) } diff --git a/internal/database/serializationutil/serializationutil.go b/internal/database/serializationutil/serializationutil.go index ef104ee1..62b5db90 100644 --- a/internal/database/serializationutil/serializationutil.go +++ b/internal/database/serializationutil/serializationutil.go @@ -46,5 +46,8 @@ func BytesToKey(b []byte) []byte { } func NormalizeSnapshotId(id string) string { + if len(id) < 8 { + return id + } return id[:8] } \ No newline at end of file diff --git a/internal/database/serializationutil/serializationutil_test.go b/internal/database/serializationutil/serializationutil_test.go index be826d69..524a768e 100644 --- a/internal/database/serializationutil/serializationutil_test.go +++ b/internal/database/serializationutil/serializationutil_test.go @@ -6,7 +6,7 @@ func TestItoa(t *testing.T) { nums := []int64{0, 1, 2, 3, 4, 1 << 32, int64(1) << 62} for _, num := range nums { b := Itob(num) - if Btoi(b) != num { + if v, _ := Btoi(b); v != num { t.Errorf("itob/btoi failed for %d", num) } } @@ -16,7 +16,7 @@ func TestStob(t *testing.T) { strs := []string{"", "a", "ab", "abc", "abcd", "abcde", "abcdef"} for _, str := range strs { b := Stob(str) - if val, _ := Btos(b); val != str { + if val, _, _ := Btos(b); val != str { t.Errorf("stob/btos failed for %s", str) } }