...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package test
16
17 import (
18 "testing"
19
20 "github.com/ghodss/yaml"
21 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
22 )
23
24
25
26
27 func addTestLabels(u *unstructured.Unstructured) {
28 labels := u.GetLabels()
29 if labels == nil {
30 labels = map[string]string{}
31 }
32 AddTestLabelsToMap(labels)
33 u.SetLabels(labels)
34 }
35
36
37
38 func AddTestLabelsToMap(m map[string]string) {
39 m["cnrm-test"] = "true"
40 }
41
42 func ToUnstruct(t *testing.T, bytes []byte) *unstructured.Unstructured {
43 u := &unstructured.Unstructured{}
44 err := yaml.Unmarshal(bytes, u)
45 if err != nil {
46 t.Fatalf("error unmarshalling bytes to unstruct: %v", err)
47 }
48 addTestLabels(u)
49 return u
50 }
51
52 func ToUnstructWithNamespace(t *testing.T, b []byte, namespace string) *unstructured.Unstructured {
53 u := ToUnstruct(t, b)
54 if u.GetNamespace() == "" {
55 u.SetNamespace(namespace)
56 }
57 return u
58 }
59
View as plain text