-
Notifications
You must be signed in to change notification settings - Fork 40k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A policy with 0 rules should return an error #51782
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ import ( | |
) | ||
|
||
const policyDefV1alpha1 = ` | ||
apiVersion: audit.k8s.io/v1beta1 | ||
apiVersion: audit.k8s.io/v1alpha1 | ||
kind: Policy | ||
rules: | ||
- level: None | ||
|
@@ -91,16 +91,11 @@ var expectedPolicy = &audit.Policy{ | |
} | ||
|
||
func TestParserV1alpha1(t *testing.T) { | ||
// Create a policy file. | ||
f, err := ioutil.TempFile("", "policy.yaml") | ||
require.NoError(t, err) | ||
defer os.Remove(f.Name()) | ||
|
||
_, err = f.WriteString(policyDefV1alpha1) | ||
f, err := writePolicy(policyDefV1alpha1, t) | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
defer os.Remove(f) | ||
|
||
policy, err := LoadPolicyFromFile(f.Name()) | ||
policy, err := LoadPolicyFromFile(f) | ||
require.NoError(t, err) | ||
|
||
assert.Len(t, policy.Rules, 3) // Sanity check. | ||
|
@@ -110,20 +105,49 @@ func TestParserV1alpha1(t *testing.T) { | |
} | ||
|
||
func TestParserV1beta1(t *testing.T) { | ||
// Create a policy file. | ||
f, err := ioutil.TempFile("", "policy.yaml") | ||
f, err := writePolicy(policyDefV1beta1, t) | ||
require.NoError(t, err) | ||
defer os.Remove(f.Name()) | ||
defer os.Remove(f) | ||
|
||
_, err = f.WriteString(policyDefV1beta1) | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
|
||
policy, err := LoadPolicyFromFile(f.Name()) | ||
policy, err := LoadPolicyFromFile(f) | ||
require.NoError(t, err) | ||
|
||
assert.Len(t, policy.Rules, 3) // Sanity check. | ||
if !reflect.DeepEqual(policy, expectedPolicy) { | ||
t.Errorf("Unexpected policy! Diff:\n%s", diff.ObjectDiff(policy, expectedPolicy)) | ||
} | ||
} | ||
|
||
func TestPolicyCntCheck(t *testing.T) { | ||
//a set of testCases | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment adds no new information => not needed |
||
var testCases = []struct { | ||
caseName, policy string | ||
}{ | ||
{ | ||
"policyWithNoRule", | ||
`apiVersion: audit.k8s.io/v1beta1 | ||
kind: Policy`, | ||
}, | ||
{"emptyPolicyFile", ""}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
f, err := writePolicy(tc.policy, t) | ||
require.NoError(t, err) | ||
defer os.Remove(f) | ||
|
||
_, err = LoadPolicyFromFile(f) | ||
assert.Errorf(t, err, "loaded illegal policy with 0 rules from testCase %s", tc.caseName) | ||
} | ||
} | ||
|
||
func writePolicy(policy string, t *testing.T) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nit: usually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thx for your careful instruction! I learned a lot.^_^ |
||
f, err := ioutil.TempFile("", "policy.yaml") | ||
require.NoError(t, err) | ||
|
||
_, err = f.WriteString(policy) | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
|
||
return f.Name(), nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
disregard. This should be alpha.