Skip to content

Commit 14fe30c

Browse files
test: add unit tests for exec.go file in pkg/utils folder (#755)
* test: add unit tests for exec.go file in pkg/utils folder Signed-off-by: Prakhar-Shankar <[email protected]> * fixing gofmt Signed-off-by: Prakhar-Shankar <[email protected]> * creating table driven test and also updates TestCheckPodStatus Signed-off-by: Prakhar-Shankar <[email protected]> --------- Signed-off-by: Prakhar-Shankar <[email protected]> Co-authored-by: Shubham Chaudhary <[email protected]>
1 parent 4ae0889 commit 14fe30c

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

pkg/utils/exec/exec.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,20 @@ func checkPodStatus(pod *apiv1.Pod, containerName string) error {
9292
if strings.ToLower(string(pod.Status.Phase)) != "running" {
9393
return cerrors.Error{ErrorCode: cerrors.ErrorTypeStatusChecks, Reason: fmt.Sprintf("%v pod is not in running state, phase: %v", pod.Name, pod.Status.Phase)}
9494
}
95+
containerFound := false
9596
for _, container := range pod.Status.ContainerStatuses {
96-
if container.Name == containerName && !container.Ready {
97-
return cerrors.Error{ErrorCode: cerrors.ErrorTypeStatusChecks, Reason: fmt.Sprintf("%v container of %v pod is not in ready state, phase: %v", container.Name, pod.Name, pod.Status.Phase)}
97+
if container.Name == containerName {
98+
containerFound = true
99+
if !container.Ready {
100+
return cerrors.Error{ErrorCode: cerrors.ErrorTypeStatusChecks, Reason: fmt.Sprintf("%v container of %v pod is not in ready state, phase: %v", container.Name, pod.Name, pod.Status.Phase)}
101+
}
102+
break
103+
}
104+
}
105+
if !containerFound {
106+
return cerrors.Error{
107+
ErrorCode: cerrors.ErrorTypeStatusChecks,
108+
Reason: fmt.Sprintf("container %v not found in pod %v", containerName, pod.Name),
98109
}
99110
}
100111
return nil

pkg/utils/exec/exec_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// exec_test.go
2+
package exec
3+
4+
import (
5+
"testing"
6+
7+
apiv1 "k8s.io/api/core/v1"
8+
)
9+
10+
func TestSetExecCommandAttributes(t *testing.T) {
11+
p := &PodDetails{}
12+
SetExecCommandAttributes(p, "test-pod", "test-container", "default")
13+
14+
if p.PodName != "test-pod" || p.ContainerName != "test-container" || p.Namespace != "default" {
15+
t.Errorf("SetExecCommandAttributes failed to set values properly")
16+
}
17+
}
18+
19+
func TestCheckPodStatus(t *testing.T) {
20+
tests := []struct {
21+
name string
22+
pod *apiv1.Pod
23+
container string
24+
wantErr bool
25+
}{
26+
{
27+
name: "Pod not running",
28+
pod: &apiv1.Pod{
29+
Status: apiv1.PodStatus{Phase: apiv1.PodPending},
30+
},
31+
container: "container1",
32+
wantErr: true,
33+
},
34+
{
35+
name: "Container not ready",
36+
pod: &apiv1.Pod{
37+
Status: apiv1.PodStatus{
38+
Phase: apiv1.PodRunning,
39+
ContainerStatuses: []apiv1.ContainerStatus{
40+
{Name: "container1", Ready: false},
41+
},
42+
},
43+
},
44+
container: "container1",
45+
wantErr: true,
46+
},
47+
{
48+
name: "Healthy pod and container",
49+
pod: &apiv1.Pod{
50+
Status: apiv1.PodStatus{
51+
Phase: apiv1.PodRunning,
52+
ContainerStatuses: []apiv1.ContainerStatus{
53+
{Name: "container1", Ready: true},
54+
},
55+
},
56+
},
57+
container: "container1",
58+
wantErr: false,
59+
},
60+
{
61+
name: "Container name not matching",
62+
pod: &apiv1.Pod{
63+
Status: apiv1.PodStatus{
64+
Phase: apiv1.PodRunning,
65+
ContainerStatuses: []apiv1.ContainerStatus{
66+
{Name: "other-container", Ready: true},
67+
},
68+
},
69+
},
70+
container: "container1",
71+
wantErr: true,
72+
},
73+
}
74+
75+
for _, tt := range tests {
76+
t.Run(tt.name, func(t *testing.T) {
77+
err := checkPodStatus(tt.pod, tt.container)
78+
if (err != nil) != tt.wantErr {
79+
t.Errorf("checkPodStatus() error = %v, wantErr %v", err, tt.wantErr)
80+
}
81+
})
82+
}
83+
}

0 commit comments

Comments
 (0)