1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
18 check "gopkg.in/check.v1"
21 type stubTransport struct {
22 Responses map[string]string
23 Requests []http.Request
27 func (stub *stubTransport) RoundTrip(req *http.Request) (*http.Response, error) {
29 stub.Requests = append(stub.Requests, *req)
32 resp := &http.Response{
40 str := stub.Responses[req.URL.Path]
42 resp.Status = "404 Not Found"
46 buf := bytes.NewBufferString(str)
47 resp.Body = ioutil.NopCloser(buf)
48 resp.ContentLength = int64(buf.Len())
52 type errorTransport struct{}
54 func (stub *errorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
55 return nil, fmt.Errorf("something awful happened")
58 type timeoutTransport struct {
62 func (stub *timeoutTransport) RoundTrip(req *http.Request) (*http.Response, error) {
63 return &http.Response{
70 Body: ioutil.NopCloser(iotest.TimeoutReader(bytes.NewReader(stub.response))),
74 var _ = check.Suite(&clientSuite{})
76 type clientSuite struct{}
78 func (*clientSuite) TestCurrentUser(c *check.C) {
79 stub := &stubTransport{
80 Responses: map[string]string{
81 "/arvados/v1/users/current": `{"uuid":"zzzzz-abcde-012340123401234"}`,
88 APIHost: "zzzzz.arvadosapi.com",
91 u, err := client.CurrentUser()
92 c.Check(err, check.IsNil)
93 c.Check(u.UUID, check.Equals, "zzzzz-abcde-012340123401234")
94 c.Check(stub.Requests, check.Not(check.HasLen), 0)
95 hdr := stub.Requests[len(stub.Requests)-1].Header
96 c.Check(hdr.Get("Authorization"), check.Equals, "OAuth2 xyzzy")
98 client.Client.Transport = &errorTransport{}
99 u, err = client.CurrentUser()
100 c.Check(err, check.NotNil)
103 func (*clientSuite) TestAnythingToValues(c *check.C) {
104 type testCase struct {
106 // ok==nil means anythingToValues should return an
107 // error, otherwise it's a func that returns true if
109 ok func(out url.Values) bool
111 for _, tc := range []testCase{
113 in: map[string]interface{}{"foo": "bar"},
114 ok: func(out url.Values) bool {
115 return out.Get("foo") == "bar"
119 in: map[string]interface{}{"foo": 2147483647},
120 ok: func(out url.Values) bool {
121 return out.Get("foo") == "2147483647"
125 in: map[string]interface{}{"foo": 1.234},
126 ok: func(out url.Values) bool {
127 return out.Get("foo") == "1.234"
131 in: map[string]interface{}{"foo": "1.234"},
132 ok: func(out url.Values) bool {
133 return out.Get("foo") == "1.234"
137 in: map[string]interface{}{"foo": map[string]interface{}{"bar": 1.234}},
138 ok: func(out url.Values) bool {
139 return out.Get("foo") == `{"bar":1.234}`
143 in: url.Values{"foo": {"bar"}},
144 ok: func(out url.Values) bool {
145 return out.Get("foo") == "bar"
158 out, err := anythingToValues(tc.in)
160 c.Check(err, check.NotNil)
163 c.Check(err, check.IsNil)
164 c.Check(tc.ok(out), check.Equals, true)
168 func (*clientSuite) TestLoadConfig(c *check.C) {
169 oldenv := os.Environ()
172 for _, s := range oldenv {
173 i := strings.IndexRune(s, '=')
174 os.Setenv(s[:i], s[i+1:])
179 os.Setenv("HOME", tmp)
180 for _, s := range os.Environ() {
181 if strings.HasPrefix(s, "ARVADOS_") {
182 i := strings.IndexRune(s, '=')
186 os.Mkdir(tmp+"/.config", 0777)
187 os.Mkdir(tmp+"/.config/arvados", 0777)
189 // Use $HOME/.config/arvados/settings.conf if no env vars are
191 os.WriteFile(tmp+"/.config/arvados/settings.conf", []byte(`
192 ARVADOS_API_HOST = localhost:1
193 ARVADOS_API_TOKEN = token_from_settings_file1
195 client := NewClientFromEnv()
196 c.Check(client.AuthToken, check.Equals, "token_from_settings_file1")
197 c.Check(client.APIHost, check.Equals, "localhost:1")
198 c.Check(client.Insecure, check.Equals, false)
200 // ..._INSECURE=true, comments, ignored lines in settings.conf
201 os.WriteFile(tmp+"/.config/arvados/settings.conf", []byte(`
202 (ignored) = (ignored)
203 #ARVADOS_API_HOST = localhost:2
204 ARVADOS_API_TOKEN = token_from_settings_file2
205 ARVADOS_API_HOST_INSECURE = true
207 client = NewClientFromEnv()
208 c.Check(client.AuthToken, check.Equals, "token_from_settings_file2")
209 c.Check(client.APIHost, check.Equals, "")
210 c.Check(client.Insecure, check.Equals, true)
212 // Environment variables override settings.conf
213 os.Setenv("ARVADOS_API_HOST", "[::]:3")
214 os.Setenv("ARVADOS_API_HOST_INSECURE", "0")
215 client = NewClientFromEnv()
216 c.Check(client.AuthToken, check.Equals, "token_from_settings_file2")
217 c.Check(client.APIHost, check.Equals, "[::]:3")
218 c.Check(client.Insecure, check.Equals, false)