8
8
)
9
9
10
10
func TestSelect (t * testing.T ) {
11
- resource := & unstructured.Unstructured {
11
+ // secret objects
12
+ secretResource := & unstructured.Unstructured {
12
13
Object : map [string ]interface {}{
13
14
"apiVersion" : "v1" ,
14
15
"kind" : "Secret" ,
@@ -27,7 +28,7 @@ func TestSelect(t *testing.T) {
27
28
},
28
29
}
29
30
30
- fieldsToSelect := []string {
31
+ secretFieldsToSelect := []string {
31
32
"apiVersion" ,
32
33
"kind" ,
33
34
"metadata.name" ,
@@ -36,13 +37,7 @@ func TestSelect(t *testing.T) {
36
37
"/data/tls.crt" ,
37
38
}
38
39
39
- err := Select (fieldsToSelect , resource )
40
- if err != nil {
41
- t .Fatalf ("unexpected error: %s" , err )
42
- }
43
-
44
- bytes , err := json .MarshalIndent (resource , "" , " " )
45
- expectedJSON := `{
40
+ secretExpectedJSON := `{
46
41
"apiVersion": "v1",
47
42
"data": {
48
43
"tls.crt": "cert data"
@@ -54,8 +49,90 @@ func TestSelect(t *testing.T) {
54
49
},
55
50
"type": "kubernetes.io/tls"
56
51
}`
57
- if string (bytes ) != expectedJSON {
58
- t .Fatalf ("unexpected JSON: \n got \n %s\n want\n %s" , string (bytes ), expectedJSON )
52
+ // route objects
53
+ routeResource := & unstructured.Unstructured {
54
+ Object : map [string ]interface {}{
55
+ "apiVersion" : "v1" ,
56
+ "kind" : "Route" ,
57
+ "metadata" : map [string ]interface {}{
58
+ "name" : "example" ,
59
+ "annotations" : map [string ]string {
60
+ "kubectl.kubernetes.io/last-applied-configuration" : "secret" ,
61
+ },
62
+ },
63
+ "spec" : map [string ]interface {}{
64
+ "host" : "www.example.com" ,
65
+ "to" : map [string ]string {
66
+ "kind" : "Service" ,
67
+ "name" : "frontend" ,
68
+ },
69
+ "tls" : map [string ]interface {}{
70
+ "termination" : "reencrypt" ,
71
+ "key" : "secret" ,
72
+ "certificate" : "cert data" ,
73
+ "caCertificate" : "caCert data" ,
74
+ "destinationCACertificate" : "destinationCaCert data" ,
75
+ },
76
+ },
77
+ },
78
+ }
79
+
80
+ routeFieldsToSelect := []string {
81
+ "apiVersion" ,
82
+ "kind" ,
83
+ "metadata.name" ,
84
+ "spec.host" ,
85
+ "spec.to.kind" ,
86
+ "spec.to.name" ,
87
+ "spec.tls.termination" ,
88
+ "spec.tls.certificate" ,
89
+ "spec.tls.caCertificate" ,
90
+ "spec.tls.destinationCACertificate" ,
91
+ }
92
+
93
+ routeExpectedJSON := `{
94
+ "apiVersion": "v1",
95
+ "kind": "Route",
96
+ "metadata": {
97
+ "name": "example"
98
+ },
99
+ "spec": {
100
+ "host": "www.example.com",
101
+ "tls": {
102
+ "caCertificate": "caCert data",
103
+ "certificate": "cert data",
104
+ "destinationCACertificate": "destinationCaCert data",
105
+ "termination": "reencrypt"
106
+ },
107
+ "to": {
108
+ "kind": "Service",
109
+ "name": "frontend"
110
+ }
111
+ }
112
+ }`
113
+
114
+ tests := map [string ]struct {
115
+ resource * unstructured.Unstructured
116
+ fieldsToSelect []string
117
+ expectedJSON string
118
+ }{
119
+ "secret" : {secretResource , secretFieldsToSelect , secretExpectedJSON },
120
+ "route" : {routeResource , routeFieldsToSelect , routeExpectedJSON },
121
+ }
122
+
123
+ for name , test := range tests {
124
+ err := Select (test .fieldsToSelect , test .resource )
125
+ if err != nil {
126
+ t .Fatalf ("unexpected error: %s" , err )
127
+ }
128
+
129
+ bytes , err := json .MarshalIndent (test .resource , "" , " " )
130
+
131
+ t .Run (name , func (t * testing.T ) {
132
+ if string (bytes ) != test .expectedJSON {
133
+ t .Fatalf ("unexpected JSON: \n got \n %s\n want\n %s" , string (bytes ), test .expectedJSON )
134
+ }
135
+ })
59
136
}
60
137
}
61
138
0 commit comments