Merge branch '8784-dir-listings'
[arvados.git] / sdk / go / arvadostest / run_servers.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package arvadostest
6
7 import (
8         "bufio"
9         "bytes"
10         "fmt"
11         "io/ioutil"
12         "log"
13         "os"
14         "os/exec"
15         "path"
16         "strconv"
17         "strings"
18 )
19
20 var authSettings = make(map[string]string)
21
22 // ResetEnv resets test env
23 func ResetEnv() {
24         for k, v := range authSettings {
25                 os.Setenv(k, v)
26         }
27 }
28
29 // APIHost returns the address:port of the current test server.
30 func APIHost() string {
31         h := authSettings["ARVADOS_API_HOST"]
32         if h == "" {
33                 log.Fatal("arvadostest.APIHost() was called but authSettings is not populated")
34         }
35         return h
36 }
37
38 // ParseAuthSettings parses auth settings from given input
39 func ParseAuthSettings(authScript []byte) {
40         scanner := bufio.NewScanner(bytes.NewReader(authScript))
41         for scanner.Scan() {
42                 line := scanner.Text()
43                 if 0 != strings.Index(line, "export ") {
44                         log.Printf("Ignoring: %v", line)
45                         continue
46                 }
47                 toks := strings.SplitN(strings.Replace(line, "export ", "", 1), "=", 2)
48                 if len(toks) == 2 {
49                         authSettings[toks[0]] = toks[1]
50                 } else {
51                         log.Fatalf("Could not parse: %v", line)
52                 }
53         }
54         log.Printf("authSettings: %v", authSettings)
55 }
56
57 var pythonTestDir string
58
59 func chdirToPythonTests() {
60         if pythonTestDir != "" {
61                 if err := os.Chdir(pythonTestDir); err != nil {
62                         log.Fatalf("chdir %s: %s", pythonTestDir, err)
63                 }
64                 return
65         }
66         for {
67                 if err := os.Chdir("sdk/python/tests"); err == nil {
68                         pythonTestDir, err = os.Getwd()
69                         return
70                 }
71                 if parent, err := os.Getwd(); err != nil || parent == "/" {
72                         log.Fatalf("sdk/python/tests/ not found in any ancestor")
73                 }
74                 if err := os.Chdir(".."); err != nil {
75                         log.Fatal(err)
76                 }
77         }
78 }
79
80 // StartAPI starts test API server
81 func StartAPI() {
82         cwd, _ := os.Getwd()
83         defer os.Chdir(cwd)
84         chdirToPythonTests()
85
86         cmd := exec.Command("python", "run_test_server.py", "start", "--auth", "admin")
87         cmd.Stdin = nil
88         cmd.Stderr = os.Stderr
89
90         authScript, err := cmd.Output()
91         if err != nil {
92                 log.Fatalf("%+v: %s", cmd.Args, err)
93         }
94         ParseAuthSettings(authScript)
95         ResetEnv()
96 }
97
98 // StopAPI stops test API server
99 func StopAPI() {
100         cwd, _ := os.Getwd()
101         defer os.Chdir(cwd)
102         chdirToPythonTests()
103
104         bgRun(exec.Command("python", "run_test_server.py", "stop"))
105 }
106
107 // StartKeep starts the given number of keep servers,
108 // optionally with -enforce-permissions enabled.
109 // Use numKeepServers = 2 and enforcePermissions = false under all normal circumstances.
110 func StartKeep(numKeepServers int, enforcePermissions bool) {
111         cwd, _ := os.Getwd()
112         defer os.Chdir(cwd)
113         chdirToPythonTests()
114
115         cmdArgs := []string{"run_test_server.py", "start_keep", "--num-keep-servers", strconv.Itoa(numKeepServers)}
116         if enforcePermissions {
117                 cmdArgs = append(cmdArgs, "--keep-enforce-permissions")
118         }
119
120         bgRun(exec.Command("python", cmdArgs...))
121 }
122
123 // StopKeep stops keep servers that were started with StartKeep.
124 // numkeepServers should be the same value that was passed to StartKeep,
125 // which is 2 under all normal circumstances.
126 func StopKeep(numKeepServers int) {
127         cwd, _ := os.Getwd()
128         defer os.Chdir(cwd)
129         chdirToPythonTests()
130
131         cmd := exec.Command("python", "run_test_server.py", "stop_keep", "--num-keep-servers", strconv.Itoa(numKeepServers))
132         cmd.Stdin = nil
133         cmd.Stderr = os.Stderr
134         cmd.Stdout = os.Stderr
135         if err := cmd.Run(); err != nil {
136                 log.Fatalf("%+v: %s", cmd.Args, err)
137         }
138 }
139
140 // Start cmd, with stderr and stdout redirected to our own
141 // stderr. Return when the process exits, but do not wait for its
142 // stderr and stdout to close: any grandchild processes will continue
143 // writing to our stderr.
144 func bgRun(cmd *exec.Cmd) {
145         cmd.Stdin = nil
146         cmd.Stderr = os.Stderr
147         cmd.Stdout = os.Stderr
148         if err := cmd.Start(); err != nil {
149                 log.Fatalf("%+v: %s", cmd.Args, err)
150         }
151         if _, err := cmd.Process.Wait(); err != nil {
152                 log.Fatalf("%+v: %s", cmd.Args, err)
153         }
154 }
155
156 // CreateBadPath creates a tmp dir, appends given string and returns that path
157 // This will guarantee that the path being returned does not exist
158 func CreateBadPath() (badpath string, err error) {
159         tempdir, err := ioutil.TempDir("", "bad")
160         if err != nil {
161                 return "", fmt.Errorf("Could not create temporary directory for bad path: %v", err)
162         }
163         badpath = path.Join(tempdir, "bad")
164         return badpath, nil
165 }
166
167 // DestroyBadPath deletes the tmp dir created by the previous CreateBadPath call
168 func DestroyBadPath(badpath string) error {
169         tempdir := path.Join(badpath, "..")
170         err := os.Remove(tempdir)
171         if err != nil {
172                 return fmt.Errorf("Could not remove bad path temporary directory %v: %v", tempdir, err)
173         }
174         return nil
175 }