Skip to content

Commit f8800ea

Browse files
authored
Merge pull request #26 from tech-geek29/add-resource-test
Adds tests for k8s resource
2 parents c035003 + ca1093e commit f8800ea

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

klient/k8s/res/main_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,12 @@ func deleteNamespace(ctx context.Context, ns *corev1.Namespace) {
142142

143143
func initializeResObjects() {
144144
namespace = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test"}}
145+
dep = getDeployment(fmt.Sprintf("deployment-name-%v", count))
146+
}
145147

146-
dep = &appsv1.Deployment{
147-
ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("deployment-name-%v", count), Namespace: namespace.Name, Labels: map[string]string{"app": fmt.Sprintf("bar-%v", count)}},
148+
func getDeployment(name string) *appsv1.Deployment {
149+
return &appsv1.Deployment{
150+
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace.Name, Labels: map[string]string{"app": "test-app"}},
148151
Spec: appsv1.DeploymentSpec{
149152
Replicas: &replicaCount,
150153
Selector: &metav1.LabelSelector{

klient/k8s/res/resource_test.go

+81-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ import (
2424
appsv1 "k8s.io/api/apps/v1"
2525
corev1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/client-go/rest"
2728
)
2829

2930
func TestCreate(t *testing.T) {
3031
res := Res(cfg)
3132
if res == nil {
32-
t.Errorf("config is nill")
33+
t.Errorf("config is nil")
3334
}
3435

3536
// create a namespace
@@ -52,7 +53,7 @@ func TestCreate(t *testing.T) {
5253
func TestRes(t *testing.T) {
5354
res := Res(cfg)
5455
if res == nil {
55-
t.Errorf("config is nill")
56+
t.Errorf("config is nil")
5657
}
5758

5859
err := res.Create(context.TODO(), dep)
@@ -80,6 +81,84 @@ func TestRes(t *testing.T) {
8081
}
8182
}
8283

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+
83162
func TestList(t *testing.T) {
84163
res := Res(cfg)
85164
if res == nil {

0 commit comments

Comments
 (0)