Welcome to mirror list, hosted at ThFree Co, Russian Federation.

check_forced_decomposition_spec.rb « initializers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 23fa3de297a2aac175783e9f3aacf0e38b6e10e2 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'check_forced_decomposition initializer', feature_category: :cell do
  subject(:check_forced_decomposition) do
    load Rails.root.join('config/initializers/check_forced_decomposition.rb')
  end

  before do
    stub_env('GITLAB_ALLOW_SEPARATE_CI_DATABASE', nil)
  end

  context 'for production env' do
    before do
      allow(Gitlab).to receive(:dev_or_test_env?).and_return(false)
    end

    context 'for single database' do
      before do
        skip_if_multiple_databases_are_setup
      end

      it { expect { check_forced_decomposition }.not_to raise_error }
    end

    context 'for multiple database' do
      before do
        skip_if_multiple_databases_not_setup
      end

      let(:main_database_config) do
        Rails.application.config.load_database_yaml
          .dig('test', 'main')
          .slice('adapter', 'encoding', 'database', 'username', 'password', 'host')
          .symbolize_keys
      end

      let(:additional_database_config) do
        # Use built-in postgres database
        main_database_config.merge(database: 'postgres')
      end

      around do |example|
        with_reestablished_active_record_base(reconnect: true) do
          with_db_configs(test: test_config) do
            example.run
          end
        end
      end

      context 'when ci and main share the same database' do
        let(:test_config) do
          {
            main: main_database_config,
            ci: additional_database_config.merge(database: main_database_config[:database])
          }
        end

        it { expect { check_forced_decomposition }.not_to raise_error }

        context 'when host is not present' do
          let(:test_config) do
            {
              main: main_database_config.except(:host),
              ci: additional_database_config.merge(database: main_database_config[:database]).except(:host)
            }
          end

          it { expect { check_forced_decomposition }.not_to raise_error }
        end
      end

      context 'when ci and main share the same database but different host' do
        let(:test_config) do
          {
            main: main_database_config,
            ci: additional_database_config.merge(
              database: main_database_config[:database],
              host: 'otherhost.localhost'
            )
          }
        end

        it { expect { check_forced_decomposition }.to raise_error(/Separate CI database is not ready/) }
      end

      context 'when ci and main are different databases' do
        let(:test_config) do
          {
            main: main_database_config,
            ci: additional_database_config
          }
        end

        it { expect { check_forced_decomposition }.to raise_error(/Separate CI database is not ready/) }

        context 'for SaaS', :saas do
          it { expect { check_forced_decomposition }.not_to raise_error }
        end

        context 'when env var GITLAB_ALLOW_SEPARATE_CI_DATABASE is true' do
          before do
            stub_env('GITLAB_ALLOW_SEPARATE_CI_DATABASE', 'true')
          end

          it { expect { check_forced_decomposition }.not_to raise_error }
        end

        context 'when env var GITLAB_ALLOW_SEPARATE_CI_DATABASE is false' do
          before do
            stub_env('GITLAB_ALLOW_SEPARATE_CI_DATABASE', 'false')
          end

          it { expect { check_forced_decomposition }.to raise_error(/Separate CI database is not ready/) }
        end
      end
    end
  end
end