Skip to content

Commit 881f8fd

Browse files
committed
pod: add basic unit tests
This PR adds unit tests to the basic pod functions (NewBuilder, Pull, Create, Delete, Get, Exists) and refactors the existing tests in the package to be more in line with those from other packages.
1 parent be29dfd commit 881f8fd

File tree

3 files changed

+358
-155
lines changed

3 files changed

+358
-155
lines changed

pkg/pod/list_test.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pod
22

33
import (
4+
"context"
45
"testing"
56
"time"
67

@@ -10,76 +11,86 @@ import (
1011
"k8s.io/apimachinery/pkg/runtime"
1112
)
1213

13-
func TestWaitForAllPodsInNamespacesHealthy(t *testing.T) {
14+
func TestPodWaitForAllPodsInNamespacesHealthy(t *testing.T) {
15+
generateTestPod := func(namespace string, conditionType corev1.PodConditionType) *corev1.Pod {
16+
pod := buildDummyPodWithPhaseAndCondition(corev1.PodRunning, conditionType, false)
17+
pod.Namespace = namespace
18+
19+
return pod
20+
}
21+
1422
testCases := []struct {
1523
namespaces []string
1624
includeSucceeded bool
1725
skipRedinessCheck bool
1826
ignoreRestartPolicyNever bool
1927
ignoreNamespaces []string
20-
expectedErrMsg string
28+
pods []runtime.Object
29+
expectedError error
2130
}{
2231
{
2332
namespaces: []string{"ns1"},
2433
includeSucceeded: true,
2534
skipRedinessCheck: true,
2635
ignoreRestartPolicyNever: true,
2736
ignoreNamespaces: []string{},
28-
expectedErrMsg: "",
37+
pods: []runtime.Object{
38+
generateTestPod("ns1", corev1.PodReady), generateTestPod("ns2", corev1.PodInitialized)},
39+
expectedError: nil,
2940
},
3041
{
3142
namespaces: []string{"ns1"},
3243
includeSucceeded: true,
3344
skipRedinessCheck: false,
3445
ignoreRestartPolicyNever: true,
3546
ignoreNamespaces: []string{},
36-
expectedErrMsg: "",
47+
pods: []runtime.Object{generateTestPod("ns1", corev1.PodReady)},
48+
expectedError: nil,
3749
},
3850
{
3951
namespaces: []string{"ns2"},
4052
includeSucceeded: true,
4153
skipRedinessCheck: false,
4254
ignoreRestartPolicyNever: true,
4355
ignoreNamespaces: []string{},
44-
expectedErrMsg: "context deadline exceeded",
56+
pods: []runtime.Object{generateTestPod("ns2", corev1.PodInitialized)},
57+
expectedError: context.DeadlineExceeded,
4558
},
4659
{
4760
namespaces: []string{},
4861
includeSucceeded: true,
4962
skipRedinessCheck: false,
5063
ignoreRestartPolicyNever: true,
5164
ignoreNamespaces: []string{},
52-
expectedErrMsg: "context deadline exceeded",
65+
pods: []runtime.Object{
66+
generateTestPod("ns1", corev1.PodReady), generateTestPod("ns2", corev1.PodInitialized)},
67+
expectedError: context.DeadlineExceeded,
5368
},
5469
{
5570
namespaces: []string{},
5671
includeSucceeded: true,
5772
skipRedinessCheck: false,
5873
ignoreRestartPolicyNever: true,
5974
ignoreNamespaces: []string{"ns2"},
60-
expectedErrMsg: "",
75+
pods: []runtime.Object{generateTestPod("ns1", corev1.PodReady)},
76+
expectedError: nil,
6177
},
6278
}
6379

64-
var runtimeObjects []runtime.Object
65-
runtimeObjects = append(runtimeObjects, generateTestPod("test1", "ns1", corev1.PodRunning, corev1.PodReady, false))
66-
runtimeObjects = append(runtimeObjects, generateTestPod("test2", "ns1", corev1.PodRunning, corev1.PodReady, false))
67-
runtimeObjects = append(runtimeObjects, generateTestPod("test3", "ns1", corev1.PodRunning, corev1.PodReady, false))
68-
runtimeObjects = append(runtimeObjects, generateTestPod("test4", "ns1", corev1.PodRunning, corev1.PodReady, false))
69-
runtimeObjects = append(runtimeObjects, generateTestPod("test5", "ns1", corev1.PodRunning, corev1.PodReady, false))
70-
runtimeObjects = append(runtimeObjects, generateTestPod("test1", "ns2", corev1.PodRunning, corev1.PodReady, false))
71-
runtimeObjects = append(runtimeObjects, generateTestPod("test2", "ns2", corev1.PodRunning, corev1.PodInitialized,
72-
false))
73-
74-
testSettings := clients.GetTestClients(clients.TestClientParams{
75-
K8sMockObjects: runtimeObjects,
76-
})
77-
7880
for _, testCase := range testCases {
79-
err := WaitForAllPodsInNamespacesHealthy(testSettings, testCase.namespaces, 2*time.Second, testCase.includeSucceeded,
81+
testSettings := clients.GetTestClients(clients.TestClientParams{
82+
K8sMockObjects: testCase.pods,
83+
})
84+
85+
err := WaitForAllPodsInNamespacesHealthy(
86+
testSettings,
87+
testCase.namespaces,
88+
time.Second,
89+
testCase.includeSucceeded,
8090
testCase.skipRedinessCheck,
81-
testCase.ignoreRestartPolicyNever, testCase.ignoreNamespaces)
91+
testCase.ignoreRestartPolicyNever,
92+
testCase.ignoreNamespaces)
8293

83-
assert.Equal(t, testCase.expectedErrMsg, getErrorString(err))
94+
assert.Equal(t, testCase.expectedError, err)
8495
}
8596
}

pkg/pod/pod.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,25 @@ func NewBuilder(apiClient *clients.Settings, name, nsname, image string) *Builde
6464
if name == "" {
6565
glog.V(100).Infof("The name of the pod is empty")
6666

67-
builder.errorMsg = "pod's name is empty"
67+
builder.errorMsg = "pod 'name' cannot be empty"
68+
69+
return builder
6870
}
6971

7072
if nsname == "" {
7173
glog.V(100).Infof("The namespace of the pod is empty")
7274

73-
builder.errorMsg = "namespace's name is empty"
75+
builder.errorMsg = "pod 'namespace' cannot be empty"
76+
77+
return builder
7478
}
7579

7680
if image == "" {
7781
glog.V(100).Infof("The image of the pod is empty")
7882

79-
builder.errorMsg = "pod's image is empty"
83+
builder.errorMsg = "pod 'image' cannot be empty"
84+
85+
return builder
8086
}
8187

8288
defaultContainer, err := NewContainerBuilder("test", image, []string{"/bin/bash", "-c", "sleep INF"}).GetContainerCfg()
@@ -85,6 +91,8 @@ func NewBuilder(apiClient *clients.Settings, name, nsname, image string) *Builde
8591
glog.V(100).Infof("Failed to define the default container settings")
8692

8793
builder.errorMsg = err.Error()
94+
95+
return builder
8896
}
8997

9098
builder.Definition.Spec.Containers = append(builder.Definition.Spec.Containers, *defaultContainer)
@@ -115,17 +123,13 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) {
115123
if name == "" {
116124
glog.V(100).Infof("The name of the pod is empty")
117125

118-
builder.errorMsg = "pod 'name' cannot be empty"
126+
return nil, fmt.Errorf("pod 'name' cannot be empty")
119127
}
120128

121129
if nsname == "" {
122130
glog.V(100).Infof("The namespace of the pod is empty")
123131

124-
builder.errorMsg = "pod 'namespace' cannot be empty"
125-
}
126-
127-
if builder.errorMsg != "" {
128-
return nil, fmt.Errorf("faield to pull pod object due to the following error: %s", builder.errorMsg)
132+
return nil, fmt.Errorf("pod 'namespace' cannot be empty")
129133
}
130134

131135
if !builder.Exists() {
@@ -402,8 +406,8 @@ func (builder *Builder) WaitUntilDeleted(timeout time.Duration) error {
402406

403407
err := wait.PollUntilContextTimeout(
404408
context.TODO(), time.Second, timeout, false, func(ctx context.Context) (bool, error) {
405-
_, err := builder.apiClient.Pods(builder.Definition.Namespace).Get(
406-
context.TODO(), builder.Definition.Name, metav1.GetOptions{})
409+
_, err := builder.apiClient.Pods(builder.Object.Namespace).Get(
410+
context.TODO(), builder.Object.Name, metav1.GetOptions{})
407411
if err == nil {
408412
glog.V(100).Infof("pod %s/%s still present", builder.Definition.Namespace, builder.Definition.Name)
409413

0 commit comments

Comments
 (0)