-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdeploy_cloudrun_test.go
254 lines (231 loc) · 6.46 KB
/
deploy_cloudrun_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
/*
Copyright 2022 The Skaffold 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 integration
import (
"context"
"fmt"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"google.golang.org/api/option"
"google.golang.org/api/run/v1"
"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/gcp"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/v2/testutil"
)
func TestDeployCloudRun(t *testing.T) {
MarkIntegrationTest(t, NeedsGcp)
// Other integration tests run with the --default-repo option.
// This one explicitly specifies the full image name.
skaffold.Deploy().InDir("testdata/deploy-cloudrun").RunOrFail(t)
ctx := context.Background()
svc, err := getRunService(ctx, "k8s-skaffold", "us-central1", "skaffold-test")
if err != nil {
t.Fatal(err)
}
if err = checkReady(svc); err != nil {
t.Fatal(err)
}
}
func TestDeployCloudRunWithHooks(t *testing.T) {
MarkIntegrationTest(t, NeedsGcp)
testutil.Run(t, "cloud run deploy with hooks", func(t *testutil.T) {
expectedOutput := []string{
"PRE-DEPLOY Cloud Run MODULE1 host hook",
"POST-DEPLOY Cloud Run MODULE1 host hook",
"PRE-DEPLOY Cloud Run MODULE2 host hook",
"POST-DEPLOY Cloud Run MODULE2 host hook",
}
out := skaffold.Run().InDir("testdata/deploy-cloudrun-with-hooks").RunOrFailOutput(t.T)
commandOutput := string(out)
previousFoundIndex := -1
for _, expectedOutput := range expectedOutput {
expectedOutputFoundIndex := strings.Index(commandOutput, expectedOutput)
isPreviousOutputBeforeThanCurrent := previousFoundIndex < expectedOutputFoundIndex
t.CheckTrue(isPreviousOutputBeforeThanCurrent)
previousFoundIndex = expectedOutputFoundIndex
}
})
}
func TestDeployJobWithMaxRetries(t *testing.T) {
MarkIntegrationTest(t, NeedsGcp)
tests := []struct {
descrition string
jobManifest string
skaffoldCfg string
args []string
expectedMaxRetries int64
}{
{
descrition: "maxRetries set to specific value",
expectedMaxRetries: 2,
jobManifest: `
apiVersion: run.googleapis.com/v1
kind: Job
metadata:
annotations:
run.googleapis.com/launch-stage: BETA
name: %v
spec:
template:
spec:
template:
spec:
containers:
- image: docker.io/library/busybox:latest
name: job
maxRetries: 2`,
skaffoldCfg: `
apiVersion: %v
kind: Config
metadata:
name: cloud-run-test
manifests:
rawYaml:
- job.yaml
deploy:
cloudrun:
projectid: %v
region: %v`,
},
{
descrition: "maxRetries set to 0",
expectedMaxRetries: 0,
jobManifest: `
apiVersion: run.googleapis.com/v1
kind: Job
metadata:
annotations:
run.googleapis.com/launch-stage: BETA
name: %v
spec:
template:
spec:
template:
spec:
containers:
- image: docker.io/library/busybox:latest
name: job
maxRetries: 0`,
skaffoldCfg: `
apiVersion: %v
kind: Config
metadata:
name: cloud-run-test
manifests:
rawYaml:
- job.yaml
deploy:
cloudrun:
projectid: %v
region: %v`,
},
{
descrition: "maxRetries not specified - default 3",
expectedMaxRetries: 3,
jobManifest: `
apiVersion: run.googleapis.com/v1
kind: Job
metadata:
annotations:
run.googleapis.com/launch-stage: BETA
name: %v
spec:
template:
spec:
template:
spec:
containers:
- image: docker.io/library/busybox:latest
name: job`,
skaffoldCfg: `
apiVersion: %v
kind: Config
metadata:
name: cloud-run-test
manifests:
rawYaml:
- job.yaml
deploy:
cloudrun:
projectid: %v
region: %v`,
},
}
for _, test := range tests {
testutil.Run(t, test.descrition, func(t *testutil.T) {
projectID := "k8s-skaffold"
region := "us-central1"
jobName := fmt.Sprintf("job-%v", uuid.New().String())
skaffoldCfg := fmt.Sprintf(test.skaffoldCfg, latest.Version, projectID, region)
jobManifest := fmt.Sprintf(test.jobManifest, jobName)
tmpDir := t.NewTempDir()
tmpDir.Write("skaffold.yaml", skaffoldCfg)
tmpDir.Write("job.yaml", jobManifest)
skaffold.Run().InDir(tmpDir.Root()).RunOrFail(t.T)
t.Cleanup(func() {
skaffold.Delete(test.args...).InDir(tmpDir.Root()).RunOrFail(t.T)
})
job, err := getJob(context.Background(), projectID, region, jobName)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(job.Spec.Template.Spec.Template.Spec.MaxRetries, test.expectedMaxRetries); diff != "" {
t.Fatalf("Job MaxRetries differ (-got,+want):\n%s", diff)
}
})
}
}
// TODO: remove nolint when test is unskipped
//
//nolint:unused
func getRunService(ctx context.Context, project, region, service string) (*run.Service, error) {
crclient, err := run.NewService(ctx, gcp.ClientOptions(ctx)...)
if err != nil {
return nil, err
}
sName := fmt.Sprintf("projects/%s/locations/%s/services/%s", project, region, service)
call := crclient.Projects.Locations.Services.Get(sName)
return call.Do()
}
func getJob(ctx context.Context, project, region, job string) (*run.Job, error) {
cOptions := []option.ClientOption{option.WithEndpoint(fmt.Sprintf("%s-run.googleapis.com", region))}
cOptions = append(gcp.ClientOptions(ctx), cOptions...)
crclient, err := run.NewService(ctx, cOptions...)
if err != nil {
return nil, err
}
jName := fmt.Sprintf("namespaces/%v/jobs/%v", project, job)
call := crclient.Namespaces.Jobs.Get(jName)
return call.Do()
}
// TODO: remove nolint when test is unskipped
//
//nolint:unused
func checkReady(svc *run.Service) error {
var ready *run.GoogleCloudRunV1Condition
for _, cond := range svc.Status.Conditions {
if cond.Type == "Ready" {
ready = cond
}
}
if ready == nil {
return fmt.Errorf("ready condition not found in service: %v", svc)
}
if ready.Status != "True" {
return fmt.Errorf("expected ready status of true, got %s with reason %s", ready.Status, ready.Message)
}
return nil
}