5824: Document ClientPool.
[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
18 // Tests that require the Keep server running
19 type ServerRequiredSuite struct{}
20
21 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
22         arvadostest.StartAPI()
23         arvadostest.StartKeep()
24 }
25
26 func (s *ServerRequiredSuite) SetUpTest(c *C) {
27         arvadostest.ResetEnv()
28 }
29
30 func (s *ServerRequiredSuite) TestMakeArvadosClientSecure(c *C) {
31         os.Setenv("ARVADOS_API_HOST_INSECURE", "")
32         kc, err := MakeArvadosClient()
33         c.Assert(err, Equals, nil)
34         c.Check(kc.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST"))
35         c.Check(kc.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN"))
36         c.Check(kc.ApiInsecure, Equals, false)
37 }
38
39 func (s *ServerRequiredSuite) TestMakeArvadosClientInsecure(c *C) {
40         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
41         kc, err := MakeArvadosClient()
42         c.Assert(err, Equals, nil)
43         c.Check(kc.ApiInsecure, Equals, true)
44         c.Check(kc.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST"))
45         c.Check(kc.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN"))
46         c.Check(kc.Client.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify, Equals, true)
47 }
48
49 func (s *ServerRequiredSuite) TestGetEmptyUUID(c *C) {
50         arv, err := MakeArvadosClient()
51
52         getback := make(Dict)
53         err = arv.Get("collections", "", nil, &getback)
54         c.Assert(err, FitsTypeOf, APIServerError{})
55         c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
56         c.Assert(len(getback), Equals, 0)
57 }
58
59 func (s *ServerRequiredSuite) TestCreatePipelineTemplate(c *C) {
60         arv, err := MakeArvadosClient()
61
62         getback := make(Dict)
63         err = arv.Create("pipeline_templates",
64                 Dict{"pipeline_template": Dict{
65                         "name": "tmp",
66                         "components": Dict{
67                                 "c1": map[string]string{"script": "script1"},
68                                 "c2": map[string]string{"script": "script2"}}}},
69                 &getback)
70         c.Assert(err, Equals, nil)
71         c.Assert(getback["name"], Equals, "tmp")
72         c.Assert(getback["components"].(map[string]interface{})["c2"].(map[string]interface{})["script"], Equals, "script2")
73
74         uuid := getback["uuid"].(string)
75
76         getback = make(Dict)
77         err = arv.Get("pipeline_templates", uuid, nil, &getback)
78         c.Assert(err, Equals, nil)
79         c.Assert(getback["name"], Equals, "tmp")
80         c.Assert(getback["components"].(map[string]interface{})["c1"].(map[string]interface{})["script"], Equals, "script1")
81
82         getback = make(Dict)
83         err = arv.Update("pipeline_templates", uuid,
84                 Dict{
85                         "pipeline_template": Dict{"name": "tmp2"}},
86                 &getback)
87         c.Assert(err, Equals, nil)
88         c.Assert(getback["name"], Equals, "tmp2")
89
90         c.Assert(getback["uuid"].(string), Equals, uuid)
91         getback = make(Dict)
92         err = arv.Delete("pipeline_templates", uuid, nil, &getback)
93         c.Assert(err, Equals, nil)
94         c.Assert(getback["name"], Equals, "tmp2")
95 }
96
97 func (s *ServerRequiredSuite) TestErrorResponse(c *C) {
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, "arvados API server error: .*")
107                 c.Assert(err, ErrorMatches, ".*unknown attribute: bogus_attr.*")
108                 c.Assert(err, FitsTypeOf, APIServerError{})
109                 c.Assert(err.(APIServerError).HttpStatusCode, Equals, 422)
110         }
111
112         {
113                 err := arv.Create("bogus",
114                         Dict{"bogus": Dict{}},
115                         &getback)
116                 c.Assert(err, ErrorMatches, "arvados API server error: .*")
117                 c.Assert(err, ErrorMatches, ".*Path not found.*")
118                 c.Assert(err, FitsTypeOf, APIServerError{})
119                 c.Assert(err.(APIServerError).HttpStatusCode, Equals, 404)
120         }
121 }
122
123 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_defaultCollectionReplication(c *C) {
124         arv, err := MakeArvadosClient()
125         value, err := arv.Discovery("defaultCollectionReplication")
126         c.Assert(err, IsNil)
127         c.Assert(value, NotNil)
128 }
129
130 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_noSuchParameter(c *C) {
131         arv, err := MakeArvadosClient()
132         value, err := arv.Discovery("noSuchParameter")
133         c.Assert(err, NotNil)
134         c.Assert(value, IsNil)
135 }