21703: Improve test failure reporting.
[arvados.git] / services / api / test / functional / sys_controller_test.rb
index e13d702983fe3c372debf3b55bc7b9156e44fe84..c3f13cf4b8e575b7f4cc088e011585a6cbc53c11 100644 (file)
@@ -91,12 +91,22 @@ class SysControllerTest < ActionController::TestCase
     assert_not_empty Group.where('uuid=? and is_trashed=true', p.uuid)
   end
 
+  test "trash_sweep - role groups are deleted" do
+    p = groups(:trashed_role_on_next_sweep)
+    assert_empty Group.where('uuid=? and is_trashed=true', p.uuid)
+    assert_not_empty Link.where(uuid: links(:foo_file_readable_by_soon_to_be_trashed_role).uuid)
+    authorize_with :admin
+    post :trash_sweep
+    assert_response :success
+    assert_empty Group.where(uuid: p.uuid)
+    assert_empty Link.where(uuid: links(:foo_file_readable_by_soon_to_be_trashed_role).uuid)
+  end
+
   test "trash_sweep - delete projects and their contents" do
     g_foo = groups(:trashed_project)
     g_bar = groups(:trashed_subproject)
     g_baz = groups(:trashed_subproject3)
     col = collections(:collection_in_trashed_subproject)
-    job = jobs(:job_in_trashed_project)
     cr = container_requests(:cr_in_trashed_project)
     # Save how many objects were before the sweep
     user_nr_was = User.all.length
@@ -104,15 +114,15 @@ class SysControllerTest < ActionController::TestCase
     group_nr_was = Group.where('group_class<>?', 'project').length
     project_nr_was = Group.where(group_class: 'project').length
     cr_nr_was = ContainerRequest.all.length
-    job_nr_was = Job.all.length
     assert_not_empty Group.where(uuid: g_foo.uuid)
     assert_not_empty Group.where(uuid: g_bar.uuid)
     assert_not_empty Group.where(uuid: g_baz.uuid)
     assert_not_empty Collection.where(uuid: col.uuid)
-    assert_not_empty Job.where(uuid: job.uuid)
     assert_not_empty ContainerRequest.where(uuid: cr.uuid)
 
     authorize_with :admin
+    Group.find_by_uuid(g_foo.uuid).update!(delete_at: Time.now - 1.second)
+
     post :trash_sweep
     assert_response :success
 
@@ -120,16 +130,45 @@ class SysControllerTest < ActionController::TestCase
     assert_empty Group.where(uuid: g_bar.uuid)
     assert_empty Group.where(uuid: g_baz.uuid)
     assert_empty Collection.where(uuid: col.uuid)
-    assert_empty Job.where(uuid: job.uuid)
     assert_empty ContainerRequest.where(uuid: cr.uuid)
     # No unwanted deletions should have happened
     assert_equal user_nr_was, User.all.length
     assert_equal coll_nr_was-2,        # collection_in_trashed_subproject
                  Collection.all.length # & deleted_on_next_sweep collections
-    assert_equal group_nr_was, Group.where('group_class<>?', 'project').length
+    assert_equal group_nr_was-1,       # trashed_role_on_next_sweep
+                 Group.where('group_class<>?', 'project').length
     assert_equal project_nr_was-3, Group.where(group_class: 'project').length
     assert_equal cr_nr_was-1, ContainerRequest.all.length
-    assert_equal job_nr_was-1, Job.all.length
   end
 
+  test "trash_sweep - delete unused uuid_locks" do
+    uuid_active = "zzzzz-zzzzz-uuidlockstest11"
+    uuid_inactive = "zzzzz-zzzzz-uuidlockstest00"
+
+    ready = Queue.new
+    insertsql = "INSERT INTO uuid_locks (uuid) VALUES ($1) ON CONFLICT (uuid) do UPDATE SET n = uuid_locks.n+1"
+    url = ENV["DATABASE_URL"].sub(/\?.*/, '')
+    Thread.new do
+      conn = PG::Connection.new(url)
+      conn.exec_params(insertsql, [uuid_active])
+      conn.exec_params(insertsql, [uuid_inactive])
+      conn.transaction do |conn|
+        conn.exec_params(insertsql, [uuid_active])
+        ready << true
+        # If we keep this transaction open while trash_sweep runs, the
+        # uuid_active row shouldn't get deleted.
+        sleep 10
+      rescue
+        # Unblock main thread
+        ready << false
+        raise
+      end
+    end
+    assert_equal true, ready.pop
+    authorize_with :admin
+    post :trash_sweep
+    rows = ActiveRecord::Base.connection.exec_query("SELECT uuid FROM uuid_locks ORDER BY uuid", "", []).rows
+    assert_includes(rows, [uuid_active], "row with active lock (still held by thread) should not have been deleted")
+    refute_includes(rows, [uuid_inactive], "row with inactive lock should have been deleted")
+  end
 end