From 8e75f57bd693d0ceb1aab86ba0e84cb19b4d155a Mon Sep 17 00:00:00 2001
From: Tom Clegg <tom@curii.com>
Date: Tue, 24 Aug 2021 16:07:43 -0400
Subject: [PATCH] 17217: Remove crufty test util funcs.

Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>
---
 lib/costanalyzer/costanalyzer_test.go         |  1 -
 lib/recovercollection/cmd_test.go             |  1 -
 sdk/go/arvadostest/run_servers.go             | 88 ++++++-------------
 sdk/go/dispatch/dispatch_test.go              |  8 --
 sdk/go/keepclient/keepclient_test.go          |  2 -
 services/arv-git-httpd/auth_handler_test.go   |  8 --
 services/arv-git-httpd/integration_test.go    |  8 --
 .../crunch-dispatch-local_test.go             |  5 --
 .../crunch-dispatch-slurm_test.go             |  5 +-
 services/keep-balance/integration_test.go     |  2 -
 services/keep-web/server_test.go              |  3 +-
 services/keepproxy/keepproxy_test.go          |  9 --
 .../keepstore/pull_worker_integration_test.go |  2 -
 .../keep-block-check/keep-block-check_test.go |  5 --
 tools/keep-rsync/keep-rsync_test.go           |  5 --
 15 files changed, 33 insertions(+), 119 deletions(-)

