This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
pod_test.go
164 lines (151 loc) · 4.06 KB
/
pod_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package k8sutils
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
k8sfake "k8s.io/client-go/kubernetes/fake"
k8stesting "k8s.io/client-go/testing"
)
func TestGetLogsOfPodHappyPath(t *testing.T) {
k8sClientSet := k8sfake.NewSimpleClientset()
jobName := "completed-job"
namespace := "namespace"
initContainerName := "init-" + jobName
initContainerImage := "keptncontrib/job-executor-service-initcontainer"
volumeSize := resource.MustParse("20Mi")
mainContainerImage := "some-work-image"
podName := "some-job-pod"
pod := v1.Pod{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Labels: map[string]string{"job-name": jobName},
},
Spec: v1.PodSpec{
InitContainers: []v1.Container{
{
Name: initContainerName,
Image: initContainerImage,
VolumeMounts: []v1.VolumeMount{
{
Name: "job-volume",
ReadOnly: false,
MountPath: "/keptn",
},
},
ImagePullPolicy: v1.PullIfNotPresent,
},
},
Containers: []v1.Container{
{
Name: jobName,
Image: mainContainerImage,
Command: []string{"do-work"},
Args: []string{"now"},
VolumeMounts: []v1.VolumeMount{
{
Name: "job-volume",
ReadOnly: false,
MountPath: "/keptn",
},
},
ImagePullPolicy: v1.PullIfNotPresent,
},
},
ServiceAccountName: "default-job-account",
Volumes: []v1.Volume{
{
Name: "job-volume",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{
Medium: v1.StorageMediumDefault,
SizeLimit: &volumeSize,
},
},
},
},
},
Status: v1.PodStatus{
Phase: v1.PodSucceeded,
InitContainerStatuses: []v1.ContainerStatus{
{
Name: initContainerName,
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
ExitCode: 0,
Reason: "Container terminated normally",
Message: "Init done.",
},
},
Image: initContainerImage,
},
},
ContainerStatuses: []v1.ContainerStatus{
{
Name: jobName,
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
ExitCode: 0,
Reason: "Container terminated normally",
Message: "Work done.",
},
},
Image: mainContainerImage,
},
},
},
}
k8sClientSet.CoreV1().Pods(namespace).Create(context.Background(), &pod, metav1.CreateOptions{})
// this block would be the right signature intercepting a k8s fake client call but doesn't work for logs,
// sadly this is sidestepped in the k8s client fake impl. See kubernetes/typed/core/v1/fake/fake_pod_expansion.go#GetLogs
//
// k8sClientSet.AddReactor(
// "get", "v1/pod", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
// if action.GetSubresource() != "log" {
// return false, nil, nil
// }
// return true, nil, nil
// },
// )
k8s := K8sImpl{clientset: k8sClientSet}
logsOfPod, err := k8s.GetLogsOfPod(jobName, namespace)
assert.NoError(t, err)
assert.Contains(t, logsOfPod, "fake logs")
// Assert that the fake received the call
getLogActionInitContainer := k8stesting.GenericActionImpl{
ActionImpl: k8stesting.ActionImpl{
Namespace: namespace,
Verb: "get",
Resource: schema.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "pods",
},
Subresource: "log",
},
Value: &v1.PodLogOptions{
Container: initContainerName,
},
}
getLogActionContainer := k8stesting.GenericActionImpl{
ActionImpl: k8stesting.ActionImpl{
Namespace: namespace,
Verb: "get",
Resource: schema.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "pods",
},
Subresource: "log",
},
Value: &v1.PodLogOptions{
Container: jobName,
},
}
assert.Contains(t, k8sClientSet.Actions(), getLogActionInitContainer)
assert.Contains(t, k8sClientSet.Actions(), getLogActionContainer)
}