summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/models/concerns/repository_storage_movable_shared_examples.rb
blob: 5a8388d01df1e6c04430354a5ac4f9c57a217b8c (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
# frozen_string_literal: true

RSpec.shared_examples 'handles repository moves' do
  describe 'associations' do
    it { is_expected.to belong_to(:container) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:container) }
    it { is_expected.to validate_presence_of(:state) }
    it { is_expected.to validate_presence_of(:source_storage_name) }
    it { is_expected.to validate_presence_of(:destination_storage_name) }

    context 'source_storage_name inclusion' do
      subject { build(repository_storage_factory_key, source_storage_name: 'missing') }

      it "does not allow repository storages that don't match a label in the configuration" do
        expect(subject).not_to be_valid
        expect(subject.errors[:source_storage_name].first).to match(/is not included in the list/)
      end
    end

    context 'destination_storage_name inclusion' do
      subject { build(repository_storage_factory_key, destination_storage_name: 'missing') }

      it "does not allow repository storages that don't match a label in the configuration" do
        expect(subject).not_to be_valid
        expect(subject.errors[:destination_storage_name].first).to match(/is not included in the list/)
      end
    end

    context 'container repository read-only' do
      subject { build(repository_storage_factory_key, container: container) }

      it "does not allow the container to be read-only on create" do
        container.update!(repository_read_only: true)

        expect(subject).not_to be_valid
        expect(subject.errors[error_key].first).to match(/is read only/)
      end
    end
  end

  describe 'defaults' do
    context 'destination_storage_name' do
      subject { build(repository_storage_factory_key) }

      it 'picks storage from ApplicationSetting' do
        expect(Gitlab::CurrentSettings).to receive(:pick_repository_storage).and_return('picked').at_least(:once)

        expect(subject.destination_storage_name).to eq('picked')
      end
    end
  end

  describe 'state transitions' do
    before do
      stub_storage_settings('test_second_storage' => { 'path' => 'tmp/tests/extra_storage' })
    end

    context 'when in the default state' do
      subject(:storage_move) { create(repository_storage_factory_key, container: container, destination_storage_name: 'test_second_storage') }

      context 'and transits to scheduled' do
        it 'triggers the corresponding repository storage worker' do
          skip unless repository_storage_worker # TODO remove after https://gitlab.com/gitlab-org/gitlab/-/issues/218991 is implemented
          expect(repository_storage_worker).to receive(:perform_async).with(container.id, 'test_second_storage', storage_move.id)

          storage_move.schedule!

          expect(container).to be_repository_read_only
        end

        context 'when the transition fails' do
          it 'does not trigger ProjectUpdateRepositoryStorageWorker and adds an error' do
            skip unless repository_storage_worker # TODO remove after https://gitlab.com/gitlab-org/gitlab/-/issues/218991 is implemented
            allow(storage_move.container).to receive(:set_repository_read_only!).and_raise(StandardError, 'foobar')
            expect(repository_storage_worker).not_to receive(:perform_async)

            storage_move.schedule!

            expect(storage_move.errors[error_key]).to include('foobar')
          end
        end
      end

      context 'and transits to started' do
        it 'does not allow the transition' do
          expect { storage_move.start! }
            .to raise_error(StateMachines::InvalidTransition)
        end
      end
    end

    context 'when started' do
      subject(:storage_move) { create(repository_storage_factory_key, :started, container: container, destination_storage_name: 'test_second_storage') }

      context 'and transits to replicated' do
        it 'marks the container as writable' do
          storage_move.finish_replication!

          expect(container).not_to be_repository_read_only
        end
      end

      context 'and transits to failed' do
        it 'marks the container as writable' do
          storage_move.do_fail!

          expect(container).not_to be_repository_read_only
        end
      end
    end
  end
end