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()
74 if parent, err := os.Getwd(); err != nil || parent == "/" {
75 log.Fatalf("sdk/python/tests/ not found in any ancestor")
77 if err := os.Chdir(".."); err != nil {
83 // StartAPI starts test API server
89 cmd := exec.Command("python", "run_test_server.py", "start", "--auth", "admin")
91 cmd.Stderr = os.Stderr
93 authScript, err := cmd.Output()
95 log.Fatalf("%+v: %s", cmd.Args, err)
97 ParseAuthSettings(authScript)
101 // StopAPI stops test API server
107 cmd := exec.Command("python", "run_test_server.py", "stop")
109 // Without Wait, "go test" in go1.10.1 tends to hang. https://github.com/golang/go/issues/24050
113 // StartKeep starts the given number of keep servers,
114 // optionally with --keep-blob-signing enabled.
115 // Use numKeepServers = 2 and blobSigning = false under all normal circumstances.
116 func StartKeep(numKeepServers int, blobSigning bool) {
121 cmdArgs := []string{"run_test_server.py", "start_keep", "--num-keep-servers", strconv.Itoa(numKeepServers)}
123 cmdArgs = append(cmdArgs, "--keep-blob-signing")
126 bgRun(exec.Command("python", cmdArgs...))
129 // StopKeep stops keep servers that were started with StartKeep.
130 // numkeepServers should be the same value that was passed to StartKeep,
131 // which is 2 under all normal circumstances.
132 func StopKeep(numKeepServers int) {
137 cmd := exec.Command("python", "run_test_server.py", "stop_keep", "--num-keep-servers", strconv.Itoa(numKeepServers))
139 // Without Wait, "go test" in go1.10.1 tends to hang. https://github.com/golang/go/issues/24050
143 // Start cmd, with stderr and stdout redirected to our own
144 // stderr. Return when the process exits, but do not wait for its
145 // stderr and stdout to close: any grandchild processes will continue
146 // writing to our stderr.
147 func bgRun(cmd *exec.Cmd) {
149 cmd.Stderr = os.Stderr
150 cmd.Stdout = os.Stderr
151 if err := cmd.Start(); err != nil {
152 log.Fatalf("%+v: %s", cmd.Args, err)
154 if _, err := cmd.Process.Wait(); err != nil {
155 log.Fatalf("%+v: %s", cmd.Args, err)
159 // CreateBadPath creates a tmp dir, appends given string and returns that path
160 // This will guarantee that the path being returned does not exist
161 func CreateBadPath() (badpath string, err error) {
162 tempdir, err := ioutil.TempDir("", "bad")
164 return "", fmt.Errorf("Could not create temporary directory for bad path: %v", err)
166 badpath = path.Join(tempdir, "bad")
170 // DestroyBadPath deletes the tmp dir created by the previous CreateBadPath call
171 func DestroyBadPath(badpath string) error {
172 tempdir := path.Join(badpath, "..")
173 err := os.Remove(tempdir)
175 return fmt.Errorf("Could not remove bad path temporary directory %v: %v", tempdir, err)