Merge master to output-tags branch and resolve conflict
[arvados.git] / sdk / go / keepclient / discover_test.go
1 package keepclient
2
3 import (
4         "crypto/md5"
5         "fmt"
6         "gopkg.in/check.v1"
7         "net/http"
8         "os"
9         "time"
10
11         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
12         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
13 )
14
15 func ExampleKeepClient_RefreshServices() {
16         arv, err := arvadosclient.MakeArvadosClient()
17         if err != nil {
18                 panic(err)
19         }
20         kc, err := MakeKeepClient(arv)
21         if err != nil {
22                 panic(err)
23         }
24         go kc.RefreshServices(5*time.Minute, 3*time.Second)
25         fmt.Printf("LocalRoots: %#v\n", kc.LocalRoots())
26 }
27
28 func (s *ServerRequiredSuite) TestOverrideDiscovery(c *check.C) {
29         defer os.Setenv("ARVADOS_KEEP_SERVICES", "")
30
31         hash := fmt.Sprintf("%x+3", md5.Sum([]byte("TestOverrideDiscovery")))
32         st := StubGetHandler{
33                 c,
34                 hash,
35                 arvadostest.ActiveToken,
36                 http.StatusOK,
37                 []byte("TestOverrideDiscovery")}
38         ks := RunSomeFakeKeepServers(st, 2)
39
40         os.Setenv("ARVADOS_KEEP_SERVICES", "")
41         arv1, err := arvadosclient.MakeArvadosClient()
42         c.Assert(err, check.IsNil)
43         arv1.ApiToken = arvadostest.ActiveToken
44
45         os.Setenv("ARVADOS_KEEP_SERVICES", ks[0].url+"  "+ks[1].url+" ")
46         arv2, err := arvadosclient.MakeArvadosClient()
47         c.Assert(err, check.IsNil)
48         arv2.ApiToken = arvadostest.ActiveToken
49
50         // ARVADOS_KEEP_SERVICES was empty when we created arv1, but
51         // it pointed to our stub servers when we created
52         // arv2. Regardless of what it's set to now, a keepclient for
53         // arv2 should use our stub servers, but one created for arv1
54         // should not.
55
56         kc1, err := MakeKeepClient(arv1)
57         c.Assert(err, check.IsNil)
58         kc2, err := MakeKeepClient(arv2)
59         c.Assert(err, check.IsNil)
60
61         _, _, _, err = kc1.Get(hash)
62         c.Check(err, check.NotNil)
63         _, _, _, err = kc2.Get(hash)
64         c.Check(err, check.IsNil)
65 }