diff --git a/lib/costanalyzer/costanalyzer_test.go b/lib/costanalyzer/costanalyzer_test.go
index 9fee66e1dd..2975e3b3de 100644
--- a/lib/costanalyzer/costanalyzer_test.go
+++ b/lib/costanalyzer/costanalyzer_test.go
@@ -33,7 +33,6 @@ func (s *Suite) TearDownSuite(c *check.C) {
 }
 
 func (s *Suite) SetUpSuite(c *check.C) {
-	arvadostest.StartAPI()
 	arvadostest.StartKeep(2, true)
 
 	// Get the various arvados, arvadosclient, and keep client objects
diff --git a/lib/recovercollection/cmd_test.go b/lib/recovercollection/cmd_test.go
index 7b3c8e1b4e..f891e55677 100644
--- a/lib/recovercollection/cmd_test.go
+++ b/lib/recovercollection/cmd_test.go
@@ -27,7 +27,6 @@ var _ = check.Suite(&Suite{})
 type Suite struct{}
 
 func (*Suite) SetUpSuite(c *check.C) {
-	arvadostest.StartAPI()
 	arvadostest.StartKeep(2, true)
 }
 
diff --git a/sdk/go/arvadostest/run_servers.go b/sdk/go/arvadostest/run_servers.go
index 5b01db5c4b..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
@@ -80,34 +67,17 @@ 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()
-
-	cmd := exec.Command("python", "run_test_server.py", "stop")
-	bgRun(cmd)
-	// Without Wait, "go test" in go1.10.1 tends to hang. https://github.com/golang/go/issues/24050
-	cmd.Wait()
+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,
diff --git a/sdk/go/dispatch/dispatch_test.go b/sdk/go/dispatch/dispatch_test.go
index 4b115229b4..2a9d84639e 100644
--- a/sdk/go/dispatch/dispatch_test.go
+++ b/sdk/go/dispatch/dispatch_test.go
@@ -18,14 +18,6 @@ var _ = Suite(&suite{})
 
 type suite struct{}
 
-func (s *suite) SetUpSuite(c *C) {
-	arvadostest.StartAPI()
-}
-
-func (s *suite) TearDownSuite(c *C) {
-	arvadostest.StopAPI()
-}
-
 func (s *suite) TestTrackContainer(c *C) {
 	arv, err := arvadosclient.MakeArvadosClient()
 	c.Assert(err, Equals, nil)
diff --git a/sdk/go/keepclient/keepclient_test.go b/sdk/go/keepclient/keepclient_test.go
index 62268fa463..cddf03bc37 100644
--- a/sdk/go/keepclient/keepclient_test.go
+++ b/sdk/go/keepclient/keepclient_test.go
@@ -52,13 +52,11 @@ func pythonDir() string {
 }
 
 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
-	arvadostest.StartAPI()
 	arvadostest.StartKeep(2, false)
 }
 
 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
 	arvadostest.StopKeep(2)
-	arvadostest.StopAPI()
 }
 
 func (s *ServerRequiredSuite) SetUpTest(c *C) {
diff --git a/services/arv-git-httpd/auth_handler_test.go b/services/arv-git-httpd/auth_handler_test.go
index 4e1a47dcb2..688b256dcb 100644
--- a/services/arv-git-httpd/auth_handler_test.go
+++ b/services/arv-git-httpd/auth_handler_test.go
@@ -26,14 +26,6 @@ type AuthHandlerSuite struct {
 	cluster *arvados.Cluster
 }
 
-func (s *AuthHandlerSuite) SetUpSuite(c *check.C) {
-	arvadostest.StartAPI()
-}
-
-func (s *AuthHandlerSuite) TearDownSuite(c *check.C) {
-	arvadostest.StopAPI()
-}
-
 func (s *AuthHandlerSuite) SetUpTest(c *check.C) {
 	arvadostest.ResetEnv()
 	repoRoot, err := filepath.Abs("../api/tmp/git/test")
diff --git a/services/arv-git-httpd/integration_test.go b/services/arv-git-httpd/integration_test.go
index 7da85ee747..93a46e2248 100644
--- a/services/arv-git-httpd/integration_test.go
+++ b/services/arv-git-httpd/integration_test.go
@@ -33,14 +33,6 @@ type IntegrationSuite struct {
 	cluster     *arvados.Cluster
 }
 
-func (s *IntegrationSuite) SetUpSuite(c *check.C) {
-	arvadostest.StartAPI()
-}
-
-func (s *IntegrationSuite) TearDownSuite(c *check.C) {
-	arvadostest.StopAPI()
-}
-
 func (s *IntegrationSuite) SetUpTest(c *check.C) {
 	arvadostest.ResetEnv()
 
diff --git a/services/crunch-dispatch-local/crunch-dispatch-local_test.go b/services/crunch-dispatch-local/crunch-dispatch-local_test.go
index 92b8d2adcd..7e8c42c25c 100644
--- a/services/crunch-dispatch-local/crunch-dispatch-local_test.go
+++ b/services/crunch-dispatch-local/crunch-dispatch-local_test.go
@@ -39,15 +39,10 @@ var initialArgs []string
 
 func (s *TestSuite) SetUpSuite(c *C) {
 	initialArgs = os.Args
-	arvadostest.StartAPI()
 	runningCmds = make(map[string]*exec.Cmd)
 	logrus.SetFormatter(&logrus.TextFormatter{DisableColors: true})
 }
 
-func (s *TestSuite) TearDownSuite(c *C) {
-	arvadostest.StopAPI()
-}
-
 func (s *TestSuite) SetUpTest(c *C) {
 	args := []string{"crunch-dispatch-local"}
 	os.Args = args
diff --git a/services/crunch-dispatch-slurm/crunch-dispatch-slurm_test.go b/services/crunch-dispatch-slurm/crunch-dispatch-slurm_test.go
index e7a89db23c..cf83257dad 100644
--- a/services/crunch-dispatch-slurm/crunch-dispatch-slurm_test.go
+++ b/services/crunch-dispatch-slurm/crunch-dispatch-slurm_test.go
@@ -42,7 +42,8 @@ type IntegrationSuite struct {
 }
 
 func (s *IntegrationSuite) SetUpTest(c *C) {
-	arvadostest.StartAPI()
+	arvadostest.ResetEnv()
+	arvadostest.ResetDB(c)
 	os.Setenv("ARVADOS_API_TOKEN", arvadostest.Dispatch1Token)
 	s.disp = Dispatcher{}
 	s.disp.cluster = &arvados.Cluster{}
@@ -52,7 +53,7 @@ func (s *IntegrationSuite) SetUpTest(c *C) {
 
 func (s *IntegrationSuite) TearDownTest(c *C) {
 	arvadostest.ResetEnv()
-	arvadostest.StopAPI()
+	arvadostest.ResetDB(c)
 }
 
 type slurmFake struct {
diff --git a/services/keep-balance/integration_test.go b/services/keep-balance/integration_test.go
index 52e6149158..a6cc328104 100644
--- a/services/keep-balance/integration_test.go
+++ b/services/keep-balance/integration_test.go
@@ -38,7 +38,6 @@ func (s *integrationSuite) SetUpSuite(c *check.C) {
 		c.Skip("-short")
 	}
 	arvadostest.ResetEnv()
-	arvadostest.StartAPI()
 	arvadostest.StartKeep(4, true)
 
 	arv, err := arvadosclient.MakeArvadosClient()
@@ -62,7 +61,6 @@ func (s *integrationSuite) TearDownSuite(c *check.C) {
 		c.Skip("-short")
 	}
 	arvadostest.StopKeep(4)
-	arvadostest.StopAPI()
 }
 
 func (s *integrationSuite) SetUpTest(c *check.C) {
diff --git a/services/keep-web/server_test.go b/services/keep-web/server_test.go
index a65a48892a..21d7672087 100644
--- a/services/keep-web/server_test.go
+++ b/services/keep-web/server_test.go
@@ -410,7 +410,7 @@ func (s *IntegrationSuite) TestMetrics(c *check.C) {
 }
 
 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
-	arvadostest.StartAPI()
+	arvadostest.ResetDB(c)
 	arvadostest.StartKeep(2, true)
 
 	arv, err := arvadosclient.MakeArvadosClient()
@@ -426,7 +426,6 @@ func (s *IntegrationSuite) SetUpSuite(c *check.C) {
 
 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
 	arvadostest.StopKeep(2)
-	arvadostest.StopAPI()
 }
 
 func (s *IntegrationSuite) SetUpTest(c *check.C) {
diff --git a/services/keepproxy/keepproxy_test.go b/services/keepproxy/keepproxy_test.go
index 4bdb420202..052109bf29 100644
--- a/services/keepproxy/keepproxy_test.go
+++ b/services/keepproxy/keepproxy_test.go
@@ -76,7 +76,6 @@ func closeListener() {
 }
 
 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
-	arvadostest.StartAPI()
 	arvadostest.StartKeep(2, false)
 }
 
@@ -86,11 +85,9 @@ func (s *ServerRequiredSuite) SetUpTest(c *C) {
 
 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
 	arvadostest.StopKeep(2)
-	arvadostest.StopAPI()
 }
 
 func (s *ServerRequiredConfigYmlSuite) SetUpSuite(c *C) {
-	arvadostest.StartAPI()
 	// config.yml defines 4 keepstores
 	arvadostest.StartKeep(4, false)
 }
@@ -101,11 +98,9 @@ func (s *ServerRequiredConfigYmlSuite) SetUpTest(c *C) {
 
 func (s *ServerRequiredConfigYmlSuite) TearDownSuite(c *C) {
 	arvadostest.StopKeep(4)
-	arvadostest.StopAPI()
 }
 
 func (s *NoKeepServerSuite) SetUpSuite(c *C) {
-	arvadostest.StartAPI()
 	// We need API to have some keep services listed, but the
 	// services themselves should be unresponsive.
 	arvadostest.StartKeep(2, false)
@@ -116,10 +111,6 @@ func (s *NoKeepServerSuite) SetUpTest(c *C) {
 	arvadostest.ResetEnv()
 }
 
-func (s *NoKeepServerSuite) TearDownSuite(c *C) {
-	arvadostest.StopAPI()
-}
-
 func runProxy(c *C, bogusClientToken bool, loadKeepstoresFromConfig bool, kp *arvados.UploadDownloadRolePermissions) (*keepclient.KeepClient, *bytes.Buffer) {
 	cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
 	c.Assert(err, Equals, nil)
diff --git a/services/keepstore/pull_worker_integration_test.go b/services/keepstore/pull_worker_integration_test.go
index ad7ec15e72..eb7fe5fd67 100644
--- a/services/keepstore/pull_worker_integration_test.go
+++ b/services/keepstore/pull_worker_integration_test.go
@@ -58,7 +58,6 @@ func (s *HandlerSuite) TestPullWorkerIntegration_GetNonExistingLocator(c *check.
 	}
 
 	pullRequest := s.setupPullWorkerIntegrationTest(c, testData, false)
-	defer arvadostest.StopAPI()
 	defer arvadostest.StopKeep(2)
 
 	s.performPullWorkerIntegrationTest(testData, pullRequest, c)
@@ -76,7 +75,6 @@ func (s *HandlerSuite) TestPullWorkerIntegration_GetExistingLocator(c *check.C)
 	}
 
 	pullRequest := s.setupPullWorkerIntegrationTest(c, testData, true)
-	defer arvadostest.StopAPI()
 	defer arvadostest.StopKeep(2)
 
 	s.performPullWorkerIntegrationTest(testData, pullRequest, c)
diff --git a/tools/keep-block-check/keep-block-check_test.go b/tools/keep-block-check/keep-block-check_test.go
index 9f409e6af0..e6519fb377 100644
--- a/tools/keep-block-check/keep-block-check_test.go
+++ b/tools/keep-block-check/keep-block-check_test.go
@@ -43,12 +43,7 @@ var TestHash2 = "aaaac516f788aec4f30932ffb6395c39"
 
 var blobSignatureTTL = time.Duration(2*7*24) * time.Hour
 
-func (s *ServerRequiredSuite) SetUpSuite(c *C) {
-	arvadostest.StartAPI()
-}
-
 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
-	arvadostest.StopAPI()
 	arvadostest.ResetEnv()
 }
 
diff --git a/tools/keep-rsync/keep-rsync_test.go b/tools/keep-rsync/keep-rsync_test.go
index 38968ae128..45ed3f67f5 100644
--- a/tools/keep-rsync/keep-rsync_test.go
+++ b/tools/keep-rsync/keep-rsync_test.go
@@ -43,12 +43,7 @@ var _ = Suite(&DoMainTestSuite{})
 
 type ServerRequiredSuite struct{}
 
-func (s *ServerRequiredSuite) SetUpSuite(c *C) {
-	arvadostest.StartAPI()
-}
-
 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
-	arvadostest.StopAPI()
 	arvadostest.ResetEnv()
 }
 
-- 
2.30.2