Skip to content

Commit

Permalink
fix: fix K8S plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WitoDelnat committed Dec 5, 2023
1 parent f5cfcbf commit bff78f2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ it('should detect deprecation error - single resource, removal', async () => {

const hasErrors = response.runs.reduce((sum, r) => sum + r.results.length, 0);

expect(hasErrors).toBe(2);
expect(hasErrors).toBe(1);

const result = response.runs[0].results[0];
expectResult(result, 'K8S003', 'error', 'ReplicaSet');
expect(result.message.text).toContain('uses removed');

expectResult(response.runs[0].results[1], 'K8S004', 'warning', 'ReplicaSet');
});

it('should detect deprecation error - multiple resources, removal', async () => {
const {response} = await processResourcesInFolder('src/__tests__/resources/deprecations-2', 'v1.29');

const hasErrors = response.runs.reduce((sum, r) => sum + r.results.length, 0);

expect(hasErrors).toBe(5);
expect(hasErrors).toBe(3);

const result0 = response.runs[0].results[0];
expectResult(result0, 'K8S003', 'error', 'FlowSchema');
Expand All @@ -39,31 +37,27 @@ it('should detect deprecation error - multiple resources, removal', async () =>
expectResult(result1, 'K8S003', 'error', 'ValidatingWebhookConfiguration');
expect(result1.message.text).toContain('uses removed');

expectResult(response.runs[0].results[2], 'K8S004', 'warning', 'Pod');
expectResult(response.runs[0].results[3], 'K8S004', 'warning', 'FlowSchema');
expectResult(response.runs[0].results[4], 'K8S004', 'warning', 'ValidatingWebhookConfiguration');
expectResult(response.runs[0].results[2], 'K8S004', 'warning', 'SomeCustomResource');
});

it('should detect deprecation error - single resource, deprecation', async () => {
const {response} = await processResourcesInFolder('src/__tests__/resources/deprecations-3', 'v1.15');

const hasErrors = response.runs.reduce((sum, r) => sum + r.results.length, 0);

expect(hasErrors).toBe(2);
expect(hasErrors).toBe(1);

const result = response.runs[0].results[0];
expectResult(result, 'K8S002', 'warning', 'ReplicaSet');
expect(result.message.text).toContain('uses deprecated');

expectResult(response.runs[0].results[1], 'K8S004', 'warning', 'ReplicaSet');
});

it('should detect deprecation error - multiple resources, removal + deprecation', async () => {
const {response} = await processResourcesInFolder('src/__tests__/resources/deprecations-4', 'v1.28');

const hasErrors = response.runs.reduce((sum, r) => sum + r.results.length, 0);

expect(hasErrors).toBe(4);
expect(hasErrors).toBe(2);

const result0 = response.runs[0].results[0];
expectResult(result0, 'K8S002', 'warning', 'KubeSchedulerConfiguration');
Expand All @@ -72,24 +66,16 @@ it('should detect deprecation error - multiple resources, removal + deprecation'
const result1 = response.runs[0].results[1];
expectResult(result1, 'K8S003', 'error', 'RuntimeClass');
expect(result1.message.text).toContain('uses removed');

expectResult(response.runs[0].results[2], 'K8S004', 'warning', 'KubeSchedulerConfiguration');
expectResult(response.runs[0].results[3], 'K8S004', 'warning', 'RuntimeClass');
});

it('should rise warning when no apiVersion present (K8S004)', async () => {
const {response} = await processResourcesInFolder('src/__tests__/resources/no-apiversion');

const hasErrors = response.runs.reduce((sum, r) => sum + r.results.length, 0);
expect(hasErrors).toBe(2);

const error1 = response.runs[0].results[1];
expectResult(error1, 'K8S004', 'warning', 'FlowSchema');
expect(error1.message.text).toContain('Missing "apiVersion"');
expect(hasErrors).toBe(1);

const error2 = response.runs[0].results[0];
expectResult(error2, 'K8S004', 'warning', 'Pod');
expect(error2.message.text).toContain('Missing "apiVersion"');
const error1 = response.runs[0].results[0];
expectResult(error1, 'K8S004', 'warning', 'SomeCustomResource');
});

async function processResourcesInFolder(path: string, schemaVersion?: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ metadata:
webhooks:
- name: webhook-server.webhook-demo.svc
sideEffects: None
admissionReviewVersions: ["v1", "v1beta1"]
admissionReviewVersions: ['v1', 'v1beta1']
clientConfig:
service:
name: webhook-server
namespace: webhook-demo
path: "/validate"
caBundle: ""
path: '/validate'
caBundle: ''
rules:
- operations: [ "CREATE" ]
apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods"]
- operations: ['CREATE']
apiGroups: ['']
apiVersions: ['v1']
resources: ['pods']
---
apiVersion: flowcontrol.apiserver.k8s.io/v1beta2
kind: FlowSchema
Expand All @@ -28,28 +28,20 @@ spec:
name: exempt
rules:
- nonResourceRules:
- nonResourceURLs:
- "/healthz"
- "/livez"
- "/readyz"
verbs:
- "*"
- nonResourceURLs:
- '/healthz'
- '/livez'
- '/readyz'
verbs:
- '*'
subjects:
- kind: Group
group:
name: "system:unauthenticated"
name: 'system:unauthenticated'
---
apiVersion: v1
kind: Pod
apiVersion: example.io/v1
kind: SomeCustomResource
metadata:
name: pod-warning
labels:
app: pod-warning
name: crd-example
spec:
restartPolicy: OnFailure
securityContext:
runAsNonRoot: false
containers:
- name: busybox
image: busybox
command: ["sh", "-c", "echo I am running as user $(id -u)"]
hello: world
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,25 @@ metadata:
webhooks:
- name: webhook-server.webhook-demo.svc
sideEffects: None
admissionReviewVersions: ["v1", "v1beta1"]
admissionReviewVersions: ['v1', 'v1beta1']
clientConfig:
service:
name: webhook-server
namespace: webhook-demo
path: "/validate"
caBundle: ""
path: '/validate'
caBundle: ''
rules:
- operations: [ "CREATE" ]
apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods"]
- operations: ['CREATE']
apiGroups: ['']
apiVersions: ['v1']
resources: ['pods']
---
apiVersion: ''
kind: FlowSchema
apiVersion: example.io/v1
kind: SomeCustomResource
metadata:
name: health-for-strangers
spec:
matchingPrecedence: 1000
priorityLevelConfiguration:
name: exempt
rules:
- nonResourceRules:
- nonResourceURLs:
- "/healthz"
- "/livez"
- "/readyz"
verbs:
- "*"
subjects:
- kind: Group
group:
name: "system:unauthenticated"
hello: world
---
apiVersion: 'test-remove'
kind: Pod
Expand All @@ -52,4 +38,4 @@ spec:
containers:
- name: busybox
image: busybox
command: ["sh", "-c", "echo I am running as user $(id -u)"]
command: ['sh', '-c', 'echo I am running as user $(id -u)']

0 comments on commit bff78f2

Please sign in to comment.