summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images_spec.rb
blob: 8a63673bf38d9e24e9400a771a08d9be49d4de24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::DisableExpirationPoliciesLinkedToNoContainerImages, :migration, schema: 20220326161803 do # rubocop:disable Layout/LineLength
  let_it_be(:projects) { table(:projects) }
  let_it_be(:container_expiration_policies) { table(:container_expiration_policies) }
  let_it_be(:container_repositories) { table(:container_repositories) }
  let_it_be(:namespaces) { table(:namespaces) }

  let!(:namespace) { namespaces.create!(name: 'test', path: 'test') }

  let!(:policy1) { create_expiration_policy(project_id: 1, enabled: true) }
  let!(:policy2) { create_expiration_policy(project_id: 2, enabled: false) }
  let!(:policy3) { create_expiration_policy(project_id: 3, enabled: false) }
  let!(:policy4) { create_expiration_policy(project_id: 4, enabled: true, with_images: true) }
  let!(:policy5) { create_expiration_policy(project_id: 5, enabled: false, with_images: true) }
  let!(:policy6) { create_expiration_policy(project_id: 6, enabled: false) }
  let!(:policy7) { create_expiration_policy(project_id: 7, enabled: true) }
  let!(:policy8) { create_expiration_policy(project_id: 8, enabled: true, with_images: true) }
  let!(:policy9) { create_expiration_policy(project_id: 9, enabled: true) }

  describe '#perform' do
    subject { described_class.new.perform(from_id, to_id) }

    shared_examples 'disabling policies with no images' do
      it 'disables the proper policies' do
        subject

        rows = container_expiration_policies.order(:project_id).to_h do |row|
          [row.project_id, row.enabled]
        end
        expect(rows).to eq(expected_rows)
      end
    end

    context 'the whole range' do
      let(:from_id) { 1 }
      let(:to_id) { 9 }

      it_behaves_like 'disabling policies with no images' do
        let(:expected_rows) do
          {
            1 => false,
            2 => false,
            3 => false,
            4 => true,
            5 => false,
            6 => false,
            7 => false,
            8 => true,
            9 => false
          }
        end
      end
    end

    context 'a range with no policies to disable' do
      let(:from_id) { 2 }
      let(:to_id) { 6 }

      it_behaves_like 'disabling policies with no images' do
        let(:expected_rows) do
          {
            1 => true,
            2 => false,
            3 => false,
            4 => true,
            5 => false,
            6 => false,
            7 => true,
            8 => true,
            9 => true
          }
        end
      end
    end

    context 'a range with only images' do
      let(:from_id) { 4 }
      let(:to_id) { 5 }

      it_behaves_like 'disabling policies with no images' do
        let(:expected_rows) do
          {
            1 => true,
            2 => false,
            3 => false,
            4 => true,
            5 => false,
            6 => false,
            7 => true,
            8 => true,
            9 => true
          }
        end
      end
    end

    context 'a range with a single element' do
      let(:from_id) { 9 }
      let(:to_id) { 9 }

      it_behaves_like 'disabling policies with no images' do
        let(:expected_rows) do
          {
            1 => true,
            2 => false,
            3 => false,
            4 => true,
            5 => false,
            6 => false,
            7 => true,
            8 => true,
            9 => false
          }
        end
      end
    end
  end

  def create_expiration_policy(project_id:, enabled:, with_images: false)
    projects.create!(id: project_id, namespace_id: namespace.id, name: "gitlab-#{project_id}")

    if with_images
      container_repositories.create!(project_id: project_id, name: "image-#{project_id}")
    end

    container_expiration_policies.create!(
      enabled: enabled,
      project_id: project_id
    )
  end

  def enabled_policies
    container_expiration_policies.where(enabled: true)
  end

  def disabled_policies
    container_expiration_policies.where(enabled: false)
  end
end