Merge branch 'master' into 6859-fix-invalid-manifests
[arvados.git] / services / api / test / unit / salvage_collection_test.rb
1 require 'test_helper'
2 require 'salvage_collection'
3
4 TEST_MANIFEST = ". 341dabea2bd78ad0d6fc3f5b926b450e+85626+Ad391622a17f61e4a254eda85d1ca751c4f368da9@55e076ce 0:85626:brca2-hg19.fa\n. d7321a918923627c972d8f8080c07d29+82570+A22e0a1d9b9bc85c848379d98bedc64238b0b1532@55e076ce 0:82570:brca1-hg19.fa\n"
5
6 module Kernel
7   def `(cmd)    # override kernel ` method
8     if cmd.include? 'arv-put'
9       file_contents = file = File.new(cmd.split[-1], "r").gets
10
11       # simulate arv-put error when it is 'user_agreement'
12       if file_contents.include? 'GNU_General_Public_License'
13         return ''
14       else
15         ". " +
16         Digest::MD5.hexdigest(TEST_MANIFEST) +
17         " 0:" + TEST_MANIFEST.length.to_s + ":invalid_manifest_text.txt\n"
18       end
19     end
20   end
21
22   def exit code
23     raise "Exit code #{code}" if code != 0
24   end
25 end
26
27 class SalvageCollectionTest < ActiveSupport::TestCase
28   include SalvageCollection
29
30   setup do
31     set_user_from_auth :admin
32     # arv-put needs ARV env variables
33     ENV['ARVADOS_API_HOST'] = 'unused_by_test'
34     ENV['ARVADOS_API_TOKEN'] = 'unused_by_test'
35   end
36
37   test "salvage test collection" do
38     # create a collection to test salvaging
39     src_collection = Collection.new name: "test collection", manifest_text: TEST_MANIFEST
40     src_collection.save!
41
42     # salvage this collection
43     SalvageCollection.salvage_collection src_collection.uuid, 'test salvage collection - see #6277, #6859'
44
45     # verify the updated src_collection data
46     updated_src_collection = Collection.find_by_uuid src_collection.uuid
47     updated_name = updated_src_collection.name
48     assert_equal true, updated_name.include?(src_collection.name)
49
50     match = updated_name.match /^test collection.*salvaged data at (.*)\)$/
51     assert_not_nil match
52     assert_not_nil match[1]
53     assert_empty updated_src_collection.manifest_text
54
55     # match[1] is the uuid of the new collection created from src_collection's salvaged data
56     # use this to get the new collection and verify
57     new_collection = Collection.find_by_uuid match[1]
58     match = new_collection.name.match /^salvaged from (.*),.*/
59     assert_not_nil match
60     assert_equal src_collection.uuid, match[1]
61
62     # verify the new collection's manifest format
63     match = new_collection.manifest_text.match /^. (.*) (.*):invalid_manifest_text.txt\n. (.*) (.*):salvaged_data/
64     assert_not_nil match
65   end
66
67   test "salvage collection with no uuid required argument" do
68     status = SalvageCollection.salvage_collection nil
69     assert_equal false, status
70   end
71
72   test "salvage collection with bogus uuid" do
73     status = SalvageCollection.salvage_collection 'bogus-uuid'
74     assert_equal false, status
75   end
76
77   test "salvage collection with no env ARVADOS_API_HOST" do
78     exited = false
79     begin
80       ENV['ARVADOS_API_HOST'] = ''
81       ENV['ARVADOS_API_TOKEN'] = ''
82       SalvageCollection.salvage_collection collections('user_agreement').uuid
83     rescue => e
84       assert_equal "Exit code 1", e.message
85       exited = true
86     end
87     assert_equal true, exited
88   end
89
90   test "salvage collection with during arv-put" do
91     # try to salvage collection while mimicking error during arv-put
92     status = SalvageCollection.salvage_collection collections('user_agreement').uuid
93     assert_equal false, status
94   end
95 end