@@ -24,12 +24,13 @@ import (
24
24
appsv1 "k8s.io/api/apps/v1"
25
25
corev1 "k8s.io/api/core/v1"
26
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
+ "k8s.io/client-go/rest"
27
28
)
28
29
29
30
func TestCreate (t * testing.T ) {
30
31
res := Res (cfg )
31
32
if res == nil {
32
- t .Errorf ("config is nill " )
33
+ t .Errorf ("config is nil " )
33
34
}
34
35
35
36
// create a namespace
@@ -52,7 +53,7 @@ func TestCreate(t *testing.T) {
52
53
func TestRes (t * testing.T ) {
53
54
res := Res (cfg )
54
55
if res == nil {
55
- t .Errorf ("config is nill " )
56
+ t .Errorf ("config is nil " )
56
57
}
57
58
58
59
err := res .Create (context .TODO (), dep )
@@ -80,6 +81,84 @@ func TestRes(t *testing.T) {
80
81
}
81
82
}
82
83
84
+ func TestResNoConfig (t * testing.T ) {
85
+ defer func () {
86
+ if r := recover (); r == nil {
87
+ t .Error ("expected panic while invoking Res without k8s config" )
88
+ }
89
+ }()
90
+
91
+ Res (nil )
92
+ }
93
+
94
+ func TestResInvalidConfig (t * testing.T ) {
95
+ defer func () {
96
+ if r := recover (); r == nil {
97
+ t .Error ("expected panic while invoking Res with invalid k8s config" )
98
+ }
99
+ }()
100
+
101
+ cfg := & rest.Config {
102
+ Host : "invalid-host" ,
103
+ }
104
+
105
+ Res (cfg )
106
+ }
107
+
108
+ func TestUpdate (t * testing.T ) {
109
+ res := Res (cfg )
110
+ if res == nil {
111
+ t .Errorf ("config is nil" )
112
+ }
113
+
114
+ depActual := getDeployment ("update-test-dep-name" )
115
+
116
+ err := res .Create (context .TODO (), depActual )
117
+ if err != nil {
118
+ t .Error ("error while creating deployment" , err )
119
+ }
120
+
121
+ depUpdated := depActual
122
+ depUpdated .ObjectMeta .Labels ["test-key" ] = "test-val"
123
+
124
+ err = res .Update (context .TODO (), depUpdated )
125
+ if err != nil {
126
+ t .Error ("error while updating deployment" , err )
127
+ }
128
+
129
+ var depObj appsv1.Deployment
130
+ err = res .Get (context .TODO (), depUpdated .Name , namespace .Name , & depObj )
131
+ if err != nil {
132
+ t .Error ("error while getting the deployment" , err )
133
+ }
134
+
135
+ val , ok := depObj .Labels ["test-key" ]
136
+ if ! ok {
137
+ t .Error ("deployment not updated" )
138
+ } else if val != "test-val" {
139
+ t .Error ("deployment label value mismatch, expected : " , "test-val" , "obtained :" , val )
140
+ }
141
+ }
142
+
143
+ func TestDelete (t * testing.T ) {
144
+ res := Res (cfg )
145
+ if res == nil {
146
+ t .Errorf ("config is nil" )
147
+ }
148
+
149
+ depActual := getDeployment ("delete-test-dep-name" )
150
+
151
+ err := res .Create (context .TODO (), depActual )
152
+ if err != nil {
153
+ t .Error ("error while creating deployment" , err )
154
+ }
155
+
156
+ err = res .Delete (context .TODO (), depActual )
157
+ if err != nil {
158
+ t .Error ("error while deleting deployment" , err )
159
+ }
160
+ }
161
+
83
162
func TestList (t * testing.T ) {
84
163
res := Res (cfg )
85
164
if res == nil {
0 commit comments