forked from kubernetes/kube-state-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob_test.go
263 lines (249 loc) · 11.4 KB
/
job_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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 store
import (
"testing"
"time"
v1batch "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
)
var (
Parallelism1 int32 = 1
Completions1 int32 = 1
ActiveDeadlineSeconds900 int64 = 900
RunningJob1StartTime, _ = time.Parse(time.RFC3339, "2017-05-26T12:00:07Z")
SuccessfulJob1StartTime, _ = time.Parse(time.RFC3339, "2017-05-26T12:00:07Z")
FailedJob1StartTime, _ = time.Parse(time.RFC3339, "2017-05-26T14:00:07Z")
SuccessfulJob2StartTime, _ = time.Parse(time.RFC3339, "2017-05-26T12:10:07Z")
SuccessfulJob1CompletionTime, _ = time.Parse(time.RFC3339, "2017-05-26T13:00:07Z")
FailedJob1CompletionTime, _ = time.Parse(time.RFC3339, "2017-05-26T15:00:07Z")
SuccessfulJob2CompletionTime, _ = time.Parse(time.RFC3339, "2017-05-26T13:10:07Z")
)
func TestJobStore(t *testing.T) {
var trueValue = true
// Fixed metadata on type and help text. We prepend this to every expected
// output so we only have to modify a single place when doing adjustments.
const metadata = `
# HELP kube_job_annotations Kubernetes annotations converted to Prometheus labels.
# TYPE kube_job_annotations gauge
# HELP kube_job_created [STABLE] Unix creation timestamp
# TYPE kube_job_created gauge
# HELP kube_job_owner [STABLE] Information about the Job's owner.
# TYPE kube_job_owner gauge
# HELP kube_job_complete [STABLE] The job has completed its execution.
# TYPE kube_job_complete gauge
# HELP kube_job_failed [STABLE] The job has failed its execution.
# TYPE kube_job_failed gauge
# HELP kube_job_info [STABLE] Information about job.
# TYPE kube_job_info gauge
# HELP kube_job_labels [STABLE] Kubernetes labels converted to Prometheus labels.
# TYPE kube_job_labels gauge
# HELP kube_job_spec_active_deadline_seconds [STABLE] The duration in seconds relative to the startTime that the job may be active before the system tries to terminate it.
# TYPE kube_job_spec_active_deadline_seconds gauge
# HELP kube_job_spec_completions [STABLE] The desired number of successfully finished pods the job should be run with.
# TYPE kube_job_spec_completions gauge
# HELP kube_job_spec_parallelism [STABLE] The maximum desired number of pods the job should run at any given time.
# TYPE kube_job_spec_parallelism gauge
# HELP kube_job_status_active [STABLE] The number of actively running pods.
# TYPE kube_job_status_active gauge
# HELP kube_job_status_completion_time [STABLE] CompletionTime represents time when the job was completed.
# TYPE kube_job_status_completion_time gauge
# HELP kube_job_status_failed [STABLE] The number of pods which reached Phase Failed and the reason for failure.
# TYPE kube_job_status_failed gauge
# HELP kube_job_status_start_time [STABLE] StartTime represents time when the job was acknowledged by the Job Manager.
# TYPE kube_job_status_start_time gauge
# HELP kube_job_status_succeeded [STABLE] The number of pods which reached Phase Succeeded.
# TYPE kube_job_status_succeeded gauge`
cases := []generateMetricsTestCase{
{
Obj: &v1batch.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "RunningJob1",
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
Namespace: "ns1",
Generation: 1,
Labels: map[string]string{
"app": "example-running-1",
},
OwnerReferences: []metav1.OwnerReference{
{
Kind: "CronJob",
Name: "cronjob-name",
Controller: &trueValue,
},
},
},
Status: v1batch.JobStatus{
Active: 1,
Failed: 0,
Succeeded: 0,
CompletionTime: nil,
StartTime: &metav1.Time{Time: RunningJob1StartTime},
},
Spec: v1batch.JobSpec{
ActiveDeadlineSeconds: &ActiveDeadlineSeconds900,
Parallelism: &Parallelism1,
Completions: &Completions1,
},
},
Want: metadata + `
kube_job_owner{job_name="RunningJob1",namespace="ns1",owner_is_controller="true",owner_kind="CronJob",owner_name="cronjob-name"} 1
kube_job_created{job_name="RunningJob1",namespace="ns1"} 1.5e+09
kube_job_info{job_name="RunningJob1",namespace="ns1"} 1
kube_job_spec_active_deadline_seconds{job_name="RunningJob1",namespace="ns1"} 900
kube_job_spec_completions{job_name="RunningJob1",namespace="ns1"} 1
kube_job_spec_parallelism{job_name="RunningJob1",namespace="ns1"} 1
kube_job_status_active{job_name="RunningJob1",namespace="ns1"} 1
kube_job_status_failed{job_name="RunningJob1",namespace="ns1"} 0
kube_job_status_start_time{job_name="RunningJob1",namespace="ns1"} 1.495800007e+09
kube_job_status_succeeded{job_name="RunningJob1",namespace="ns1"} 0
`,
},
{
Obj: &v1batch.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "SuccessfulJob1",
Namespace: "ns1",
Generation: 1,
Labels: map[string]string{
"app": "example-successful-1",
},
},
Status: v1batch.JobStatus{
Active: 0,
Failed: 0,
Succeeded: 1,
CompletionTime: &metav1.Time{Time: SuccessfulJob1CompletionTime},
StartTime: &metav1.Time{Time: SuccessfulJob1StartTime},
Conditions: []v1batch.JobCondition{
{Type: v1batch.JobComplete, Status: v1.ConditionTrue},
},
},
Spec: v1batch.JobSpec{
ActiveDeadlineSeconds: &ActiveDeadlineSeconds900,
Parallelism: &Parallelism1,
Completions: &Completions1,
},
},
Want: metadata + `
kube_job_owner{job_name="SuccessfulJob1",namespace="ns1",owner_is_controller="",owner_kind="",owner_name=""} 1
kube_job_complete{condition="false",job_name="SuccessfulJob1",namespace="ns1"} 0
kube_job_complete{condition="true",job_name="SuccessfulJob1",namespace="ns1"} 1
kube_job_complete{condition="unknown",job_name="SuccessfulJob1",namespace="ns1"} 0
kube_job_info{job_name="SuccessfulJob1",namespace="ns1"} 1
kube_job_spec_active_deadline_seconds{job_name="SuccessfulJob1",namespace="ns1"} 900
kube_job_spec_completions{job_name="SuccessfulJob1",namespace="ns1"} 1
kube_job_spec_parallelism{job_name="SuccessfulJob1",namespace="ns1"} 1
kube_job_status_active{job_name="SuccessfulJob1",namespace="ns1"} 0
kube_job_status_completion_time{job_name="SuccessfulJob1",namespace="ns1"} 1.495803607e+09
kube_job_status_failed{job_name="SuccessfulJob1",namespace="ns1"} 0
kube_job_status_start_time{job_name="SuccessfulJob1",namespace="ns1"} 1.495800007e+09
kube_job_status_succeeded{job_name="SuccessfulJob1",namespace="ns1"} 1
`,
},
{
Obj: &v1batch.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "FailedJob1",
Namespace: "ns1",
Generation: 1,
Labels: map[string]string{
"app": "example-failed-1",
},
},
Status: v1batch.JobStatus{
Active: 0,
Failed: 1,
Succeeded: 0,
CompletionTime: &metav1.Time{Time: FailedJob1CompletionTime},
StartTime: &metav1.Time{Time: FailedJob1StartTime},
Conditions: []v1batch.JobCondition{
{Type: v1batch.JobFailed, Status: v1.ConditionTrue, Reason: "BackoffLimitExceeded"},
},
},
Spec: v1batch.JobSpec{
ActiveDeadlineSeconds: &ActiveDeadlineSeconds900,
Parallelism: &Parallelism1,
Completions: &Completions1,
},
},
Want: metadata + `
kube_job_owner{job_name="FailedJob1",namespace="ns1",owner_is_controller="",owner_kind="",owner_name=""} 1
kube_job_failed{condition="false",job_name="FailedJob1",namespace="ns1"} 0
kube_job_failed{condition="true",job_name="FailedJob1",namespace="ns1"} 1
kube_job_failed{condition="unknown",job_name="FailedJob1",namespace="ns1"} 0
kube_job_info{job_name="FailedJob1",namespace="ns1"} 1
kube_job_spec_active_deadline_seconds{job_name="FailedJob1",namespace="ns1"} 900
kube_job_spec_completions{job_name="FailedJob1",namespace="ns1"} 1
kube_job_spec_parallelism{job_name="FailedJob1",namespace="ns1"} 1
kube_job_status_active{job_name="FailedJob1",namespace="ns1"} 0
kube_job_status_completion_time{job_name="FailedJob1",namespace="ns1"} 1.495810807e+09
kube_job_status_failed{job_name="FailedJob1",namespace="ns1",reason="BackoffLimitExceeded"} 1
kube_job_status_failed{job_name="FailedJob1",namespace="ns1",reason="DeadlineExceeded"} 0
kube_job_status_failed{job_name="FailedJob1",namespace="ns1",reason="Evicted"} 0
kube_job_status_start_time{job_name="FailedJob1",namespace="ns1"} 1.495807207e+09
kube_job_status_succeeded{job_name="FailedJob1",namespace="ns1"} 0
`,
},
{
Obj: &v1batch.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "SuccessfulJob2NoActiveDeadlineSeconds",
Namespace: "ns1",
Generation: 1,
Labels: map[string]string{
"app": "example-successful-2",
},
},
Status: v1batch.JobStatus{
Active: 0,
Failed: 0,
Succeeded: 1,
CompletionTime: &metav1.Time{Time: SuccessfulJob2CompletionTime},
StartTime: &metav1.Time{Time: SuccessfulJob2StartTime},
Conditions: []v1batch.JobCondition{
{Type: v1batch.JobComplete, Status: v1.ConditionTrue},
},
},
Spec: v1batch.JobSpec{
ActiveDeadlineSeconds: nil,
Parallelism: &Parallelism1,
Completions: &Completions1,
},
},
Want: metadata + `
kube_job_owner{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1",owner_is_controller="",owner_kind="",owner_name=""} 1
kube_job_complete{condition="false",job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_complete{condition="true",job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 1
kube_job_complete{condition="unknown",job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_info{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 1
kube_job_spec_completions{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 1
kube_job_spec_parallelism{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 1
kube_job_status_active{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_status_completion_time{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 1.495804207e+09
kube_job_status_failed{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_status_start_time{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 1.495800607e+09
kube_job_status_succeeded{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 1
`,
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(jobMetricFamilies(nil, nil))
c.Headers = generator.ExtractMetricFamilyHeaders(jobMetricFamilies(nil, nil))
if err := c.run(); err != nil {
t.Errorf("unexpected collecting result in %vth run:\n%s", i, err)
}
}
}