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 SalvageCollection
7   def self.salvage_collection_arv_put(cmd)
8     file_contents = File.new(cmd.split[-1], "r").gets
9
10     # simulate arv-put error when it is 'user_agreement'
11     if file_contents.include? 'GNU_General_Public_License'
12       raise("Error during arv-put")
13     else
14       ". " +
15       Digest::MD5.hexdigest(TEST_MANIFEST) +
16       " 0:" + TEST_MANIFEST.length.to_s + ":invalid_manifest_text.txt\n"
17     end
18   end
19 end
20
21 class SalvageCollectionMockTest < ActiveSupport::TestCase
22   include SalvageCollection
23
24   setup do
25     set_user_from_auth :admin
26     # arv-put needs ARV env variables
27     ENV['ARVADOS_API_HOST'] = 'unused_by_test'
28     ENV['ARVADOS_API_TOKEN'] = 'unused_by_test'
29   end
30
31   teardown do
32     ENV['ARVADOS_API_HOST'] = ''
33     ENV['ARVADOS_API_TOKEN'] = ''
34   end
35
36   test "salvage test collection" do
37     # create a collection to test salvaging
38     src_collection = Collection.new name: "test collection", manifest_text: TEST_MANIFEST
39     src_collection.save!
40
41     # salvage this collection
42     SalvageCollection.salvage_collection src_collection.uuid, 'test salvage collection - see #6277, #6859'
43
44     # verify the updated src_collection data
45     updated_src_collection = Collection.find_by_uuid src_collection.uuid
46     updated_name = updated_src_collection.name
47     assert_equal true, updated_name.include?(src_collection.name)
48
49     match = updated_name.match /^test collection.*salvaged data at (.*)\)$/
50     assert_not_nil match
51     assert_not_nil match[1]
52     assert_empty updated_src_collection.manifest_text
53
54     # match[1] is the uuid of the new collection created from src_collection's salvaged data
55     # use this to get the new collection and verify
56     new_collection = Collection.find_by_uuid match[1]
57     match = new_collection.name.match /^salvaged from (.*),.*/
58     assert_not_nil match
59     assert_equal src_collection.uuid, match[1]
60
61     # verify the new collection's manifest format
62     match = new_collection.manifest_text.match /^. (.*) (.*):invalid_manifest_text.txt\n. (.*) (.*):salvaged_data/
63     assert_not_nil match
64   end
65
66   test "salvage collection with no uuid required argument" do
67     e = assert_raises RuntimeError do
68       SalvageCollection.salvage_collection nil
69     end
70   end
71
72   test "salvage collection with bogus uuid" do
73     e = assert_raises RuntimeError do
74       SalvageCollection.salvage_collection 'bogus-uuid'
75     end
76     assert_equal "No collection found for bogus-uuid.", e.message
77   end
78
79   test "salvage collection with no env ARVADOS_API_HOST" do
80     e = assert_raises RuntimeError do
81       ENV['ARVADOS_API_HOST'] = ''
82       ENV['ARVADOS_API_TOKEN'] = ''
83       SalvageCollection.salvage_collection collections('user_agreement').uuid
84     end
85     assert_equal "ARVADOS environment variables missing. Please set your admin user credentials as ARVADOS environment variables.", e.message
86   end
87
88   test "salvage collection with error during arv-put" do
89     # try to salvage collection while mimicking error during arv-put
90     e = assert_raises RuntimeError do
91       SalvageCollection.salvage_collection collections('user_agreement').uuid
92     end
93     assert_equal "Error during arv-put", e.message
94   end
95
96   # This test has two invalid locators:
97   #     341dabea2bd78ad0d6fc3f5b926b450e+abc
98   #     341dabea2bd78ad0d6fc3f5b926b450e
99   # These locators should be preserved in salvaged_data
100   test "invalid locators dropped during salvaging" do
101     manifest = ". 341dabea2bd78ad0d6fc3f5b926b450e+abc 341dabea2bd78ad0d6fc3f5b926abcdf 0:85626:brca2-hg19.fa\n. 341dabea2bd78ad0d6fc3f5b926b450e+1000 0:1000:brca-hg19.fa\n . d7321a918923627c972d8f8080c07d29+2000+A22e0a1d9b9bc85c848379d98bedc64238b0b1532@55e076ce 0:2000:brca1-hg19.fa\n"
102
103     # salvage this collection
104     locator_data = SalvageCollection.salvage_collection_locator_data manifest
105     assert_equal true, locator_data[0].size.eql?(4)
106     assert_equal false, locator_data[0].include?("341dabea2bd78ad0d6fc3f5b926b450e+abc")
107     assert_equal true, locator_data[0].include?("341dabea2bd78ad0d6fc3f5b926b450e")
108     assert_equal true, locator_data[0].include?("341dabea2bd78ad0d6fc3f5b926abcdf")
109     assert_equal true, locator_data[0].include?("341dabea2bd78ad0d6fc3f5b926b450e+1000")
110     assert_equal true, locator_data[0].include?("d7321a918923627c972d8f8080c07d29+2000")
111     assert_equal true, locator_data[1].eql?(1000 + 2000)   # size
112   end
113 end