6859: add test for salvage_collection
authorradhika <radhika@curoverse.com>
Fri, 14 Aug 2015 18:43:55 +0000 (14:43 -0400)
committerradhika <radhika@curoverse.com>
Fri, 14 Aug 2015 18:43:55 +0000 (14:43 -0400)
services/api/lib/salvage_collection.rb
services/api/test/unit/salvage_collection_test.rb [new file with mode: 0644]

index 8781ec4f0aec54912200b566be42ccddb4c65ea0..e5420f16f77fd91a5b7b7cfc155103cc65e6053d 100755 (executable)
@@ -15,17 +15,22 @@ module SalvageCollection
   require 'tempfile'
   require 'shellwords'
 
-  def self.salvage_collection uuid, reason
+  def self.salvage_collection uuid, reason='salvaged - see #6277, #6859'
     act_as_system_user do
-      if !ENV['ARVADOS_API_TOKEN'] or !ENV['ARVADOS_API_HOST']
-        $stderr.puts "Please set ARVADOS_API_HOST and ARVADOS_API_TOKEN environment variables. Exiting."
+      if !ENV['ARVADOS_API_TOKEN'].present? or !ENV['ARVADOS_API_HOST'].present?
+        $stderr.puts "Please set your admin user credentials as ARVADOS environment variables."
         exit 1
       end
 
+      if !uuid.present?
+        $stderr.puts "Required uuid argument is missing."
+        return false
+      end
+
       src_collection = Collection.find_by_uuid uuid
       if !src_collection
         $stderr.puts "No collection found for #{uuid}. Returning."
-        exit 1
+        return false
       end
 
       begin
@@ -48,11 +53,12 @@ module SalvageCollection
         temp_file.write(src_manifest)
         temp_file.close
         new_manifest = %x(arv-put --as-stream --use-filename invalid_manifest_text.txt #{Shellwords::shellescape(temp_file.path)})
+
         temp_file.unlink
 
         if !new_manifest.present?
           $stderr.puts "arv-put --as-stream failed for #{uuid}"
-          exit 1
+          return false
         end
 
         words = []
@@ -85,7 +91,7 @@ module SalvageCollection
         puts "Created new collection #{new_collection.uuid}"
       rescue => error
         $stderr.puts "Error creating collection for #{uuid}: #{error}"
-        exit 1
+        return false
       end
 
       begin
@@ -97,7 +103,7 @@ module SalvageCollection
         $stderr.puts "Collection #{uuid} emptied and renamed to #{src_collection.name.inspect}."
       rescue => error
         $stderr.puts "Error salvaging collection #{new_collection.uuid}: #{error}"
-        exit 1
+        return false
       end
     end
   end
diff --git a/services/api/test/unit/salvage_collection_test.rb b/services/api/test/unit/salvage_collection_test.rb
new file mode 100644 (file)
index 0000000..4cc5378
--- /dev/null
@@ -0,0 +1,82 @@
+require 'test_helper'
+require 'salvage_collection'
+
+TEST_MANIFEST = ". 341dabea2bd78ad0d6fc3f5b926b450e+85626+Ad391622a17f61e4a254eda85d1ca751c4f368da9@55e076ce 0:85626:brca2-hg19.fa\n. d7321a918923627c972d8f8080c07d29+82570+A22e0a1d9b9bc85c848379d98bedc64238b0b1532@55e076ce 0:82570:brca1-hg19.fa\n"
+
+module Kernel
+  def `(cmd)    # override kernel ` method
+    if cmd.include? 'arv-put'
+      ". " +
+      Digest::MD5.hexdigest(TEST_MANIFEST) +
+      " 0:" + TEST_MANIFEST.length.to_s + ":invalid_manifest_text.txt\n"
+    end
+  end
+
+  def exit code
+    raise "Exit code #{code}" if code != 0
+  end
+end
+
+class SalvageCollectionTest < ActiveSupport::TestCase
+  include SalvageCollection
+
+  setup do
+    set_user_from_auth :admin
+    # arv-put needs ARV env variables
+    ENV['ARVADOS_API_HOST'] = 'unused_by_test'
+    ENV['ARVADOS_API_TOKEN'] = 'unused_by_test'
+  end
+
+  test "salvage test collection" do
+    # create a collection to test salvaging
+    src_collection = Collection.new name: "test collection", manifest_text: TEST_MANIFEST
+    src_collection.save!
+
+    # salvage this collection
+    SalvageCollection.salvage_collection src_collection.uuid, 'test salvage collection - see #6277, #6859'
+
+    # verify the updated src_collection data
+    updated_src_collection = Collection.find_by_uuid src_collection.uuid
+    updated_name = updated_src_collection.name
+    assert_equal true, updated_name.include?(src_collection.name)
+
+    match = updated_name.match /^test collection.*salvaged data at (.*)\)$/
+    assert_not_nil match
+    assert_not_nil match[1]
+    assert_empty updated_src_collection.manifest_text
+
+    # match[1] is the uuid of the new collection created from src_collection's salvaged data
+    # use this to get the new collection and verify
+    new_collection = Collection.find_by_uuid match[1]
+    match = new_collection.name.match /^salvaged from (.*),.*/
+    assert_not_nil match
+    assert_equal src_collection.uuid, match[1]
+
+    # verify the new collection's manifest format
+    match = new_collection.manifest_text.match /^. (.*) (.*):invalid_manifest_text.txt\n. (.*) (.*):salvaged_data/
+    assert_not_nil match
+  end
+
+  test "salvage collection with no uuid required argument" do
+    status = SalvageCollection.salvage_collection nil
+    assert_equal false, status
+  end
+
+  test "salvage collection with bogus uuid" do
+    status = SalvageCollection.salvage_collection 'bogus-uuid'
+    assert_equal false, status
+  end
+
+  test "salvage collection with no env ARVADOS_API_HOST" do
+    exited = false
+    begin
+      ENV['ARVADOS_API_HOST'] = ''
+      ENV['ARVADOS_API_TOKEN'] = ''
+      SalvageCollection.salvage_collection collections('user_agreement').uuid
+    rescue => e
+      assert_equal "Exit code 1", e.message
+      exited = true
+    end
+    assert_equal true, exited
+  end
+end