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

database_config_spec.rb « patch « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 73452853050012601f21b423cdf6108cadd72eb9 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Patch::DatabaseConfig do
  it 'module is included' do
    expect(Rails::Application::Configuration).to include(described_class)
  end

  describe '#database_configuration' do
    let(:configuration) { Rails::Application::Configuration.new(Rails.root) }

    before do
      # The `AS::ConfigurationFile` calls `read` in `def initialize`
      # thus we cannot use `expect_next_instance_of`
      # rubocop:disable RSpec/AnyInstanceOf
      expect_any_instance_of(ActiveSupport::ConfigurationFile)
        .to receive(:read).with(Rails.root.join('config/database.yml')).and_return(database_yml)
      # rubocop:enable RSpec/AnyInstanceOf
    end

    shared_examples 'hash containing main: connection name' do
      it 'returns a hash containing only main:' do
        database_configuration = configuration.database_configuration

        expect(database_configuration).to match(
          "production" => { "main" => a_hash_including("adapter") },
          "development" => { "main" => a_hash_including("adapter" => "postgresql") },
          "test" => { "main" => a_hash_including("adapter" => "postgresql") }
        )
      end
    end

    let(:database_yml) do
      <<-EOS
          production:
            main:
              adapter: postgresql
              encoding: unicode
              database: gitlabhq_production
              username: git
              password: "secure password"
              host: localhost

          development:
            main:
              adapter: postgresql
              encoding: unicode
              database: gitlabhq_development
              username: postgres
              password: "secure password"
              host: localhost
              variables:
                statement_timeout: 15s

          test: &test
            main:
              adapter: postgresql
              encoding: unicode
              database: gitlabhq_test
              username: postgres
              password:
              host: localhost
              prepared_statements: false
              variables:
                statement_timeout: 15s
      EOS
    end

    include_examples 'hash containing main: connection name'

    context 'when config/database.yml contains extra configuration through an external command' do
      let(:database_yml) do
        <<-EOS
            production:
              config_command: '/opt/database-config.sh'
              main:
                adapter: postgresql
                encoding: unicode
                database: gitlabhq_production
                username: git
                password: "dummy password"
                host: localhost

            development:
              config_command: '/opt/database-config.sh'
              main:
                adapter: postgresql
                encoding: unicode
                database: gitlabhq_development
                username: postgres
                password: "dummy password"
                host: localhost
                variables:
                  statement_timeout: 15s

            test: &test
              config_command: '/opt/database-config.sh'
              main:
                adapter: postgresql
                encoding: unicode
                database: gitlabhq_test
                username: postgres
                password:
                host: localhost
                prepared_statements: false
                variables:
                  statement_timeout: 15s
        EOS
      end

      context 'when the external command returns valid yaml' do
        before do
          allow(Gitlab::Popen)
            .to receive(:popen)
            .and_return(["---\nmain:\n  password: 'secure password'\n", 0])
        end

        it 'merges the extra configuration' do
          database_configuration = configuration.database_configuration

          expect(database_configuration).to match(
            "production" => { "main" => a_hash_including("password" => "secure password") },
            "development" => { "main" => a_hash_including("password" => "secure password") },
            "test" => { "main" => a_hash_including("password" => "secure password") }
          )
        end
      end

      context 'when the external command returns invalid yaml' do
        before do
          allow(Gitlab::Popen)
            .to receive(:popen)
            .and_return(["---\nmain:\n  password: 'secure password\n", 0])
        end

        it 'raises an error' do
          expect { configuration.database_configuration }
            .to raise_error(
              Gitlab::Patch::DatabaseConfig::CommandExecutionError,
              %r{database.yml: Execution of `/opt/database-config.sh` generated invalid yaml}
            )
        end
      end

      context 'when the external command fails' do
        before do
          allow(Gitlab::Popen).to receive(:popen).and_return(["", 125])
        end

        it 'raises error' do
          expect { configuration.database_configuration }
            .to raise_error(
              Gitlab::Patch::DatabaseConfig::CommandExecutionError,
              %r{database.yml: Execution of `/opt/database-config.sh` failed}
            )
        end
      end
    end
  end
end