Refactored resource to store all previous names and namespaces

This commit is contained in:
Natasha Sarkar
2021-02-01 14:50:20 -08:00
parent 6246262965
commit f71854a0c8
23 changed files with 376 additions and 442 deletions

View File

@@ -695,322 +695,165 @@ spec:
}
}
func TestSetOriginalNameAndNs(t *testing.T) {
input := `apiVersion: apps/v1
func TestResource_StorePreviousId(t *testing.T) {
tests := map[string]struct {
input string
newName string
newNs string
expected string
}{
"default namespace, first previous name": {
input: `apiVersion: apps/v1
kind: Secret
metadata:
name: newName`
factory := provider.NewDefaultDepProvider().GetResourceFactory()
resources, err := factory.SliceFromBytes([]byte(input))
if err != nil {
t.Fatal(err)
}
res := resources[0]
res.SetOriginalName("oldName", false)
res.SetOriginalNs("default", false)
expected := `apiVersion: apps/v1
name: oldName
`,
newName: "newName",
newNs: "",
expected: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
config.kubernetes.io/originalNs: default
name: newName
`
bytes, err := res.AsYAML()
if err != nil {
t.Fatal(err)
`,
},
"default namespace, second previous name": {
input: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
config.kubernetes.io/originalNs: default
name: oldName2
`,
newName: "newName",
newNs: "",
expected: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName,oldName2
config.kubernetes.io/originalNs: default,default
name: newName
`,
},
"non-default namespace": {
input: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
config.kubernetes.io/originalNs: default
name: oldName2
namespace: oldNamespace
`,
newName: "newName",
newNs: "newNamespace",
expected: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName,oldName2
config.kubernetes.io/originalNs: default,oldNamespace
name: newName
namespace: newNamespace
`,
},
}
factory := provider.NewDefaultDepProvider().GetResourceFactory()
for i := range tests {
test := tests[i]
t.Run(i, func(t *testing.T) {
resources, err := factory.SliceFromBytes([]byte(test.input))
if !assert.NoError(t, err) || len(resources) == 0 {
t.FailNow()
}
r := resources[0]
r.StorePreviousId()
r.SetName(test.newName)
if test.newNs != "" {
r.SetNamespace(test.newNs)
}
bytes, err := r.AsYAML()
if !assert.NoError(t, err) {
t.FailNow()
}
assert.Equal(t, test.expected, string(bytes))
})
}
assert.Equal(t, expected, string(bytes))
}
func TestGetOriginalName(t *testing.T) {
tests := []struct {
func TestResource_PrevIds(t *testing.T) {
tests := map[string]struct {
input string
expected string
expected []resid.ResId
}{
{
// no name annotation, return the name
"no previous IDs": {
input: `apiVersion: apps/v1
kind: Secret
metadata:
name: mySecret`,
expected: "mySecret",
name: name
`,
expected: nil,
},
{
// return name from name annotation
"one previous ID": {
input: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
name: newName`,
expected: "oldName",
},
}
for _, test := range tests {
factory := provider.NewDefaultDepProvider().GetResourceFactory()
resources, err := factory.SliceFromBytes([]byte(test.input))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, test.expected, resources[0].GetOriginalName())
}
}
func TestSetOriginalName(t *testing.T) {
tests := []struct {
input string
originalName string
overwrite bool
expected string
}{
{
// no original name set, overwrite is false
input: `apiVersion: apps/v1
kind: Secret
metadata:
name: newName`,
originalName: "oldName",
overwrite: false,
expected: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
config.kubernetes.io/originalNs: default
name: newName
`,
expected: []resid.ResId{
{
Gvk: resid.Gvk{Group: "apps", Version: "v1", Kind: "Secret"},
Name: "oldName",
Namespace: resid.DefaultNamespace,
},
},
},
{
// no original name set, overwrite is true
input: `apiVersion: apps/v1
kind: Secret
metadata:
name: newName`,
originalName: "oldName",
overwrite: true,
expected: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
name: newName
`,
},
{
// original name is set, overwrite is false, resource shouldn't change
"two ids": {
input: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
name: newName`,
originalName: "newOriginalName",
overwrite: false,
expected: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
name: newName
`,
},
{
// original name is set, overwrite is true, resource should change
input: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
name: newName`,
originalName: "newOriginalName",
overwrite: true,
expected: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: newOriginalName
name: newName
`,
},
}
for _, test := range tests {
factory := provider.NewDefaultDepProvider().GetResourceFactory()
resources, err := factory.SliceFromBytes([]byte(test.input))
if err != nil {
t.Fatal(err)
}
res := resources[0]
res.SetOriginalName(test.originalName, test.overwrite)
bytes, err := res.AsYAML()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, test.expected, string(bytes))
}
}
func TestGetOriginalNs(t *testing.T) {
tests := []struct {
input string
expected string
}{
{
// no namespace, return default
input: `apiVersion: apps/v1
kind: Secret
metadata:
name: mySecret`,
expected: "",
},
{
// return old namespace
input: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalNs: oldNamespace
name: mySecret
namespace: myNamespace`,
expected: "oldNamespace",
},
{
// return namespace
input: `apiVersion: apps/v1
kind: Secret
metadata:
name: mySecret
namespace: myNamespace`,
expected: "myNamespace",
},
}
for _, test := range tests {
factory := provider.NewDefaultDepProvider().GetResourceFactory()
resources, err := factory.SliceFromBytes([]byte(test.input))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, test.expected, resources[0].GetOriginalNs())
}
}
func TestSetOriginalNs(t *testing.T) {
tests := []struct {
input string
originalNs string
overwrite bool
expected string
}{
{
// no original namespace set, overwrite is false
input: `apiVersion: apps/v1
kind: Secret
metadata:
name: newName
namespace: newNamespace`,
originalNs: "oldNamespace",
overwrite: false,
expected: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalNs: oldNamespace
name: newName
namespace: newNamespace
`,
},
{
// no original name set, overwrite is true
input: `apiVersion: apps/v1
kind: Secret
metadata:
name: newName
namespace: newNamespace`,
originalNs: "oldNamespace",
overwrite: true,
expected: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalNs: oldNamespace
name: newName
namespace: newNamespace
`,
},
{
// original name is set, overwrite is false, resource shouldn't change
input: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalNs: oldNamespace
name: newName
namespace: newNamespace`,
originalNs: "newOriginalNamespace",
overwrite: false,
expected: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalNs: oldNamespace
name: newName
namespace: newNamespace
`,
},
{
// original name is set, overwrite is true, resource should change
input: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalNs: oldNamespace
name: newName
namespace: newNamespace`,
originalNs: "newOriginalNamespace",
overwrite: true,
expected: `apiVersion: apps/v1
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalNs: newOriginalNamespace
config.kubernetes.io/originalName: oldName,oldName2
config.kubernetes.io/originalNs: default,oldNamespace
name: newName
namespace: newNamespace
`,
expected: []resid.ResId{
{
Gvk: resid.Gvk{Group: "apps", Version: "v1", Kind: "Secret"},
Name: "oldName",
Namespace: resid.DefaultNamespace,
},
{
Gvk: resid.Gvk{Group: "apps", Version: "v1", Kind: "Secret"},
Name: "oldName2",
Namespace: "oldNamespace",
},
},
},
}
for _, test := range tests {
factory := provider.NewDefaultDepProvider().GetResourceFactory()
resources, err := factory.SliceFromBytes([]byte(test.input))
if err != nil {
t.Fatal(err)
}
res := resources[0]
res.SetOriginalNs(test.originalNs, test.overwrite)
bytes, err := res.AsYAML()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, test.expected, string(bytes))
factory := provider.NewDefaultDepProvider().GetResourceFactory()
for i := range tests {
test := tests[i]
t.Run(i, func(t *testing.T) {
resources, err := factory.SliceFromBytes([]byte(test.input))
if !assert.NoError(t, err) || len(resources) == 0 {
t.FailNow()
}
r := resources[0]
assert.Equal(t, test.expected, r.PrevIds())
})
}
}