X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/0eb72b526bf8bbb011551ecf019f604e17a534f1..13c8401a89d34cc412e76ade8f112a31b9988e4f:/sdk/go/arvadostest/run_servers.go diff --git a/sdk/go/arvadostest/run_servers.go b/sdk/go/arvadostest/run_servers.go index c567560bc1..8f70c5ee26 100644 --- a/sdk/go/arvadostest/run_servers.go +++ b/sdk/go/arvadostest/run_servers.go @@ -5,53 +5,40 @@ package arvadostest import ( - "bufio" - "bytes" + "crypto/tls" "fmt" "io/ioutil" "log" + "net/http" "os" "os/exec" "path" "strconv" "strings" + + "gopkg.in/check.v1" ) var authSettings = make(map[string]string) -// ResetEnv resets test env +// ResetEnv resets ARVADOS_* env vars to whatever they were the first +// time this func was called. +// +// Call it from your SetUpTest or SetUpSuite func if your tests modify +// env vars. func ResetEnv() { - for k, v := range authSettings { - os.Setenv(k, v) - } -} - -// APIHost returns the address:port of the current test server. -func APIHost() string { - h := authSettings["ARVADOS_API_HOST"] - if h == "" { - log.Fatal("arvadostest.APIHost() was called but authSettings is not populated") - } - return h -} - -// ParseAuthSettings parses auth settings from given input -func ParseAuthSettings(authScript []byte) { - scanner := bufio.NewScanner(bytes.NewReader(authScript)) - for scanner.Scan() { - line := scanner.Text() - if 0 != strings.Index(line, "export ") { - log.Printf("Ignoring: %v", line) - continue + if len(authSettings) == 0 { + for _, e := range os.Environ() { + e := strings.SplitN(e, "=", 2) + if len(e) == 2 { + authSettings[e[0]] = e[1] + } } - toks := strings.SplitN(strings.Replace(line, "export ", "", 1), "=", 2) - if len(toks) == 2 { - authSettings[toks[0]] = toks[1] - } else { - log.Fatalf("Could not parse: %v", line) + } else { + for k, v := range authSettings { + os.Setenv(k, v) } } - log.Printf("authSettings: %v", authSettings) } var pythonTestDir string @@ -66,6 +53,9 @@ func chdirToPythonTests() { for { if err := os.Chdir("sdk/python/tests"); err == nil { pythonTestDir, err = os.Getwd() + if err != nil { + log.Fatal(err) + } return } if parent, err := os.Getwd(); err != nil || parent == "/" { @@ -77,44 +67,30 @@ func chdirToPythonTests() { } } -// StartAPI starts test API server -func StartAPI() { - cwd, _ := os.Getwd() - defer os.Chdir(cwd) - chdirToPythonTests() - - cmd := exec.Command("python", "run_test_server.py", "start", "--auth", "admin") - cmd.Stdin = nil - cmd.Stderr = os.Stderr - - authScript, err := cmd.Output() - if err != nil { - log.Fatalf("%+v: %s", cmd.Args, err) - } - ParseAuthSettings(authScript) - ResetEnv() -} - -// StopAPI stops test API server -func StopAPI() { - cwd, _ := os.Getwd() - defer os.Chdir(cwd) - chdirToPythonTests() - - bgRun(exec.Command("python", "run_test_server.py", "stop")) +func ResetDB(c *check.C) { + hc := http.Client{Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + }} + req, err := http.NewRequest("POST", "https://"+os.Getenv("ARVADOS_TEST_API_HOST")+"/database/reset", nil) + c.Assert(err, check.IsNil) + req.Header.Set("Authorization", "Bearer "+AdminToken) + resp, err := hc.Do(req) + c.Assert(err, check.IsNil) + defer resp.Body.Close() + c.Check(resp.StatusCode, check.Equals, http.StatusOK) } // StartKeep starts the given number of keep servers, -// optionally with -enforce-permissions enabled. -// Use numKeepServers = 2 and enforcePermissions = false under all normal circumstances. -func StartKeep(numKeepServers int, enforcePermissions bool) { +// optionally with --keep-blob-signing enabled. +// Use numKeepServers = 2 and blobSigning = false under all normal circumstances. +func StartKeep(numKeepServers int, blobSigning bool) { cwd, _ := os.Getwd() defer os.Chdir(cwd) chdirToPythonTests() cmdArgs := []string{"run_test_server.py", "start_keep", "--num-keep-servers", strconv.Itoa(numKeepServers)} - if enforcePermissions { - cmdArgs = append(cmdArgs, "--keep-enforce-permissions") + if blobSigning { + cmdArgs = append(cmdArgs, "--keep-blob-signing") } bgRun(exec.Command("python", cmdArgs...)) @@ -129,12 +105,9 @@ func StopKeep(numKeepServers int) { chdirToPythonTests() cmd := exec.Command("python", "run_test_server.py", "stop_keep", "--num-keep-servers", strconv.Itoa(numKeepServers)) - cmd.Stdin = nil - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stderr - if err := cmd.Run(); err != nil { - log.Fatalf("%+v: %s", cmd.Args, err) - } + bgRun(cmd) + // Without Wait, "go test" in go1.10.1 tends to hang. https://github.com/golang/go/issues/24050 + cmd.Wait() } // Start cmd, with stderr and stdout redirected to our own