forked from fluxcd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob_test.go
More file actions
237 lines (220 loc) · 6.87 KB
/
Copy pathjob_test.go
File metadata and controls
237 lines (220 loc) · 6.87 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
Copyright 2025 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package statusreaders
import (
"testing"
. "github.com/onsi/gomega"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/fluxcd/cli-utils/pkg/kstatus/status"
"github.com/fluxcd/pkg/runtime/patch"
)
func Test_jobConditions(t *testing.T) {
t.Run("job without Complete condition returns InProgress status", func(t *testing.T) {
g := NewWithT(t)
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job",
},
Spec: batchv1.JobSpec{},
Status: batchv1.JobStatus{},
}
us, err := patch.ToUnstructured(job)
g.Expect(err).ToNot(HaveOccurred())
result, err := jobConditions(us)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(result.Status).To(Equal(status.InProgressStatus))
})
t.Run("job with Complete condition as True returns Current status", func(t *testing.T) {
g := NewWithT(t)
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job",
},
Spec: batchv1.JobSpec{},
Status: batchv1.JobStatus{
Conditions: []batchv1.JobCondition{
{
Type: batchv1.JobComplete,
Status: corev1.ConditionTrue,
},
},
},
}
us, err := patch.ToUnstructured(job)
g.Expect(err).ToNot(HaveOccurred())
result, err := jobConditions(us)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(result.Status).To(Equal(status.CurrentStatus))
g.Expect(result.Message).To(ContainSubstring("Job Completed"))
})
t.Run("suspended job returns Current status", func(t *testing.T) {
g := NewWithT(t)
suspend := true
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job",
},
Spec: batchv1.JobSpec{
Suspend: &suspend,
},
Status: batchv1.JobStatus{},
}
us, err := patch.ToUnstructured(job)
g.Expect(err).ToNot(HaveOccurred())
result, err := jobConditions(us)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(result.Status).To(Equal(status.CurrentStatus))
g.Expect(result.Message).To(Equal("Job is suspended"))
})
t.Run("job with Failed condition as True returns Failed status", func(t *testing.T) {
g := NewWithT(t)
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job",
},
Spec: batchv1.JobSpec{},
Status: batchv1.JobStatus{
Failed: 1,
Conditions: []batchv1.JobCondition{
{
Type: batchv1.JobFailed,
Status: corev1.ConditionTrue,
},
},
},
}
us, err := patch.ToUnstructured(job)
g.Expect(err).ToNot(HaveOccurred())
result, err := jobConditions(us)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(result.Status).To(Equal(status.FailedStatus))
g.Expect(result.Message).To(ContainSubstring("Job Failed"))
g.Expect(result.Conditions).To(HaveLen(1))
g.Expect(result.Conditions[0].Type).To(Equal(status.ConditionStalled))
g.Expect(result.Conditions[0].Reason).To(Equal("JobFailed"))
})
t.Run("job not started returns InProgress status", func(t *testing.T) {
g := NewWithT(t)
parallelism := int32(3)
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job",
},
Spec: batchv1.JobSpec{
Parallelism: ¶llelism,
},
Status: batchv1.JobStatus{},
}
us, err := patch.ToUnstructured(job)
g.Expect(err).ToNot(HaveOccurred())
result, err := jobConditions(us)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(result.Status).To(Equal(status.InProgressStatus))
g.Expect(result.Message).To(Equal("Job not started. active: 0/3"))
g.Expect(result.Conditions).To(HaveLen(1))
g.Expect(result.Conditions[0].Type).To(Equal(status.ConditionReconciling))
g.Expect(result.Conditions[0].Reason).To(Equal("JobNotStarted"))
})
t.Run("job without status field returns InProgress status", func(t *testing.T) {
g := NewWithT(t)
// Create an unstructured Job with no status field at all.
us := &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": map[string]any{
"name": "job",
},
"spec": map[string]any{
"parallelism": int64(2),
},
},
}
result, err := jobConditions(us)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(result.Status).To(Equal(status.InProgressStatus))
g.Expect(result.Message).To(Equal("Job not started. active: 0/2"))
})
t.Run("job in progress returns InProgress status", func(t *testing.T) {
g := NewWithT(t)
now := metav1.Now()
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job",
},
Spec: batchv1.JobSpec{},
Status: batchv1.JobStatus{
StartTime: &now,
Active: 2,
Succeeded: 1,
Failed: 1,
},
}
us, err := patch.ToUnstructured(job)
g.Expect(err).ToNot(HaveOccurred())
result, err := jobConditions(us)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(result.Status).To(Equal(status.InProgressStatus))
g.Expect(result.Message).To(Equal("Job in progress. success:1, active: 2, failed: 1"))
g.Expect(result.Conditions).To(HaveLen(1))
g.Expect(result.Conditions[0].Type).To(Equal(status.ConditionReconciling))
g.Expect(result.Conditions[0].Reason).To(Equal("JobInProgress"))
})
}
func Test_ComputeJobStatus(t *testing.T) {
t.Run("returns Unknown status when jobConditions fails", func(t *testing.T) {
g := NewWithT(t)
// Create an unstructured object with invalid conditions to trigger an error.
us := &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": map[string]any{
"name": "job",
},
"status": map[string]any{
// Invalid conditions type (string instead of array) to trigger error.
"conditions": "invalid",
},
},
}
result := ComputeJobStatus(us)
g.Expect(result.Status).To(Equal(status.UnknownStatus))
g.Expect(result.Message).To(ContainSubstring("Failed to compute job status"))
})
t.Run("returns result from jobConditions on success", func(t *testing.T) {
g := NewWithT(t)
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job",
},
Spec: batchv1.JobSpec{},
Status: batchv1.JobStatus{
Conditions: []batchv1.JobCondition{
{
Type: batchv1.JobComplete,
Status: corev1.ConditionTrue,
},
},
},
}
us, err := patch.ToUnstructured(job)
g.Expect(err).ToNot(HaveOccurred())
result := ComputeJobStatus(us)
g.Expect(result.Status).To(Equal(status.CurrentStatus))
g.Expect(result.Message).To(ContainSubstring("Job Completed"))
})
}