18004: Fixes a couple of race condition bugs related to caching remote users.
[arvados.git] / services / api / test / tasks / delete_old_job_logs_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6 require 'rake'
7
8 Rake.application.rake_require "tasks/delete_old_job_logs"
9 Rake::Task.define_task(:environment)
10
11 class DeleteOldJobLogsTaskTest < ActiveSupport::TestCase
12   TASK_NAME = "db:delete_old_job_logs"
13
14   def log_uuids(*fixture_names)
15     fixture_names.map { |name| logs(name).uuid }
16   end
17
18   def run_with_expiry(clean_after)
19     Rails.configuration.Containers.Logging.MaxAge = clean_after
20     Rake::Task[TASK_NAME].reenable
21     Rake.application.invoke_task TASK_NAME
22   end
23
24   def job_stderr_logs
25     Log.where("object_uuid LIKE :pattern AND event_type = :etype",
26               pattern: "_____-8i9sb-_______________",
27               etype: "stderr")
28   end
29
30   def check_existence(test_method, fixture_uuids)
31     uuids_now = job_stderr_logs.map(&:uuid)
32     fixture_uuids.each do |expect_uuid|
33       send(test_method, uuids_now, expect_uuid)
34     end
35   end
36
37   test "delete all logs" do
38     uuids_to_keep = log_uuids(:crunchstat_for_running_job)
39     uuids_to_clean = log_uuids(:crunchstat_for_previous_job,
40                                :crunchstat_for_ancient_job)
41     run_with_expiry(1)
42     check_existence(:assert_includes, uuids_to_keep)
43     check_existence(:refute_includes, uuids_to_clean)
44   end
45
46   test "delete only old logs" do
47     uuids_to_keep = log_uuids(:crunchstat_for_running_job,
48                               :crunchstat_for_previous_job)
49     uuids_to_clean = log_uuids(:crunchstat_for_ancient_job)
50     run_with_expiry(360.days)
51     check_existence(:assert_includes, uuids_to_keep)
52     check_existence(:refute_includes, uuids_to_clean)
53   end
54 end