Skip to content

Commit ad91176

Browse files
Fix evaluationInterval 'watch' bug
Policy generator plugin does not allow evaluationInterval to be set to "watch". The policy generator should generate policy successfully with "watch" Ref: https://issues.redhat.com/browse/ACM-17774 Signed-off-by: yiraeChristineKim <[email protected]>
1 parent 7717bea commit ad91176

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

internal/plugin.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,9 @@ func (p *Plugin) assertValidConfig() error {
10281028
p.PolicyDefaults.Namespace, policy.Name)
10291029
}
10301030

1031-
if policy.EvaluationInterval.Compliant != "" && policy.EvaluationInterval.Compliant != "never" {
1031+
if policy.EvaluationInterval.Compliant != "" &&
1032+
policy.EvaluationInterval.Compliant != "never" &&
1033+
policy.EvaluationInterval.Compliant != "watch" {
10321034
_, err := time.ParseDuration(policy.EvaluationInterval.Compliant)
10331035
if err != nil {
10341036
return fmt.Errorf(
@@ -1037,7 +1039,9 @@ func (p *Plugin) assertValidConfig() error {
10371039
}
10381040
}
10391041

1040-
if policy.EvaluationInterval.NonCompliant != "" && policy.EvaluationInterval.NonCompliant != "never" {
1042+
if policy.EvaluationInterval.NonCompliant != "" &&
1043+
policy.EvaluationInterval.NonCompliant != "never" &&
1044+
policy.EvaluationInterval.Compliant != "watch" {
10411045
_, err := time.ParseDuration(policy.EvaluationInterval.NonCompliant)
10421046
if err != nil {
10431047
return fmt.Errorf(
@@ -1149,7 +1153,8 @@ func (p *Plugin) assertValidConfig() error {
11491153
}
11501154
}
11511155

1152-
if evalInterval.Compliant != "" && evalInterval.Compliant != "never" {
1156+
if evalInterval.Compliant != "" && evalInterval.Compliant != "never" &&
1157+
evalInterval.Compliant != "watch" {
11531158
_, err := time.ParseDuration(evalInterval.Compliant)
11541159
if err != nil {
11551160
return fmt.Errorf(
@@ -1161,7 +1166,8 @@ func (p *Plugin) assertValidConfig() error {
11611166
}
11621167
}
11631168

1164-
if evalInterval.NonCompliant != "" && evalInterval.NonCompliant != "never" {
1169+
if evalInterval.NonCompliant != "" && evalInterval.NonCompliant != "never" &&
1170+
evalInterval.NonCompliant != "watch" {
11651171
_, err := time.ParseDuration(evalInterval.NonCompliant)
11661172
if err != nil {
11671173
return fmt.Errorf(

internal/plugin_config_test.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ policies:
956956
assertEqual(t, err.Error(), expected)
957957
}
958958

959-
func TestConfigInvalidEvalInterval(t *testing.T) {
959+
func TestConfigEvalInterval(t *testing.T) {
960960
t.Parallel()
961961
tmpDir := t.TempDir()
962962
createConfigMap(t, tmpDir, "configmap.yaml")
@@ -1025,6 +1025,12 @@ func TestConfigInvalidEvalInterval(t *testing.T) {
10251025
`the policy policy-app has an invalid policy.evaluationInterval.noncompliant value: time: unknown unit ` +
10261026
`"w" in duration "1w2d"`,
10271027
},
1028+
{
1029+
`{"compliant": "watch", "noncompliant": "watch"}`,
1030+
`{"compliant": "watch", "noncompliant": "watch"}`,
1031+
`{"compliant": "watch", "noncompliant": "watch"}`,
1032+
``,
1033+
},
10281034
}
10291035

10301036
for _, test := range tests {
@@ -1056,12 +1062,19 @@ policies:
10561062
)
10571063

10581064
p := Plugin{}
1065+
10591066
err := p.Config([]byte(config), tmpDir)
10601067
if err == nil {
1061-
t.Fatal("Expected an error but did not get one")
1068+
if test.expectedMsg != "" {
1069+
t.Fatal("Expected an error but did not receive one")
1070+
}
1071+
} else {
1072+
if test.expectedMsg == "" {
1073+
t.Fatalf("Expected no error but received: %v", err)
1074+
}
1075+
1076+
assertEqual(t, err.Error(), test.expectedMsg)
10621077
}
1063-
1064-
assertEqual(t, err.Error(), test.expectedMsg)
10651078
},
10661079
)
10671080
}

0 commit comments

Comments
 (0)