17217: Remove crufty test util funcs.
[arvados.git] / sdk / go / arvadostest / run_servers.go
index d3b48ea9fb738fb897ce2ed0926dc619c4522bbf..8f70c5ee26a97b20ac9ac04bd3434ca946eee0b9 100644 (file)
@@ -1,53 +1,44 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: Apache-2.0
+
 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
@@ -62,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 == "/" {
@@ -73,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...))
@@ -125,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