Skip to content

Commit ef9bfb2

Browse files
author
Hantao (Will) Wang
committed
add functionality to use gke with kubetest
1 parent 168218c commit ef9bfb2

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

test/k8s-integration/cluster.go

+39
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,42 @@ func downloadKubernetesSource(pkgDir, k8sIoDir, kubeVersion string) error {
155155

156156
return nil
157157
}
158+
159+
func getGKEKubeTestArgs() ([]string, error) {
160+
var gkeEnv string
161+
switch gkeURL := os.Getenv("CLOUDSDK_API_ENDPOINT_OVERRIDES_CONTAINER"); gkeURL {
162+
case "https://staging-container.sandbox.googleapis.com/":
163+
gkeEnv = "staging"
164+
case "https://test-container.sandbox.googleapis.com/":
165+
gkeEnv = "test"
166+
case "":
167+
gkeEnv = "prod"
168+
default:
169+
// if the URL does not match to an option, assume it is a custom GKE backend
170+
// URL and pass that to kubetest
171+
gkeEnv = gkeURL
172+
}
173+
174+
cmd := exec.Command("gcloud", "config", "get-value", "project")
175+
project, err := cmd.Output()
176+
if err != nil {
177+
return nil, fmt.Errorf("failed to get current project: %v", err)
178+
}
179+
180+
args := []string{
181+
"--up=false",
182+
"--down=false",
183+
"--provider=gke",
184+
"--gcp-network=default",
185+
"--check-version-skew=false",
186+
"--deployment=gke",
187+
"--gcp-node-image=cos",
188+
"--gcp-network=default",
189+
fmt.Sprintf("--cluster=%s", gkeTestClusterName),
190+
fmt.Sprintf("--gke-environment=%s", gkeEnv),
191+
fmt.Sprintf("--gcp-zone=%s", *gceZone),
192+
fmt.Sprintf("--gcp-project=%s", project[:len(project)-1]),
193+
}
194+
195+
return args, nil
196+
}

test/k8s-integration/main.go

+26-12
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,17 @@ func handle() error {
216216
if err != nil {
217217
return fmt.Errorf("failed to build Kubernetes: %v", err)
218218
}
219+
// kubetest relies on ginkgo already built in the test k8s directory
220+
err = buildKubernetes(testDir, "ginkgo")
221+
if err != nil {
222+
return fmt.Errorf("failed to build gingko: %v", err)
223+
}
219224
} else {
220225
testDir = k8sDir
221226
}
222227

228+
var cloudProviderArgs []string
229+
223230
// Create a cluster either through GKE or GCE
224231
if *bringupCluster {
225232
var err error = nil
@@ -228,6 +235,10 @@ func handle() error {
228235
err = clusterUpGCE(k8sDir, *gceZone, *numNodes)
229236
case "gke":
230237
err = clusterUpGKE(*gceZone, *numNodes)
238+
cloudProviderArgs, err = getGKEKubeTestArgs()
239+
if err != nil {
240+
return fmt.Errorf("failed to build GKE kubetest args: %v", err)
241+
}
231242
default:
232243
err = fmt.Errorf("deployment-strategy must be set to 'gce' or 'gke', but is: %s", *deploymentStrat)
233244
}
@@ -272,9 +283,9 @@ func handle() error {
272283

273284
// Run the tests using the testDir kubernetes
274285
if len(*storageClassFile) != 0 {
275-
err = runCSITests(pkgDir, testDir, *testFocus, *storageClassFile)
286+
err = runCSITests(pkgDir, testDir, *testFocus, *storageClassFile, cloudProviderArgs)
276287
} else if *migrationTest {
277-
err = runMigrationTests(pkgDir, testDir, *testFocus)
288+
err = runMigrationTests(pkgDir, testDir, *testFocus, cloudProviderArgs)
278289
} else {
279290
return fmt.Errorf("did not run either CSI or Migration test")
280291
}
@@ -299,20 +310,20 @@ func setEnvProject(project string) error {
299310
return nil
300311
}
301312

302-
func runMigrationTests(pkgDir, testDir, testFocus string) error {
303-
return runTestsWithConfig(testDir, testFocus, "--storage.migratedPlugins=kubernetes.io/gce-pd")
313+
func runMigrationTests(pkgDir, testDir, testFocus string, cloudProviderArgs []string) error {
314+
return runTestsWithConfig(testDir, testFocus, "--storage.migratedPlugins=kubernetes.io/gce-pd", cloudProviderArgs)
304315
}
305316

306-
func runCSITests(pkgDir, testDir, testFocus, storageClassFile string) error {
317+
func runCSITests(pkgDir, testDir, testFocus, storageClassFile string, cloudProviderArgs []string) error {
307318
testDriverConfigFile, err := generateDriverConfigFile(pkgDir, storageClassFile)
308319
if err != nil {
309320
return err
310321
}
311322
testConfigArg := fmt.Sprintf("--storage.testdriver=%s", testDriverConfigFile)
312-
return runTestsWithConfig(testDir, testFocus, testConfigArg)
323+
return runTestsWithConfig(testDir, testFocus, testConfigArg, cloudProviderArgs)
313324
}
314325

315-
func runTestsWithConfig(testDir, testFocus, testConfigArg string) error {
326+
func runTestsWithConfig(testDir, testFocus, testConfigArg string, cloudProviderArgs []string) error {
316327
err := os.Chdir(testDir)
317328
if err != nil {
318329
return err
@@ -324,18 +335,21 @@ func runTestsWithConfig(testDir, testFocus, testConfigArg string) error {
324335
artifactsDir, _ := os.LookupEnv("ARTIFACTS")
325336
reportArg := fmt.Sprintf("-report-dir=%s", artifactsDir)
326337

327-
kubetestArgs := fmt.Sprintf("--ginkgo.focus=%s --ginkgo.skip=%s %s %s",
338+
testArgs := fmt.Sprintf("--ginkgo.focus=%s --ginkgo.skip=%s %s %s",
328339
testFocus,
329340
"\\[Disruptive\\]|\\[Serial\\]|\\[Feature:.+\\]",
330341
testConfigArg,
331342
reportArg)
332343

333-
cmd := exec.Command("kubetest",
344+
kubeTestArgs := []string{
334345
"--test",
335346
"--ginkgo-parallel",
336-
fmt.Sprintf("--test_args=%s", kubetestArgs),
337-
)
338-
err = runCommand("Running Tests", cmd)
347+
fmt.Sprintf("--test_args=%s", testArgs),
348+
}
349+
350+
kubeTestArgs = append(kubeTestArgs, cloudProviderArgs...)
351+
352+
err = runCommand("Running Tests", exec.Command("kubetest", kubeTestArgs...))
339353
if err != nil {
340354
return fmt.Errorf("failed to run tests on e2e cluster: %v", err)
341355
}

0 commit comments

Comments
 (0)