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