Merge branch 'master' of git.curoverse.com:arvados into 3408-production-datamanager
[arvados.git] / sdk / go / arvadosclient / arvadosclient_test.go
1 package arvadosclient
2
3 import (
4         "fmt"
5         . "gopkg.in/check.v1"
6         "net/http"
7         "os"
8         "os/exec"
9         "testing"
10 )
11
12 // Gocheck boilerplate
13 func Test(t *testing.T) {
14         TestingT(t)
15 }
16
17 var _ = Suite(&ServerRequiredSuite{})
18
19 // Tests that require the Keep server running
20 type ServerRequiredSuite struct{}
21
22 func pythonDir() string {
23         cwd, _ := os.Getwd()
24         return fmt.Sprintf("%s/../../python/tests", cwd)
25 }
26
27 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
28         os.Chdir(pythonDir())
29         if err := exec.Command("python", "run_test_server.py", "start").Run(); err != nil {
30                 panic("'python run_test_server.py start' returned error")
31         }
32         if err := exec.Command("python", "run_test_server.py", "start_keep").Run(); err != nil {
33                 panic("'python run_test_server.py start_keep' returned error")
34         }
35 }
36
37 func (s *ServerRequiredSuite) TestMakeArvadosClient(c *C) {
38         os.Setenv("ARVADOS_API_HOST", "localhost:3000")
39         os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
40         os.Setenv("ARVADOS_API_HOST_INSECURE", "")
41
42         kc, err := MakeArvadosClient()
43         c.Check(kc.ApiServer, Equals, "localhost:3000")
44         c.Check(kc.ApiToken, Equals, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
45         c.Check(kc.ApiInsecure, Equals, false)
46
47         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
48
49         kc, err = MakeArvadosClient()
50         c.Check(kc.ApiServer, Equals, "localhost:3000")
51         c.Check(kc.ApiToken, Equals, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
52         c.Check(kc.ApiInsecure, Equals, true)
53         c.Check(kc.Client.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify, Equals, true)
54
55         c.Assert(err, Equals, nil)
56 }
57
58 func (s *ServerRequiredSuite) TestCreatePipelineTemplate(c *C) {
59         os.Setenv("ARVADOS_API_HOST", "localhost:3000")
60         os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
61         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
62
63         arv, err := MakeArvadosClient()
64
65         getback := make(Dict)
66         err = arv.Create("pipeline_templates",
67                 Dict{"pipeline_template": Dict{
68                         "name": "tmp",
69                         "components": Dict{
70                                 "c1": map[string]string{"script": "script1"},
71                                 "c2": map[string]string{"script": "script2"}}}},
72                 &getback)
73         c.Assert(err, Equals, nil)
74         c.Assert(getback["name"], Equals, "tmp")
75         c.Assert(getback["components"].(map[string]interface{})["c2"].(map[string]interface{})["script"], Equals, "script2")
76
77         uuid := getback["uuid"].(string)
78         getback = make(Dict)
79         err = arv.Update("pipeline_templates", uuid,
80                 Dict{
81                         "pipeline_template": Dict{"name": "tmp2"}},
82                 &getback)
83         c.Assert(err, Equals, nil)
84         c.Assert(getback["name"], Equals, "tmp2")
85
86         c.Assert(getback["uuid"].(string), Equals, uuid)
87         getback = make(Dict)
88         err = arv.Delete("pipeline_templates", uuid, nil, &getback)
89         c.Assert(err, Equals, nil)
90         c.Assert(getback["name"], Equals, "tmp2")
91 }
92
93 func (s *ServerRequiredSuite) TestErrorResponse(c *C) {
94         os.Setenv("ARVADOS_API_HOST", "localhost:3000")
95         os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
96         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
97
98         arv, _ := MakeArvadosClient()
99
100         getback := make(Dict)
101
102         {
103                 err := arv.Create("logs",
104                         Dict{"log": Dict{"bogus_attr": "foo"}},
105                         &getback)
106                 c.Assert(err, ErrorMatches, ".*unknown attribute: bogus_attr.*")
107                 c.Assert(err, FitsTypeOf, ArvadosApiError{})
108                 c.Assert(err.(ArvadosApiError).HttpStatusCode, Equals, 422)
109         }
110
111         {
112                 err := arv.Create("bogus",
113                         Dict{"bogus": Dict{}},
114                         &getback)
115                 c.Assert(err, ErrorMatches, "Path not found")
116                 c.Assert(err, FitsTypeOf, ArvadosApiError{})
117                 c.Assert(err.(ArvadosApiError).HttpStatusCode, Equals, 404)
118         }
119 }