From bff78f2fdea31c432374053b04ebb63a1640960b Mon Sep 17 00:00:00 2001 From: Delnat Wito Date: Tue, 5 Dec 2023 18:31:08 +0100 Subject: [PATCH] fix: fix K8S plugin tests --- ...MonokleValidator.kubernetes-schema.test.ts | 30 ++++--------- .../resources/deprecations-2/resources.yaml | 44 ++++++++----------- .../resources/no-apiversion/resources.yaml | 36 +++++---------- 3 files changed, 37 insertions(+), 73 deletions(-) diff --git a/packages/validation/src/__tests__/MonokleValidator.kubernetes-schema.test.ts b/packages/validation/src/__tests__/MonokleValidator.kubernetes-schema.test.ts index 84a37e0cc..bd293445a 100644 --- a/packages/validation/src/__tests__/MonokleValidator.kubernetes-schema.test.ts +++ b/packages/validation/src/__tests__/MonokleValidator.kubernetes-schema.test.ts @@ -15,13 +15,11 @@ 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 () => { @@ -29,7 +27,7 @@ it('should detect deprecation error - multiple resources, removal', async () => 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'); @@ -39,9 +37,7 @@ 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 () => { @@ -49,13 +45,11 @@ it('should detect deprecation error - single resource, deprecation', 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, '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 () => { @@ -63,7 +57,7 @@ it('should detect deprecation error - multiple resources, removal + deprecation' 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'); @@ -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) { diff --git a/packages/validation/src/__tests__/resources/deprecations-2/resources.yaml b/packages/validation/src/__tests__/resources/deprecations-2/resources.yaml index 44f66bbca..0c1843e77 100644 --- a/packages/validation/src/__tests__/resources/deprecations-2/resources.yaml +++ b/packages/validation/src/__tests__/resources/deprecations-2/resources.yaml @@ -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 @@ -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 diff --git a/packages/validation/src/__tests__/resources/no-apiversion/resources.yaml b/packages/validation/src/__tests__/resources/no-apiversion/resources.yaml index bc70daa52..b6f90c262 100644 --- a/packages/validation/src/__tests__/resources/no-apiversion/resources.yaml +++ b/packages/validation/src/__tests__/resources/no-apiversion/resources.yaml @@ -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 @@ -52,4 +38,4 @@ spec: containers: - name: busybox image: busybox - command: ["sh", "-c", "echo I am running as user $(id -u)"] \ No newline at end of file + command: ['sh', '-c', 'echo I am running as user $(id -u)']