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 "github.com/prometheus/client_golang/prometheus"
14 "github.com/prometheus/common/expfmt"
18 func (s *UnitSuite) checkCacheMetrics(c *check.C, reg *prometheus.Registry, regs ...string) {
19 mfs, err := reg.Gather()
20 c.Check(err, check.IsNil)
21 buf := &bytes.Buffer{}
22 enc := expfmt.NewEncoder(buf, expfmt.FmtText)
23 for _, mf := range mfs {
24 c.Check(enc.Encode(mf), check.IsNil)
27 for _, reg := range regs {
28 c.Check(mm, check.Matches, `(?ms).*collectioncache_`+reg+`\n.*`)
32 func (s *UnitSuite) TestCache(c *check.C) {
33 arv, err := arvadosclient.MakeArvadosClient()
34 c.Assert(err, check.Equals, nil)
36 cache := newConfig(s.Config).Cache
37 cache.registry = prometheus.NewRegistry()
39 // Hit the same collection 5 times using the same token. Only
40 // the first req should cause an API call; the next 4 should
42 arv.ApiToken = arvadostest.AdminToken
43 var coll *arvados.Collection
44 for i := 0; i < 5; i++ {
45 coll, err = cache.Get(arv, arvadostest.FooCollection, false)
46 c.Check(err, check.Equals, nil)
47 c.Assert(coll, check.NotNil)
48 c.Check(coll.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
49 c.Check(coll.ManifestText[:2], check.Equals, ". ")
51 s.checkCacheMetrics(c, cache.registry,
57 // Hit the same collection 2 more times, this time requesting
58 // it by PDH and using a different token. The first req should
59 // miss the permission cache and fetch the new manifest; the
60 // second should hit the Collection cache and skip the API
62 arv.ApiToken = arvadostest.ActiveToken
64 coll2, err := cache.Get(arv, arvadostest.FooCollectionPDH, false)
65 c.Check(err, check.Equals, nil)
66 c.Assert(coll2, check.NotNil)
67 c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
68 c.Check(coll2.ManifestText[:2], check.Equals, ". ")
69 c.Check(coll2.ManifestText, check.Not(check.Equals), coll.ManifestText)
71 s.checkCacheMetrics(c, cache.registry,
77 coll2, err = cache.Get(arv, arvadostest.FooCollectionPDH, false)
78 c.Check(err, check.Equals, nil)
79 c.Assert(coll2, check.NotNil)
80 c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
81 c.Check(coll2.ManifestText[:2], check.Equals, ". ")
83 s.checkCacheMetrics(c, cache.registry,
89 // Alternating between two collections N times should produce
90 // only 2 more API calls.
91 arv.ApiToken = arvadostest.AdminToken
92 for i := 0; i < 20; i++ {
95 target = arvadostest.HelloWorldCollection
97 target = arvadostest.FooBarDirCollection
99 _, err := cache.Get(arv, target, false)
100 c.Check(err, check.Equals, nil)
102 s.checkCacheMetrics(c, cache.registry,
109 func (s *UnitSuite) TestCacheForceReloadByPDH(c *check.C) {
110 arv, err := arvadosclient.MakeArvadosClient()
111 c.Assert(err, check.Equals, nil)
113 cache := newConfig(s.Config).Cache
114 cache.registry = prometheus.NewRegistry()
116 for _, forceReload := range []bool{false, true, false, true} {
117 _, err := cache.Get(arv, arvadostest.FooCollectionPDH, forceReload)
118 c.Check(err, check.Equals, nil)
121 s.checkCacheMetrics(c, cache.registry,
128 func (s *UnitSuite) TestCacheForceReloadByUUID(c *check.C) {
129 arv, err := arvadosclient.MakeArvadosClient()
130 c.Assert(err, check.Equals, nil)
132 cache := newConfig(s.Config).Cache
133 cache.registry = prometheus.NewRegistry()
135 for _, forceReload := range []bool{false, true, false, true} {
136 _, err := cache.Get(arv, arvadostest.FooCollection, forceReload)
137 c.Check(err, check.Equals, nil)
140 s.checkCacheMetrics(c, cache.registry,