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)
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)
66 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
67 ].each do |manifest_text|
68 test "create collection with valid manifest text #{manifest_text.inspect} and expect success" do
70 c = Collection.create(manifest_text: manifest_text)
78 ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
79 "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
80 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
81 ].each do |manifest_text|
82 test "update collection with invalid manifest text #{manifest_text} and expect error" do
84 c = create_collection 'foo', Encoding::US_ASCII
87 c.update_attribute 'manifest_text', manifest_text
96 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
97 ].each do |manifest_text|
98 test "update collection with valid manifest text #{manifest_text.inspect} and expect success" do
100 c = create_collection 'foo', Encoding::US_ASCII
103 c.update_attribute 'manifest_text', manifest_text
109 test 'create and update collection and verify file_names' do
110 act_as_system_user do
111 c = create_collection 'foo', Encoding::US_ASCII
113 created_file_names = c.file_names
114 assert created_file_names
115 assert_match(/foo.txt/, c.file_names)
117 c.update_attribute 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo2.txt\n"
118 assert_not_equal created_file_names, c.file_names
119 assert_match(/foo2.txt/, c.file_names)
126 ].each do |manifest_size, allow_truncate|
127 test "create collection with manifest size #{manifest_size} with allow_truncate=#{allow_truncate},
128 and not expect exceptions even on very large manifest texts" do
129 # file_names has a max size, hence there will be no errors even on large manifests
130 act_as_system_user do
133 while manifest_text.length < manifest_size
134 manifest_text += "./blurfl d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylongfilename000000000000#{index}.txt\n"
137 manifest_text += "./laststreamname d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylastfilename.txt\n"
138 c = Collection.create(manifest_text: manifest_text)
142 assert_match(/veryverylongfilename0000000000001.txt/, c.file_names)
143 assert_match(/veryverylongfilename0000000000002.txt/, c.file_names)
144 if not allow_truncate
145 assert_match(/veryverylastfilename/, c.file_names)
146 assert_match(/laststreamname/, c.file_names)
152 test "full text search for collections" do
153 # file_names column does not get populated when fixtures are loaded, hence setup test data
154 act_as_system_user do
155 Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n")
156 Collection.create(manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n")
157 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")
162 ['foo bar', false], # no collection matching both
163 ['foo&bar', false], # no collection matching both
164 ['foo|bar', true], # works only no spaces between the words
165 ['Gnu public', true], # both prefixes found, though not consecutively
166 ['Gnu&public', true], # both prefixes found, though not consecutively
167 ['file4', true], # prefix match
168 ['file4.txt', true], # whole string match
169 ['filex', false], # no such prefix
170 ['subdir', true], # prefix matches
173 ['subdir2/subdir3', true],
174 ['subdir2/subdir3/subdir4', true],
175 ['subdir2 file4', true], # look for both prefixes
176 ['subdir4', false], # not a prefix match
177 ].each do |search_filter, expect_results|
178 search_filters = search_filter.split.each {|s| s.concat(':*')}.join('&')
179 results = Collection.where("#{Collection.full_text_tsvector} @@ to_tsquery(?)",
189 test 'portable data hash with missing size hints' do
190 [[". d41d8cd98f00b204e9800998ecf8427e+0+Bar 0:0:x\n",
191 ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"],
192 [". d41d8cd98f00b204e9800998ecf8427e+Foo 0:0:x\n",
193 ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
194 [". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n",
195 ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
196 ].each do |unportable, portable|
197 c = Collection.new(manifest_text: unportable)
199 assert_equal(Digest::MD5.hexdigest(portable)+"+#{portable.length}",
200 c.portable_data_hash)
204 pdhmanifest = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"
205 pdhmd5 = Digest::MD5.hexdigest pdhmanifest
208 [true, pdhmd5+'+12345'],
209 [true, pdhmd5+'+'+pdhmanifest.length.to_s],
210 [true, pdhmd5+'+12345+Foo'],
211 [true, pdhmd5+'+Foo'],
212 [false, Digest::MD5.hexdigest(pdhmanifest.strip)],
213 [false, Digest::MD5.hexdigest(pdhmanifest.strip)+'+'+pdhmanifest.length.to_s],
214 [false, pdhmd5[0..30]],
215 [false, pdhmd5[0..30]+'z'],
216 [false, pdhmd5[0..24]+'000000000'],
217 [false, pdhmd5[0..24]+'000000000+0']].each do |isvalid, pdh|
218 test "portable_data_hash #{pdh.inspect} valid? == #{isvalid}" do
219 c = Collection.new manifest_text: pdhmanifest, portable_data_hash: pdh
220 assert_equal isvalid, c.valid?, c.errors.full_messages.to_s
224 test "storage_classes_desired cannot be empty" do
225 act_as_user users(:active) do
226 c = collections(:collection_owned_by_active)
227 c.update_attributes storage_classes_desired: ["hot"]
228 assert_equal ["hot"], c.storage_classes_desired
229 assert_raise ArvadosModel::InvalidStateTransitionError do
230 c.update_attributes storage_classes_desired: []
235 test "storage classes lists should only contain non-empty strings" do
236 c = collections(:storage_classes_desired_default_unconfirmed)
237 act_as_user users(:admin) do
238 assert c.update_attributes(storage_classes_desired: ["default", "a_string"],
239 storage_classes_confirmed: ["another_string"])
241 ["storage_classes_desired", ["default", 42]],
242 ["storage_classes_confirmed", [{the_answer: 42}]],
243 ["storage_classes_desired", ["default", ""]],
244 ["storage_classes_confirmed", [""]],
245 ].each do |attr, val|
246 assert_raise ArvadosModel::InvalidStateTransitionError do
247 assert c.update_attributes({attr => val})
253 test "storage_classes_confirmed* can be set by admin user" do
254 c = collections(:storage_classes_desired_default_unconfirmed)
255 act_as_user users(:admin) do
256 assert c.update_attributes(storage_classes_confirmed: ["default"],
257 storage_classes_confirmed_at: Time.now)
261 test "storage_classes_confirmed* cannot be set by non-admin user" do
262 act_as_user users(:active) do
263 c = collections(:storage_classes_desired_default_unconfirmed)
264 # Cannot set just one at a time.
265 assert_raise ArvadosModel::PermissionDeniedError do
266 c.update_attributes storage_classes_confirmed: ["default"]
269 assert_raise ArvadosModel::PermissionDeniedError do
270 c.update_attributes storage_classes_confirmed_at: Time.now
272 # Cannot set bot at once, either.
274 assert_raise ArvadosModel::PermissionDeniedError do
275 assert c.update_attributes(storage_classes_confirmed: ["default"],
276 storage_classes_confirmed_at: Time.now)
281 test "storage_classes_confirmed* can be cleared (but only together) by non-admin user" do
282 act_as_user users(:active) do
283 c = collections(:storage_classes_desired_default_confirmed_default)
284 # Cannot clear just one at a time.
285 assert_raise ArvadosModel::PermissionDeniedError do
286 c.update_attributes storage_classes_confirmed: []
289 assert_raise ArvadosModel::PermissionDeniedError do
290 c.update_attributes storage_classes_confirmed_at: nil
292 # Can clear both at once.
294 assert c.update_attributes(storage_classes_confirmed: [],
295 storage_classes_confirmed_at: nil)
299 [0, 2, 4, nil].each do |ask|
300 test "set replication_desired to #{ask.inspect}" do
301 Rails.configuration.default_collection_replication = 2
302 act_as_user users(:active) do
303 c = collections(:replication_undesired_unconfirmed)
304 c.update_attributes replication_desired: ask
305 assert_equal ask, c.replication_desired
310 test "replication_confirmed* can be set by admin user" do
311 c = collections(:replication_desired_2_unconfirmed)
312 act_as_user users(:admin) do
313 assert c.update_attributes(replication_confirmed: 2,
314 replication_confirmed_at: Time.now)
318 test "replication_confirmed* cannot be set by non-admin user" do
319 act_as_user users(:active) do
320 c = collections(:replication_desired_2_unconfirmed)
321 # Cannot set just one at a time.
322 assert_raise ArvadosModel::PermissionDeniedError do
323 c.update_attributes replication_confirmed: 1
325 assert_raise ArvadosModel::PermissionDeniedError do
326 c.update_attributes replication_confirmed_at: Time.now
328 # Cannot set both at once, either.
329 assert_raise ArvadosModel::PermissionDeniedError do
330 c.update_attributes(replication_confirmed: 1,
331 replication_confirmed_at: Time.now)
336 test "replication_confirmed* can be cleared (but only together) by non-admin user" do
337 act_as_user users(:active) do
338 c = collections(:replication_desired_2_confirmed_2)
339 # Cannot clear just one at a time.
340 assert_raise ArvadosModel::PermissionDeniedError do
341 c.update_attributes replication_confirmed: nil
344 assert_raise ArvadosModel::PermissionDeniedError do
345 c.update_attributes replication_confirmed_at: nil
347 # Can clear both at once.
349 assert c.update_attributes(replication_confirmed: nil,
350 replication_confirmed_at: nil)
354 test "clear replication_confirmed* when introducing a new block in manifest" do
355 c = collections(:replication_desired_2_confirmed_2)
356 act_as_user users(:active) do
357 assert c.update_attributes(manifest_text: collections(:user_agreement).signed_manifest_text)
358 assert_nil c.replication_confirmed
359 assert_nil c.replication_confirmed_at
363 test "don't clear replication_confirmed* when just renaming a file" do
364 c = collections(:replication_desired_2_confirmed_2)
365 act_as_user users(:active) do
366 new_manifest = c.signed_manifest_text.sub(':bar', ':foo')
367 assert c.update_attributes(manifest_text: new_manifest)
368 assert_equal 2, c.replication_confirmed
369 assert_not_nil c.replication_confirmed_at
373 test "don't clear replication_confirmed* when just deleting a data block" do
374 c = collections(:replication_desired_2_confirmed_2)
375 act_as_user users(:active) do
376 new_manifest = c.signed_manifest_text
377 new_manifest.sub!(/ \S+:bar/, '')
378 new_manifest.sub!(/ acbd\S+/, '')
380 # Confirm that we did just remove a block from the manifest (if
381 # not, this test would pass without testing the relevant case):
382 assert_operator new_manifest.length+40, :<, c.signed_manifest_text.length
384 assert c.update_attributes(manifest_text: new_manifest)
385 assert_equal 2, c.replication_confirmed
386 assert_not_nil c.replication_confirmed_at
390 test 'signature expiry does not exceed trash_at' do
391 act_as_user users(:active) do
393 c = Collection.create!(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n", name: 'foo')
394 c.update_attributes! trash_at: (t0 + 1.hours)
396 sig_exp = /\+A[0-9a-f]{40}\@([0-9]+)/.match(c.signed_manifest_text)[1].to_i
397 assert_operator sig_exp.to_i, :<=, (t0 + 1.hours).to_i
401 test 'far-future expiry date cannot be used to circumvent configured permission ttl' do
402 act_as_user users(:active) do
403 c = Collection.create!(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n",
405 trash_at: db_current_time + 1.years)
406 sig_exp = /\+A[0-9a-f]{40}\@([0-9]+)/.match(c.signed_manifest_text)[1].to_i
407 expect_max_sig_exp = db_current_time.to_i + Rails.configuration.blob_signature_ttl
408 assert_operator c.trash_at.to_i, :>, expect_max_sig_exp
409 assert_operator sig_exp.to_i, :<=, expect_max_sig_exp
413 test "create collection with properties" do
414 act_as_system_user do
415 c = Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n",
416 properties: {'property_1' => 'value_1'})
418 assert_equal 'value_1', c.properties['property_1']
422 test 'create, delete, recreate collection with same name and owner' do
423 act_as_user users(:active) do
424 # create collection with name
425 c = Collection.create(manifest_text: '',
426 name: "test collection name")
430 c = Collection.readable_by(current_user).where(uuid: uuid)
431 assert_not_empty c, 'Should be able to find live collection'
433 # mark collection as expired
434 c.first.update_attributes!(trash_at: Time.new.strftime("%Y-%m-%d"))
435 c = Collection.readable_by(current_user).where(uuid: uuid)
436 assert_empty c, 'Should not be able to find expired collection'
438 # recreate collection with the same name
439 c = Collection.create(manifest_text: '',
440 name: "test collection name")
445 test 'trash_at cannot be set too far in the past' do
446 act_as_user users(:active) do
448 c = Collection.create!(manifest_text: '', name: 'foo')
449 c.update_attributes! trash_at: (t0 - 2.weeks)
451 assert_operator c.trash_at, :>, t0
456 [['trash-to-delete interval negative',
457 :collection_owned_by_active,
458 {trash_at: now+2.weeks, delete_at: now},
460 ['now-to-delete interval short',
461 :collection_owned_by_active,
462 {trash_at: now+3.days, delete_at: now+7.days},
463 {state: :trash_future}],
464 ['now-to-delete interval short, trash=delete',
465 :collection_owned_by_active,
466 {trash_at: now+3.days, delete_at: now+3.days},
467 {state: :trash_future}],
468 ['trash-to-delete interval ok',
469 :collection_owned_by_active,
470 {trash_at: now, delete_at: now+15.days},
471 {state: :trash_now}],
472 ['trash-to-delete interval short, but far enough in future',
473 :collection_owned_by_active,
474 {trash_at: now+13.days, delete_at: now+15.days},
475 {state: :trash_future}],
476 ['trash by setting is_trashed bool',
477 :collection_owned_by_active,
479 {state: :trash_now}],
480 ['trash in future by setting just trash_at',
481 :collection_owned_by_active,
482 {trash_at: now+1.week},
483 {state: :trash_future}],
484 ['trash in future by setting trash_at and delete_at',
485 :collection_owned_by_active,
486 {trash_at: now+1.week, delete_at: now+4.weeks},
487 {state: :trash_future}],
488 ['untrash by clearing is_trashed bool',
491 {state: :not_trash}],
492 ].each do |test_name, fixture_name, updates, expect|
494 act_as_user users(:active) do
495 min_exp = (db_current_time +
496 Rails.configuration.blob_signature_ttl.seconds)
497 if fixture_name == :expired_collection
498 # Fixture-finder shorthand doesn't find trashed collections
499 # because they're not in the default scope.
500 c = Collection.find_by_uuid('zzzzz-4zz18-mto52zx1s7sn3ih')
502 c = collections(fixture_name)
504 updates_ok = c.update_attributes(updates)
505 expect_valid = expect[:state] != :invalid
506 assert_equal expect_valid, updates_ok, c.errors.full_messages.to_s
512 assert_not_nil c.trash_at
513 assert_operator c.trash_at, :<=, db_current_time
514 assert_not_nil c.delete_at
515 assert_operator c.delete_at, :>=, min_exp
518 assert_not_nil c.trash_at
519 assert_operator c.trash_at, :>, db_current_time
520 assert_not_nil c.delete_at
521 assert_operator c.delete_at, :>=, c.trash_at
522 # Currently this minimum interval is needed to prevent early
523 # garbage collection:
524 assert_operator c.delete_at, :>=, min_exp
527 assert_nil c.trash_at
528 assert_nil c.delete_at
530 raise "bad expect[:state]==#{expect[:state].inspect} in test case"
536 test 'default trash interval > blob signature ttl' do
537 Rails.configuration.default_trash_lifetime = 86400 * 21 # 3 weeks
538 start = db_current_time
539 act_as_user users(:active) do
540 c = Collection.create!(manifest_text: '', name: 'foo')
541 c.update_attributes!(trash_at: start + 86400.seconds)
542 assert_operator c.delete_at, :>=, start + (86400*22).seconds
543 assert_operator c.delete_at, :<, start + (86400*22 + 30).seconds
546 c = Collection.create!(manifest_text: '', name: 'foo')
547 c.update_attributes!(is_trashed: true)
548 assert_operator c.delete_at, :>=, start + (86400*21).seconds
552 test "find_all_for_docker_image resolves names that look like hashes" do
553 coll_list = Collection.
554 find_all_for_docker_image('a' * 64, nil, [users(:active)])
555 coll_uuids = coll_list.map(&:uuid)
556 assert_includes(coll_uuids, collections(:docker_image).uuid)
559 test "move collections to trash in SweepTrashedObjects" do
560 c = collections(:trashed_on_next_sweep)
561 refute_empty Collection.where('uuid=? and is_trashed=false', c.uuid)
562 assert_raises(ActiveRecord::RecordNotUnique) do
563 act_as_user users(:active) do
564 Collection.create!(owner_uuid: c.owner_uuid,
568 SweepTrashedObjects.sweep_now
569 c = Collection.where('uuid=? and is_trashed=true', c.uuid).first
571 act_as_user users(:active) do
572 assert Collection.create!(owner_uuid: c.owner_uuid,
577 test "delete collections in SweepTrashedObjects" do
578 uuid = 'zzzzz-4zz18-3u1p5umicfpqszp' # deleted_on_next_sweep
579 assert_not_empty Collection.where(uuid: uuid)
580 SweepTrashedObjects.sweep_now
581 assert_empty Collection.where(uuid: uuid)
584 test "delete referring links in SweepTrashedObjects" do
585 uuid = collections(:trashed_on_next_sweep).uuid
586 act_as_system_user do
587 Link.create!(head_uuid: uuid,
588 tail_uuid: system_user_uuid,
589 link_class: 'whatever',
592 past = db_current_time
593 Collection.where(uuid: uuid).
594 update_all(is_trashed: true, trash_at: past, delete_at: past)
595 assert_not_empty Collection.where(uuid: uuid)
596 SweepTrashedObjects.sweep_now
597 assert_empty Collection.where(uuid: uuid)