1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.arvados.org/arvados.git/lib/ctrlctx"
18 "git.arvados.org/arvados.git/sdk/go/arvados"
19 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
20 "git.arvados.org/arvados.git/sdk/go/arvadostest"
21 "git.arvados.org/arvados.git/sdk/go/keepclient"
22 check "gopkg.in/check.v1"
25 var _ = check.Suite(&CollectionSuite{})
27 type CollectionSuite struct {
31 func (s *CollectionSuite) TestCollectionCreateAndUpdateWithProperties(c *check.C) {
32 s.setUpVocabulary(c, "")
36 props map[string]interface{}
39 {"Invalid prop key", map[string]interface{}{"Priority": "IDVALIMPORTANCES1"}, false},
40 {"Invalid prop value", map[string]interface{}{"IDTAGIMPORTANCES": "high"}, false},
41 {"Valid prop key & value", map[string]interface{}{"IDTAGIMPORTANCES": "IDVALIMPORTANCES1"}, true},
42 {"Empty properties", map[string]interface{}{}, true},
44 for _, tt := range tests {
45 c.Log(c.TestName()+" ", tt.name)
47 // Create with properties
48 coll, err := s.localdb.CollectionCreate(s.userctx, arvados.CreateOptions{
49 Select: []string{"uuid", "properties"},
50 Attrs: map[string]interface{}{
51 "properties": tt.props,
54 c.Assert(err, check.IsNil)
55 c.Assert(coll.Properties, check.DeepEquals, tt.props)
57 c.Assert(err, check.NotNil)
60 // Create, then update with properties
61 coll, err = s.localdb.CollectionCreate(s.userctx, arvados.CreateOptions{})
62 c.Assert(err, check.IsNil)
63 coll, err = s.localdb.CollectionUpdate(s.userctx, arvados.UpdateOptions{
65 Select: []string{"uuid", "properties"},
66 Attrs: map[string]interface{}{
67 "properties": tt.props,
70 c.Assert(err, check.IsNil)
71 c.Assert(coll.Properties, check.DeepEquals, tt.props)
73 c.Assert(err, check.NotNil)
78 func (s *CollectionSuite) TestCollectionReplaceFiles(c *check.C) {
79 adminctx := ctrlctx.NewWithToken(s.ctx, s.cluster, arvadostest.AdminToken)
80 foo, err := s.localdb.railsProxy.CollectionCreate(adminctx, arvados.CreateOptions{
81 Attrs: map[string]interface{}{
82 "owner_uuid": arvadostest.ActiveUserUUID,
83 "manifest_text": ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo.txt\n",
85 c.Assert(err, check.IsNil)
86 s.localdb.signCollection(adminctx, &foo)
87 foobarbaz, err := s.localdb.railsProxy.CollectionCreate(adminctx, arvados.CreateOptions{
88 Attrs: map[string]interface{}{
89 "owner_uuid": arvadostest.ActiveUserUUID,
90 "manifest_text": "./foo/bar 73feffa4b7f6bb68e44cf984c85f6e88+3 0:3:baz.txt\n",
92 c.Assert(err, check.IsNil)
93 s.localdb.signCollection(adminctx, &foobarbaz)
94 wazqux, err := s.localdb.railsProxy.CollectionCreate(adminctx, arvados.CreateOptions{
95 Attrs: map[string]interface{}{
96 "owner_uuid": arvadostest.ActiveUserUUID,
97 "manifest_text": "./waz d85b1213473c2fd7c2045020a6b9c62b+3 0:3:qux.txt\n",
99 c.Assert(err, check.IsNil)
100 s.localdb.signCollection(adminctx, &wazqux)
102 // Create using content from existing collections
103 dst, err := s.localdb.CollectionCreate(s.userctx, arvados.CreateOptions{
104 ReplaceFiles: map[string]string{
105 "/f": foo.PortableDataHash + "/foo.txt",
106 "/b": foobarbaz.PortableDataHash + "/foo/bar",
107 "/q": wazqux.PortableDataHash + "/",
108 "/w": wazqux.PortableDataHash + "/waz",
110 Attrs: map[string]interface{}{
111 "owner_uuid": arvadostest.ActiveUserUUID,
113 c.Assert(err, check.IsNil)
114 s.expectFiles(c, dst, "f", "b/baz.txt", "q/waz/qux.txt", "w/qux.txt")
116 // Delete a file and a directory
117 dst, err = s.localdb.CollectionUpdate(s.userctx, arvados.UpdateOptions{
119 ReplaceFiles: map[string]string{
123 c.Assert(err, check.IsNil)
124 s.expectFiles(c, dst, "b/baz.txt", "q/", "w/qux.txt")
126 // Move and copy content within collection
127 dst, err = s.localdb.CollectionUpdate(s.userctx, arvados.UpdateOptions{
129 ReplaceFiles: map[string]string{
130 // Note splicing content to /b/corge.txt but
131 // removing everything else from /b
133 "/b/corge.txt": dst.PortableDataHash + "/b/baz.txt",
134 "/quux/corge.txt": dst.PortableDataHash + "/b/baz.txt",
136 c.Assert(err, check.IsNil)
137 s.expectFiles(c, dst, "b/corge.txt", "q/", "w/qux.txt", "quux/corge.txt")
139 // Remove everything except one file
140 dst, err = s.localdb.CollectionUpdate(s.userctx, arvados.UpdateOptions{
142 ReplaceFiles: map[string]string{
144 "/b/corge.txt": dst.PortableDataHash + "/b/corge.txt",
146 c.Assert(err, check.IsNil)
147 s.expectFiles(c, dst, "b/corge.txt")
149 // Copy entire collection to root
150 dstcopy, err := s.localdb.CollectionCreate(s.userctx, arvados.CreateOptions{
151 ReplaceFiles: map[string]string{
152 "/": dst.PortableDataHash,
154 c.Check(err, check.IsNil)
155 c.Check(dstcopy.PortableDataHash, check.Equals, dst.PortableDataHash)
156 s.expectFiles(c, dstcopy, "b/corge.txt")
158 // Check invalid targets, sources, and combinations
159 for _, badrepl := range []map[string]string{
161 "/foo/nope": dst.PortableDataHash + "/b",
162 "/foo": dst.PortableDataHash + "/b",
165 "/foo": dst.PortableDataHash + "/b",
169 "/": dst.PortableDataHash + "/",
173 "/": dst.PortableDataHash + "/",
174 "/nope": dst.PortableDataHash + "/b",
186 {"/bad": dst.UUID + "/b"},
188 _, err = s.localdb.CollectionUpdate(s.userctx, arvados.UpdateOptions{
190 ReplaceFiles: badrepl,
192 c.Logf("badrepl %#v\n... got err: %s", badrepl, err)
193 c.Check(err, check.NotNil)
196 // Check conflicting replace_files and manifest_text
197 _, err = s.localdb.CollectionUpdate(s.userctx, arvados.UpdateOptions{
199 ReplaceFiles: map[string]string{"/": ""},
200 Attrs: map[string]interface{}{
201 "manifest_text": ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:z\n",
203 c.Logf("replace_files+manifest_text\n... got err: %s", err)
204 c.Check(err, check.ErrorMatches, "ambiguous request: both.*replace_files.*manifest_text.*")
207 // expectFiles checks coll's directory structure against the given
208 // list of expected files and empty directories. An expected path with
209 // a trailing slash indicates an empty directory.
210 func (s *CollectionSuite) expectFiles(c *check.C, coll arvados.Collection, expected ...string) {
211 client := arvados.NewClientFromEnv()
212 ac, err := arvadosclient.New(client)
213 c.Assert(err, check.IsNil)
214 kc, err := keepclient.MakeKeepClient(ac)
215 c.Assert(err, check.IsNil)
216 cfs, err := coll.FileSystem(client, kc)
217 c.Assert(err, check.IsNil)
219 nonemptydirs := map[string]bool{}
220 fs.WalkDir(arvados.FS(cfs), "/", func(path string, d fs.DirEntry, err error) error {
221 dir, _ := filepath.Split(path)
222 nonemptydirs[dir] = true
227 if !nonemptydirs[path] {
228 nonemptydirs[path] = false
231 found = append(found, path)
235 for d, nonempty := range nonemptydirs {
237 found = append(found, d)
240 for i, path := range found {
242 found[i] = strings.TrimPrefix(path, "/")
246 sort.Strings(expected)
247 c.Check(found, check.DeepEquals, expected)
250 // Until #21701 it's hard to test from the outside whether the
251 // uuid_lock mechanism is effectively serializing concurrent
252 // replace_files updates to a single collection. For now, we're
253 // really just checking that it doesn't cause updates to deadlock or
254 // anything like that.
255 func (s *CollectionSuite) TestCollectionUpdateLock(c *check.C) {
256 adminctx := ctrlctx.NewWithToken(s.ctx, s.cluster, arvadostest.AdminToken)
257 foo, err := s.localdb.railsProxy.CollectionCreate(adminctx, arvados.CreateOptions{
258 Attrs: map[string]interface{}{
259 "owner_uuid": arvadostest.ActiveUserUUID,
260 "manifest_text": ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo.txt\n",
262 c.Assert(err, check.IsNil)
263 dst, err := s.localdb.CollectionCreate(s.userctx, arvados.CreateOptions{
264 ReplaceFiles: map[string]string{
265 "/foo.txt": foo.PortableDataHash + "/foo.txt",
267 Attrs: map[string]interface{}{
268 "owner_uuid": arvadostest.ActiveUserUUID,
270 c.Assert(err, check.IsNil)
271 s.expectFiles(c, dst, "foo.txt")
273 var wg sync.WaitGroup
274 for i := 0; i < 10; i++ {
275 name1, name2 := "a", "b"
277 name1, name2 = "b", "a"
282 upd, err := s.localdb.CollectionUpdate(s.userctx, arvados.UpdateOptions{
284 ReplaceFiles: map[string]string{
285 "/" + name1: foo.PortableDataHash + "/foo.txt",
289 c.Assert(err, check.IsNil)
290 s.expectFiles(c, upd, name1)
296 func (s *CollectionSuite) TestSignatures(c *check.C) {
297 resp, err := s.localdb.CollectionGet(s.userctx, arvados.GetOptions{UUID: arvadostest.FooCollection})
298 c.Check(err, check.IsNil)
299 c.Check(resp.ManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3\+A[0-9a-f]+@[0-9a-f]+ 0:.*`)
300 s.checkSignatureExpiry(c, resp.ManifestText, time.Hour*24*7*2)
302 resp, err = s.localdb.CollectionGet(s.userctx, arvados.GetOptions{UUID: arvadostest.FooCollection, Select: []string{"manifest_text"}})
303 c.Check(err, check.IsNil)
304 c.Check(resp.ManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3\+A[0-9a-f]+@[0-9a-f]+ 0:.*`)
306 lresp, err := s.localdb.CollectionList(s.userctx, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}}})
307 c.Check(err, check.IsNil)
308 if c.Check(lresp.Items, check.HasLen, 1) {
309 c.Check(lresp.Items[0].UUID, check.Equals, arvadostest.FooCollection)
310 c.Check(lresp.Items[0].ManifestText, check.Equals, "")
311 c.Check(lresp.Items[0].UnsignedManifestText, check.Equals, "")
314 lresp, err = s.localdb.CollectionList(s.userctx, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}}, Select: []string{"manifest_text"}})
315 c.Check(err, check.IsNil)
316 if c.Check(lresp.Items, check.HasLen, 1) {
317 c.Check(lresp.Items[0].ManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3\+A[0-9a-f]+@[0-9a-f]+ 0:.*`)
318 c.Check(lresp.Items[0].UnsignedManifestText, check.Equals, "")
321 lresp, err = s.localdb.CollectionList(s.userctx, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}}, Select: []string{"unsigned_manifest_text"}})
322 c.Check(err, check.IsNil)
323 if c.Check(lresp.Items, check.HasLen, 1) {
324 c.Check(lresp.Items[0].ManifestText, check.Equals, "")
325 c.Check(lresp.Items[0].UnsignedManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3 0:.*`)
328 // early trash date causes lower signature TTL (even if
329 // trash_at and is_trashed fields are unselected)
330 trashed, err := s.localdb.CollectionCreate(s.userctx, arvados.CreateOptions{
331 Select: []string{"uuid", "manifest_text"},
332 Attrs: map[string]interface{}{
333 "manifest_text": ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo\n",
334 "trash_at": time.Now().UTC().Add(time.Hour),
336 c.Assert(err, check.IsNil)
337 s.checkSignatureExpiry(c, trashed.ManifestText, time.Hour)
338 resp, err = s.localdb.CollectionGet(s.userctx, arvados.GetOptions{UUID: trashed.UUID})
339 c.Assert(err, check.IsNil)
340 s.checkSignatureExpiry(c, resp.ManifestText, time.Hour)
342 // distant future trash date does not cause higher signature TTL
343 trashed, err = s.localdb.CollectionUpdate(s.userctx, arvados.UpdateOptions{
345 Attrs: map[string]interface{}{
346 "trash_at": time.Now().UTC().Add(time.Hour * 24 * 365),
348 c.Assert(err, check.IsNil)
349 s.checkSignatureExpiry(c, trashed.ManifestText, time.Hour*24*7*2)
350 resp, err = s.localdb.CollectionGet(s.userctx, arvados.GetOptions{UUID: trashed.UUID})
351 c.Assert(err, check.IsNil)
352 s.checkSignatureExpiry(c, resp.ManifestText, time.Hour*24*7*2)
354 // Make sure groups/contents doesn't return manifest_text with
355 // collections (if it did, we'd need to sign it).
356 gresp, err := s.localdb.GroupContents(s.userctx, arvados.GroupContentsOptions{
358 Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}},
359 Select: []string{"uuid", "manifest_text"},
362 c.Check(err, check.ErrorMatches, `.*Invalid attribute.*manifest_text.*`)
363 } else if c.Check(gresp.Items, check.HasLen, 1) {
364 c.Check(gresp.Items[0].(map[string]interface{})["uuid"], check.Equals, arvadostest.FooCollection)
365 c.Check(gresp.Items[0].(map[string]interface{})["manifest_text"], check.Equals, nil)
369 func (s *CollectionSuite) checkSignatureExpiry(c *check.C, manifestText string, expectedTTL time.Duration) {
370 m := regexp.MustCompile(`@([[:xdigit:]]+)`).FindStringSubmatch(manifestText)
371 c.Assert(m, check.HasLen, 2)
372 sigexp, err := strconv.ParseInt(m[1], 16, 64)
373 c.Assert(err, check.IsNil)
374 expectedExp := time.Now().Add(expectedTTL).Unix()
375 c.Check(sigexp > expectedExp-60, check.Equals, true)
376 c.Check(sigexp <= expectedExp, check.Equals, true)
379 func (s *CollectionSuite) TestSignaturesDisabled(c *check.C) {
380 s.localdb.cluster.Collections.BlobSigning = false
381 resp, err := s.localdb.CollectionGet(s.userctx, arvados.GetOptions{UUID: arvadostest.FooCollection})
382 c.Check(err, check.IsNil)
383 c.Check(resp.ManifestText, check.Matches, `(?ms).* acbd[^ +]*\+3 0:.*`)