mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Delete substitution and fix delete setters
This commit is contained in:
@@ -7,116 +7,12 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
|
||||
)
|
||||
|
||||
func TestDelete_Filter(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
description string
|
||||
setter string
|
||||
input string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
name: "delete-replicas",
|
||||
setter: "replicas",
|
||||
input: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
annotations:
|
||||
replicas: 3 # {"$openapi":"replicas"}
|
||||
spec:
|
||||
replicas: 3 # {"$openapi":"replicas"}
|
||||
`,
|
||||
expectedOutput: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
annotations:
|
||||
replicas: 3
|
||||
spec:
|
||||
replicas: 3
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "delete-foo-annotation",
|
||||
setter: "foo",
|
||||
input: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
annotations:
|
||||
foo: 3 # {"$openapi":"foo"}
|
||||
`,
|
||||
expectedOutput: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
annotations:
|
||||
foo: 3
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "delete-replicas-enum",
|
||||
setter: "replicas",
|
||||
input: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
spec:
|
||||
replicas: 1 # {"$openapi":"replicas"}
|
||||
`,
|
||||
expectedOutput: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
spec:
|
||||
replicas: 1
|
||||
`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// parse the input to be modified
|
||||
r, err := yaml.Parse(test.input)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// invoke the delete
|
||||
instance := &Delete{FieldName: test.setter}
|
||||
result, err := instance.Filter(r)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// compare the actual and expected output
|
||||
actual, err := result.String()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
actual = strings.TrimSpace(actual)
|
||||
expected := strings.TrimSpace(test.expectedOutput)
|
||||
if !assert.Equal(t, expected, actual) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var resourcefile2 = `apiVersion: resource.dev/v1alpha1
|
||||
kind: resourcefile
|
||||
metadata:
|
||||
@@ -154,7 +50,8 @@ func TestDelete_Filter2(t *testing.T) {
|
||||
|
||||
//add a deleter definition
|
||||
dd := DeleterDefinition{
|
||||
Name: "image",
|
||||
Name: "image",
|
||||
DefinitionPrefix: fieldmeta.SetterDefinitionPrefix,
|
||||
}
|
||||
|
||||
err = dd.DeleteFromFile(path)
|
||||
|
||||
Reference in New Issue
Block a user