Add logic for renting test project from Boskos

This commit is contained in:
Xuewei Zhang
2019-09-14 15:22:09 -07:00
parent e1939ebc03
commit fb7fd239bb
2 changed files with 49 additions and 0 deletions
+48
View File
@@ -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()