1
2
3
4 package object
5
6 import (
7 "testing"
8
9 rbacv1 "k8s.io/api/rbac/v1"
10 "k8s.io/apimachinery/pkg/runtime/schema"
11 )
12
13 func TestParseObjMetadata(t *testing.T) {
14 tests := map[string]struct {
15 invStr string
16 inventory *ObjMetadata
17 isError bool
18 }{
19 "Simple inventory string parse with empty namespace": {
20 invStr: "_test-name_apps_ReplicaSet",
21 inventory: &ObjMetadata{
22 Name: "test-name",
23 GroupKind: schema.GroupKind{
24 Group: "apps",
25 Kind: "ReplicaSet",
26 },
27 },
28 isError: false,
29 },
30 "Basic inventory string parse": {
31 invStr: "test-namespace_test-name_apps_Deployment",
32 inventory: &ObjMetadata{
33 Namespace: "test-namespace",
34 Name: "test-name",
35 GroupKind: schema.GroupKind{
36 Group: "apps",
37 Kind: "Deployment",
38 },
39 },
40 isError: false,
41 },
42 "RBAC resources can have colon (double underscore) in their name": {
43 invStr: "test-namespace_kubeadm__nodes-kubeadm-config_rbac.authorization.k8s.io_Role",
44 inventory: &ObjMetadata{
45 Namespace: "test-namespace",
46 Name: "kubeadm:nodes-kubeadm-config",
47 GroupKind: schema.GroupKind{
48 Group: rbacv1.GroupName,
49 Kind: "Role",
50 },
51 },
52 isError: false,
53 },
54 "RBAC resources can have double colon (double underscore) in their name": {
55 invStr: "test-namespace_system____leader-locking-kube-scheduler_rbac.authorization.k8s.io_Role",
56 inventory: &ObjMetadata{
57 Namespace: "test-namespace",
58 Name: "system::leader-locking-kube-scheduler",
59 GroupKind: schema.GroupKind{
60 Group: rbacv1.GroupName,
61 Kind: "Role",
62 },
63 },
64 isError: false,
65 },
66 "Test double underscore (colon) at beginning of name": {
67 invStr: "test-namespace___leader-locking-kube-scheduler_rbac.authorization.k8s.io_ClusterRole",
68 inventory: &ObjMetadata{
69 Namespace: "test-namespace",
70 Name: ":leader-locking-kube-scheduler",
71 GroupKind: schema.GroupKind{
72 Group: rbacv1.GroupName,
73 Kind: "ClusterRole",
74 },
75 },
76 isError: false,
77 },
78 "Test double underscore (colon) at end of name": {
79 invStr: "test-namespace_leader-locking-kube-scheduler___rbac.authorization.k8s.io_RoleBinding",
80 inventory: &ObjMetadata{
81 Namespace: "test-namespace",
82 Name: "leader-locking-kube-scheduler:",
83 GroupKind: schema.GroupKind{
84 Group: rbacv1.GroupName,
85 Kind: "RoleBinding",
86 },
87 },
88 isError: false,
89 },
90 "Not enough fields -- error": {
91 invStr: "_test-name_apps",
92 inventory: &ObjMetadata{},
93 isError: true,
94 },
95 "Only one field (no separators) -- error": {
96 invStr: "test-namespacetest-nametest-grouptest-kind",
97 inventory: &ObjMetadata{},
98 isError: true,
99 },
100 "Too many fields": {
101 invStr: "test-namespace_test-name_apps_foo_Deployment",
102 inventory: &ObjMetadata{},
103 isError: true,
104 },
105 }
106
107 for tn, tc := range tests {
108 t.Run(tn, func(t *testing.T) {
109 actual, err := ParseObjMetadata(tc.invStr)
110 if !tc.isError {
111 if err != nil {
112 t.Errorf("Error parsing inventory when it should have worked: %s", err)
113 } else if !tc.inventory.Equals(&actual) {
114 t.Errorf("Expected inventory (%s) != parsed inventory (%s)\n", tc.inventory, actual)
115 }
116 }
117 if tc.isError && err == nil {
118 t.Errorf("Should have returned an error in ParseObjMetadata()")
119 }
120 })
121 }
122 }
123
View as plain text