Debian Codegraph @

kyaml/fn/runtime/exec/exec_test.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package exec_test
5
6 import (
7 "os"
8 "strings"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 "sigs.k8s.io/kustomize/kyaml/fn/runtime/exec"
14 "sigs.k8s.io/kustomize/kyaml/yaml"
15 )
16
17 func TestFunctionFilter_Filter(t *testing.T) {
18 wd, err := os.Getwd()
19 require.NoError(t, err)
20 tests := []struct {
21 name string
22 input []string
23 functionConfig string
24 expectedOutput []string
25 expectedError string
26 instance exec.Filter
27 }{
28 {
29 name: "exec_sed",
30 input: []string{
31 `apiVersion: apps/v1
32 kind: Deployment
33 metadata:
34 name: deployment-foo`,
35 `apiVersion: v1
36 kind: Service
37 metadata:
38 name: service-foo`,
39 },
40 expectedOutput: []string{
41 `apiVersion: apps/v1
42 kind: StatefulSet
43 metadata:
44 name: deployment-foo
45 annotations:
46 internal.config.kubernetes.io/path: 'statefulset_deployment-foo.yaml'
47 config.kubernetes.io/path: 'statefulset_deployment-foo.yaml'
48 `,
49 `apiVersion: v1
50 kind: Service
51 metadata:
52 name: service-foo
53 annotations:
54 internal.config.kubernetes.io/path: 'service_service-foo.yaml'
55 config.kubernetes.io/path: 'service_service-foo.yaml'
56 `,
57 },
58 expectedError: "",
59 instance: exec.Filter{
60 Path: "sh",
61 Env: []string{"TARGET=StatefulSet"},
62 Args: []string{"-c", `sed "s/Deployment/$TARGET/g"`},
63 WorkingDir: wd,
64 },
65 },
66 }
67
68 for i := range tests {
69 tt := tests[i]
70 t.Run(tt.name, func(t *testing.T) {
71 // initialize the inputs for the FunctionFilter
72 var inputs []*yaml.RNode
73 for i := range tt.input {
74 node, err := yaml.Parse(tt.input[i])
75 if !assert.NoError(t, err) {
76 t.FailNow()
77 }
78 inputs = append(inputs, node)
79 }
80 if tt.functionConfig != "" {
81 fc, err := yaml.Parse(tt.functionConfig)
82 if !assert.NoError(t, err) {
83 t.FailNow()
84 }
85 tt.instance.FunctionConfig = fc
86 }
87
88 // run the function
89 output, err := tt.instance.Filter(inputs)
90
91 // check for errors
92 if tt.expectedError != "" {
93 if !assert.EqualError(t, err, tt.expectedError) {
94 t.FailNow()
95 }
96 return
97 }
98 if !assert.NoError(t, err) {
99 t.FailNow()
100 }
101
102 // verify the output
103 var actual []string
104 for i := range output {
105 s, err := output[i].String()
106 if !assert.NoError(t, err) {
107 t.FailNow()
108 }
109 actual = append(actual, strings.TrimSpace(s))
110 }
111 var expected []string
112 for i := range tt.expectedOutput {
113 expected = append(expected, strings.TrimSpace(tt.expectedOutput[i]))
114 }
115 if !assert.Equal(t, expected, actual) {
116 t.FailNow()
117 }
118 })
119 }
120 }