1 package mapper
2
3 import (
4 "fmt"
5 "strconv"
6 "testing"
7 "time"
8
9 "github.com/stretchr/testify/assert"
10 appsv1 "k8s.io/api/apps/v1"
11 v1 "k8s.io/api/core/v1"
12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13 "k8s.io/apimachinery/pkg/util/intstr"
14
15 "edge-infra.dev/pkg/edge/api/graph/model"
16 )
17
18 const (
19 RestartCount = 1
20 PeriodSeconds = 2
21 TimeoutSeconds = 3
22 SuccessThreshold = 4
23 FailureThreshold = 5
24 Ready = true
25 Started = true
26 ConditionStatus = v1.ConditionTrue
27 ConditionType = v1.PodReady
28 DepName = "test-deployment-1"
29 SSName = "test-statefulset-1"
30 DSName = "test-daemonset-1"
31 PodName = "test-pod-1"
32 HTTPPath = "test-path"
33 HTTPHost = "test-host"
34 HTTPScheme = "test-scheme"
35 ReleaseName = "test-release"
36 ReleaseNS = "default"
37 )
38
39 func GetTestWorkloadDeployment(httpPort intstr.IntOrString) *appsv1.Deployment {
40 return &appsv1.Deployment{
41 TypeMeta: metav1.TypeMeta{
42 APIVersion: appsv1.SchemeGroupVersion.Version,
43 Kind: "Deployment",
44 },
45 ObjectMeta: metav1.ObjectMeta{Name: DepName,
46 Annotations: map[string]string{
47 "meta.helm.sh/release-name": fmt.Sprintf("%s-%s", ReleaseNS, ReleaseName),
48 "meta.helm.sh/release-namespace": ReleaseNS,
49 },
50 },
51 Spec: appsv1.DeploymentSpec{
52 Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"name": "test-app", "helm": "test-helm"}},
53 Template: v1.PodTemplateSpec{
54 Spec: v1.PodSpec{
55 Containers: []v1.Container{
56 {
57 LivenessProbe: &v1.Probe{
58 ProbeHandler: v1.ProbeHandler{
59 HTTPGet: &v1.HTTPGetAction{
60 Path: HTTPPath,
61 Port: httpPort,
62 Host: HTTPHost,
63 Scheme: HTTPScheme,
64 },
65 },
66 TimeoutSeconds: TimeoutSeconds,
67 PeriodSeconds: PeriodSeconds,
68 SuccessThreshold: SuccessThreshold,
69 FailureThreshold: FailureThreshold,
70 },
71 ReadinessProbe: &v1.Probe{
72 ProbeHandler: v1.ProbeHandler{
73 HTTPGet: &v1.HTTPGetAction{
74 Path: HTTPPath,
75 Port: httpPort,
76 Host: HTTPHost,
77 Scheme: HTTPScheme,
78 },
79 },
80 TimeoutSeconds: TimeoutSeconds,
81 PeriodSeconds: PeriodSeconds,
82 SuccessThreshold: SuccessThreshold,
83 FailureThreshold: FailureThreshold,
84 },
85 },
86 },
87 },
88 },
89 },
90 }
91 }
92
93 func GetTestWorkloadStatefulSet(httpPort intstr.IntOrString) *appsv1.StatefulSet {
94 return &appsv1.StatefulSet{
95 TypeMeta: metav1.TypeMeta{
96 APIVersion: appsv1.SchemeGroupVersion.Version,
97 Kind: "StatefulSet",
98 },
99 ObjectMeta: metav1.ObjectMeta{Name: SSName,
100 Annotations: map[string]string{
101 "meta.helm.sh/release-name": fmt.Sprintf("%s-%s", ReleaseNS, ReleaseName),
102 "meta.helm.sh/release-namespace": ReleaseNS,
103 },
104 },
105 Spec: appsv1.StatefulSetSpec{
106 Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"name": "test-app", "helm": "test-helm"}},
107 Template: v1.PodTemplateSpec{
108 Spec: v1.PodSpec{
109 Containers: []v1.Container{
110 {
111 LivenessProbe: &v1.Probe{
112 ProbeHandler: v1.ProbeHandler{
113 HTTPGet: &v1.HTTPGetAction{
114 Path: HTTPPath,
115 Port: httpPort,
116 Host: HTTPHost,
117 Scheme: HTTPScheme,
118 },
119 },
120 TimeoutSeconds: TimeoutSeconds,
121 PeriodSeconds: PeriodSeconds,
122 SuccessThreshold: SuccessThreshold,
123 FailureThreshold: FailureThreshold,
124 },
125 ReadinessProbe: &v1.Probe{
126 ProbeHandler: v1.ProbeHandler{
127 HTTPGet: &v1.HTTPGetAction{
128 Path: HTTPPath,
129 Port: httpPort,
130 Host: HTTPHost,
131 Scheme: HTTPScheme,
132 },
133 },
134 TimeoutSeconds: TimeoutSeconds,
135 PeriodSeconds: PeriodSeconds,
136 SuccessThreshold: SuccessThreshold,
137 FailureThreshold: FailureThreshold,
138 },
139 },
140 },
141 },
142 },
143 },
144 }
145 }
146
147 func GetTestWorkloadDaemonSet(httpPort intstr.IntOrString) *appsv1.DaemonSet {
148 return &appsv1.DaemonSet{
149 TypeMeta: metav1.TypeMeta{
150 APIVersion: appsv1.SchemeGroupVersion.Version,
151 Kind: "DaemonSet",
152 },
153 ObjectMeta: metav1.ObjectMeta{Name: DSName,
154 Annotations: map[string]string{
155 "meta.helm.sh/release-name": fmt.Sprintf("%s-%s", ReleaseNS, ReleaseName),
156 "meta.helm.sh/release-namespace": ReleaseNS,
157 },
158 },
159 Spec: appsv1.DaemonSetSpec{
160 Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"name": "test-app", "helm": "test-helm"}},
161 Template: v1.PodTemplateSpec{
162 Spec: v1.PodSpec{
163 Containers: []v1.Container{
164 {
165 LivenessProbe: &v1.Probe{
166 ProbeHandler: v1.ProbeHandler{
167 HTTPGet: &v1.HTTPGetAction{
168 Path: HTTPPath,
169 Port: httpPort,
170 Host: HTTPHost,
171 Scheme: HTTPScheme,
172 },
173 },
174 TimeoutSeconds: TimeoutSeconds,
175 PeriodSeconds: PeriodSeconds,
176 SuccessThreshold: SuccessThreshold,
177 FailureThreshold: FailureThreshold,
178 },
179 ReadinessProbe: &v1.Probe{
180 ProbeHandler: v1.ProbeHandler{
181 HTTPGet: &v1.HTTPGetAction{
182 Path: HTTPPath,
183 Port: httpPort,
184 Host: HTTPHost,
185 Scheme: HTTPScheme,
186 },
187 },
188 TimeoutSeconds: TimeoutSeconds,
189 PeriodSeconds: PeriodSeconds,
190 SuccessThreshold: SuccessThreshold,
191 FailureThreshold: FailureThreshold,
192 },
193 },
194 },
195 },
196 },
197 },
198 }
199 }
200
201 func GetTestWorkloadPodList(conditionTime time.Time, startedPointer bool) *v1.PodList {
202 return &v1.PodList{
203 Items: []v1.Pod{
204 {
205 ObjectMeta: metav1.ObjectMeta{
206 Name: PodName,
207 Labels: map[string]string{"name": "test-app", "helm": "test-helm"},
208 },
209 Status: v1.PodStatus{
210 Conditions: []v1.PodCondition{
211 {
212 LastProbeTime: metav1.Time{Time: conditionTime},
213 LastTransitionTime: metav1.Time{Time: conditionTime},
214 Status: ConditionStatus,
215 Type: ConditionType,
216 },
217 },
218 ContainerStatuses: []v1.ContainerStatus{
219 {
220 RestartCount: RestartCount,
221 Ready: Ready,
222 Started: &startedPointer,
223 },
224 },
225 },
226 },
227 },
228 }
229 }
230
231 func ComapreLivenessReadinessModel(model *model.LivenessReadinessResponse, port intstr.IntOrString, conditionTime time.Time, t *testing.T) {
232 startedPointer := Started
233 timeString := conditionTime.Format(TimeFormat)
234 assert.Equal(t, model.ReadinessProbe.HTTPGet.Scheme, HTTPScheme)
235 assert.Equal(t, model.ReadinessProbe.HTTPGet.Port, int(port.IntVal))
236 assert.Equal(t, model.ReadinessProbe.HTTPGet.Path, HTTPPath)
237 assert.Equal(t, model.ReadinessProbe.TimeoutSeconds, TimeoutSeconds)
238 assert.Equal(t, model.ReadinessProbe.SuccessThreshold, SuccessThreshold)
239 assert.Equal(t, model.ReadinessProbe.PeriodSeconds, PeriodSeconds)
240 assert.Equal(t, model.ReadinessProbe.FailureThreshold, strconv.Itoa(FailureThreshold))
241 assert.Equal(t, model.LivenessProbe.HTTPGet.Scheme, HTTPScheme)
242 assert.Equal(t, model.LivenessProbe.HTTPGet.Port, int(port.IntVal))
243 assert.Equal(t, model.LivenessProbe.HTTPGet.Path, HTTPPath)
244 assert.Equal(t, model.LivenessProbe.TimeoutSeconds, TimeoutSeconds)
245 assert.Equal(t, model.LivenessProbe.SuccessThreshold, SuccessThreshold)
246 assert.Equal(t, model.LivenessProbe.PeriodSeconds, PeriodSeconds)
247 assert.Equal(t, model.LivenessProbe.FailureThreshold, strconv.Itoa(FailureThreshold))
248 for _, podStatus := range model.PodStatus {
249 assert.Equal(t, podStatus.Started, startedPointer)
250 assert.Equal(t, podStatus.Ready, Ready)
251 assert.Equal(t, podStatus.RestartCount, RestartCount)
252 for _, condition := range podStatus.Conditions {
253 assert.Equal(t, condition.Type, string(ConditionType))
254 assert.Equal(t, condition.Status, string(ConditionStatus))
255 assert.Equal(t, condition.LastTransitionTime, timeString)
256 assert.Equal(t, condition.LastProbeTime, &timeString)
257 }
258 }
259 }
260
View as plain text