88)
99
1010func TestSelect (t * testing.T ) {
11- resource := & unstructured.Unstructured {
11+ // secret objects
12+ secretResource := & unstructured.Unstructured {
1213 Object : map [string ]interface {}{
1314 "apiVersion" : "v1" ,
1415 "kind" : "Secret" ,
@@ -27,7 +28,7 @@ func TestSelect(t *testing.T) {
2728 },
2829 }
2930
30- fieldsToSelect := []string {
31+ secretFieldsToSelect := []string {
3132 "apiVersion" ,
3233 "kind" ,
3334 "metadata.name" ,
@@ -36,13 +37,7 @@ func TestSelect(t *testing.T) {
3637 "/data/tls.crt" ,
3738 }
3839
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 := `{
4641 "apiVersion": "v1",
4742 "data": {
4843 "tls.crt": "cert data"
@@ -54,8 +49,90 @@ func TestSelect(t *testing.T) {
5449 },
5550 "type": "kubernetes.io/tls"
5651}`
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+ })
59136 }
60137}
61138
0 commit comments