1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
6 require 'sweep_trashed_objects'
8 class CollectionTest < ActiveSupport::TestCase
11 def create_collection name, enc=nil
12 txt = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:#{name}.txt\n"
13 txt.force_encoding(enc) if enc
14 return Collection.create(manifest_text: txt, name: name)
17 test 'accept ASCII manifest_text' do
19 c = create_collection 'foo', Encoding::US_ASCII
24 test 'accept UTF-8 manifest_text' do
26 c = create_collection "f\xc3\x98\xc3\x98", Encoding::UTF_8
31 test 'refuse manifest_text with invalid UTF-8 byte sequence' do
33 c = create_collection "f\xc8o", Encoding::UTF_8
35 assert_equal [:manifest_text], c.errors.messages.keys
36 assert_match(/UTF-8/, c.errors.messages[:manifest_text].first)
40 test 'refuse manifest_text with non-UTF-8 encoding' do
42 c = create_collection "f\xc8o", Encoding::ASCII_8BIT
44 assert_equal [:manifest_text], c.errors.messages.keys
45 assert_match(/UTF-8/, c.errors.messages[:manifest_text].first)
51 ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
52 "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
53 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
54 ].each do |manifest_text|
55 test "create collection with invalid manifest text #{manifest_text} and expect error" do
57 c = Collection.create(manifest_text: manifest_text)
64 [". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", 1, 34],
65 [". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt 0:30:foo.txt 0:30:foo1.txt 0:30:foo2.txt 0:30:foo3.txt 0:30:foo4.txt\n", 5, 184],
66 [". d41d8cd98f00b204e9800998ecf8427e 0:0:.\n", 0, 0]
67 ].each do |manifest, count, size|
68 test "file stats on create collection with #{manifest}" do
70 c = Collection.create(manifest_text: manifest)
71 assert_equal count, c.file_count
72 assert_equal size, c.file_size_total
77 test "file stats cannot be changed unless through manifest change" do
79 # Direct changes to file stats should be ignored
80 c = Collection.create(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n")
82 c.file_size_total = 30
84 assert_equal 1, c.file_count
85 assert_equal 34, c.file_size_total
87 # File stats specified on create should be ignored and overwritten
88 c = Collection.create(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", file_count: 10, file_size_total: 10)
90 assert_equal 1, c.file_count
91 assert_equal 34, c.file_size_total
93 # Updating the manifest should change file stats
94 c.update_attributes(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt 0:34:foo2.txt\n")
96 assert_equal 2, c.file_count
97 assert_equal 68, c.file_size_total
99 # Updating file stats and the manifest should use manifest values
100 c.update_attributes(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", file_count:10, file_size_total: 10)
102 assert_equal 1, c.file_count
103 assert_equal 34, c.file_size_total
105 # Updating just the file stats should be ignored
106 c.update_attributes(file_count: 10, file_size_total: 10)
108 assert_equal 1, c.file_count
109 assert_equal 34, c.file_size_total
116 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
117 ].each do |manifest_text|
118 test "create collection with valid manifest text #{manifest_text.inspect} and expect success" do
119 act_as_system_user do
120 c = Collection.create(manifest_text: manifest_text)
128 ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
129 "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
130 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
131 ].each do |manifest_text|
132 test "update collection with invalid manifest text #{manifest_text} and expect error" do
133 act_as_system_user do
134 c = create_collection 'foo', Encoding::US_ASCII
137 c.update_attribute 'manifest_text', manifest_text
146 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
147 ].each do |manifest_text|
148 test "update collection with valid manifest text #{manifest_text.inspect} and expect success" do
149 act_as_system_user do
150 c = create_collection 'foo', Encoding::US_ASCII
153 c.update_attribute 'manifest_text', manifest_text
159 test "auto-create version after idle setting" do
160 Rails.configuration.Collections.CollectionVersioning = true
161 Rails.configuration.Collections.PreserveVersionIfIdle = 600 # 10 minutes
162 act_as_user users(:active) do
163 # Set up initial collection
164 c = create_collection 'foo', Encoding::US_ASCII
166 assert_equal 1, c.version
167 assert_equal false, c.preserve_version
168 # Make a versionable update, it shouldn't create a new version yet
169 c.update_attributes!({'name' => 'bar'})
171 assert_equal 'bar', c.name
172 assert_equal 1, c.version
173 # Update modified_at to trigger a version auto-creation
174 fifteen_min_ago = Time.now - 15.minutes
175 c.update_column('modified_at', fifteen_min_ago) # Update without validations/callbacks
177 assert_equal fifteen_min_ago.to_i, c.modified_at.to_i
178 c.update_attributes!({'name' => 'baz'})
180 assert_equal 'baz', c.name
181 assert_equal 2, c.version
182 # Make another update, no new version should be created
183 c.update_attributes!({'name' => 'foobar'})
185 assert_equal 'foobar', c.name
186 assert_equal 2, c.version
190 test "preserve_version=false assignment is ignored while being true and not producing a new version" do
191 Rails.configuration.Collections.CollectionVersioning = true
192 Rails.configuration.Collections.PreserveVersionIfIdle = 3600
193 act_as_user users(:active) do
194 # Set up initial collection
195 c = create_collection 'foo', Encoding::US_ASCII
197 assert_equal 1, c.version
198 assert_equal false, c.preserve_version
199 # This update shouldn't produce a new version, as the idle time is not up
200 c.update_attributes!({
202 'preserve_version' => true
205 assert_equal 1, c.version
206 assert_equal 'bar', c.name
207 assert_equal true, c.preserve_version
208 # Make sure preserve_version is not disabled after being enabled, unless
209 # a new version is created.
210 c.update_attributes!({
211 'preserve_version' => false,
212 'replication_desired' => 2
215 assert_equal 1, c.version
216 assert_equal 2, c.replication_desired
217 assert_equal true, c.preserve_version
218 c.update_attributes!({'name' => 'foobar'})
220 assert_equal 2, c.version
221 assert_equal false, c.preserve_version
222 assert_equal 'foobar', c.name
228 ['current_version_uuid', 'zzzzz-4zz18-bv31uwvy3neko21'],
229 ].each do |name, new_value|
230 test "'#{name}' updates on current version collections are not allowed" do
231 act_as_user users(:active) do
232 # Set up initial collection
233 c = create_collection 'foo', Encoding::US_ASCII
235 assert_equal 1, c.version
237 assert_raises(ActiveRecord::RecordInvalid) do
238 c.update_attributes!({
246 test "uuid updates on current version make older versions update their pointers" do
247 Rails.configuration.Collections.CollectionVersioning = true
248 Rails.configuration.Collections.PreserveVersionIfIdle = 0
249 act_as_system_user do
250 # Set up initial collection
251 c = create_collection 'foo', Encoding::US_ASCII
253 assert_equal 1, c.version
254 # Make changes so that a new version is created
255 c.update_attributes!({'name' => 'bar'})
257 assert_equal 2, c.version
258 assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
259 new_uuid = 'zzzzz-4zz18-somefakeuuidnow'
260 assert_empty Collection.where(uuid: new_uuid)
261 # Update UUID on current version, check that both collections point to it
262 c.update_attributes!({'uuid' => new_uuid})
264 assert_equal new_uuid, c.uuid
265 assert_equal 2, Collection.where(current_version_uuid: new_uuid).count
269 # This test exposes a bug related to JSONB attributes, see #15725.
270 test "recently loaded collection shouldn't list changed attributes" do
271 col = Collection.where("properties != '{}'::jsonb").limit(1).first
272 refute col.properties_changed?, 'Properties field should not be seen as changed'
275 test "older versions' modified_at indicate when they're created" do
276 Rails.configuration.Collections.CollectionVersioning = true
277 Rails.configuration.Collections.PreserveVersionIfIdle = 0
278 act_as_user users(:active) do
279 # Set up initial collection
280 c = create_collection 'foo', Encoding::US_ASCII
282 # Make changes so that a new version is created
283 c.update_attributes!({'name' => 'bar'})
285 assert_equal 2, c.version
286 # Get the old version
287 c_old = Collection.where(current_version_uuid: c.uuid, version: 1).first
290 version_creation_datetime = c_old.modified_at.to_f
291 assert_equal c.created_at.to_f, c_old.created_at.to_f
292 # Current version is updated just a few milliseconds before the version is
293 # saved on the database.
294 assert_operator c.modified_at.to_f, :<, version_creation_datetime
296 # Make update on current version so old version get the attribute synced;
297 # its modified_at should not change.
299 c.update_attributes!({'replication_desired' => new_replication})
301 assert_equal new_replication, c.replication_desired
303 assert_equal new_replication, c_old.replication_desired
304 assert_equal version_creation_datetime, c_old.modified_at.to_f
305 assert_operator c.modified_at.to_f, :>, c_old.modified_at.to_f
309 test "past versions should not be directly updatable" do
310 Rails.configuration.Collections.CollectionVersioning = true
311 Rails.configuration.Collections.PreserveVersionIfIdle = 0
312 act_as_system_user do
313 # Set up initial collection
314 c = create_collection 'foo', Encoding::US_ASCII
316 # Make changes so that a new version is created
317 c.update_attributes!({'name' => 'bar'})
319 assert_equal 2, c.version
320 # Get the old version
321 c_old = Collection.where(current_version_uuid: c.uuid, version: 1).first
323 # With collection versioning still being enabled, try to update
324 c_old.name = 'this was foo'
325 assert c_old.invalid?
327 # Try to fool the validator attempting to make c_old to look like a
328 # current version, it should also fail.
329 c_old.current_version_uuid = c_old.uuid
330 assert c_old.invalid?
332 # Now disable collection versioning, it should behave the same way
333 Rails.configuration.Collections.CollectionVersioning = false
334 c_old.name = 'this was foo'
335 assert c_old.invalid?
340 ['owner_uuid', 'zzzzz-tpzed-d9tiejq69daie8f', 'zzzzz-tpzed-xurymjxw79nv3jz'],
341 ['replication_desired', 2, 3],
342 ['storage_classes_desired', ['hot'], ['archive']],
343 ].each do |attr, first_val, second_val|
344 test "sync #{attr} with older versions" do
345 Rails.configuration.Collections.CollectionVersioning = true
346 Rails.configuration.Collections.PreserveVersionIfIdle = 0
347 act_as_system_user do
348 # Set up initial collection
349 c = create_collection 'foo', Encoding::US_ASCII
351 assert_equal 1, c.version
352 assert_not_equal first_val, c.attributes[attr]
353 # Make changes so that a new version is created and a synced field is
355 c.update_attributes!({'name' => 'bar', attr => first_val})
357 assert_equal 2, c.version
358 assert_equal first_val, c.attributes[attr]
359 assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
360 assert_equal first_val, Collection.where(current_version_uuid: c.uuid, version: 1).first.attributes[attr]
361 # Only make an update on the same synced field & check that the previously
362 # created version also gets it.
363 c.update_attributes!({attr => second_val})
365 assert_equal 2, c.version
366 assert_equal second_val, c.attributes[attr]
367 assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
368 assert_equal second_val, Collection.where(current_version_uuid: c.uuid, version: 1).first.attributes[attr]
374 [false, 'name', 'bar', false],
375 [false, 'description', 'The quick brown fox jumps over the lazy dog', false],
376 [false, 'properties', {'new_version' => true}, false],
377 [false, 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n", false],
378 [true, 'name', 'bar', true],
379 [true, 'description', 'The quick brown fox jumps over the lazy dog', true],
380 [true, 'properties', {'new_version' => true}, true],
381 [true, 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n", true],
382 # Non-versionable attribute updates shouldn't create new versions
383 [true, 'replication_desired', 5, false],
384 [false, 'replication_desired', 5, false],
385 ].each do |versioning, attr, val, new_version_expected|
386 test "update #{attr} with versioning #{versioning ? '' : 'not '}enabled should #{new_version_expected ? '' : 'not '}create a new version" do
387 Rails.configuration.Collections.CollectionVersioning = versioning
388 Rails.configuration.Collections.PreserveVersionIfIdle = 0
389 act_as_user users(:active) do
390 # Create initial collection
391 c = create_collection 'foo', Encoding::US_ASCII
393 assert_equal 'foo', c.name
395 # Check current version attributes
396 assert_equal 1, c.version
397 assert_equal c.uuid, c.current_version_uuid
399 # Update attribute and check if version number should be incremented
400 old_value = c.attributes[attr]
401 c.update_attributes!({attr => val})
402 assert_equal new_version_expected, c.version == 2
403 assert_equal val, c.attributes[attr]
405 if versioning && new_version_expected
406 # Search for the snapshot & previous value
407 assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
408 s = Collection.where(current_version_uuid: c.uuid, version: 1).first
410 assert_equal old_value, s.attributes[attr]
412 # If versioning is disabled or no versionable attribute was updated,
413 # only the current version should exist
414 assert_equal 1, Collection.where(current_version_uuid: c.uuid).count
415 assert_equal c, Collection.where(current_version_uuid: c.uuid).first
421 test 'current_version_uuid is ignored during update' do
422 Rails.configuration.Collections.CollectionVersioning = true
423 Rails.configuration.Collections.PreserveVersionIfIdle = 0
424 act_as_user users(:active) do
425 # Create 1st collection
426 col1 = create_collection 'foo', Encoding::US_ASCII
428 assert_equal 1, col1.version
430 # Create 2nd collection, update it so it becomes version:2
431 # (to avoid unique index violation)
432 col2 = create_collection 'bar', Encoding::US_ASCII
434 assert_equal 1, col2.version
435 col2.update_attributes({name: 'baz'})
436 assert_equal 2, col2.version
438 # Try to make col2 a past version of col1. It shouldn't be possible
439 col2.update_attributes({current_version_uuid: col1.uuid})
442 assert_not_equal col1.uuid, col2.current_version_uuid
446 test 'with versioning enabled, simultaneous updates increment version correctly' do
447 Rails.configuration.Collections.CollectionVersioning = true
448 Rails.configuration.Collections.PreserveVersionIfIdle = 0
449 act_as_user users(:active) do
450 # Create initial collection
451 col = create_collection 'foo', Encoding::US_ASCII
453 assert_equal 1, col.version
455 # Simulate simultaneous updates
456 c1 = Collection.where(uuid: col.uuid).first
457 assert_equal 1, c1.version
459 c2 = Collection.where(uuid: col.uuid).first
460 c2.description = 'foo collection'
462 assert_equal 1, c2.version
463 # with_lock forces a reload, so this shouldn't produce an unique violation error
465 assert_equal 3, c2.version
466 assert_equal 'foo collection', c2.description
470 test 'create and update collection and verify file_names' do
471 act_as_system_user do
472 c = create_collection 'foo', Encoding::US_ASCII
474 created_file_names = c.file_names
475 assert created_file_names
476 assert_match(/foo.txt/, c.file_names)
478 c.update_attribute 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo2.txt\n"
479 assert_not_equal created_file_names, c.file_names
480 assert_match(/foo2.txt/, c.file_names)
487 ].each do |manifest_size, allow_truncate|
488 test "create collection with manifest size #{manifest_size} with allow_truncate=#{allow_truncate},
489 and not expect exceptions even on very large manifest texts" do
490 # file_names has a max size, hence there will be no errors even on large manifests
491 act_as_system_user do
494 while manifest_text.length < manifest_size
495 manifest_text += "./blurfl d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylongfilename000000000000#{index}.txt\n"
498 manifest_text += "./laststreamname d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylastfilename.txt\n"
499 c = Collection.create(manifest_text: manifest_text)
503 assert_match(/veryverylongfilename0000000000001.txt/, c.file_names)
504 assert_match(/veryverylongfilename0000000000002.txt/, c.file_names)
505 if not allow_truncate
506 assert_match(/veryverylastfilename/, c.file_names)
507 assert_match(/laststreamname/, c.file_names)
513 test "full text search for collections" do
514 # file_names column does not get populated when fixtures are loaded, hence setup test data
515 act_as_system_user do
516 Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n")
517 Collection.create(manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n")
518 Collection.create(manifest_text: ". 85877ca2d7e05498dd3d109baf2df106+95+A3a4e26a366ee7e4ed3e476ccf05354761be2e4ae@545a9920 0:95:file_in_subdir1\n./subdir2/subdir3 2bbc341c702df4d8f42ec31f16c10120+64+A315d7e7bad2ce937e711fc454fae2d1194d14d64@545a9920 0:32:file1.txt 32:32:file2.txt\n./subdir2/subdir3/subdir4 2bbc341c702df4d8f42ec31f16c10120+64+A315d7e7bad2ce937e711fc454fae2d1194d14d64@545a9920 0:32:file3.txt 32:32:file4.txt\n")
523 ['foo bar', false], # no collection matching both
524 ['foo&bar', false], # no collection matching both
525 ['foo|bar', true], # works only no spaces between the words
526 ['Gnu public', true], # both prefixes found, though not consecutively
527 ['Gnu&public', true], # both prefixes found, though not consecutively
528 ['file4', true], # prefix match
529 ['file4.txt', true], # whole string match
530 ['filex', false], # no such prefix
531 ['subdir', true], # prefix matches
534 ['subdir2/subdir3', true],
535 ['subdir2/subdir3/subdir4', true],
536 ['subdir2 file4', true], # look for both prefixes
537 ['subdir4', false], # not a prefix match
538 ].each do |search_filter, expect_results|
539 search_filters = search_filter.split.each {|s| s.concat(':*')}.join('&')
540 results = Collection.where("#{Collection.full_text_tsvector} @@ to_tsquery(?)",
550 test 'portable data hash with missing size hints' do
551 [[". d41d8cd98f00b204e9800998ecf8427e+0+Bar 0:0:x\n",
552 ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"],
553 [". d41d8cd98f00b204e9800998ecf8427e+Foo 0:0:x\n",
554 ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
555 [". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n",
556 ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
557 ].each do |unportable, portable|
558 c = Collection.new(manifest_text: unportable)
560 assert_equal(Digest::MD5.hexdigest(portable)+"+#{portable.length}",
561 c.portable_data_hash)
565 pdhmanifest = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"
566 pdhmd5 = Digest::MD5.hexdigest pdhmanifest
569 [true, pdhmd5+'+12345'],
570 [true, pdhmd5+'+'+pdhmanifest.length.to_s],
571 [true, pdhmd5+'+12345+Foo'],
572 [true, pdhmd5+'+Foo'],
573 [false, Digest::MD5.hexdigest(pdhmanifest.strip)],
574 [false, Digest::MD5.hexdigest(pdhmanifest.strip)+'+'+pdhmanifest.length.to_s],
575 [false, pdhmd5[0..30]],
576 [false, pdhmd5[0..30]+'z'],
577 [false, pdhmd5[0..24]+'000000000'],
578 [false, pdhmd5[0..24]+'000000000+0']].each do |isvalid, pdh|
579 test "portable_data_hash #{pdh.inspect} valid? == #{isvalid}" do
580 c = Collection.new manifest_text: pdhmanifest, portable_data_hash: pdh
581 assert_equal isvalid, c.valid?, c.errors.full_messages.to_s
585 test "storage_classes_desired cannot be empty" do
586 act_as_user users(:active) do
587 c = collections(:collection_owned_by_active)
588 c.update_attributes storage_classes_desired: ["hot"]
589 assert_equal ["hot"], c.storage_classes_desired
590 assert_raise ArvadosModel::InvalidStateTransitionError do
591 c.update_attributes storage_classes_desired: []
596 test "storage classes lists should only contain non-empty strings" do
597 c = collections(:storage_classes_desired_default_unconfirmed)
598 act_as_user users(:admin) do
599 assert c.update_attributes(storage_classes_desired: ["default", "a_string"],
600 storage_classes_confirmed: ["another_string"])
602 ["storage_classes_desired", ["default", 42]],
603 ["storage_classes_confirmed", [{the_answer: 42}]],
604 ["storage_classes_desired", ["default", ""]],
605 ["storage_classes_confirmed", [""]],
606 ].each do |attr, val|
607 assert_raise ArvadosModel::InvalidStateTransitionError do
608 assert c.update_attributes({attr => val})
614 test "storage_classes_confirmed* can be set by admin user" do
615 c = collections(:storage_classes_desired_default_unconfirmed)
616 act_as_user users(:admin) do
617 assert c.update_attributes(storage_classes_confirmed: ["default"],
618 storage_classes_confirmed_at: Time.now)
622 test "storage_classes_confirmed* cannot be set by non-admin user" do
623 act_as_user users(:active) do
624 c = collections(:storage_classes_desired_default_unconfirmed)
625 # Cannot set just one at a time.
626 assert_raise ArvadosModel::PermissionDeniedError do
627 c.update_attributes storage_classes_confirmed: ["default"]
630 assert_raise ArvadosModel::PermissionDeniedError do
631 c.update_attributes storage_classes_confirmed_at: Time.now
633 # Cannot set bot at once, either.
635 assert_raise ArvadosModel::PermissionDeniedError do
636 assert c.update_attributes(storage_classes_confirmed: ["default"],
637 storage_classes_confirmed_at: Time.now)
642 test "storage_classes_confirmed* can be cleared (but only together) by non-admin user" do
643 act_as_user users(:active) do
644 c = collections(:storage_classes_desired_default_confirmed_default)
645 # Cannot clear just one at a time.
646 assert_raise ArvadosModel::PermissionDeniedError do
647 c.update_attributes storage_classes_confirmed: []
650 assert_raise ArvadosModel::PermissionDeniedError do
651 c.update_attributes storage_classes_confirmed_at: nil
653 # Can clear both at once.
655 assert c.update_attributes(storage_classes_confirmed: [],
656 storage_classes_confirmed_at: nil)
660 [0, 2, 4, nil].each do |ask|
661 test "set replication_desired to #{ask.inspect}" do
662 Rails.configuration.Collections.DefaultReplication = 2
663 act_as_user users(:active) do
664 c = collections(:replication_undesired_unconfirmed)
665 c.update_attributes replication_desired: ask
666 assert_equal ask, c.replication_desired
671 test "replication_confirmed* can be set by admin user" do
672 c = collections(:replication_desired_2_unconfirmed)
673 act_as_user users(:admin) do
674 assert c.update_attributes(replication_confirmed: 2,
675 replication_confirmed_at: Time.now)
679 test "replication_confirmed* cannot be set by non-admin user" do
680 act_as_user users(:active) do
681 c = collections(:replication_desired_2_unconfirmed)
682 # Cannot set just one at a time.
683 assert_raise ArvadosModel::PermissionDeniedError do
684 c.update_attributes replication_confirmed: 1
686 assert_raise ArvadosModel::PermissionDeniedError do
687 c.update_attributes replication_confirmed_at: Time.now
689 # Cannot set both at once, either.
690 assert_raise ArvadosModel::PermissionDeniedError do
691 c.update_attributes(replication_confirmed: 1,
692 replication_confirmed_at: Time.now)
697 test "replication_confirmed* can be cleared (but only together) by non-admin user" do
698 act_as_user users(:active) do
699 c = collections(:replication_desired_2_confirmed_2)
700 # Cannot clear just one at a time.
701 assert_raise ArvadosModel::PermissionDeniedError do
702 c.update_attributes replication_confirmed: nil
705 assert_raise ArvadosModel::PermissionDeniedError do
706 c.update_attributes replication_confirmed_at: nil
708 # Can clear both at once.
710 assert c.update_attributes(replication_confirmed: nil,
711 replication_confirmed_at: nil)
715 test "clear replication_confirmed* when introducing a new block in manifest" do
716 c = collections(:replication_desired_2_confirmed_2)
717 act_as_user users(:active) do
718 assert c.update_attributes(manifest_text: collections(:user_agreement).signed_manifest_text)
719 assert_nil c.replication_confirmed
720 assert_nil c.replication_confirmed_at
724 test "don't clear replication_confirmed* when just renaming a file" do
725 c = collections(:replication_desired_2_confirmed_2)
726 act_as_user users(:active) do
727 new_manifest = c.signed_manifest_text.sub(':bar', ':foo')
728 assert c.update_attributes(manifest_text: new_manifest)
729 assert_equal 2, c.replication_confirmed
730 assert_not_nil c.replication_confirmed_at
734 test "don't clear replication_confirmed* when just deleting a data block" do
735 c = collections(:replication_desired_2_confirmed_2)
736 act_as_user users(:active) do
737 new_manifest = c.signed_manifest_text
738 new_manifest.sub!(/ \S+:bar/, '')
739 new_manifest.sub!(/ acbd\S+/, '')
741 # Confirm that we did just remove a block from the manifest (if
742 # not, this test would pass without testing the relevant case):
743 assert_operator new_manifest.length+40, :<, c.signed_manifest_text.length
745 assert c.update_attributes(manifest_text: new_manifest)
746 assert_equal 2, c.replication_confirmed
747 assert_not_nil c.replication_confirmed_at
751 test 'signature expiry does not exceed trash_at' do
752 act_as_user users(:active) do
754 c = Collection.create!(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n", name: 'foo')
755 c.update_attributes! trash_at: (t0 + 1.hours)
757 sig_exp = /\+A[0-9a-f]{40}\@([0-9]+)/.match(c.signed_manifest_text)[1].to_i
758 assert_operator sig_exp.to_i, :<=, (t0 + 1.hours).to_i
762 test 'far-future expiry date cannot be used to circumvent configured permission ttl' do
763 act_as_user users(:active) do
764 c = Collection.create!(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n",
766 trash_at: db_current_time + 1.years)
767 sig_exp = /\+A[0-9a-f]{40}\@([0-9]+)/.match(c.signed_manifest_text)[1].to_i
768 expect_max_sig_exp = db_current_time.to_i + Rails.configuration.Collections.BlobSigningTTL.to_i
769 assert_operator c.trash_at.to_i, :>, expect_max_sig_exp
770 assert_operator sig_exp.to_i, :<=, expect_max_sig_exp
774 test "create collection with properties" do
775 act_as_system_user do
776 c = Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n",
777 properties: {'property_1' => 'value_1'})
779 assert_equal 'value_1', c.properties['property_1']
783 test 'create, delete, recreate collection with same name and owner' do
784 act_as_user users(:active) do
785 # create collection with name
786 c = Collection.create(manifest_text: '',
787 name: "test collection name")
791 c = Collection.readable_by(current_user).where(uuid: uuid)
792 assert_not_empty c, 'Should be able to find live collection'
794 # mark collection as expired
795 c.first.update_attributes!(trash_at: Time.new.strftime("%Y-%m-%d"))
796 c = Collection.readable_by(current_user).where(uuid: uuid)
797 assert_empty c, 'Should not be able to find expired collection'
799 # recreate collection with the same name
800 c = Collection.create(manifest_text: '',
801 name: "test collection name")
806 test 'trash_at cannot be set too far in the past' do
807 act_as_user users(:active) do
809 c = Collection.create!(manifest_text: '', name: 'foo')
810 c.update_attributes! trash_at: (t0 - 2.weeks)
812 assert_operator c.trash_at, :>, t0
817 [['trash-to-delete interval negative',
818 :collection_owned_by_active,
819 {trash_at: now+2.weeks, delete_at: now},
821 ['now-to-delete interval short',
822 :collection_owned_by_active,
823 {trash_at: now+3.days, delete_at: now+7.days},
824 {state: :trash_future}],
825 ['now-to-delete interval short, trash=delete',
826 :collection_owned_by_active,
827 {trash_at: now+3.days, delete_at: now+3.days},
828 {state: :trash_future}],
829 ['trash-to-delete interval ok',
830 :collection_owned_by_active,
831 {trash_at: now, delete_at: now+15.days},
832 {state: :trash_now}],
833 ['trash-to-delete interval short, but far enough in future',
834 :collection_owned_by_active,
835 {trash_at: now+13.days, delete_at: now+15.days},
836 {state: :trash_future}],
837 ['trash by setting is_trashed bool',
838 :collection_owned_by_active,
840 {state: :trash_now}],
841 ['trash in future by setting just trash_at',
842 :collection_owned_by_active,
843 {trash_at: now+1.week},
844 {state: :trash_future}],
845 ['trash in future by setting trash_at and delete_at',
846 :collection_owned_by_active,
847 {trash_at: now+1.week, delete_at: now+4.weeks},
848 {state: :trash_future}],
849 ['untrash by clearing is_trashed bool',
852 {state: :not_trash}],
853 ].each do |test_name, fixture_name, updates, expect|
855 act_as_user users(:active) do
856 min_exp = (db_current_time +
857 Rails.configuration.Collections.BlobSigningTTL)
858 if fixture_name == :expired_collection
859 # Fixture-finder shorthand doesn't find trashed collections
860 # because they're not in the default scope.
861 c = Collection.find_by_uuid('zzzzz-4zz18-mto52zx1s7sn3ih')
863 c = collections(fixture_name)
865 updates_ok = c.update_attributes(updates)
866 expect_valid = expect[:state] != :invalid
867 assert_equal expect_valid, updates_ok, c.errors.full_messages.to_s
873 assert_not_nil c.trash_at
874 assert_operator c.trash_at, :<=, db_current_time
875 assert_not_nil c.delete_at
876 assert_operator c.delete_at, :>=, min_exp
879 assert_not_nil c.trash_at
880 assert_operator c.trash_at, :>, db_current_time
881 assert_not_nil c.delete_at
882 assert_operator c.delete_at, :>=, c.trash_at
883 # Currently this minimum interval is needed to prevent early
884 # garbage collection:
885 assert_operator c.delete_at, :>=, min_exp
888 assert_nil c.trash_at
889 assert_nil c.delete_at
891 raise "bad expect[:state]==#{expect[:state].inspect} in test case"
897 test 'default trash interval > blob signature ttl' do
898 Rails.configuration.Collections.DefaultTrashLifetime = 86400 * 21 # 3 weeks
899 start = db_current_time
900 act_as_user users(:active) do
901 c = Collection.create!(manifest_text: '', name: 'foo')
902 c.update_attributes!(trash_at: start + 86400.seconds)
903 assert_operator c.delete_at, :>=, start + (86400*22).seconds
904 assert_operator c.delete_at, :<, start + (86400*22 + 30).seconds
907 c = Collection.create!(manifest_text: '', name: 'foo')
908 c.update_attributes!(is_trashed: true)
909 assert_operator c.delete_at, :>=, start + (86400*21).seconds
913 test "find_all_for_docker_image resolves names that look like hashes" do
914 coll_list = Collection.
915 find_all_for_docker_image('a' * 64, nil, [users(:active)])
916 coll_uuids = coll_list.map(&:uuid)
917 assert_includes(coll_uuids, collections(:docker_image).uuid)
920 test "move collections to trash in SweepTrashedObjects" do
921 c = collections(:trashed_on_next_sweep)
922 refute_empty Collection.where('uuid=? and is_trashed=false', c.uuid)
923 assert_raises(ActiveRecord::RecordNotUnique) do
924 act_as_user users(:active) do
925 Collection.create!(owner_uuid: c.owner_uuid,
929 SweepTrashedObjects.sweep_now
930 c = Collection.where('uuid=? and is_trashed=true', c.uuid).first
932 act_as_user users(:active) do
933 assert Collection.create!(owner_uuid: c.owner_uuid,
938 test "delete collections in SweepTrashedObjects" do
939 uuid = 'zzzzz-4zz18-3u1p5umicfpqszp' # deleted_on_next_sweep
940 assert_not_empty Collection.where(uuid: uuid)
941 SweepTrashedObjects.sweep_now
942 assert_empty Collection.where(uuid: uuid)
945 test "delete referring links in SweepTrashedObjects" do
946 uuid = collections(:trashed_on_next_sweep).uuid
947 act_as_system_user do
948 Link.create!(head_uuid: uuid,
949 tail_uuid: system_user_uuid,
950 link_class: 'whatever',
953 past = db_current_time
954 Collection.where(uuid: uuid).
955 update_all(is_trashed: true, trash_at: past, delete_at: past)
956 assert_not_empty Collection.where(uuid: uuid)
957 SweepTrashedObjects.sweep_now
958 assert_empty Collection.where(uuid: uuid)