@@ -22,10 +22,14 @@ import (
22
22
"strings"
23
23
"testing"
24
24
25
+ "github.com/google/go-cmp/cmp"
26
+ "github.com/google/uuid"
27
+ "google.golang.org/api/option"
25
28
"google.golang.org/api/run/v1"
26
29
27
30
"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
28
31
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/gcp"
32
+ "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
29
33
"github.com/GoogleContainerTools/skaffold/v2/testutil"
30
34
)
31
35
@@ -69,6 +73,142 @@ func TestDeployCloudRunWithHooks(t *testing.T) {
69
73
})
70
74
}
71
75
76
+ func TestDeployJobWithMaxRetries (t * testing.T ) {
77
+ MarkIntegrationTest (t , NeedsGcp )
78
+
79
+ tests := []struct {
80
+ descrition string
81
+ jobManifest string
82
+ skaffoldCfg string
83
+ args []string
84
+ expectedMaxRetries int64
85
+ }{
86
+ {
87
+ descrition : "maxRetries set to specific value" ,
88
+ expectedMaxRetries : 2 ,
89
+ jobManifest : `
90
+ apiVersion: run.googleapis.com/v1
91
+ kind: Job
92
+ metadata:
93
+ annotations:
94
+ run.googleapis.com/launch-stage: BETA
95
+ name: %v
96
+ spec:
97
+ template:
98
+ spec:
99
+ template:
100
+ spec:
101
+ containers:
102
+ - image: docker.io/library/busybox:latest
103
+ name: job
104
+ maxRetries: 2` ,
105
+ skaffoldCfg : `
106
+ apiVersion: %v
107
+ kind: Config
108
+ metadata:
109
+ name: cloud-run-test
110
+ manifests:
111
+ rawYaml:
112
+ - job.yaml
113
+ deploy:
114
+ cloudrun:
115
+ projectid: %v
116
+ region: %v` ,
117
+ },
118
+ {
119
+ descrition : "maxRetries set to 0" ,
120
+ expectedMaxRetries : 0 ,
121
+ jobManifest : `
122
+ apiVersion: run.googleapis.com/v1
123
+ kind: Job
124
+ metadata:
125
+ annotations:
126
+ run.googleapis.com/launch-stage: BETA
127
+ name: %v
128
+ spec:
129
+ template:
130
+ spec:
131
+ template:
132
+ spec:
133
+ containers:
134
+ - image: docker.io/library/busybox:latest
135
+ name: job
136
+ maxRetries: 0` ,
137
+ skaffoldCfg : `
138
+ apiVersion: %v
139
+ kind: Config
140
+ metadata:
141
+ name: cloud-run-test
142
+ manifests:
143
+ rawYaml:
144
+ - job.yaml
145
+ deploy:
146
+ cloudrun:
147
+ projectid: %v
148
+ region: %v` ,
149
+ },
150
+ {
151
+ descrition : "maxRetries not specified - default 3" ,
152
+ expectedMaxRetries : 3 ,
153
+ jobManifest : `
154
+ apiVersion: run.googleapis.com/v1
155
+ kind: Job
156
+ metadata:
157
+ annotations:
158
+ run.googleapis.com/launch-stage: BETA
159
+ name: %v
160
+ spec:
161
+ template:
162
+ spec:
163
+ template:
164
+ spec:
165
+ containers:
166
+ - image: docker.io/library/busybox:latest
167
+ name: job` ,
168
+ skaffoldCfg : `
169
+ apiVersion: %v
170
+ kind: Config
171
+ metadata:
172
+ name: cloud-run-test
173
+ manifests:
174
+ rawYaml:
175
+ - job.yaml
176
+ deploy:
177
+ cloudrun:
178
+ projectid: %v
179
+ region: %v` ,
180
+ },
181
+ }
182
+
183
+ for _ , test := range tests {
184
+ testutil .Run (t , test .descrition , func (t * testutil.T ) {
185
+ projectID := "k8s-skaffold"
186
+ region := "us-central1"
187
+ jobName := fmt .Sprintf ("job-%v" , uuid .New ().String ())
188
+ skaffoldCfg := fmt .Sprintf (test .skaffoldCfg , latest .Version , projectID , region )
189
+ jobManifest := fmt .Sprintf (test .jobManifest , jobName )
190
+
191
+ tmpDir := t .NewTempDir ()
192
+ tmpDir .Write ("skaffold.yaml" , skaffoldCfg )
193
+ tmpDir .Write ("job.yaml" , jobManifest )
194
+
195
+ skaffold .Run ().InDir (tmpDir .Root ()).RunOrFail (t .T )
196
+ t .Cleanup (func () {
197
+ skaffold .Delete (test .args ... ).InDir (tmpDir .Root ()).RunOrFail (t .T )
198
+ })
199
+
200
+ job , err := getJob (context .Background (), projectID , region , jobName )
201
+ if err != nil {
202
+ t .Fatal (err )
203
+ }
204
+
205
+ if diff := cmp .Diff (job .Spec .Template .Spec .Template .Spec .MaxRetries , test .expectedMaxRetries ); diff != "" {
206
+ t .Fatalf ("Job MaxRetries differ (-got,+want):\n %s" , diff )
207
+ }
208
+ })
209
+ }
210
+ }
211
+
72
212
// TODO: remove nolint when test is unskipped
73
213
//
74
214
//nolint:unused
@@ -82,6 +222,18 @@ func getRunService(ctx context.Context, project, region, service string) (*run.S
82
222
return call .Do ()
83
223
}
84
224
225
+ func getJob (ctx context.Context , project , region , job string ) (* run.Job , error ) {
226
+ cOptions := []option.ClientOption {option .WithEndpoint (fmt .Sprintf ("%s-run.googleapis.com" , region ))}
227
+ cOptions = append (gcp .ClientOptions (ctx ), cOptions ... )
228
+ crclient , err := run .NewService (ctx , cOptions ... )
229
+ if err != nil {
230
+ return nil , err
231
+ }
232
+ jName := fmt .Sprintf ("namespaces/%v/jobs/%v" , project , job )
233
+ call := crclient .Namespaces .Jobs .Get (jName )
234
+ return call .Do ()
235
+ }
236
+
85
237
// TODO: remove nolint when test is unskipped
86
238
//
87
239
//nolint:unused
0 commit comments