diff --git a/internal/protoutil/conditions.go b/internal/protoutil/conditions.go index 20a41324..8c6aa6e3 100644 --- a/internal/protoutil/conditions.go +++ b/internal/protoutil/conditions.go @@ -8,6 +8,7 @@ var startConditionsMap = map[v1.Hook_Condition]bool{ v1.Hook_CONDITION_CHECK_START: true, v1.Hook_CONDITION_PRUNE_START: true, v1.Hook_CONDITION_SNAPSHOT_START: true, + v1.Hook_CONDITION_FORGET_START: true, } var errorConditionsMap = map[v1.Hook_Condition]bool{ @@ -15,6 +16,7 @@ var errorConditionsMap = map[v1.Hook_Condition]bool{ v1.Hook_CONDITION_CHECK_ERROR: true, v1.Hook_CONDITION_PRUNE_ERROR: true, v1.Hook_CONDITION_SNAPSHOT_ERROR: true, + v1.Hook_CONDITION_FORGET_ERROR: true, v1.Hook_CONDITION_UNKNOWN: true, } @@ -26,6 +28,7 @@ var successConditionsMap = map[v1.Hook_Condition]bool{ v1.Hook_CONDITION_CHECK_SUCCESS: true, v1.Hook_CONDITION_PRUNE_SUCCESS: true, v1.Hook_CONDITION_SNAPSHOT_SUCCESS: true, + v1.Hook_CONDITION_FORGET_SUCCESS: true, } // IsErrorCondition returns true if the event is an error condition. diff --git a/internal/protoutil/conditions_test.go b/internal/protoutil/conditions_test.go new file mode 100644 index 00000000..fb814609 --- /dev/null +++ b/internal/protoutil/conditions_test.go @@ -0,0 +1,73 @@ +package protoutil + +import ( + "strings" + "testing" + + v1 "github.com/garethgeorge/backrest/gen/go/v1" +) + +func TestStartConditionsMap(t *testing.T) { + // Test that all conditions with "_START" in their name are correctly identified by IsStartCondition + for cond := range v1.Hook_Condition_name { + condEnum := v1.Hook_Condition(cond) + condName := condEnum.String() + if strings.Contains(condName, "_START") { + if !IsStartCondition(condEnum) { + t.Errorf("Condition %s contains '_START' but IsStartCondition returned false", condName) + } + } else { + if IsStartCondition(condEnum) { + t.Errorf("Condition %s does not contain '_START' but IsStartCondition returned true", condName) + } + } + } +} + +func TestErrorConditionsMap(t *testing.T) { + // Special case for CONDITION_UNKNOWN which should be identified as an error condition + if !IsErrorCondition(v1.Hook_CONDITION_UNKNOWN) { + t.Errorf("CONDITION_UNKNOWN should be identified as an error condition") + } + + // Special case for ANY_ERROR which should be identified as an error condition + if !IsErrorCondition(v1.Hook_CONDITION_ANY_ERROR) { + t.Errorf("CONDITION_ANY_ERROR should be identified as an error condition") + } + + // Test that all conditions with "_ERROR" in their name are correctly identified by IsErrorCondition + for cond := range v1.Hook_Condition_name { + condEnum := v1.Hook_Condition(cond) + condName := condEnum.String() + + // Skip the special cases we already checked + if condEnum == v1.Hook_CONDITION_UNKNOWN || condEnum == v1.Hook_CONDITION_ANY_ERROR { + continue + } + + if strings.Contains(condName, "_ERROR") { + if !IsErrorCondition(condEnum) { + t.Errorf("Condition %s contains '_ERROR' but IsErrorCondition returned false", condName) + } + } else if IsErrorCondition(condEnum) { + t.Errorf("Condition %s does not contain '_ERROR' but IsErrorCondition returned true", condName) + } + } +} + +func TestSuccessConditionsMap(t *testing.T) { + // Test that all conditions with "_SUCCESS" in their name are correctly identified by IsSuccessCondition + for cond := range v1.Hook_Condition_name { + condEnum := v1.Hook_Condition(cond) + condName := condEnum.String() + if strings.Contains(condName, "_SUCCESS") { + if !IsSuccessCondition(condEnum) { + t.Errorf("Condition %s contains '_SUCCESS' but IsSuccessCondition returned false", condName) + } + } else { + if IsSuccessCondition(condEnum) { + t.Errorf("Condition %s does not contain '_SUCCESS' but IsSuccessCondition returned true", condName) + } + } + } +}