1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
22 var authSettings = make(map[string]string)
24 // ResetEnv resets ARVADOS_* env vars to whatever they were the first
25 // time this func was called.
27 // Call it from your SetUpTest or SetUpSuite func if your tests modify
30 if len(authSettings) == 0 {
31 for _, e := range os.Environ() {
32 e := strings.SplitN(e, "=", 2)
34 authSettings[e[0]] = e[1]
38 for k, v := range authSettings {
44 var pythonTestDir string
46 func chdirToPythonTests() {
47 if pythonTestDir != "" {
48 if err := os.Chdir(pythonTestDir); err != nil {
49 log.Fatalf("chdir %s: %s", pythonTestDir, err)
54 if err := os.Chdir("sdk/python/tests"); err == nil {
55 pythonTestDir, err = os.Getwd()
61 if parent, err := os.Getwd(); err != nil || parent == "/" {
62 log.Fatalf("sdk/python/tests/ not found in any ancestor")
64 if err := os.Chdir(".."); err != nil {
70 func ResetDB(c *check.C) {
71 hc := http.Client{Transport: &http.Transport{
72 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
74 req, err := http.NewRequest("POST", "https://"+os.Getenv("ARVADOS_TEST_API_HOST")+"/database/reset", nil)
75 c.Assert(err, check.IsNil)
76 req.Header.Set("Authorization", "Bearer "+AdminToken)
77 resp, err := hc.Do(req)
78 c.Assert(err, check.IsNil)
79 defer resp.Body.Close()
80 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
83 // StartKeep starts the given number of keep servers,
84 // optionally with --keep-blob-signing enabled.
85 // Use numKeepServers = 2 and blobSigning = false under all normal circumstances.
86 func StartKeep(numKeepServers int, blobSigning bool) {
91 cmdArgs := []string{"run_test_server.py", "start_keep", "--num-keep-servers", strconv.Itoa(numKeepServers)}
93 cmdArgs = append(cmdArgs, "--keep-blob-signing")
96 bgRun(exec.Command("python", cmdArgs...))
99 // StopKeep stops keep servers that were started with StartKeep.
100 // numkeepServers should be the same value that was passed to StartKeep,
101 // which is 2 under all normal circumstances.
102 func StopKeep(numKeepServers int) {
107 cmd := exec.Command("python", "run_test_server.py", "stop_keep", "--num-keep-servers", strconv.Itoa(numKeepServers))
109 // Without Wait, "go test" in go1.10.1 tends to hang. https://github.com/golang/go/issues/24050
113 // Start cmd, with stderr and stdout redirected to our own
114 // stderr. Return when the process exits, but do not wait for its
115 // stderr and stdout to close: any grandchild processes will continue
116 // writing to our stderr.
117 func bgRun(cmd *exec.Cmd) {
119 cmd.Stderr = os.Stderr
120 cmd.Stdout = os.Stderr
121 if err := cmd.Start(); err != nil {
122 log.Fatalf("%+v: %s", cmd.Args, err)
124 if _, err := cmd.Process.Wait(); err != nil {
125 log.Fatalf("%+v: %s", cmd.Args, err)
129 // CreateBadPath creates a tmp dir, appends given string and returns that path
130 // This will guarantee that the path being returned does not exist
131 func CreateBadPath() (badpath string, err error) {
132 tempdir, err := ioutil.TempDir("", "bad")
134 return "", fmt.Errorf("Could not create temporary directory for bad path: %v", err)
136 badpath = path.Join(tempdir, "bad")
140 // DestroyBadPath deletes the tmp dir created by the previous CreateBadPath call
141 func DestroyBadPath(badpath string) error {
142 tempdir := path.Join(badpath, "..")
143 err := os.Remove(tempdir)
145 return fmt.Errorf("Could not remove bad path temporary directory %v: %v", tempdir, err)