mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
276 lines
5.9 KiB
Go
276 lines
5.9 KiB
Go
// Copyright 2019 The Kubernetes Authors.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package commands_test
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
|
"sigs.k8s.io/kustomize/kyaml/openapi"
|
|
)
|
|
|
|
func TestSetCommand(t *testing.T) {
|
|
var tests = []struct {
|
|
name string
|
|
inputOpenAPI string
|
|
input string
|
|
args []string
|
|
out string
|
|
expectedOpenAPI string
|
|
expectedResources string
|
|
}{
|
|
{
|
|
name: "set replicas",
|
|
args: []string{"replicas", "4", "--description", "hi there", "--set-by", "pw"},
|
|
out: "set 1 fields\n",
|
|
inputOpenAPI: `
|
|
apiVersion: v1alpha1
|
|
kind: Example
|
|
openAPI:
|
|
definitions:
|
|
io.k8s.cli.setters.replicas:
|
|
description: hello world
|
|
x-k8s-cli:
|
|
setter:
|
|
name: replicas
|
|
value: "3"
|
|
setBy: me
|
|
`,
|
|
input: `
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: nginx-deployment
|
|
spec:
|
|
replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"}
|
|
`,
|
|
expectedOpenAPI: `
|
|
apiVersion: v1alpha1
|
|
kind: Example
|
|
openAPI:
|
|
definitions:
|
|
io.k8s.cli.setters.replicas:
|
|
description: hi there
|
|
x-k8s-cli:
|
|
setter:
|
|
name: replicas
|
|
value: "4"
|
|
setBy: pw
|
|
`,
|
|
expectedResources: `
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: nginx-deployment
|
|
spec:
|
|
replicas: 4 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"}
|
|
`,
|
|
},
|
|
{
|
|
name: "set replicas no description",
|
|
args: []string{"replicas", "4"},
|
|
out: "set 1 fields\n",
|
|
inputOpenAPI: `
|
|
apiVersion: v1alpha1
|
|
kind: Example
|
|
openAPI:
|
|
definitions:
|
|
io.k8s.cli.setters.replicas:
|
|
description: hello world
|
|
x-k8s-cli:
|
|
setter:
|
|
name: replicas
|
|
value: "3"
|
|
setBy: me
|
|
`,
|
|
input: `
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: nginx-deployment
|
|
spec:
|
|
replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"}
|
|
`,
|
|
expectedOpenAPI: `
|
|
apiVersion: v1alpha1
|
|
kind: Example
|
|
openAPI:
|
|
definitions:
|
|
io.k8s.cli.setters.replicas:
|
|
description: hello world
|
|
x-k8s-cli:
|
|
setter:
|
|
name: replicas
|
|
value: "4"
|
|
setBy: me
|
|
`,
|
|
expectedResources: `
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: nginx-deployment
|
|
spec:
|
|
replicas: 4 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"}
|
|
`,
|
|
},
|
|
{
|
|
name: "set image",
|
|
args: []string{"tag", "1.8.1"},
|
|
out: "set 1 fields\n",
|
|
inputOpenAPI: `
|
|
apiVersion: v1alpha1
|
|
kind: Example
|
|
openAPI:
|
|
definitions:
|
|
io.k8s.cli.setters.image:
|
|
x-k8s-cli:
|
|
setter:
|
|
name: image
|
|
value: "nginx"
|
|
io.k8s.cli.setters.tag:
|
|
x-k8s-cli:
|
|
setter:
|
|
name: tag
|
|
value: "1.7.9"
|
|
io.k8s.cli.substitutions.image:
|
|
x-k8s-cli:
|
|
substitution:
|
|
name: image
|
|
pattern: IMAGE:TAG
|
|
values:
|
|
- marker: IMAGE
|
|
ref: '#/definitions/io.k8s.cli.setters.image'
|
|
- marker: TAG
|
|
ref: '#/definitions/io.k8s.cli.setters.tag'
|
|
`,
|
|
input: `
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: nginx-deployment
|
|
spec:
|
|
replicas: 3
|
|
template:
|
|
spec:
|
|
containers:
|
|
- name: nginx
|
|
image: nginx:1.7.9 # {"$ref":"#/definitions/io.k8s.cli.substitutions.image"}
|
|
- name: sidecar
|
|
image: sidecar:1.7.9
|
|
`,
|
|
expectedOpenAPI: `
|
|
apiVersion: v1alpha1
|
|
kind: Example
|
|
openAPI:
|
|
definitions:
|
|
io.k8s.cli.setters.image:
|
|
x-k8s-cli:
|
|
setter:
|
|
name: image
|
|
value: "nginx"
|
|
io.k8s.cli.setters.tag:
|
|
x-k8s-cli:
|
|
setter:
|
|
name: tag
|
|
value: "1.8.1"
|
|
io.k8s.cli.substitutions.image:
|
|
x-k8s-cli:
|
|
substitution:
|
|
name: image
|
|
pattern: IMAGE:TAG
|
|
values:
|
|
- marker: IMAGE
|
|
ref: '#/definitions/io.k8s.cli.setters.image'
|
|
- marker: TAG
|
|
ref: '#/definitions/io.k8s.cli.setters.tag'
|
|
|
|
`,
|
|
expectedResources: `
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: nginx-deployment
|
|
spec:
|
|
replicas: 3
|
|
template:
|
|
spec:
|
|
containers:
|
|
- name: nginx
|
|
image: nginx:1.8.1 # {"$ref":"#/definitions/io.k8s.cli.substitutions.image"}
|
|
- name: sidecar
|
|
image: sidecar:1.7.9
|
|
`,
|
|
},
|
|
}
|
|
for i := range tests {
|
|
test := tests[i]
|
|
t.Run(test.name, func(t *testing.T) {
|
|
// reset the openAPI afterward
|
|
openapi.ResetOpenAPI()
|
|
defer openapi.ResetOpenAPI()
|
|
|
|
f, err := ioutil.TempFile("", "k8s-cli-")
|
|
if !assert.NoError(t, err) {
|
|
t.FailNow()
|
|
}
|
|
defer os.Remove(f.Name())
|
|
err = ioutil.WriteFile(f.Name(), []byte(test.inputOpenAPI), 0600)
|
|
if !assert.NoError(t, err) {
|
|
t.FailNow()
|
|
}
|
|
commands.GetOpenAPIFile = func(args []string) (s string, err error) {
|
|
return f.Name(), nil
|
|
}
|
|
|
|
r, err := ioutil.TempFile("", "k8s-cli-*.yaml")
|
|
if !assert.NoError(t, err) {
|
|
t.FailNow()
|
|
}
|
|
defer os.Remove(r.Name())
|
|
err = ioutil.WriteFile(r.Name(), []byte(test.input), 0600)
|
|
if !assert.NoError(t, err) {
|
|
t.FailNow()
|
|
}
|
|
|
|
runner := commands.NewSetRunner("")
|
|
out := &bytes.Buffer{}
|
|
runner.Command.SetOut(out)
|
|
runner.Command.SetArgs(append([]string{r.Name()}, test.args...))
|
|
err = runner.Command.Execute()
|
|
if !assert.NoError(t, err) {
|
|
t.FailNow()
|
|
}
|
|
|
|
if !assert.Equal(t, test.out, out.String()) {
|
|
t.FailNow()
|
|
}
|
|
|
|
actualResources, err := ioutil.ReadFile(r.Name())
|
|
if !assert.NoError(t, err) {
|
|
t.FailNow()
|
|
}
|
|
if !assert.Equal(t,
|
|
strings.TrimSpace(test.expectedResources),
|
|
strings.TrimSpace(string(actualResources))) {
|
|
t.FailNow()
|
|
}
|
|
|
|
actualOpenAPI, err := ioutil.ReadFile(f.Name())
|
|
if !assert.NoError(t, err) {
|
|
t.FailNow()
|
|
}
|
|
if !assert.Equal(t,
|
|
strings.TrimSpace(test.expectedOpenAPI),
|
|
strings.TrimSpace(string(actualOpenAPI))) {
|
|
t.FailNow()
|
|
}
|
|
})
|
|
}
|
|
}
|