Skip to content

Commit

Permalink
test: adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
f1ames committed Jan 4, 2024
1 parent ea3c92e commit 493ec65
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
6 changes: 3 additions & 3 deletions admission-controller/synchronizer/src/utils/policy-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,16 @@ export class PolicyUpdater {
}

protected mapValidationAction(action: string) {
const actionNormalized = action.toLowerCase().trim();
const actionNormalized = (action || '').toLowerCase().trim();

switch (actionNormalized) {
case 'warn':
return 'Warn';
case 'deny':
return 'Deny';
default:
this._logger.error({ msg: 'Unknown validation action', action });
return 'Warn';
this._logger.error({ msg: 'Unknown validation action.', action });
return action;
}
}
}
15 changes: 15 additions & 0 deletions tests/src/cloud.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ describe(`Cloud (dir: ${mainDir})`, () => {
assertResource(policy2, 'cluster-1-binding-2-policy');
}, 45 * 1000);

it('correctly maps deny action', async () => {
mockServer = await startMockServer('actionDeny');

// Wait for getCluster query to run.
await waitForRequests(mockServer, 2);
// Wait for CRDs propagation.
await sleep(500);

const policy1 = await run('kubectl get monoklepolicy.monokle.io/cluster-1-binding-1-policy -o yaml');
const binding1 = await run('kubectl get monoklepolicybinding.monokle.io/cluster-1-binding-1-deny -o yaml');

assertResource(binding1, 'cluster-1-binding-1-deny');
assertResource(policy1, 'cluster-1-binding-1-policy');
}, 45 * 1000);

// @TODO updates policy CRDs with new data
// @TODO deletes policy CRDs
// @TODO updates binding CRDs with new data
Expand Down
20 changes: 20 additions & 0 deletions tests/src/utils/expected-crds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ export const EXPECTED_CRDS: Record<string, any> = {
}
}
},
'cluster-1-binding-1-deny': {
apiVersion: 'monokle.io/v1alpha1',
kind: 'MonoklePolicyBinding',
metadata: {
name: 'cluster-1-binding-1'
},
spec: {
policyName: 'cluster-1-binding-1-policy',
validationActions: ['Deny'],
matchResources: {
namespaceSelector: {
matchExpressions: [{
key: 'name',
operator: 'In',
values: ['my-namespace-0'],
}]
}
}
}
},
'cluster-1-binding-2': {
apiVersion: 'monokle.io/v1alpha1',
kind: 'MonoklePolicyBinding',
Expand Down
33 changes: 33 additions & 0 deletions tests/src/utils/response-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const RESPONSE_MOCK: Record<string, any> = {
{
id: "cluster-1-binding-1",
mode: "ALLOW_LIST",
action: "warn",
namespaces: ["ns-0","ns-1"],
policy: {
id: "cluster-1-binding-1-policy",
Expand All @@ -58,6 +59,7 @@ export const RESPONSE_MOCK: Record<string, any> = {
{
id: "cluster-1-binding-2",
mode: "ALLOW_LIST",
action: "warn",
namespaces: ["ns-2","ns-1"],
policy: {
id: "cluster-1-binding-2-policy",
Expand All @@ -71,5 +73,36 @@ export const RESPONSE_MOCK: Record<string, any> = {
]
}
}
},
actionDeny: {
data: {
getCluster: {
id: "cluster-1",
name: "Cluster 1",
namespaceSync: true,
namespaces: [
{
id: "ns-0",
name: "my-namespace-0"
}
],
bindings: [
{
id: "cluster-1-binding-1-deny",
mode: "ALLOW_LIST",
action: "deny",
namespaces: ["ns-0"],
policy: {
id: "cluster-1-binding-1-policy",
content: "plugins:\n open-policy-agent: true\n pod-security-standards: true\n",
project: {
id: "cluster-1-binding-1-policy-project",
name: "cluster-1-binding-1-policy-project"
}
}
}
]
}
}
}
}
2 changes: 1 addition & 1 deletion tests/src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cors from 'cors';
import _ from 'lodash';
import {RESPONSE_MOCK} from './response-mocks.js';

type ResponseMockName = 'empty' | 'emptySync' | 'dataAllow' | 'dataBlock';
type ResponseMockName = 'empty' | 'emptySync' | 'dataAllow' | 'actionDeny';

type MockServer = {
server: Server;
Expand Down

0 comments on commit 493ec65

Please sign in to comment.