This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
jobs_test.go
141 lines (109 loc) · 4.4 KB
/
jobs_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
package containers
import (
"strings"
"testing"
client2 "github.com/acorn-io/runtime/integration/client"
"github.com/acorn-io/runtime/integration/helper"
apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/client"
kclient "github.com/acorn-io/runtime/pkg/k8sclient"
"github.com/acorn-io/runtime/pkg/publicname"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestJobList(t *testing.T) {
helper.StartController(t)
restConfig := helper.StartAPI(t)
ctx := helper.GetCTX(t)
lclient, err := kclient.New(restConfig)
require.NoError(t, err)
kclient := helper.MustReturn(kclient.Default)
project := helper.TempProject(t, kclient)
c, err := client.New(restConfig, "", project.Name)
require.NoError(t, err)
imageID := client2.NewImageWithJobs(t, project.Name)
app, err := c.AppRun(ctx, imageID, nil)
require.NoError(t, err)
app = helper.WaitForObject(t, lclient.Watch, &apiv1.AppList{}, app, func(app *apiv1.App) bool {
return app.Status.AppStatus.Jobs["job"].CompletionTime != nil
})
jobs, err := c.JobList(ctx, nil)
require.NoError(t, err)
require.Len(t, jobs, 2)
for _, job := range jobs {
assert.Truef(t, strings.HasPrefix(job.Name, app.Name+"."), "not prefix %s %s", job.Name, app.Name)
assert.Equal(t, app.Namespace, job.Namespace)
}
}
func TestJobGet(t *testing.T) {
helper.StartController(t)
restConfig := helper.StartAPI(t)
ctx := helper.GetCTX(t)
lclient, err := kclient.New(restConfig)
require.NoError(t, err)
kclient := helper.MustReturn(kclient.Default)
project := helper.TempProject(t, kclient)
c, err := client.New(restConfig, "", project.Name)
require.NoError(t, err)
imageID := client2.NewImageWithJobs(t, project.Name)
app, err := c.AppRun(ctx, imageID, nil)
require.NoError(t, err)
helper.WaitForObject(t, lclient.Watch, &apiv1.AppList{}, app, func(app *apiv1.App) bool {
return app.Status.AppStatus.Jobs["job"].CompletionTime != nil
})
jobs, err := c.JobList(ctx, nil)
require.NoError(t, err)
// Determine which job is the cronjob and which is the job
require.Len(t, jobs, 2)
jobFromList, cronjobFromList := jobs[0], jobs[1]
if cronjobFromList.Spec.Schedule == "" {
jobFromList = jobs[1]
cronjobFromList = jobs[0]
}
// Check that the job without a schedule is correct
job, err := c.JobGet(ctx, jobFromList.Name)
require.NoError(t, err)
assert.Nil(t, jobFromList.Status.NextRun)
assert.Equal(t, jobFromList.Name, job.Name)
assert.Equal(t, jobFromList.Namespace, job.Namespace)
assert.Equal(t, jobFromList.UID, job.UID)
// Check that the cronjob is correct
cronjob, err := c.JobGet(ctx, cronjobFromList.Name)
require.NoError(t, err)
assert.Equal(t, cronjobFromList.Name, cronjob.Name)
assert.Equal(t, cronjobFromList.Namespace, cronjob.Namespace)
assert.Equal(t, cronjobFromList.UID, cronjob.UID)
}
func TestJobRestart(t *testing.T) {
helper.StartController(t)
restConfig := helper.StartAPI(t)
ctx := helper.GetCTX(t)
lclient, err := kclient.New(restConfig)
require.NoError(t, err)
kclient := helper.MustReturn(kclient.Default)
project := helper.TempProject(t, kclient)
c, err := client.New(restConfig, "", project.Name)
require.NoError(t, err)
imageID := client2.NewImageWithJobs(t, project.Name)
app, err := c.AppRun(ctx, imageID, nil)
require.NoError(t, err)
// Wait for the Job to initially complete
var firstCompletion *metav1.Time
helper.WaitForObject(t, lclient.Watch, &apiv1.AppList{}, app, func(app *apiv1.App) bool {
firstCompletion = app.Status.AppStatus.Jobs["job"].CompletionTime
return app.Status.Namespace != "" && app.Status.AppStatus.Jobs["job"].CompletionTime != nil
})
require.NoError(t, c.JobRestart(ctx, publicname.ForChild(app, "job")))
// Wait for the Job to complete again by checking for a difference in the completion time
helper.WaitForObject(t, lclient.Watch, &apiv1.AppList{}, app, func(app *apiv1.App) bool {
secondCompletion := app.Status.AppStatus.Jobs["job"].CompletionTime
return app.Status.Namespace != "" && !firstCompletion.Equal(secondCompletion)
})
require.NoError(t, c.JobRestart(ctx, publicname.ForChild(app, "cronjob")))
// Wait for the CronJob to complete once, which means it has been restarted since the job
// is scheduled to never run
helper.WaitForObject(t, lclient.Watch, &apiv1.AppList{}, app, func(app *apiv1.App) bool {
return app.Status.AppStatus.Jobs["cronjob"].LastRun != nil
})
}