closes #6203
[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) TestCreatePipelineTemplate(c *C) {
50         arv, err := MakeArvadosClient()
51
52         getback := make(Dict)
53         err = arv.Create("pipeline_templates",
54                 Dict{"pipeline_template": Dict{
55                         "name": "tmp",
56                         "components": Dict{
57                                 "c1": map[string]string{"script": "script1"},
58                                 "c2": map[string]string{"script": "script2"}}}},
59                 &getback)
60         c.Assert(err, Equals, nil)
61         c.Assert(getback["name"], Equals, "tmp")
62         c.Assert(getback["components"].(map[string]interface{})["c2"].(map[string]interface{})["script"], Equals, "script2")
63
64         uuid := getback["uuid"].(string)
65         getback = make(Dict)
66         err = arv.Update("pipeline_templates", uuid,
67                 Dict{
68                         "pipeline_template": Dict{"name": "tmp2"}},
69                 &getback)
70         c.Assert(err, Equals, nil)
71         c.Assert(getback["name"], Equals, "tmp2")
72
73         c.Assert(getback["uuid"].(string), Equals, uuid)
74         getback = make(Dict)
75         err = arv.Delete("pipeline_templates", uuid, nil, &getback)
76         c.Assert(err, Equals, nil)
77         c.Assert(getback["name"], Equals, "tmp2")
78 }
79
80 func (s *ServerRequiredSuite) TestErrorResponse(c *C) {
81         arv, _ := MakeArvadosClient()
82
83         getback := make(Dict)
84
85         {
86                 err := arv.Create("logs",
87                         Dict{"log": Dict{"bogus_attr": "foo"}},
88                         &getback)
89                 c.Assert(err, ErrorMatches, "arvados API server error: .*")
90                 c.Assert(err, ErrorMatches, ".*unknown attribute: bogus_attr.*")
91                 c.Assert(err, FitsTypeOf, APIServerError{})
92                 c.Assert(err.(APIServerError).HttpStatusCode, Equals, 422)
93         }
94
95         {
96                 err := arv.Create("bogus",
97                         Dict{"bogus": Dict{}},
98                         &getback)
99                 c.Assert(err, ErrorMatches, "arvados API server error: .*")
100                 c.Assert(err, ErrorMatches, ".*Path not found.*")
101                 c.Assert(err, FitsTypeOf, APIServerError{})
102                 c.Assert(err.(APIServerError).HttpStatusCode, Equals, 404)
103         }
104 }
105
106 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_defaultCollectionReplication(c *C) {
107         arv, err := MakeArvadosClient()
108         value, err := arv.Discovery("defaultCollectionReplication")
109         c.Assert(err, IsNil)
110         c.Assert(value, NotNil)
111 }
112
113 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_noSuchParameter(c *C) {
114         arv, err := MakeArvadosClient()
115         value, err := arv.Discovery("noSuchParameter")
116         c.Assert(err, NotNil)
117         c.Assert(value, IsNil)
118 }