From 067559127dabee78263bdc8d4ece54a076398ad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ortiz=20Garc=C3=ADa?= Date: Mon, 11 Oct 2021 19:16:08 -0500 Subject: [PATCH] test cases for framework wrapping/unwrapping bug --- kyaml/fn/framework/framework_test.go | 132 +++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/kyaml/fn/framework/framework_test.go b/kyaml/fn/framework/framework_test.go index 400f1ba7fc..82961ad199 100644 --- a/kyaml/fn/framework/framework_test.go +++ b/kyaml/fn/framework/framework_test.go @@ -90,3 +90,135 @@ results: tags: foo: bar`, strings.TrimSpace(out.String())) } + +func TestExecute_NoErrorResult(t *testing.T) { + singleDeployment := `kind: Deployment +apiVersion: v1 +metadata: + name: tester + namespace: default +spec: + replicas: 0` + resourceListDeployment := `kind: ResourceList +apiVersion: config.kubernetes.io/v1alpha1 +items: +- kind: Deployment + apiVersion: v1 + metadata: + name: tester + namespace: default + spec: + replicas: 0` + outputResourceList := `apiVersion: config.kubernetes.io/v1alpha1 +kind: ResourceList +items: +- kind: Deployment + apiVersion: v1 + metadata: + name: tester + namespace: default + spec: + replicas: 0 +results: + name: Incompatible config + items: + - message: bad value for replicas + severity: error + resourceRef: + apiVersion: v1 + kind: Deployment + name: tester + namespace: default + field: + path: .spec.Replicas + currentValue: "0" + suggestedValue: "3" + file: + path: /path/to/deployment.yaml + - message: some error + severity: error` + + testCases := []struct { + desc string + noWrap bool + wrapKind string + wrapAPIVersion string + input string + want string + }{ + { + desc: "resource list", + input: resourceListDeployment, + want: outputResourceList, + }, + { + desc: "individual resources", + input: singleDeployment, + want: singleDeployment, + }, + { + desc: "wrap resource list", + wrapKind: kio.ResourceListKind, + wrapAPIVersion: kio.ResourceListAPIVersion, + input: resourceListDeployment, + want: outputResourceList, + }, + { + desc: "unwrap resource list", + noWrap: true, + input: resourceListDeployment, + want: singleDeployment, + }, + { + desc: "wrap individual resources", + wrapKind: kio.ResourceListKind, + wrapAPIVersion: kio.ResourceListAPIVersion, + input: singleDeployment, + want: outputResourceList, + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + p := framework.ResourceListProcessorFunc(func(rl *framework.ResourceList) error { + rl.Result = &framework.Result{ + Name: "Incompatible config", + Items: []framework.ResultItem{ + { + Message: "bad value for replicas", + Severity: framework.Error, + ResourceRef: yaml.ResourceIdentifier{ + TypeMeta: yaml.TypeMeta{APIVersion: "v1", Kind: "Deployment"}, + NameMeta: yaml.NameMeta{Name: "tester", Namespace: "default"}, + }, + Field: framework.Field{ + Path: ".spec.Replicas", + CurrentValue: "0", + SuggestedValue: "3", + }, + File: framework.File{ + Path: "/path/to/deployment.yaml", + Index: 0, + }, + }, + { + Message: "some error", + Severity: framework.Error, + }, + }, + } + return nil + }) + got := new(bytes.Buffer) + source := &kio.ByteReadWriter{ + Reader: bytes.NewBufferString(tc.input), + Writer: got, + WrappingAPIVersion: tc.wrapAPIVersion, + WrappingKind: tc.wrapKind, + NoWrap: tc.noWrap, + } + assert.NoError(t, framework.Execute(p, source)) + assert.Equal(t, tc.want, strings.TrimSpace(got.String())) + }) + } +}