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 exit code
8     raise "Exit code #{code}" if code == 200
9   end
10 end
11
12 module SalvageCollection
13   def self.salvage_collection_arv_put(temp_file)
14     file_contents = file = File.new(temp_file.path, "r").gets
15
16     # simulate arv-put error when it is 'user_agreement'
17     if file_contents.include? 'GNU_General_Public_License'
18       return ''
19     else
20       ". " +
21       Digest::MD5.hexdigest(TEST_MANIFEST) +
22       " 0:" + TEST_MANIFEST.length.to_s + ":invalid_manifest_text.txt\n"
23     end
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   teardown do
38     ENV['ARVADOS_API_HOST'] = ''
39     ENV['ARVADOS_API_TOKEN'] = ''
40   end
41
42   test "salvage test collection" do
43     # create a collection to test salvaging
44     src_collection = Collection.new name: "test collection", manifest_text: TEST_MANIFEST
45     src_collection.save!
46
47     # salvage this collection
48     SalvageCollection.salvage_collection src_collection.uuid, 'test salvage collection - see #6277, #6859'
49
50     # verify the updated src_collection data
51     updated_src_collection = Collection.find_by_uuid src_collection.uuid
52     updated_name = updated_src_collection.name
53     assert_equal true, updated_name.include?(src_collection.name)
54
55     match = updated_name.match /^test collection.*salvaged data at (.*)\)$/
56     assert_not_nil match
57     assert_not_nil match[1]
58     assert_empty updated_src_collection.manifest_text
59
60     # match[1] is the uuid of the new collection created from src_collection's salvaged data
61     # use this to get the new collection and verify
62     new_collection = Collection.find_by_uuid match[1]
63     match = new_collection.name.match /^salvaged from (.*),.*/
64     assert_not_nil match
65     assert_equal src_collection.uuid, match[1]
66
67     # verify the new collection's manifest format
68     match = new_collection.manifest_text.match /^. (.*) (.*):invalid_manifest_text.txt\n. (.*) (.*):salvaged_data/
69     assert_not_nil match
70   end
71
72   test "salvage collection with no uuid required argument" do
73     status = SalvageCollection.salvage_collection nil
74     assert_equal false, status
75   end
76
77   test "salvage collection with bogus uuid" do
78     status = SalvageCollection.salvage_collection 'bogus-uuid'
79     assert_equal false, status
80   end
81
82   test "salvage collection with no env ARVADOS_API_HOST" do
83     exited = false
84     begin
85       ENV['ARVADOS_API_HOST'] = ''
86       ENV['ARVADOS_API_TOKEN'] = ''
87       SalvageCollection.salvage_collection collections('user_agreement').uuid
88     rescue => e
89       assert_equal "Exit code 200", e.message
90       exited = true
91     end
92     assert_equal true, exited
93   end
94
95   test "salvage collection with error during arv-put" do
96     # try to salvage collection while mimicking error during arv-put
97     status = SalvageCollection.salvage_collection collections('user_agreement').uuid
98     assert_equal false, status
99   end
100 end