Skip to content

Commit e7199d1

Browse files
committed
Add unit test for varying job timeout
1 parent e881752 commit e7199d1

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

pkg/controller/bundle/bundle_unpacker.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,9 @@ func (c *ConfigMapUnpacker) UnpackBundle(lookup *operatorsv1alpha1.BundleLookup,
432432
}
433433
var job *batchv1.Job
434434
job, err = c.ensureJob(cmRef, result.Path, secrets, timeout)
435-
if err != nil {
435+
if err != nil || job == nil {
436+
// ensureJob can return nil if the job present does not match the expected job (spec and ownerefs)
437+
// The current job is deleted in that case so UnpackBundle needs to be retried
436438
return
437439
}
438440

@@ -525,7 +527,7 @@ func (c *ConfigMapUnpacker) pendingContainerStatusMessages(job *batchv1.Job) (st
525527

526528
// Aggregate the wait reasons for all pending containers
527529
containerStatusMessages = append(containerStatusMessages,
528-
fmt.Sprintf("Unpack pod(%s/%s) container(%s) is pending. Reason: %s, Message: %s | ",
530+
fmt.Sprintf("Unpack pod(%s/%s) container(%s) is pending. Reason: %s, Message: %s",
529531
pod.Namespace, pod.Name, ic.Name, ic.State.Waiting.Reason, ic.State.Waiting.Message))
530532
}
531533
}

pkg/controller/bundle/bundle_unpacker_test.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,23 @@ func TestConfigMapUnpacker(t *testing.T) {
4040
return start
4141
}
4242
backoffLimit := int32(3)
43+
// Used to set the default value for job.spec.ActiveDeadlineSeconds
44+
// that would normally be passed from the cmdline flag
45+
defaultUnpackDuration := 10 * time.Minute
46+
defaultUnpackTimeoutSeconds := int64(defaultUnpackDuration.Seconds())
47+
48+
// Custom timeout to override the default cmdline flag ActiveDeadlineSeconds value
49+
customAnnotationDuration := 2 * time.Minute
50+
customAnnotationTimeoutSeconds := int64(customAnnotationDuration.Seconds())
4351

4452
type fields struct {
4553
objs []runtime.Object
4654
crs []runtime.Object
4755
}
4856
type args struct {
4957
lookup *operatorsv1alpha1.BundleLookup
58+
// A negative timeout duration arg means it will be ignored and the default flag timeout will be used
59+
annotationTimeout time.Duration
5060
}
5161
type expected struct {
5262
res *BundleUnpackResult
@@ -67,6 +77,7 @@ func TestConfigMapUnpacker(t *testing.T) {
6777
description: "NoCatalogSource/NoConfigMap/NoJob/NotCreated/Pending",
6878
fields: fields{},
6979
args: args{
80+
annotationTimeout: -1 * time.Minute,
7081
lookup: &operatorsv1alpha1.BundleLookup{
7182
Path: bundlePath,
7283
Replaces: "",
@@ -108,7 +119,7 @@ func TestConfigMapUnpacker(t *testing.T) {
108119
},
109120
},
110121
{
111-
description: "CatalogSourcePresent/NoConfigMap/NoJob/Created/Pending",
122+
description: "CatalogSourcePresent/NoConfigMap/NoJob/JobCreated/Pending/WithCustomTimeout",
112123
fields: fields{
113124
crs: []runtime.Object{
114125
&operatorsv1alpha1.CatalogSource{
@@ -123,6 +134,9 @@ func TestConfigMapUnpacker(t *testing.T) {
123134
},
124135
},
125136
args: args{
137+
// We override the default timeout and expect to see the job created with
138+
// the custom annotation based timeout value
139+
annotationTimeout: customAnnotationDuration,
126140
lookup: &operatorsv1alpha1.BundleLookup{
127141
Path: bundlePath,
128142
Replaces: "",
@@ -194,7 +208,9 @@ func TestConfigMapUnpacker(t *testing.T) {
194208
},
195209
},
196210
Spec: batchv1.JobSpec{
197-
BackoffLimit: &backoffLimit,
211+
// The expected job's timeout should be set to the custom annotation timeout
212+
ActiveDeadlineSeconds: &customAnnotationTimeoutSeconds,
213+
BackoffLimit: &backoffLimit,
198214
Template: corev1.PodTemplateSpec{
199215
ObjectMeta: metav1.ObjectMeta{
200216
Name: pathHash,
@@ -371,7 +387,8 @@ func TestConfigMapUnpacker(t *testing.T) {
371387
},
372388
},
373389
Spec: batchv1.JobSpec{
374-
BackoffLimit: &backoffLimit,
390+
ActiveDeadlineSeconds: &defaultUnpackTimeoutSeconds,
391+
BackoffLimit: &backoffLimit,
375392
Template: corev1.PodTemplateSpec{
376393
ObjectMeta: metav1.ObjectMeta{
377394
Name: pathHash,
@@ -507,6 +524,7 @@ func TestConfigMapUnpacker(t *testing.T) {
507524
},
508525
},
509526
args: args{
527+
annotationTimeout: -1 * time.Minute,
510528
lookup: &operatorsv1alpha1.BundleLookup{
511529
Path: bundlePath,
512530
Replaces: "",
@@ -586,7 +604,8 @@ func TestConfigMapUnpacker(t *testing.T) {
586604
},
587605
},
588606
Spec: batchv1.JobSpec{
589-
BackoffLimit: &backoffLimit,
607+
ActiveDeadlineSeconds: &defaultUnpackTimeoutSeconds,
608+
BackoffLimit: &backoffLimit,
590609
Template: corev1.PodTemplateSpec{
591610
ObjectMeta: metav1.ObjectMeta{
592611
Name: pathHash,
@@ -797,7 +816,8 @@ func TestConfigMapUnpacker(t *testing.T) {
797816
},
798817
},
799818
Spec: batchv1.JobSpec{
800-
BackoffLimit: &backoffLimit,
819+
ActiveDeadlineSeconds: &defaultUnpackTimeoutSeconds,
820+
BackoffLimit: &backoffLimit,
801821
Template: corev1.PodTemplateSpec{
802822
ObjectMeta: metav1.ObjectMeta{
803823
Name: pathHash,
@@ -915,6 +935,7 @@ func TestConfigMapUnpacker(t *testing.T) {
915935
},
916936

917937
args: args{
938+
annotationTimeout: -1 * time.Minute,
918939
lookup: &operatorsv1alpha1.BundleLookup{
919940
Path: bundlePath,
920941
Replaces: "",
@@ -949,7 +970,7 @@ func TestConfigMapUnpacker(t *testing.T) {
949970
Type: operatorsv1alpha1.BundleLookupPending,
950971
Status: corev1.ConditionTrue,
951972
Reason: JobIncompleteReason,
952-
Message: fmt.Sprintf("%s: Unpack pod(ns-a/%s) container(pull) is pending. Reason: ErrImagePull, Message: pod pending for some reason | ",
973+
Message: fmt.Sprintf("%s: Unpack pod(ns-a/%s) container(pull) is pending. Reason: ErrImagePull, Message: pod pending for some reason",
953974
JobIncompleteMessage, pathHash+"-pod"),
954975
LastTransitionTime: &start,
955976
},
@@ -977,7 +998,8 @@ func TestConfigMapUnpacker(t *testing.T) {
977998
},
978999
},
9791000
Spec: batchv1.JobSpec{
980-
BackoffLimit: &backoffLimit,
1001+
ActiveDeadlineSeconds: &defaultUnpackTimeoutSeconds,
1002+
BackoffLimit: &backoffLimit,
9811003
Template: corev1.PodTemplateSpec{
9821004
ObjectMeta: metav1.ObjectMeta{
9831005
Name: pathHash,
@@ -1106,6 +1128,7 @@ func TestConfigMapUnpacker(t *testing.T) {
11061128
},
11071129
},
11081130
args: args{
1131+
annotationTimeout: -1 * time.Minute,
11091132
lookup: &operatorsv1alpha1.BundleLookup{
11101133
Path: bundlePath,
11111134
Replaces: "",
@@ -1168,7 +1191,8 @@ func TestConfigMapUnpacker(t *testing.T) {
11681191
},
11691192
},
11701193
Spec: batchv1.JobSpec{
1171-
BackoffLimit: &backoffLimit,
1194+
ActiveDeadlineSeconds: &defaultUnpackTimeoutSeconds,
1195+
BackoffLimit: &backoffLimit,
11721196
Template: corev1.PodTemplateSpec{
11731197
ObjectMeta: metav1.ObjectMeta{
11741198
Name: pathHash,
@@ -1310,11 +1334,11 @@ func TestConfigMapUnpacker(t *testing.T) {
13101334
WithOPMImage(opmImage),
13111335
WithUtilImage(utilImage),
13121336
WithNow(now),
1337+
WithUnpackTimeout(defaultUnpackDuration),
13131338
)
13141339
require.NoError(t, err)
13151340

1316-
// The annotation unpack timeout arg is negative so it is ignored for these tests
1317-
res, err := unpacker.UnpackBundle(tt.args.lookup, -1*time.Minute)
1341+
res, err := unpacker.UnpackBundle(tt.args.lookup, tt.args.annotationTimeout)
13181342
require.Equal(t, tt.expected.err, err)
13191343

13201344
if tt.expected.res == nil {

0 commit comments

Comments
 (0)