Skip to content

Commit 18a433a

Browse files
author
Hantao (Will) Wang
committed
add flag checking and temp dir utilities
1 parent 46f7a67 commit 18a433a

File tree

1 file changed

+62
-38
lines changed

1 file changed

+62
-38
lines changed

test/k8s-integration/main.go

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package main
1717
import (
1818
"flag"
1919
"fmt"
20+
"io/ioutil"
2021
"os"
2122
"os/exec"
2223
"path/filepath"
@@ -69,46 +70,41 @@ func init() {
6970
}
7071
func main() {
7172
flag.Parse()
72-
73-
if len(*stagingImage) == 0 && !*inProw {
74-
glog.Fatalf("staging-image is a required flag, please specify the name of image to stage to")
75-
}
76-
77-
if len(*saFile) == 0 {
78-
glog.Fatalf("service-account-file is a required flag")
79-
}
80-
81-
if len(*deployOverlayName) == 0 {
82-
glog.Fatalf("deploy-overlay-name is a required flag")
83-
}
84-
85-
if len(*storageClassFile) == 0 && !*migrationTest {
86-
glog.Fatalf("One of storageclass-file and migration-test must be set")
73+
74+
if !*inProw {
75+
ensureVariable(stagingImage, true, "staging-image is a required flag, please specify the name of image to stage to")
8776
}
8877

89-
if len(*storageClassFile) != 0 && *migrationTest {
90-
glog.Fatalf("storage-class-file and migration-test cannot both be set")
91-
}
78+
ensureVariable(saFile, true, "service-account-file is a required flag")
79+
ensureVariable(deployOverlayName, true, "deploy-overlay-name is a required flag")
80+
ensureVariable(testFocus, true, "test-focus is a required flag")
81+
ensureVariable(gceZone, true, "gce-zone is a required flag")
9282

93-
if !*bringupCluster && len(*kubeFeatureGates) > 0 {
94-
glog.Fatalf("kube-feature-gates set but not bringing up new cluster")
83+
if *migrationTest {
84+
ensureVariable(storageClassFile, false, "storage-class-file and migration-test cannot both be set")
85+
} else {
86+
ensureVariable(storageClassFile, true, "One of storageclass-file and migration-test must be set")
9587
}
9688

97-
if len(*testFocus) == 0 {
98-
glog.Fatalf("test-focus is a required flag")
89+
if !*bringupCluster {
90+
ensureVariable(kubeFeatureGates, false, "kube-feature-gates set but not bringing up new cluster")
9991
}
10092

101-
if len(*gceZone) == 0 {
102-
glog.Fatalf("gce-zone is a required flag")
93+
if *bringupCluster || *teardownCluster {
94+
ensureVariable(deploymentStrat, true, "Must set the deployment strategy if bringing up or down cluster.")
95+
} else {
96+
ensureVariable(deploymentStrat, false, "Cannot set the deployment strategy if not bringing up or down cluster.")
10397
}
10498

105-
if *deploymentStrat == "gke" && *migrationTest {
106-
glog.Fatalf("Cannot set deployment strategy to 'gke' for migration tests.")
99+
if *deploymentStrat == "gke" {
100+
ensureFlag(migrationTest, false, "Cannot set deployment strategy to 'gke' for migration tests.")
101+
ensureVariable(kubeVersion, false, "Cannot set kube-version when using deployment strategy 'gke'. Use gke-cluster-version.")
102+
ensureVariable(gkeClusterVer, true, "Must set gke-cluster-version when using deployment strategy 'gke'.")
103+
ensureVariable(kubeFeatureGates, false, "Cannot set feature gates when using deployment strategy 'gke'.")
107104
}
108105

109-
err := handle()
110-
if err != nil {
111-
glog.Fatalf("Failed to run integration test: %v", err)
106+
if len(*localK8sDir) != 0 {
107+
ensureVariable(kubeVersion, false, "Cannot set a kube version when using a local k8s dir.")
112108
}
113109
}
114110

@@ -123,8 +119,9 @@ func handle() error {
123119
return fmt.Errorf("Could not find env variable GOPATH")
124120
}
125121
pkgDir := filepath.Join(goPath, "src", "sigs.k8s.io", "gcp-compute-persistent-disk-csi-driver")
126-
k8sIoDir := filepath.Join(pkgDir, "test", "k8s-integration", "src", "k8s.io")
127-
k8sDir := filepath.Join(k8sIoDir, "kubernetes")
122+
k8sParentDir := generateUniqueTmpDir()
123+
k8sDir := filepath.Join(k8sParentDir, "kubernetes")
124+
defer removeDir(k8sParentDir)
128125

129126
if *inProw {
130127
project, _ := testutils.SetupProwConfig(*boskosResourceType)
@@ -173,7 +170,7 @@ func handle() error {
173170
}
174171

175172
if *bringupCluster {
176-
err := downloadKubernetesSource(pkgDir, k8sIoDir, *kubeVersion)
173+
err := downloadKubernetesSource(pkgDir, k8sParentDir, *kubeVersion)
177174
if err != nil {
178175
return fmt.Errorf("failed to download Kubernetes source: %v", err)
179176
}
@@ -239,7 +236,7 @@ func handle() error {
239236
}()
240237
}
241238

242-
err := installDriver(goPath, pkgDir, k8sDir, *stagingImage, stagingVersion, *deployOverlayName, *doDriverBuild)
239+
err := installDriver(goPath, pkgDir, *stagingImage, stagingVersion, *deployOverlayName, *doDriverBuild)
243240
if *teardownDriver {
244241
defer func() {
245242
// TODO (#140): collect driver logs
@@ -419,7 +416,7 @@ func getOverlayDir(pkgDir, deployOverlayName string) string {
419416
return filepath.Join(pkgDir, "deploy", "kubernetes", "overlays", deployOverlayName)
420417
}
421418

422-
func installDriver(goPath, pkgDir, k8sDir, stagingImage, stagingVersion, deployOverlayName string, doDriverBuild bool) error {
419+
func installDriver(goPath, pkgDir, stagingImage, stagingVersion, deployOverlayName string, doDriverBuild bool) error {
423420
if doDriverBuild {
424421
// Install kustomize
425422
out, err := exec.Command(filepath.Join(pkgDir, "deploy", "kubernetes", "install-kustomize.sh")).CombinedOutput()
@@ -448,10 +445,8 @@ func installDriver(goPath, pkgDir, k8sDir, stagingImage, stagingVersion, deployO
448445
}
449446

450447
// setup service account file for secret creation
451-
tmpSaFile := fmt.Sprintf("/tmp/%s/cloud-sa.json", string(uuid.NewUUID()))
452-
453-
os.MkdirAll(filepath.Dir(tmpSaFile), 0750)
454-
defer os.Remove(filepath.Dir(tmpSaFile))
448+
tmpSaFile := filepath.Join(generateUniqueTmpDir(), "cloud-sa.json")
449+
defer removeDir(filepath.Dir(tmpSaFile))
455450

456451
// Need to copy it to name the file "cloud-sa.json"
457452
out, err := exec.Command("cp", *saFile, tmpSaFile).CombinedOutput()
@@ -601,3 +596,32 @@ func deleteImage(stagingImage, stagingVersion string) error {
601596
}
602597
return nil
603598
}
599+
600+
func generateUniqueTmpDir() string {
601+
dir, err := ioutil.TempDir("", "gcp-pd-driver-tmp")
602+
if err != nil {
603+
klog.Fatalf("Error creating temp dir: %v", err)
604+
}
605+
return dir
606+
}
607+
608+
func removeDir(dir string) {
609+
err := os.RemoveAll(dir)
610+
if err != nil {
611+
klog.Fatalf("Error removing temp dir: %v", err)
612+
}
613+
}
614+
615+
func ensureVariable(v *string, set bool, msgOnError string) {
616+
if set && len(*v) == 0 {
617+
klog.Fatal(msgOnError)
618+
} else if !set && len(*v) != 0 {
619+
klog.Fatal(msgOnError)
620+
}
621+
}
622+
623+
func ensureFlag(v *bool, setTo bool, msgOnError string) {
624+
if *v != setTo {
625+
klog.Fatal(msgOnError)
626+
}
627+
}

0 commit comments

Comments
 (0)