Merge branch '4753-websocket-test-port'
[arvados.git] / services / api / test / tasks / delete_old_job_logs_test.rb
1 require 'test_helper'
2 require 'rake'
3
4 Rake.application.rake_require "tasks/delete_old_job_logs"
5 Rake::Task.define_task(:environment)
6
7 class DeleteOldJobLogsTaskTest < ActiveSupport::TestCase
8   TASK_NAME = "db:delete_old_job_logs"
9
10   def log_uuids(*fixture_names)
11     fixture_names.map { |name| logs(name).uuid }
12   end
13
14   def run_with_expiry(clean_after)
15     Rails.configuration.clean_job_log_rows_after = clean_after
16     Rake::Task[TASK_NAME].reenable
17     Rake.application.invoke_task TASK_NAME
18   end
19
20   def job_stderr_logs
21     Log.where("object_uuid LIKE :pattern AND event_type = :etype",
22               pattern: "_____-8i9sb-_______________",
23               etype: "stderr")
24   end
25
26   def check_existence(test_method, fixture_uuids)
27     uuids_now = job_stderr_logs.map(&:uuid)
28     fixture_uuids.each do |expect_uuid|
29       send(test_method, uuids_now, expect_uuid)
30     end
31   end
32
33   test "delete all logs" do
34     uuids_to_keep = log_uuids(:crunchstat_for_running_job)
35     uuids_to_clean = log_uuids(:crunchstat_for_previous_job,
36                                :crunchstat_for_ancient_job)
37     run_with_expiry(1)
38     check_existence(:assert_includes, uuids_to_keep)
39     check_existence(:refute_includes, uuids_to_clean)
40   end
41
42   test "delete only old logs" do
43     uuids_to_keep = log_uuids(:crunchstat_for_running_job,
44                               :crunchstat_for_previous_job)
45     uuids_to_clean = log_uuids(:crunchstat_for_ancient_job)
46     run_with_expiry(360.days)
47     check_existence(:assert_includes, uuids_to_keep)
48     check_existence(:refute_includes, uuids_to_clean)
49   end
50 end