...
1
2
3
4 package command_test
5
6 import (
7 "bytes"
8 "fmt"
9 "os"
10 "path/filepath"
11 "strings"
12 "testing"
13
14 "github.com/spf13/cobra"
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
17
18 "sigs.k8s.io/kustomize/kyaml/fn/framework"
19 "sigs.k8s.io/kustomize/kyaml/fn/framework/command"
20 "sigs.k8s.io/kustomize/kyaml/fn/framework/frameworktestutil"
21 "sigs.k8s.io/kustomize/kyaml/kio"
22 "sigs.k8s.io/kustomize/kyaml/yaml"
23 )
24
25 func TestCommand_dockerfile(t *testing.T) {
26 d := t.TempDir()
27
28
29 cmd := command.Build(&framework.SimpleProcessor{}, command.StandaloneEnabled, false)
30
31 command.AddGenerateDockerfile(cmd)
32
33
34 cmd.SetArgs([]string{"gen", d})
35 if !assert.NoError(t, cmd.Execute()) {
36 t.FailNow()
37 }
38
39 b, err := os.ReadFile(filepath.Join(d, "Dockerfile"))
40 if !assert.NoError(t, err) {
41 t.FailNow()
42 }
43
44 expected := `FROM golang:1.21-alpine as builder
45 ENV CGO_ENABLED=0
46 WORKDIR /go/src/
47 COPY go.mod go.sum ./
48 RUN go mod download
49 COPY . .
50 RUN go build -ldflags '-w -s' -v -o /usr/local/bin/function ./
51
52 FROM alpine:latest
53 COPY --from=builder /usr/local/bin/function /usr/local/bin/function
54 ENTRYPOINT ["function"]
55 `
56 if !assert.Equal(t, expected, string(b)) {
57 t.FailNow()
58 }
59 }
60
61
62 func TestCommand_standalone(t *testing.T) {
63 var config struct {
64 A string `json:"a" yaml:"a"`
65 B int `json:"b" yaml:"b"`
66 }
67
68 fn := func(items []*yaml.RNode) ([]*yaml.RNode, error) {
69 items = append(items, yaml.MustParse(`
70 apiVersion: apps/v1
71 kind: Deployment
72 metadata:
73 name: bar1
74 namespace: default
75 annotations:
76 foo: bar1
77 `))
78 for i := range items {
79 err := items[i].PipeE(yaml.SetAnnotation("a", config.A))
80 if err != nil {
81 return nil, err
82 }
83 err = items[i].PipeE(yaml.SetAnnotation("b", fmt.Sprintf("%v", config.B)))
84 if err != nil {
85 return nil, err
86 }
87 }
88
89 return items, nil
90 }
91
92 cmdFn := func() *cobra.Command {
93 return command.Build(&framework.SimpleProcessor{Filter: kio.FilterFunc(fn), Config: &config}, command.StandaloneEnabled, false)
94 }
95
96 tc := frameworktestutil.CommandResultsChecker{Command: cmdFn}
97 tc.Assert(t)
98 }
99
100 func TestCommand_standalone_stdin(t *testing.T) {
101 var config struct {
102 A string `json:"a" yaml:"a"`
103 B int `json:"b" yaml:"b"`
104 }
105
106 p := &framework.SimpleProcessor{
107 Config: &config,
108
109 Filter: kio.FilterFunc(func(items []*yaml.RNode) ([]*yaml.RNode, error) {
110 items = append(items, yaml.MustParse(`
111 apiVersion: apps/v1
112 kind: Deployment
113 metadata:
114 name: bar2
115 namespace: default
116 annotations:
117 foo: bar2
118 `))
119 for i := range items {
120 err := items[i].PipeE(yaml.SetAnnotation("a", config.A))
121 if err != nil {
122 return nil, err
123 }
124 err = items[i].PipeE(yaml.SetAnnotation("b", fmt.Sprintf("%v", config.B)))
125 if err != nil {
126 return nil, err
127 }
128 }
129
130 return items, nil
131 }),
132 }
133 cmd := command.Build(p, command.StandaloneEnabled, false)
134 cmd.SetIn(bytes.NewBufferString(`
135 apiVersion: apps/v1
136 kind: Deployment
137 metadata:
138 name: bar1
139 namespace: default
140 annotations:
141 foo: bar1
142 spec:
143 replicas: 1
144 `))
145 var out bytes.Buffer
146 cmd.SetOut(&out)
147 cmd.SetArgs([]string{filepath.Join("testdata", "standalone", "config.yaml"), "-"})
148
149 require.NoError(t, cmd.Execute())
150
151 require.Equal(t, strings.TrimSpace(`
152 apiVersion: apps/v1
153 kind: Deployment
154 metadata:
155 name: bar1
156 namespace: default
157 annotations:
158 foo: bar1
159 a: 'c'
160 b: '1'
161 spec:
162 replicas: 1
163 ---
164 apiVersion: apps/v1
165 kind: Deployment
166 metadata:
167 name: bar2
168 namespace: default
169 annotations:
170 foo: bar2
171 a: 'c'
172 b: '1'
173 `), strings.TrimSpace(out.String()))
174 }
175
View as plain text