mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Bug hunting; add more tests high and low.
This commit is contained in:
@@ -334,21 +334,90 @@ func TestFilter_Filter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetGVK(t *testing.T) {
|
func TestGetGVK(t *testing.T) {
|
||||||
obj, err := yaml.Parse(`
|
testCases := map[string]struct {
|
||||||
|
input string
|
||||||
|
expected resid.Gvk
|
||||||
|
parseError string
|
||||||
|
metaError string
|
||||||
|
}{
|
||||||
|
"empty": {
|
||||||
|
input: `
|
||||||
|
`,
|
||||||
|
parseError: "EOF",
|
||||||
|
},
|
||||||
|
"junk": {
|
||||||
|
input: `
|
||||||
|
congress: effective
|
||||||
|
`,
|
||||||
|
metaError: "missing Resource metadata",
|
||||||
|
},
|
||||||
|
"normal": {
|
||||||
|
input: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
`)
|
`,
|
||||||
|
expected: resid.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"},
|
||||||
|
},
|
||||||
|
"apiVersionOnlyWithSlash": {
|
||||||
|
input: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
`,
|
||||||
|
expected: resid.Gvk{Group: "apps", Version: "v1", Kind: ""},
|
||||||
|
},
|
||||||
|
// When apiVersion is just "v1" (not, say, "apps/v1"), that
|
||||||
|
// could be interpreted as Group="", Version="v1"
|
||||||
|
// (implying the original "core" api group) or the other way around
|
||||||
|
// (Group="v1", Version="").
|
||||||
|
// At the time of writing, fsslice.go does the latter -
|
||||||
|
// might have to change that.
|
||||||
|
"apiVersionOnlyNoSlash1": {
|
||||||
|
input: `
|
||||||
|
apiVersion: apps
|
||||||
|
`,
|
||||||
|
expected: resid.Gvk{Group: "apps", Version: "", Kind: ""},
|
||||||
|
},
|
||||||
|
"apiVersionOnlyNoSlash2": {
|
||||||
|
input: `
|
||||||
|
apiVersion: v1
|
||||||
|
`,
|
||||||
|
expected: resid.Gvk{Group: "v1", Version: "", Kind: ""},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for tn, tc := range testCases {
|
||||||
|
t.Run(tn, func(t *testing.T) {
|
||||||
|
obj, err := yaml.Parse(tc.input)
|
||||||
|
if len(tc.parseError) != 0 {
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected parse error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), tc.parseError) {
|
||||||
|
t.Errorf("expected parse err '%s', got '%v'", tc.parseError, err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
meta, err := obj.GetMeta()
|
meta, err := obj.GetMeta()
|
||||||
|
if len(tc.metaError) != 0 {
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected meta error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), tc.metaError) {
|
||||||
|
t.Errorf("expected meta err '%s', got '%v'", tc.metaError, err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
gvk := fsslice.GetGVK(meta)
|
gvk := fsslice.GetGVK(meta)
|
||||||
expected := resid.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}
|
if !assert.Equal(t, tc.expected, gvk) {
|
||||||
if !assert.Equal(t, expected, gvk) {
|
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,19 +8,16 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
filtertest_test "sigs.k8s.io/kustomize/api/testutils/filtertest"
|
filtertest_test "sigs.k8s.io/kustomize/api/testutils/filtertest"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var labelsFs = builtinconfig.MakeDefaultConfig().CommonLabels
|
|
||||||
|
|
||||||
func TestLabels_Filter(t *testing.T) {
|
func TestLabels_Filter(t *testing.T) {
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
input string
|
input string
|
||||||
expectedOutput string
|
expectedOutput string
|
||||||
filter Filter
|
filter Filter
|
||||||
fsSlice types.FsSlice
|
|
||||||
}{
|
}{
|
||||||
"add": {
|
"add": {
|
||||||
input: `
|
input: `
|
||||||
@@ -45,12 +42,20 @@ metadata:
|
|||||||
clown: emmett kelley
|
clown: emmett kelley
|
||||||
dragon: smaug
|
dragon: smaug
|
||||||
`,
|
`,
|
||||||
filter: Filter{Labels: labelMap{
|
filter: Filter{
|
||||||
|
Labels: labelMap{
|
||||||
"clown": "emmett kelley",
|
"clown": "emmett kelley",
|
||||||
"auto": "ford",
|
"auto": "ford",
|
||||||
"dragon": "smaug",
|
"dragon": "smaug",
|
||||||
"bean": "cannellini",
|
"bean": "cannellini",
|
||||||
}},
|
},
|
||||||
|
FsSlice: []types.FieldSpec{
|
||||||
|
{
|
||||||
|
Path: "metadata/labels",
|
||||||
|
CreateIfNotPresent: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"update": {
|
"update": {
|
||||||
input: `
|
input: `
|
||||||
@@ -73,13 +78,21 @@ metadata:
|
|||||||
bean: cannellini
|
bean: cannellini
|
||||||
clown: emmett kelley
|
clown: emmett kelley
|
||||||
`,
|
`,
|
||||||
filter: Filter{Labels: labelMap{
|
filter: Filter{
|
||||||
|
Labels: labelMap{
|
||||||
"clown": "emmett kelley",
|
"clown": "emmett kelley",
|
||||||
"hero": "superman",
|
"hero": "superman",
|
||||||
"fiend": "luthor",
|
"fiend": "luthor",
|
||||||
"bean": "cannellini",
|
"bean": "cannellini",
|
||||||
}},
|
}, FsSlice: []types.FieldSpec{
|
||||||
|
{
|
||||||
|
Path: "metadata/labels",
|
||||||
|
CreateIfNotPresent: true,
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
"data-fieldspecs": {
|
"data-fieldspecs": {
|
||||||
input: `
|
input: `
|
||||||
apiVersion: example.com/v1
|
apiVersion: example.com/v1
|
||||||
@@ -113,25 +126,178 @@ a:
|
|||||||
b:
|
b:
|
||||||
sleater: kinney
|
sleater: kinney
|
||||||
`,
|
`,
|
||||||
filter: Filter{Labels: labelMap{
|
filter: Filter{
|
||||||
|
Labels: labelMap{
|
||||||
"sleater": "kinney",
|
"sleater": "kinney",
|
||||||
}},
|
},
|
||||||
fsSlice: []types.FieldSpec{
|
FsSlice: []types.FieldSpec{
|
||||||
|
{
|
||||||
|
Path: "metadata/labels",
|
||||||
|
CreateIfNotPresent: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Path: "a/b",
|
Path: "a/b",
|
||||||
CreateIfNotPresent: true,
|
CreateIfNotPresent: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
"fieldSpecWithKind": {
|
||||||
|
input: `
|
||||||
|
apiVersion: example.com/v1
|
||||||
|
kind: Foo
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
---
|
||||||
|
apiVersion: example.com/v2
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
`,
|
||||||
|
expectedOutput: `
|
||||||
|
apiVersion: example.com/v1
|
||||||
|
kind: Foo
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
labels:
|
||||||
|
cheese: cheddar
|
||||||
|
---
|
||||||
|
apiVersion: example.com/v2
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
labels:
|
||||||
|
cheese: cheddar
|
||||||
|
a:
|
||||||
|
b:
|
||||||
|
cheese: cheddar
|
||||||
|
`,
|
||||||
|
filter: Filter{
|
||||||
|
Labels: labelMap{
|
||||||
|
"cheese": "cheddar",
|
||||||
|
},
|
||||||
|
FsSlice: []types.FieldSpec{
|
||||||
|
{
|
||||||
|
Path: "metadata/labels",
|
||||||
|
CreateIfNotPresent: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Gvk: resid.Gvk{
|
||||||
|
Kind: "Bar",
|
||||||
|
},
|
||||||
|
Path: "a/b",
|
||||||
|
CreateIfNotPresent: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
"fieldSpecWithVersion": {
|
||||||
|
input: `
|
||||||
|
apiVersion: example.com/v1
|
||||||
|
kind: Foo
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
---
|
||||||
|
apiVersion: example.com/v2
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
`,
|
||||||
|
expectedOutput: `
|
||||||
|
apiVersion: example.com/v1
|
||||||
|
kind: Foo
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
labels:
|
||||||
|
cheese: cheddar
|
||||||
|
a:
|
||||||
|
b:
|
||||||
|
cheese: cheddar
|
||||||
|
---
|
||||||
|
apiVersion: example.com/v2
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
labels:
|
||||||
|
cheese: cheddar
|
||||||
|
`,
|
||||||
|
filter: Filter{
|
||||||
|
Labels: labelMap{
|
||||||
|
"cheese": "cheddar",
|
||||||
|
},
|
||||||
|
FsSlice: []types.FieldSpec{
|
||||||
|
{
|
||||||
|
Path: "metadata/labels",
|
||||||
|
CreateIfNotPresent: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Gvk: resid.Gvk{
|
||||||
|
Version: "v1",
|
||||||
|
},
|
||||||
|
Path: "a/b",
|
||||||
|
CreateIfNotPresent: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"fieldSpecWithVersionInConfigButNoGroupInData": {
|
||||||
|
input: `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Foo
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
---
|
||||||
|
apiVersion: v2
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
`,
|
||||||
|
expectedOutput: `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Foo
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
labels:
|
||||||
|
cheese: cheddar
|
||||||
|
a:
|
||||||
|
b:
|
||||||
|
cheese: cheddar
|
||||||
|
---
|
||||||
|
apiVersion: v2
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: instance
|
||||||
|
labels:
|
||||||
|
cheese: cheddar
|
||||||
|
`,
|
||||||
|
filter: Filter{
|
||||||
|
Labels: labelMap{
|
||||||
|
"cheese": "cheddar",
|
||||||
|
},
|
||||||
|
FsSlice: []types.FieldSpec{
|
||||||
|
{
|
||||||
|
Path: "metadata/labels",
|
||||||
|
CreateIfNotPresent: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Gvk: resid.Gvk{
|
||||||
|
Group: "v1",
|
||||||
|
},
|
||||||
|
Path: "a/b",
|
||||||
|
CreateIfNotPresent: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for tn, tc := range testCases {
|
for tn, tc := range testCases {
|
||||||
t.Run(tn, func(t *testing.T) {
|
t.Run(tn, func(t *testing.T) {
|
||||||
filter := tc.filter
|
|
||||||
filter.FsSlice = append(labelsFs, tc.fsSlice...)
|
|
||||||
if !assert.Equal(t,
|
if !assert.Equal(t,
|
||||||
strings.TrimSpace(tc.expectedOutput),
|
strings.TrimSpace(tc.expectedOutput),
|
||||||
strings.TrimSpace(filtertest_test.RunFilter(t, tc.input, filter))) {
|
strings.TrimSpace(filtertest_test.RunFilter(t, tc.input, tc.filter))) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -23,8 +23,18 @@ labels:
|
|||||||
app: myApp
|
app: myApp
|
||||||
env: production
|
env: production
|
||||||
fieldSpecs:
|
fieldSpecs:
|
||||||
- path: metadata/labels
|
- path: spec/selector
|
||||||
create: true
|
create: true
|
||||||
|
version: v1
|
||||||
|
kind: Service
|
||||||
|
- path: metadata/labels
|
||||||
|
create: true
|
||||||
|
- path: spec/selector/matchLabels
|
||||||
|
create: true
|
||||||
|
kind: Deployment
|
||||||
|
- path: spec/template/metadata/labels
|
||||||
|
create: true
|
||||||
|
kind: Deployment
|
||||||
`, `
|
`, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
@@ -33,6 +43,23 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
ports:
|
ports:
|
||||||
- port: 7002
|
- port: 7002
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: mungebot
|
||||||
|
labels:
|
||||||
|
app: mungebot
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: mungebot
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: nginx
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.AssertActualEqualsExpected(rm, `
|
th.AssertActualEqualsExpected(rm, `
|
||||||
@@ -46,5 +73,31 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
ports:
|
ports:
|
||||||
- port: 7002
|
- port: 7002
|
||||||
|
selector:
|
||||||
|
app: myApp
|
||||||
|
env: production
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: myApp
|
||||||
|
env: production
|
||||||
|
name: mungebot
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: myApp
|
||||||
|
env: production
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: myApp
|
||||||
|
env: production
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx
|
||||||
|
name: nginx
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user