1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
10 "git.arvados.org/arvados.git/sdk/go/arvados"
11 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
12 "git.arvados.org/arvados.git/sdk/go/arvadostest"
13 "git.arvados.org/arvados.git/sdk/go/ctxlog"
14 "github.com/prometheus/client_golang/prometheus"
15 "github.com/prometheus/common/expfmt"
19 func (s *UnitSuite) checkCacheMetrics(c *check.C, reg *prometheus.Registry, regs ...string) {
20 mfs, err := reg.Gather()
21 c.Check(err, check.IsNil)
22 buf := &bytes.Buffer{}
23 enc := expfmt.NewEncoder(buf, expfmt.FmtText)
24 for _, mf := range mfs {
25 c.Check(enc.Encode(mf), check.IsNil)
28 for _, reg := range regs {
29 c.Check(mm, check.Matches, `(?ms).*collectioncache_`+reg+`\n.*`)
33 func (s *UnitSuite) TestCache(c *check.C) {
34 arv, err := arvadosclient.MakeArvadosClient()
35 c.Assert(err, check.Equals, nil)
37 cache := newConfig(ctxlog.TestLogger(c), s.Config).Cache
38 cache.registry = prometheus.NewRegistry()
40 // Hit the same collection 5 times using the same token. Only
41 // the first req should cause an API call; the next 4 should
43 arv.ApiToken = arvadostest.AdminToken
44 var coll *arvados.Collection
45 for i := 0; i < 5; i++ {
46 coll, err = cache.Get(arv, arvadostest.FooCollection, false)
47 c.Check(err, check.Equals, nil)
48 c.Assert(coll, check.NotNil)
49 c.Check(coll.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
50 c.Check(coll.ManifestText[:2], check.Equals, ". ")
52 s.checkCacheMetrics(c, cache.registry,
58 // Hit the same collection 2 more times, this time requesting
59 // it by PDH and using a different token. The first req should
60 // miss the permission cache and fetch the new manifest; the
61 // second should hit the Collection cache and skip the API
63 arv.ApiToken = arvadostest.ActiveToken
65 coll2, err := cache.Get(arv, arvadostest.FooCollectionPDH, false)
66 c.Check(err, check.Equals, nil)
67 c.Assert(coll2, check.NotNil)
68 c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
69 c.Check(coll2.ManifestText[:2], check.Equals, ". ")
70 c.Check(coll2.ManifestText, check.Not(check.Equals), coll.ManifestText)
72 s.checkCacheMetrics(c, cache.registry,
78 coll2, err = cache.Get(arv, arvadostest.FooCollectionPDH, false)
79 c.Check(err, check.Equals, nil)
80 c.Assert(coll2, check.NotNil)
81 c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
82 c.Check(coll2.ManifestText[:2], check.Equals, ". ")
84 s.checkCacheMetrics(c, cache.registry,
90 // Alternating between two collections N times should produce
91 // only 2 more API calls.
92 arv.ApiToken = arvadostest.AdminToken
93 for i := 0; i < 20; i++ {
96 target = arvadostest.HelloWorldCollection
98 target = arvadostest.FooBarDirCollection
100 _, err := cache.Get(arv, target, false)
101 c.Check(err, check.Equals, nil)
103 s.checkCacheMetrics(c, cache.registry,
110 func (s *UnitSuite) TestCacheForceReloadByPDH(c *check.C) {
111 arv, err := arvadosclient.MakeArvadosClient()
112 c.Assert(err, check.Equals, nil)
114 cache := newConfig(ctxlog.TestLogger(c), s.Config).Cache
115 cache.registry = prometheus.NewRegistry()
117 for _, forceReload := range []bool{false, true, false, true} {
118 _, err := cache.Get(arv, arvadostest.FooCollectionPDH, forceReload)
119 c.Check(err, check.Equals, nil)
122 s.checkCacheMetrics(c, cache.registry,
129 func (s *UnitSuite) TestCacheForceReloadByUUID(c *check.C) {
130 arv, err := arvadosclient.MakeArvadosClient()
131 c.Assert(err, check.Equals, nil)
133 cache := newConfig(ctxlog.TestLogger(c), s.Config).Cache
134 cache.registry = prometheus.NewRegistry()
136 for _, forceReload := range []bool{false, true, false, true} {
137 _, err := cache.Get(arv, arvadostest.FooCollection, forceReload)
138 c.Check(err, check.Equals, nil)
141 s.checkCacheMetrics(c, cache.registry,