Merge branch 'master' into 7167-keep-rsync-test-setup
[arvados.git] / sdk / go / arvadosclient / arvadosclient_test.go
1 package arvadosclient
2
3 import (
4         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
5         . "gopkg.in/check.v1"
6         "net/http"
7         "os"
8         "testing"
9 )
10
11 // Gocheck boilerplate
12 func Test(t *testing.T) {
13         TestingT(t)
14 }
15
16 var _ = Suite(&ServerRequiredSuite{})
17 var _ = Suite(&UnitSuite{})
18
19 // Tests that require the Keep server running
20 type ServerRequiredSuite struct{}
21
22 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
23         arvadostest.StartAPI()
24         arvadostest.StartKeep()
25 }
26
27 func (s *ServerRequiredSuite) SetUpTest(c *C) {
28         arvadostest.ResetEnv()
29 }
30
31 func (s *ServerRequiredSuite) TestMakeArvadosClientSecure(c *C) {
32         os.Setenv("ARVADOS_API_HOST_INSECURE", "")
33         kc, err := MakeArvadosClient()
34         c.Assert(err, Equals, nil)
35         c.Check(kc.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST"))
36         c.Check(kc.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN"))
37         c.Check(kc.ApiInsecure, Equals, false)
38 }
39
40 func (s *ServerRequiredSuite) TestMakeArvadosClientInsecure(c *C) {
41         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
42         kc, err := MakeArvadosClient()
43         c.Assert(err, Equals, nil)
44         c.Check(kc.ApiInsecure, Equals, true)
45         c.Check(kc.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST"))
46         c.Check(kc.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN"))
47         c.Check(kc.Client.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify, Equals, true)
48 }
49
50 func (s *ServerRequiredSuite) TestGetInvalidUUID(c *C) {
51         arv, err := MakeArvadosClient()
52
53         getback := make(Dict)
54         err = arv.Get("collections", "", nil, &getback)
55         c.Assert(err, Equals, ErrInvalidArgument)
56         c.Assert(len(getback), Equals, 0)
57
58         err = arv.Get("collections", "zebra-moose-unicorn", nil, &getback)
59         c.Assert(err, Equals, ErrInvalidArgument)
60         c.Assert(len(getback), Equals, 0)
61
62         err = arv.Get("collections", "acbd18db4cc2f85cedef654fccc4a4d8", nil, &getback)
63         c.Assert(err, Equals, ErrInvalidArgument)
64         c.Assert(len(getback), Equals, 0)
65 }
66
67 func (s *ServerRequiredSuite) TestGetValidUUID(c *C) {
68         arv, err := MakeArvadosClient()
69
70         getback := make(Dict)
71         err = arv.Get("collections", "zzzzz-4zz18-abcdeabcdeabcde", nil, &getback)
72         c.Assert(err, FitsTypeOf, APIServerError{})
73         c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
74         c.Assert(len(getback), Equals, 0)
75
76         err = arv.Get("collections", "acbd18db4cc2f85cedef654fccc4a4d8+3", nil, &getback)
77         c.Assert(err, FitsTypeOf, APIServerError{})
78         c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
79         c.Assert(len(getback), Equals, 0)
80 }
81
82 func (s *ServerRequiredSuite) TestInvalidResourceType(c *C) {
83         arv, err := MakeArvadosClient()
84
85         getback := make(Dict)
86         err = arv.Get("unicorns", "zzzzz-zebra-unicorn7unicorn", nil, &getback)
87         c.Assert(err, FitsTypeOf, APIServerError{})
88         c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
89         c.Assert(len(getback), Equals, 0)
90
91         err = arv.Update("unicorns", "zzzzz-zebra-unicorn7unicorn", nil, &getback)
92         c.Assert(err, FitsTypeOf, APIServerError{})
93         c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
94         c.Assert(len(getback), Equals, 0)
95
96         err = arv.List("unicorns", nil, &getback)
97         c.Assert(err, FitsTypeOf, APIServerError{})
98         c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
99         c.Assert(len(getback), Equals, 0)
100 }
101
102 func (s *ServerRequiredSuite) TestCreatePipelineTemplate(c *C) {
103         arv, err := MakeArvadosClient()
104
105         getback := make(Dict)
106         err = arv.Create("pipeline_templates",
107                 Dict{"pipeline_template": Dict{
108                         "name": "tmp",
109                         "components": Dict{
110                                 "c1": map[string]string{"script": "script1"},
111                                 "c2": map[string]string{"script": "script2"}}}},
112                 &getback)
113         c.Assert(err, Equals, nil)
114         c.Assert(getback["name"], Equals, "tmp")
115         c.Assert(getback["components"].(map[string]interface{})["c2"].(map[string]interface{})["script"], Equals, "script2")
116
117         uuid := getback["uuid"].(string)
118
119         getback = make(Dict)
120         err = arv.Get("pipeline_templates", uuid, nil, &getback)
121         c.Assert(err, Equals, nil)
122         c.Assert(getback["name"], Equals, "tmp")
123         c.Assert(getback["components"].(map[string]interface{})["c1"].(map[string]interface{})["script"], Equals, "script1")
124
125         getback = make(Dict)
126         err = arv.Update("pipeline_templates", uuid,
127                 Dict{
128                         "pipeline_template": Dict{"name": "tmp2"}},
129                 &getback)
130         c.Assert(err, Equals, nil)
131         c.Assert(getback["name"], Equals, "tmp2")
132
133         c.Assert(getback["uuid"].(string), Equals, uuid)
134         getback = make(Dict)
135         err = arv.Delete("pipeline_templates", uuid, nil, &getback)
136         c.Assert(err, Equals, nil)
137         c.Assert(getback["name"], Equals, "tmp2")
138 }
139
140 func (s *ServerRequiredSuite) TestErrorResponse(c *C) {
141         arv, _ := MakeArvadosClient()
142
143         getback := make(Dict)
144
145         {
146                 err := arv.Create("logs",
147                         Dict{"log": Dict{"bogus_attr": "foo"}},
148                         &getback)
149                 c.Assert(err, ErrorMatches, "arvados API server error: .*")
150                 c.Assert(err, ErrorMatches, ".*unknown attribute: bogus_attr.*")
151                 c.Assert(err, FitsTypeOf, APIServerError{})
152                 c.Assert(err.(APIServerError).HttpStatusCode, Equals, 422)
153         }
154
155         {
156                 err := arv.Create("bogus",
157                         Dict{"bogus": Dict{}},
158                         &getback)
159                 c.Assert(err, ErrorMatches, "arvados API server error: .*")
160                 c.Assert(err, ErrorMatches, ".*Path not found.*")
161                 c.Assert(err, FitsTypeOf, APIServerError{})
162                 c.Assert(err.(APIServerError).HttpStatusCode, Equals, 404)
163         }
164 }
165
166 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_defaultCollectionReplication(c *C) {
167         arv, err := MakeArvadosClient()
168         value, err := arv.Discovery("defaultCollectionReplication")
169         c.Assert(err, IsNil)
170         c.Assert(value, NotNil)
171 }
172
173 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_noSuchParameter(c *C) {
174         arv, err := MakeArvadosClient()
175         value, err := arv.Discovery("noSuchParameter")
176         c.Assert(err, NotNil)
177         c.Assert(value, IsNil)
178 }
179
180 type UnitSuite struct{}
181
182 func (s *UnitSuite) TestUUIDMatch(c *C) {
183         c.Assert(UUIDMatch("zzzzz-tpzed-000000000000000"), Equals, true)
184         c.Assert(UUIDMatch("zzzzz-zebra-000000000000000"), Equals, true)
185         c.Assert(UUIDMatch("00000-00000-zzzzzzzzzzzzzzz"), Equals, true)
186         c.Assert(UUIDMatch("ZEBRA-HORSE-AFRICANELEPHANT"), Equals, false)
187         c.Assert(UUIDMatch(" zzzzz-tpzed-000000000000000"), Equals, false)
188         c.Assert(UUIDMatch("d41d8cd98f00b204e9800998ecf8427e"), Equals, false)
189         c.Assert(UUIDMatch("d41d8cd98f00b204e9800998ecf8427e+0"), Equals, false)
190         c.Assert(UUIDMatch(""), Equals, false)
191 }
192
193 func (s *UnitSuite) TestPDHMatch(c *C) {
194         c.Assert(PDHMatch("zzzzz-tpzed-000000000000000"), Equals, false)
195         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e"), Equals, false)
196         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+0"), Equals, true)
197         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345"), Equals, true)
198         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e 12345"), Equals, false)
199         c.Assert(PDHMatch("D41D8CD98F00B204E9800998ECF8427E+12345"), Equals, false)
200         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345 "), Equals, false)
201         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+abcdef"), Equals, false)
202         c.Assert(PDHMatch("da39a3ee5e6b4b0d3255bfef95601890afd80709"), Equals, false)
203         c.Assert(PDHMatch("da39a3ee5e6b4b0d3255bfef95601890afd80709+0"), Equals, false)
204         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427+12345"), Equals, false)
205         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345\n"), Equals, false)
206         c.Assert(PDHMatch("+12345"), Equals, false)
207         c.Assert(PDHMatch(""), Equals, false)
208 }