test(soteria): cover helper fallback branches

This commit is contained in:
codex 2026-04-20 19:48:35 -03:00
parent d923551cf8
commit 8d4dd26d14
4 changed files with 67 additions and 0 deletions

View File

@ -46,6 +46,12 @@ func TestJobSupportHelpersCoverFallbackAndFormattingBranches(t *testing.T) {
if got := parseBoolWithDefault("yes", false); got != true { if got := parseBoolWithDefault("yes", false); got != true {
t.Fatalf("expected explicit true parse, got %v", got) t.Fatalf("expected explicit true parse, got %v", got)
} }
if got := parseBoolWithDefault("0", true); got != false {
t.Fatalf("expected numeric false parse, got %v", got)
}
if got := parseBoolWithDefault("maybe", false); got != false {
t.Fatalf("expected unknown value to fall back, got %v", got)
}
} }
func TestJobNameBackupCommandAndResticEnvCoverRemainingBranches(t *testing.T) { func TestJobNameBackupCommandAndResticEnvCoverRemainingBranches(t *testing.T) {

View File

@ -167,6 +167,15 @@ func TestResolvePVCMountedNodeIgnoresDeadPodsAndFindsMountedClaim(t *testing.T)
if err != nil || nodeName != "" { if err != nil || nodeName != "" {
t.Fatalf("expected missing pvc mount to return empty node, got %q %v", nodeName, err) t.Fatalf("expected missing pvc mount to return empty node, got %q %v", nodeName, err)
} }
clientset := k8sfake.NewSimpleClientset()
clientset.PrependReactor("list", "pods", func(action k8stesting.Action) (bool, runtime.Object, error) {
return true, nil, errors.New("resolve pods exploded")
})
client = &Client{Clientset: clientset}
if _, err := client.resolvePVCMountedNode(context.Background(), "apps", "data"); err == nil || !strings.Contains(err.Error(), "resolve pods exploded") {
t.Fatalf("expected pod list error, got %v", err)
}
} }
func TestReadBackupJobLogCoversSuccessAndListFailures(t *testing.T) { func TestReadBackupJobLogCoversSuccessAndListFailures(t *testing.T) {

View File

@ -528,6 +528,9 @@ func TestExecuteRestoreResticAndHelperFunctions(t *testing.T) {
if got := targetPVCName("", "Data_PVC"); got != "restore-data-pvc" { if got := targetPVCName("", "Data_PVC"); got != "restore-data-pvc" {
t.Fatalf("expected default restore prefix, got %q", got) t.Fatalf("expected default restore prefix, got %q", got)
} }
if got := targetPVCName("___", "Data_PVC"); got != "restore-data-pvc" {
t.Fatalf("expected sanitized-empty prefix to fall back to restore, got %q", got)
}
if got := targetPVCName("drill", "cache"); got != "drill-cache" { if got := targetPVCName("drill", "cache"); got != "drill-cache" {
t.Fatalf("expected explicit prefix, got %q", got) t.Fatalf("expected explicit prefix, got %q", got)
} }

View File

@ -1097,6 +1097,55 @@ func TestParseResticStoredBytesFromJSONSummary(t *testing.T) {
} }
} }
func TestParseResticStoredBytesRejectsEmptyAndInvalidSummaries(t *testing.T) {
testCases := []string{
"",
`{"message_type":"summary","data_added":"not-a-number"}`,
`Added to the repository: 120.500 MiB (not-a-size stored)`,
}
for _, logBody := range testCases {
if value, ok := parseResticStoredBytes(logBody); ok || value != 0 {
t.Fatalf("expected invalid summary to fail parse, got value=%f ok=%v for %q", value, ok, logBody)
}
}
}
func TestHandleUICoversMethodNilRendererAndIndexErrors(t *testing.T) {
t.Run("method not allowed", func(t *testing.T) {
srv := &Server{}
req := httptest.NewRequest(http.MethodPost, "/", nil)
res := httptest.NewRecorder()
srv.handleUI(res, req)
if res.Code != http.StatusMethodNotAllowed {
t.Fatalf("expected method not allowed, got %d", res.Code)
}
})
t.Run("nil renderer", func(t *testing.T) {
srv := &Server{}
req := httptest.NewRequest(http.MethodGet, "/", nil)
res := httptest.NewRecorder()
srv.handleUI(res, req)
if res.Code != http.StatusInternalServerError || !strings.Contains(res.Body.String(), "UI renderer is unavailable") {
t.Fatalf("expected nil renderer error, got %d %q", res.Code, res.Body.String())
}
})
t.Run("index render failure", func(t *testing.T) {
srv := &Server{ui: &uiRenderer{}}
req := httptest.NewRequest(http.MethodGet, "/", nil)
res := httptest.NewRecorder()
srv.handleUI(res, req)
if res.Code != http.StatusInternalServerError || !strings.Contains(res.Body.String(), "UI assets are not available") {
t.Fatalf("expected ui index error, got %d %q", res.Code, res.Body.String())
}
})
}
func TestMetricsStayPublic(t *testing.T) { func TestMetricsStayPublic(t *testing.T) {
srv := &Server{ srv := &Server{
cfg: &config.Config{AuthRequired: true, AllowedGroups: []string{"admin"}}, cfg: &config.Config{AuthRequired: true, AllowedGroups: []string{"admin"}},