File: namebackreferences_test.go

package info (click to toggle)
golang-k8s-sigs-kustomize-api 0.20.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,768 kB
  • sloc: makefile: 206; sh: 67
file content (123 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package builtinconfig

import (
	"reflect"
	"testing"

	"sigs.k8s.io/kustomize/api/types"
	"sigs.k8s.io/kustomize/kyaml/resid"
)

func TestMergeAll(t *testing.T) {
	fsSlice1 := []types.FieldSpec{
		{
			Gvk: resid.Gvk{
				Kind: "Pod",
			},
			Path:               "path/to/a/name",
			CreateIfNotPresent: false,
		},
		{
			Gvk: resid.Gvk{
				Kind: "Deployment",
			},
			Path:               "another/path/to/some/name",
			CreateIfNotPresent: false,
		},
	}
	fsSlice2 := []types.FieldSpec{
		{
			Gvk: resid.Gvk{
				Kind: "Job",
			},
			Path:               "morepath/to/name",
			CreateIfNotPresent: false,
		},
		{
			Gvk: resid.Gvk{
				Kind: "StatefulSet",
			},
			Path:               "yet/another/path/to/a/name",
			CreateIfNotPresent: false,
		},
	}

	nbrsSlice1 := nbrSlice{
		{
			Gvk: resid.Gvk{
				Kind: "ConfigMap",
			},
			Referrers: fsSlice1,
		},
		{
			Gvk: resid.Gvk{
				Kind: "Secret",
			},
			Referrers: fsSlice2,
		},
	}
	nbrsSlice2 := nbrSlice{
		{
			Gvk: resid.Gvk{
				Kind: "ConfigMap",
			},
			Referrers: fsSlice1,
		},
		{
			Gvk: resid.Gvk{
				Kind: "Secret",
			},
			Referrers: fsSlice2,
		},
	}
	expected := nbrSlice{
		{
			Gvk: resid.Gvk{
				Kind: "ConfigMap",
			},
			Referrers: fsSlice1,
		},
		{
			Gvk: resid.Gvk{
				Kind: "Secret",
			},
			Referrers: fsSlice2,
		},
	}
	actual, err := nbrsSlice1.mergeAll(nbrsSlice2)
	if err != nil {
		t.Fatalf("unexpected err: %v", err)
	}
	if !reflect.DeepEqual(actual, expected) {
		t.Fatalf("expected\n %v\n but got\n %v\n", expected, actual)
	}
}

func TestNbrSlice_DeepCopy(t *testing.T) {
	original := make(nbrSlice, 2, 4)
	original[0] = NameBackReferences{Gvk: resid.FromKind("A"), Referrers: types.FsSlice{{Path: "a"}}}
	original[1] = NameBackReferences{Gvk: resid.FromKind("B"), Referrers: types.FsSlice{{Path: "b"}}}

	copied := original.DeepCopy()

	original, _ = original.mergeOne(NameBackReferences{Gvk: resid.FromKind("C"), Referrers: types.FsSlice{{Path: "c"}}})

	// perform mutations which should not affect original
	copied.Swap(0, 1)
	copied[0].Referrers[0].Path = "very b" // ensure Referrers are not shared
	_, _ = copied.mergeOne(NameBackReferences{Gvk: resid.FromKind("D"), Referrers: types.FsSlice{{Path: "d"}}})

	// if DeepCopy does not work, original would be {very b,a,d} instead of {a,b,c}
	expected := nbrSlice{
		{Gvk: resid.FromKind("A"), Referrers: types.FsSlice{{Path: "a"}}},
		{Gvk: resid.FromKind("B"), Referrers: types.FsSlice{{Path: "b"}}},
		{Gvk: resid.FromKind("C"), Referrers: types.FsSlice{{Path: "c"}}},
	}

	if !reflect.DeepEqual(original, expected) {
		t.Fatalf("original affected by mutations to copied object:\ngot\t%+v,\nexpected: %+v", original, expected)
	}
}