1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
20 var authSettings = make(map[string]string)
22 // ResetEnv resets test env
24 for k, v := range authSettings {
29 // APIHost returns the address:port of the current test server.
30 func APIHost() string {
31 h := authSettings["ARVADOS_API_HOST"]
33 log.Fatal("arvadostest.APIHost() was called but authSettings is not populated")
38 // ParseAuthSettings parses auth settings from given input
39 func ParseAuthSettings(authScript []byte) {
40 scanner := bufio.NewScanner(bytes.NewReader(authScript))
42 line := scanner.Text()
43 if 0 != strings.Index(line, "export ") {
44 log.Printf("Ignoring: %v", line)
47 toks := strings.SplitN(strings.Replace(line, "export ", "", 1), "=", 2)
49 authSettings[toks[0]] = toks[1]
51 log.Fatalf("Could not parse: %v", line)
54 log.Printf("authSettings: %v", authSettings)
57 var pythonTestDir string
59 func chdirToPythonTests() {
60 if pythonTestDir != "" {
61 if err := os.Chdir(pythonTestDir); err != nil {
62 log.Fatalf("chdir %s: %s", pythonTestDir, err)
67 if err := os.Chdir("sdk/python/tests"); err == nil {
68 pythonTestDir, err = os.Getwd()
71 if parent, err := os.Getwd(); err != nil || parent == "/" {
72 log.Fatalf("sdk/python/tests/ not found in any ancestor")
74 if err := os.Chdir(".."); err != nil {
80 // StartAPI starts test API server
86 cmd := exec.Command("python", "run_test_server.py", "start", "--auth", "admin")
88 cmd.Stderr = os.Stderr
90 authScript, err := cmd.Output()
92 log.Fatalf("%+v: %s", cmd.Args, err)
94 ParseAuthSettings(authScript)
98 // StopAPI stops test API server
104 bgRun(exec.Command("python", "run_test_server.py", "stop"))
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) {
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")
120 bgRun(exec.Command("python", cmdArgs...))
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) {
131 cmd := exec.Command("python", "run_test_server.py", "stop_keep", "--num-keep-servers", strconv.Itoa(numKeepServers))
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)
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) {
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)
151 if _, err := cmd.Process.Wait(); err != nil {
152 log.Fatalf("%+v: %s", cmd.Args, err)
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")
161 return "", fmt.Errorf("Could not create temporary directory for bad path: %v", err)
163 badpath = path.Join(tempdir, "bad")
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)
172 return fmt.Errorf("Could not remove bad path temporary directory %v: %v", tempdir, err)