-
Notifications
You must be signed in to change notification settings - Fork 26
/
handler.go
237 lines (195 loc) · 7.57 KB
/
handler.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
/*
Copyright 2018 The Kubernetes Authors.
Copyright 2022 Google LLC
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
https://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 utils
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/klog/v2"
)
var envAPIMap = map[string]string{
"https://container.googleapis.com/": "prod",
"https://staging-container.sandbox.googleapis.com/": "staging",
"https://staging2-container.sandbox.googleapis.com/": "staging2",
"https://test-container.sandbox.googleapis.com/": "test",
}
type TestParameters struct {
PkgDir string
GkeClusterRegion string
GkeClusterVersion string
GkeNodeVersion string
GkeClusterName string
NodeImageType string
NodeMachineType string
NumNodes int
ProjectID string
UseGKEAutopilot bool
APIEndpointOverride string
InProw bool
BoskosResourceType string
ImageRegistry string
BuildGcsFuseCsiDriver bool
BuildGcsFuseFromSource bool
BuildArm bool
DeployOverlayName string
UseGKEManagedDriver bool
GinkgoSkip string
GinkgoFocus string
GinkgoProcs string
GinkgoTimeout string
GinkgoFlakeAttempts string
GinkgoSkipGcpSaTest bool
SupportsNativeSidecar bool
}
const TestWithNativeSidecarEnvVar = "TEST_WITH_NATIVE_SIDECAR"
func Handle(testParams *TestParameters) error {
oldMask := syscall.Umask(0o000)
defer syscall.Umask(oldMask)
// If the test is running in Prow, do the following steps:
// 1. Get the old project ID.
// 2. Acquire and set up a new project through Boskos.
// 3. Create a GKE cluster.
// 4. After the test, tear down the cluster, and switch back to the old project.
if testParams.InProw {
// 1. Get the old project ID.
output, err := exec.Command("gcloud", "config", "get-value", "project").CombinedOutput()
if err != nil {
return fmt.Errorf("failed to get gcloud project, output: %v, err: %w", string(output), err)
}
oldProject := string(output)
// 2. Acquire and set up a new project through Boskos.
newProject := setupProwConfig(testParams.BoskosResourceType)
if _, ok := os.LookupEnv("USER"); !ok {
if err := os.Setenv("USER", "prow"); err != nil {
return fmt.Errorf("failed to set user in prow to prow: %w", err)
}
}
if err := setEnvProject(newProject); err != nil {
return fmt.Errorf("failed to set project environment to %s: %w", newProject, err)
}
testParams.ProjectID = newProject
testParams.ImageRegistry = fmt.Sprintf("gcr.io/%s/gcs-fuse-csi-driver", strings.TrimSpace(newProject))
// 4. After the test, tear down the cluster, and switch back to the old project.
defer func() {
if err := setEnvProject(oldProject); err != nil {
klog.Errorf("failed to set project environment to %s: %v", oldProject, err)
}
}()
// 3. Create a GKE cluster.
testParams.GkeClusterName = "gcsfuse" + string(uuid.NewUUID())[0:4]
if err := clusterUpGKE(testParams); err != nil {
return fmt.Errorf("failed to cluster up: %w", err)
}
// 4. After the test, tear down the cluster, and switch back to the old project.
defer func() {
if err := clusterDownGKE(testParams); err != nil {
klog.Errorf("failed to cluster down: %v", err)
}
}()
}
// TODO(jaimebz): Extract server version using kubeapi if not present.
// Build and push the driver if the test does not use the pre-installed managed CSI driver. Defer the driver image deletion.
if !testParams.UseGKEManagedDriver {
if testParams.BuildGcsFuseCsiDriver {
klog.Infof("Building GCS FUSE CSI Driver")
if err := buildAndPushImage(testParams.PkgDir, testParams.ImageRegistry, testParams.BuildGcsFuseFromSource, testParams.BuildArm); err != nil {
return fmt.Errorf("failed pushing GCS FUSE CSI Driver images: %w", err)
}
// Defer the image deletion.
if testParams.InProw {
defer func() {
if err := deleteImage(); err != nil {
klog.Errorf("failed to delete GCS FUSE CSI Driver images: %v", err)
}
}()
}
}
// Uninstall and install the CSI driver.
if err := deleteDriver(testParams.PkgDir, testParams.DeployOverlayName); err != nil {
klog.Errorf("failed to delete CSI driver: %v", err)
}
if err := installDriver(testParams.PkgDir, testParams.ImageRegistry, testParams.DeployOverlayName); err != nil {
return fmt.Errorf("failed to install CSI Driver: %w", err)
}
}
// Now that cluster is running and the CSI driver is installed, run the ginkgo tests on the cluster.
artifactsDir, ok := os.LookupEnv("ARTIFACTS")
if !ok {
artifactsDir = testParams.PkgDir + "/_artifacts"
}
testFocusStr := testParams.GinkgoFocus
if len(testFocusStr) != 0 {
testFocusStr = fmt.Sprintf(".*%s.*", testFocusStr)
}
supportsNativeSidecar, err := ClusterSupportsNativeSidecar(testParams.GkeClusterVersion, testParams.GkeNodeVersion)
if err != nil {
klog.Fatalf(`native sidecar support could not be determined: %v`, err)
}
testParams.SupportsNativeSidecar = supportsNativeSidecar
if err = os.Setenv(TestWithNativeSidecarEnvVar, strconv.FormatBool(supportsNativeSidecar)); err != nil {
klog.Fatalf(`env variable "%s" could not be set: %v`, TestWithNativeSidecarEnvVar, err)
}
//nolint:gosec
cmd := exec.Command("ginkgo", "run", "-v",
"--procs", testParams.GinkgoProcs,
"--flake-attempts", testParams.GinkgoFlakeAttempts,
"--timeout", testParams.GinkgoTimeout,
"--focus", testFocusStr,
"--skip", generateTestSkip(testParams),
"--junit-report", "junit-gcsfusecsi.xml",
"--output-dir", artifactsDir,
testParams.PkgDir+"/test/e2e/",
"--",
"--provider", "skeleton",
"--test-bucket-location", testParams.GkeClusterRegion,
"--skip-gcp-sa-test", strconv.FormatBool(testParams.GinkgoSkipGcpSaTest),
"--api-env", envAPIMap[testParams.APIEndpointOverride],
)
if err := runCommand("Running Ginkgo e2e test...", cmd); err != nil {
return fmt.Errorf("failed to run e2e tests with ginkgo: %w", err)
}
return nil
}
func generateTestSkip(testParams *TestParameters) string {
skipTests := []string{}
if testParams.GinkgoSkip != "" {
skipTests = append(skipTests, testParams.GinkgoSkip)
}
if testParams.DeployOverlayName == "stable" {
skipTests = append(skipTests, "Dynamic.PV")
}
if testParams.UseGKEAutopilot {
skipTests = append(skipTests, "OOM", "high.resource.usage", "gcsfuseIntegration")
if strings.HasPrefix(testParams.GkeClusterVersion, "1.29") {
skipTests = append(skipTests, "buffer.volume")
}
}
if !testParams.SupportsNativeSidecar {
skipTests = append(skipTests, "init.container", "fast.termination")
}
// TODO(songjiaxun) remove this logic after the next CSI driver release.
if testParams.UseGKEManagedDriver {
skipTests = append(skipTests, "Pod.RestartPolicy.is.OnFailure$", "Job.with.RestartPolicy.OnFailure.eventually.succeed", "fast.termination", "fileCache", "gcsfuseIntegrationFileCache", "init.container", "istio")
if strings.HasPrefix(testParams.GkeClusterVersion, "1.29") && testParams.SupportsNativeSidecar {
skipTests = append(skipTests, "autoTermination", "custom.sidecar.container")
}
}
skipString := strings.Join(skipTests, "|")
klog.Infof("Generated ginkgo skip string: %q", skipString)
return skipString
}