From fb7fd239bbafc93bee1469541d0e28e69d10a728 Mon Sep 17 00:00:00 2001 From: Xuewei Zhang Date: Fri, 13 Sep 2019 17:44:48 -0700 Subject: [PATCH] Add logic for renting test project from Boskos --- Makefile | 1 + test/e2e/metriconly/e2e_npd_test.go | 48 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/Makefile b/Makefile index 9cdf42c6..2243aadc 100644 --- a/Makefile +++ b/Makefile @@ -115,6 +115,7 @@ e2e-test: vet fmt build-tar -image=$(VM_IMAGE) -image-project=$(IMAGE_PROJECT) \ -ssh-user=$(SSH_USER) -ssh-key=$(SSH_KEY) \ -npd-build-tar=`pwd`/$(TARBALL) \ + -boskos-project-type=$(BOSKOS_PROJECT_TYPE) -job-name=$(JOB_NAME) \ -artifacts-dir=$(ARTIFACTS) build-binaries: ./bin/node-problem-detector ./bin/log-counter diff --git a/test/e2e/metriconly/e2e_npd_test.go b/test/e2e/metriconly/e2e_npd_test.go index d747dd42..8a5075bd 100644 --- a/test/e2e/metriconly/e2e_npd_test.go +++ b/test/e2e/metriconly/e2e_npd_test.go @@ -17,13 +17,16 @@ limitations under the License. package e2e_metric_only import ( + "context" "flag" "fmt" "os" "path" "testing" + "time" "k8s.io/node-problem-detector/test/e2e/lib/gce" + "k8s.io/test-infra/boskos/client" "github.com/onsi/ginkgo" "github.com/onsi/ginkgo/reporters" @@ -36,10 +39,17 @@ var zone = flag.String("zone", "", "gce zone the hosts live in") var project = flag.String("project", "", "gce project the hosts live in") var image = flag.String("image", "", "image to test") var imageProject = flag.String("image-project", "", "gce project of the OS image") +var jobName = flag.String("job-name", "", "name of the Prow job running the test") var sshKey = flag.String("ssh-key", "", "path to ssh private key.") var sshUser = flag.String("ssh-user", "", "use predefined user for ssh.") var npdBuildTar = flag.String("npd-build-tar", "", "tarball containing NPD to be tested.") var artifactsDir = flag.String("artifacts-dir", "", "local directory to save test artifacts into.") +var boskosProjectType = flag.String("boskos-project-type", "gce-project", + "specifies which project type to select from Boskos.") +var boskosServerURL = flag.String("boskos-server-url", "http://boskos.test-pods.svc.cluster.local", + "specifies Boskos server URL.") +var boskosWaitDuration = flag.Duration("boskos-wait-duration", 5*time.Minute, + "Duration to wait before quitting getting Boskos resource.") var computeService *compute.Service @@ -48,6 +58,13 @@ func TestNPD(t *testing.T) { t.Skip("skipping test in short mode.") } + if *project == "" { + boskosClient := client.NewClient(*jobName, *boskosServerURL) + *project = acquireProjectOrDie(boskosClient) + + defer releaseProjectOrDie(boskosClient) + } + if *artifactsDir != "" { _, err := os.Stat(*artifactsDir) if err != nil && os.IsNotExist(err) { @@ -60,6 +77,37 @@ func TestNPD(t *testing.T) { ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "NPD Metric-only Suite", []ginkgo.Reporter{junitReporter}) } +func acquireProjectOrDie(boskosClient *client.Client) string { + fmt.Printf("Renting project from Boskos\n") + ctx, cancel := context.WithTimeout(context.Background(), *boskosWaitDuration) + defer cancel() + p, err := boskosClient.AcquireWait(ctx, *boskosProjectType, "free", "busy") + if err != nil { + panic(fmt.Sprintf("Unable to rent project from Boskos: %v\n", err)) + } + fmt.Printf("Rented project %s from Boskos", p.Name) + + go func(boskosClient *client.Client, projectName string) { + for range time.Tick(5 * time.Minute) { + if err := boskosClient.UpdateOne(projectName, "busy", nil); err != nil { + fmt.Printf("Failed to update status for project %s with Boskos: %v\n", projectName, err) + } + } + }(boskosClient, p.Name) + + return p.Name +} + +func releaseProjectOrDie(boskosClient *client.Client) { + if !boskosClient.HasResource() { + return + } + err := boskosClient.ReleaseAll("dirty") + if err != nil { + panic(fmt.Sprintf("Failed to release project to Boskos: %v", err)) + } +} + func TestMain(m *testing.M) { flag.Parse()