Merge branch '17984-cwl-project-uuid-users' into main
[arvados.git] / services / api / test / tasks / delete_old_container_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_container_logs"
9 Rake::Task.define_task(:environment)
10
11 class DeleteOldContainerLogsTaskTest < ActiveSupport::TestCase
12   TASK_NAME = "db:delete_old_container_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 check_log_existence(test_method, fixture_uuids)
25     uuids_now = Log.where("object_uuid LIKE :pattern AND event_type in ('stdout', 'stderr', 'arv-mount', 'crunch-run', 'crunchstat')", pattern: "%-dz642-%").map(&:uuid)
26     fixture_uuids.each do |expect_uuid|
27       send(test_method, uuids_now, expect_uuid)
28     end
29   end
30
31   test "delete all finished logs" do
32     uuids_to_keep = log_uuids(:stderr_for_running_container,
33                               :crunchstat_for_running_container)
34     uuids_to_clean = log_uuids(:stderr_for_previous_container,
35                                :crunchstat_for_previous_container,
36                                :stderr_for_ancient_container,
37                                :crunchstat_for_ancient_container)
38     run_with_expiry(1)
39     check_log_existence(:assert_includes, uuids_to_keep)
40     check_log_existence(:refute_includes, uuids_to_clean)
41   end
42
43   test "delete old finished logs" do
44     uuids_to_keep = log_uuids(:stderr_for_running_container,
45                               :crunchstat_for_running_container,
46                               :stderr_for_previous_container,
47                               :crunchstat_for_previous_container)
48     uuids_to_clean = log_uuids(:stderr_for_ancient_container,
49                                :crunchstat_for_ancient_container)
50     run_with_expiry(360.days)
51     check_log_existence(:assert_includes, uuids_to_keep)
52     check_log_existence(:refute_includes, uuids_to_clean)
53   end
54 end