postgresql_spec.rb 35 KB
Newer Older
1
2
require 'chef_helper'

Ian Baum's avatar
Ian Baum committed
3
RSpec.describe 'postgresql 9.2' do
Ian Baum's avatar
Ian Baum committed
4
  let(:chef_run) { ChefSpec::SoloRunner.new(step_into: %w(runit_service postgresql_config)).converge('gitlab::default') }
5
6
7
8
  let(:postgresql_data_dir) { '/var/opt/gitlab/postgresql/data' }
  let(:postgresql_ssl_cert) { File.join(postgresql_data_dir, 'server.crt') }
  let(:postgresql_ssl_key) { File.join(postgresql_data_dir, 'server.key') }
  let(:postgresql_conf) { File.join(postgresql_data_dir, 'postgresql.conf') }
9
  let(:runtime_conf) { '/var/opt/gitlab/postgresql/data/runtime.conf' }
10
11
12
  let(:gitlab_psql_rc) do
    <<-EOF
psql_user='gitlab-psql'
13
psql_group='gitlab-psql'
14
15
16
17
psql_host='/var/opt/gitlab/postgresql'
psql_port='5432'
    EOF
  end
18

19
20
  before do
    allow(Gitlab).to receive(:[]).and_call_original
21
22
23
    allow_any_instance_of(PgHelper).to receive(:version).and_return(PGVersion.new('9.2.18'))
    allow_any_instance_of(PgHelper).to receive(:running_version).and_return(PGVersion.new('9.2.18'))
    allow_any_instance_of(PgHelper).to receive(:database_version).and_return(PGVersion.new('9.2'))
24
  end
25

26
27
  it 'includes the postgresql::bin recipe' do
    expect(chef_run).to include_recipe('postgresql::bin')
28
29
  end

30
31
  it 'includes the postgresql::user recipe' do
    expect(chef_run).to include_recipe('postgresql::user')
32
33
  end

34
35
36
37
38
  it 'creates gitlab-psql-rc' do
    expect(chef_run).to render_file('/opt/gitlab/etc/gitlab-psql-rc')
      .with_content(gitlab_psql_rc)
  end

39
  it_behaves_like 'enabled runit service', 'postgresql', 'root', 'root', 'gitlab-psql', 'gitlab-psql'
40

41
  context 'renders postgresql.conf' do
Stan Hu's avatar
Stan Hu committed
42
    it 'includes runtime.conf in postgresql.conf' do
43
44
      expect(chef_run).to render_file(postgresql_conf)
        .with_content(/include 'runtime.conf'/)
45
    end
46

47
48
    context 'with default settings' do
      it 'correctly sets the shared_preload_libraries default setting' do
49
        expect(chef_run.node['postgresql']['shared_preload_libraries'])
50
          .to be_nil
51

52
53
54
        expect(chef_run).to render_file(postgresql_conf)
          .with_content(/shared_preload_libraries = ''/)
      end
55

56
57
58
59
60
      it 'sets archive settings' do
        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/archive_mode = off/)
      end
61
62
    end

63
64
65
66
    context 'when user settings are set' do
      before do
        stub_gitlab_rb(postgresql: {
                         shared_preload_libraries: 'pg_stat_statements',
67
68
69
                         archive_mode: 'on',
                         username: 'foo',
                         group: 'bar'
70
71
72
                       })
      end

73
74
      it_behaves_like 'enabled runit service', 'postgresql', 'root', 'root', 'foo', 'bar'

75
      it 'correctly sets the shared_preload_libraries setting' do
76
        expect(chef_run.node['postgresql']['shared_preload_libraries'])
77
78
79
80
81
82
83
84
85
86
87
          .to eql('pg_stat_statements')

        expect(chef_run).to render_file(postgresql_conf)
          .with_content(/shared_preload_libraries = 'pg_stat_statements'/)
      end

      it 'sets archive settings' do
        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/archive_mode = on/)
      end
88
    end
89
90

    context 'sets SSL settings' do
91
      it 'enables SSL by default' do
92
        expect(chef_run.node['postgresql']['ssl'])
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
          .to eq('on')

        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/ssl = on/)
      end

      it 'generates a self-signed certificate and key' do
        stub_gitlab_rb(postgresql: { ssl_cert_file: 'certfile', ssl_key_file: 'keyfile' })

        absolute_cert_path = File.join(postgresql_data_dir, 'certfile')
        absolute_key_path = File.join(postgresql_data_dir, 'keyfile')

        expect(chef_run).to create_file(absolute_cert_path).with(
          user: 'gitlab-psql',
          group: 'gitlab-psql',
          mode: 0400
        )

        expect(chef_run).to create_file(absolute_key_path).with(
          user: 'gitlab-psql',
          group: 'gitlab-psql',
          mode: 0400
        )

        expect(chef_run).to render_file(absolute_cert_path)
          .with_content(/-----BEGIN CERTIFICATE-----/)
        expect(chef_run).to render_file(absolute_key_path)
          .with_content(/-----BEGIN RSA PRIVATE KEY-----/)
      end

      it 'disables SSL' do
        stub_gitlab_rb(postgresql: { ssl: 'off' })
126
127
128
129

        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/ssl = off/)
130
131
132

        expect(chef_run).not_to render_file(postgresql_ssl_cert)
        expect(chef_run).not_to render_file(postgresql_ssl_key)
133
134
135
      end

      it 'activates SSL' do
136
        stub_gitlab_rb(postgresql: { ssl_crl_file: 'revoke.crl' })
137
138
139
140

        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/ssl = on/)
141
        expect(chef_run).to render_file(
142
          postgresql_conf
143
        ).with_content(%r{ssl_ciphers = 'HIGH:MEDIUM:\+3DES:!aNULL:!SSLv3:!TLSv1'})
144
145
146
147
148
149
150
151
        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/ssl_cert_file = 'server.crt'/)
        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/ssl_key_file = 'server.key'/)
        expect(chef_run).to render_file(
          postgresql_conf
152
        ).with_content(%r{ssl_ca_file = '/opt/gitlab/embedded/ssl/certs/cacert.pem'})
153
154
155
156
157
158
159
160
161
162
163
164
165
        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/ssl_crl_file = 'revoke.crl'/)
      end

      it 'sets SSL ciphers' do
        stub_gitlab_rb(postgresql: { ssl_ciphers: 'ALL' })

        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/ssl_ciphers = 'ALL'/)
      end
    end
166
167
  end

168
169
170
  context 'renders runtime.conf' do
    context 'with default settings' do
      it 'correctly sets the log_line_prefix default setting' do
171
        expect(chef_run.node['postgresql']['log_line_prefix'])
172
173
174
175
176
          .to be_nil

        expect(chef_run).to render_file(runtime_conf)
          .with_content(/log_line_prefix = ''/)
      end
177

178
179
180
181
182
      it 'does not include log_statement by default' do
        expect(chef_run).not_to render_file(runtime_conf)
          .with_content(/log_statement = /)
      end

183
184
185
186
187
188
189
190
      it 'sets max_standby settings' do
        expect(chef_run).to render_file(
          runtime_conf
        ).with_content(/max_standby_archive_delay = 30s/)
        expect(chef_run).to render_file(
          runtime_conf
        ).with_content(/max_standby_streaming_delay = 30s/)
      end
Marin Jankovski's avatar
Marin Jankovski committed
191

192
193
194
195
196
197
      it 'sets archive settings' do
        expect(chef_run).to render_file(
          runtime_conf
        ).with_content(/archive_command = ''/)
        expect(chef_run).to render_file(
          runtime_conf
198
        ).with_content(/archive_timeout = 0/)
199
      end
200
    end
201

202
203
204
205
    context 'when user settings are set' do
      before do
        stub_gitlab_rb(postgresql: {
                         log_line_prefix: '%a',
206
                         log_statement: 'all',
207
208
209
210
211
212
                         max_standby_archive_delay: '60s',
                         max_standby_streaming_delay: '120s',
                         archive_command: 'command',
                         archive_timeout: '120',
                       })
      end
213

214
      it 'correctly sets the log_line_prefix setting' do
215
        expect(chef_run.node['postgresql']['log_line_prefix'])
216
          .to eql('%a')
217

218
219
220
        expect(chef_run).to render_file(runtime_conf)
          .with_content(/log_line_prefix = '%a'/)
      end
221

222
      it 'correctly sets the log_line_prefix setting' do
223
        expect(chef_run.node['postgresql']['log_statement'])
224
225
226
227
228
229
          .to eql('all')

        expect(chef_run).to render_file(runtime_conf)
          .with_content(/log_statement = 'all'/)
      end

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
      it 'sets max_standby settings' do
        expect(chef_run).to render_file(
          runtime_conf
        ).with_content(/max_standby_archive_delay = 60s/)
        expect(chef_run).to render_file(
          runtime_conf
        ).with_content(/max_standby_streaming_delay = 120s/)
      end

      it 'sets archive settings' do
        expect(chef_run).to render_file(
          runtime_conf
        ).with_content(/archive_command = 'command'/)
        expect(chef_run).to render_file(
          runtime_conf
        ).with_content(/archive_timeout = 120/)
      end
247
    end
248
  end
249

250
251
  context 'version specific settings' do
    it 'sets unix_socket_directory' do
252
      expect(chef_run.node['postgresql']['unix_socket_directory'])
253
        .to eq('/var/opt/gitlab/postgresql')
254
      expect(chef_run.node['postgresql']['unix_socket_directories'])
255
256
        .to eq(nil)
      expect(chef_run).to render_file(
257
        postgresql_conf
258
259
260
261
262
263
264
265
266
267
      ).with_content { |content|
        expect(content).to match(
          /unix_socket_directory = '\/var\/opt\/gitlab\/postgresql'/
        )
        expect(content).not_to match(
          /unix_socket_directories = '\/var\/opt\/gitlab\/postgresql'/
        )
      }
    end

268
    it 'sets checkpoint_segments' do
269
      expect(chef_run.node['postgresql']['checkpoint_segments'])
270
271
        .to eq(10)
      expect(chef_run).to render_file(
272
        runtime_conf
273
274
275
276
277
      ).with_content(/checkpoint_segments = 10/)
    end

    it 'does not set the max_replication_slots setting' do
      expect(chef_run).to render_file(
278
        postgresql_conf
279
      ).with_content { |content|
Balasankar C's avatar
Balasankar C committed
280
        expect(content).not_to match(/max_replication_slots = /)
281
282
283
      }
    end

284
285
    context 'running version differs from data version' do
      before do
286
287
        allow_any_instance_of(PgHelper).to receive(:version).and_return(PGVersion.new('9.6.1'))
        allow_any_instance_of(PgHelper).to receive(:running_version).and_return(PGVersion.new('9.6.1'))
288
289
290
291
        allow(File).to receive(:exists?).and_call_original
        allow(File).to receive(:exists?).with("/var/opt/gitlab/postgresql/data/PG_VERSION").and_return(true)
        allow(Dir).to receive(:glob).and_call_original
        allow(Dir).to receive(:glob).with("/opt/gitlab/embedded/postgresql/9.2*").and_return(
292
          ['/opt/gitlab/embedded/postgresql/9.2']
293
        )
294
        allow(Dir).to receive(:glob).with("/opt/gitlab/embedded/postgresql/9.2/bin/*").and_return(
295
          %w(
296
297
298
            /opt/gitlab/embedded/postgresql/9.2/bin/foo_one
            /opt/gitlab/embedded/postgresql/9.2/bin/foo_two
            /opt/gitlab/embedded/postgresql/9.2/bin/foo_three
299
300
301
302
303
          )
        )
      end

      it 'corrects symlinks to the correct location' do
304
        allow(FileUtils).to receive(:ln_sf).and_return(true)
305
        %w(foo_one foo_two foo_three).each do |pg_bin|
306
          expect(FileUtils).to receive(:ln_sf).with(
307
            "/opt/gitlab/embedded/postgresql/9.2/bin/#{pg_bin}",
308
309
310
            "/opt/gitlab/embedded/bin/#{pg_bin}"
          )
        end
311
        chef_run.ruby_block('Link postgresql bin files to the correct version').block.call
312
      end
313
314
315
316
317

      it 'does not warn the user that a restart is needed' do
        allow_any_instance_of(PgHelper).to receive(:is_running?).and_return(true)
        expect(chef_run).not_to run_ruby_block('warn pending postgresql restart')
      end
318
    end
319
320
321
  end
end

Ian Baum's avatar
Ian Baum committed
322
RSpec.describe 'postgresql 9.6' do
323
  let(:chef_run) { ChefSpec::SoloRunner.new(step_into: %w(runit_service postgresql_config database_objects)).converge('gitlab::default') }
324
325
  let(:postgresql_conf) { '/var/opt/gitlab/postgresql/data/postgresql.conf' }
  let(:runtime_conf) { '/var/opt/gitlab/postgresql/data/runtime.conf' }
326

327
328
  before do
    allow(Gitlab).to receive(:[]).and_call_original
329
330
331
    allow_any_instance_of(PgHelper).to receive(:version).and_return(PGVersion.new('9.6.1'))
    allow_any_instance_of(PgHelper).to receive(:running_version).and_return(PGVersion.new('9.6.1'))
    allow_any_instance_of(PgHelper).to receive(:database_version).and_return(PGVersion.new('9.6'))
332
333
  end

334
335
336
337
338
  it 'does not warn the user that a restart is needed by default' do
    allow_any_instance_of(PgHelper).to receive(:is_running?).and_return(true)
    expect(chef_run).not_to run_ruby_block('warn pending postgresql restart')
  end

339
340
  context 'version specific settings' do
    it 'sets unix_socket_directories' do
341
      expect(chef_run.node['postgresql']['unix_socket_directory'])
342
343
        .to eq('/var/opt/gitlab/postgresql')
      expect(chef_run).to render_file(
344
        postgresql_conf
345
346
347
348
349
350
351
352
353
354
      ).with_content { |content|
        expect(content).to match(
          /unix_socket_directories = '\/var\/opt\/gitlab\/postgresql'/
        )
        expect(content).not_to match(
          /unix_socket_directory = '\/var\/opt\/gitlab\/postgresql'/
        )
      }
    end

355
356
357
358
359
360
    context 'renders postgresql.conf' do
      it 'does not set checkpoint_segments' do
        expect(chef_run).not_to render_file(
          postgresql_conf
        ).with_content(/checkpoint_segments = 10/)
      end
361

362
      it 'sets the max_replication_slots setting' do
363
        expect(chef_run.node['postgresql']['max_replication_slots'])
364
          .to eq(0)
365

366
367
368
369
        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/max_replication_slots = 0/)
      end
370

371
      it 'sets the synchronous_commit setting' do
372
        expect(chef_run.node['postgresql']['synchronous_standby_names'])
373
          .to eq('')
374

375
376
377
378
        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/synchronous_standby_names = ''/)
      end
379

380
381
382
383
384
385
386
387
      it 'disables wal_log_hints setting' do
        expect(chef_run.node['postgresql']['wal_log_hints']).to eq('off')

        expect(chef_run).to render_file(
          postgresql_conf
        ).with_content(/wal_log_hints = off/)
      end

388
389
390
391
392
      it 'does not set dynamic_shared_memory_type by default' do
        expect(chef_run).not_to render_file(
          postgresql_conf
        ).with_content(/^dynamic_shared_memory_type = /)
      end
393

394
395
396
397
398
      it 'sets logging directory' do
        expect(chef_run.node['postgresql']['log_directory'])
          .to eq('/var/log/gitlab/postgresql')

        expect(chef_run).to render_file(
399
          runtime_conf
400
401
402
        ).with_content(%r(^log_directory = '/var/log/gitlab/postgresql'))
      end

403
      it 'sets the max_locks_per_transaction setting' do
404
        expect(chef_run.node['postgresql']['max_locks_per_transaction'])
405
          .to eq(128)
406

407
408
        expect(chef_run).to render_file(
          postgresql_conf
409
        ).with_content(/max_locks_per_transaction = 128/)
410
      end
411

412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
      context 'with geo_secondary_role enabled' do
        before { stub_gitlab_rb(geo_secondary_role: { enable: true }) }

        it 'includes gitlab-geo.conf in postgresql.conf' do
          expect(chef_run).to render_file(postgresql_conf)
            .with_content(/include_if_exists 'gitlab-geo.conf'/)
        end
      end

      context 'with geo_secondary_role disabled' do
        before { stub_gitlab_rb(geo_secondary_role: { enable: false }) }

        it 'does not gitlab-geo.conf in postgresql.conf' do
          expect(chef_run).to render_file(postgresql_conf)
            .with_content { |content|
              expect(content).not_to match('gitlab-geo.conf')
            }
        end
      end

432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
      context 'with custom logging settings set' do
        before do
          stub_gitlab_rb({
                           postgresql: {
                             log_destination: 'csvlog',
                             logging_collector: 'on',
                             log_filename: 'test.log',
                             log_file_mode: '0600',
                             log_truncate_on_rotation: 'on',
                             log_rotation_age: '1d',
                             log_rotation_size: '10MB'
                           }
                         })
        end

        it 'sets logging parameters' do
          expect(chef_run).to render_file(postgresql_conf).with_content { |content|
449
450
451
452
                                expect(content).to match(/logging_collector = on/)
                              }

          expect(chef_run).to render_file(runtime_conf).with_content { |content|
453
454
455
456
457
458
459
460
461
462
            expect(content).to match(/log_destination = 'csvlog'/)
            expect(content).to match(/log_filename = 'test.log'/)
            expect(content).to match(/log_file_mode = 0600/)
            expect(content).to match(/log_truncate_on_rotation = on/)
            expect(content).to match(/log_rotation_age = 1d/)
            expect(content).to match(/log_rotation_size = 10MB/)
          }
        end
      end

463
464
465
466
467
468
469
470
      context 'when dynamic_shared_memory_type is none' do
        before do
          stub_gitlab_rb({
                           postgresql: {
                             dynamic_shared_memory_type: 'none'
                           }
                         })
        end
471

472
473
474
475
476
477
        it 'sets the dynamic_shared_memory_type' do
          expect(chef_run).to render_file(
            postgresql_conf
          ).with_content(/^dynamic_shared_memory_type = none/)
        end
      end
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493

      context 'when wal_log_hints is on' do
        before do
          stub_gitlab_rb({
                           postgresql: {
                             wal_log_hints: 'on'
                           }
                         })
        end

        it 'enables wal_log_hints' do
          expect(chef_run).to render_file(
            postgresql_conf
          ).with_content(/^wal_log_hints = on/)
        end
      end
494
495
    end

496
497
    context 'renders runtime.conf' do
      it 'sets the synchronous_commit setting' do
498
        expect(chef_run.node['postgresql']['synchronous_commit'])
499
          .to eq('on')
500

501
502
503
504
        expect(chef_run).to render_file(
          runtime_conf
        ).with_content(/synchronous_commit = on/)
      end
505

506
      it 'sets the hot_standby_feedback setting' do
507
        expect(chef_run.node['postgresql']['hot_standby_feedback'])
508
          .to eq('off')
509

510
511
512
513
        expect(chef_run).to render_file(
          runtime_conf
        ).with_content(/hot_standby_feedback = off/)
      end
514

515
      it 'sets the random_page_cost setting' do
516
        expect(chef_run.node['postgresql']['random_page_cost'])
517
          .to eq(2.0)
518

519
520
        expect(chef_run).to render_file(
          runtime_conf
521
        ).with_content(/random_page_cost = 2\.0/)
522
      end
523

524
      it 'sets the log_temp_files setting' do
525
        expect(chef_run.node['postgresql']['log_temp_files'])
526
          .to eq(-1)
527

528
529
530
        expect(chef_run).to render_file(
          runtime_conf
        ).with_content(/log_temp_files = -1/)
531
532
      end

533
      it 'sets the log_checkpoints setting' do
534
        expect(chef_run.node['postgresql']['log_checkpoints'])
535
536
          .to eq('off')

537
        expect(chef_run).to render_file(
538
539
          runtime_conf
        ).with_content(/log_checkpoints = off/)
540
      end
541
542

      it 'sets idle_in_transaction_session_timeout' do
543
        expect(chef_run.node['postgresql']['idle_in_transaction_session_timeout'])
544
545
546
547
548
          .to eq('60000')

        expect(chef_run).to render_file(runtime_conf)
          .with_content(/idle_in_transaction_session_timeout = 60000/)
      end
Yorick Peterse's avatar
Yorick Peterse committed
549
550

      it 'sets effective_io_concurrency' do
551
        expect(chef_run.node['postgresql']['effective_io_concurrency'])
Yorick Peterse's avatar
Yorick Peterse committed
552
553
554
555
556
557
558
          .to eq(1)

        expect(chef_run).to render_file(runtime_conf)
          .with_content(/effective_io_concurrency = 1/)
      end

      it 'sets max_worker_processes' do
559
        expect(chef_run.node['postgresql']['max_worker_processes'])
Yorick Peterse's avatar
Yorick Peterse committed
560
561
562
563
564
565
566
          .to eq(8)

        expect(chef_run).to render_file(runtime_conf)
          .with_content(/max_worker_processes = 8/)
      end

      it 'sets max_parallel_workers_per_gather' do
567
        expect(chef_run.node['postgresql']['max_parallel_workers_per_gather'])
Yorick Peterse's avatar
Yorick Peterse committed
568
569
570
571
572
573
574
          .to eq(0)

        expect(chef_run).to render_file(runtime_conf)
          .with_content(/max_parallel_workers_per_gather = 0/)
      end

      it 'sets log_lock_waits' do
575
        expect(chef_run.node['postgresql']['log_lock_waits'])
Yorick Peterse's avatar
Yorick Peterse committed
576
577
578
579
580
581
582
          .to eq(1)

        expect(chef_run).to render_file(runtime_conf)
          .with_content(/log_lock_waits = 1/)
      end

      it 'sets deadlock_timeout' do
583
        expect(chef_run.node['postgresql']['deadlock_timeout'])
Yorick Peterse's avatar
Yorick Peterse committed
584
585
586
587
588
589
590
          .to eq('5s')

        expect(chef_run).to render_file(runtime_conf)
          .with_content(/deadlock_timeout = '5s'/)
      end

      it 'sets track_io_timing' do
591
        expect(chef_run.node['postgresql']['track_io_timing'])
Yorick Peterse's avatar
Yorick Peterse committed
592
593
594
595
596
          .to eq('off')

        expect(chef_run).to render_file(runtime_conf)
          .with_content(/track_io_timing = 'off'/)
      end
597
598

      it 'sets default_statistics_target' do
599
        expect(chef_run.node['postgresql']['default_statistics_target'])
600
601
602
603
604
          .to eq(1000)

        expect(chef_run).to render_file(runtime_conf)
          .with_content(/default_statistics_target = 1000/)
      end
605
606
    end

607
608
609
    it 'notifies reload postgresql when postgresql.conf changes' do
      allow_any_instance_of(OmnibusHelper).to receive(:should_notify?).and_call_original
      allow_any_instance_of(OmnibusHelper).to receive(:should_notify?).with('postgresql').and_return(true)
Ian Baum's avatar
Ian Baum committed
610
611
      allow_any_instance_of(OmnibusHelper).to receive(:service_dir_enabled?).and_call_original
      allow_any_instance_of(OmnibusHelper).to receive(:service_dir_enabled?).with('postgresql').and_return(true)
Ian Baum's avatar
Ian Baum committed
612
613
      expect(chef_run).to create_postgresql_config('gitlab')
      postgresql_config = chef_run.postgresql_config('gitlab')
614
615
616
617
      expect(postgresql_config).to notify('execute[reload postgresql]').to(:run).immediately
      expect(postgresql_config).to notify('execute[start postgresql]').to(:run).immediately
    end

618
619
620
621
622
623
624
625
    it 'notifies restarts postgresql when the postgresql runit run file changes' do
      allow_any_instance_of(OmnibusHelper).to receive(:should_notify?).and_call_original
      allow_any_instance_of(OmnibusHelper).to receive(:should_notify?).with('postgresql').and_return(true)

      psql_service = chef_run.service('postgresql')
      expect(psql_service).not_to subscribe_to('template[/opt/gitlab/sv/postgresql/run]').on(:restart).delayed
    end

626
627
    it 'creates the pg_trgm extension when it is possible' do
      allow_any_instance_of(PgHelper).to receive(:extension_can_be_enabled?).with('pg_trgm', 'gitlabhq_production').and_return(true)
628
      expect(chef_run).to enable_postgresql_extension('pg_trgm')
629
630
    end

631
632
    it 'does not create the pg_trgm extension if it is not possible' do
      allow_any_instance_of(PgHelper).to receive(:extension_can_be_enabled?).with('pg_trgm', 'gitlabhq_production').and_return(false)
633
      expect(chef_run).not_to run_execute('enable pg_trgm extension')
634
635
636
637
638
639
640
641
642
643
    end

    it 'creates the btree_gist extension when it is possible' do
      allow_any_instance_of(PgHelper).to receive(:extension_can_be_enabled?).with('btree_gist', 'gitlabhq_production').and_return(true)
      expect(chef_run).to enable_postgresql_extension('btree_gist')
    end

    it 'does not create the btree_gist extension if it is not possible' do
      allow_any_instance_of(PgHelper).to receive(:extension_can_be_enabled?).with('btree_gist', 'gitlabhq_production').and_return(false)
      expect(chef_run).not_to run_execute('enable btree_gist extension')
644
645
    end

646
647
    context 'running version differs from installed version' do
      before do
648
        allow_any_instance_of(PgHelper).to receive(:version).and_return(PGVersion.new('9.2.18'))
649
650
651
652
653
654
655
656
657
658
659
660
      end

      it 'warns the user that a restart is needed' do
        allow_any_instance_of(PgHelper).to receive(:is_running?).and_return(true)
        expect(chef_run).to run_ruby_block('warn pending postgresql restart')
      end

      it 'does not warns the user that a restart is needed when postgres is stopped' do
        expect(chef_run).not_to run_ruby_block('warn pending postgresql restart')
      end
    end

661
662
    context 'running version differs from data version' do
      before do
663
664
        allow_any_instance_of(PgHelper).to receive(:version).and_return(PGVersion.new('9.2.18'))
        allow_any_instance_of(PgHelper).to receive(:running_version).and_return(PGVersion.new('9.2.18'))
665
666
667
668
        allow(File).to receive(:exists?).and_call_original
        allow(File).to receive(:exists?).with("/var/opt/gitlab/postgresql/data/PG_VERSION").and_return(true)
        allow(Dir).to receive(:glob).and_call_original
        allow(Dir).to receive(:glob).with("/opt/gitlab/embedded/postgresql/9.6*").and_return(
669
          ['/opt/gitlab/embedded/postgresql/9.6']
670
        )
671
        allow(Dir).to receive(:glob).with("/opt/gitlab/embedded/postgresql/9.6/bin/*").and_return(
672
          %w(
673
674
675
            /opt/gitlab/embedded/postgresql/9.6/bin/foo_one
            /opt/gitlab/embedded/postgresql/9.6/bin/foo_two
            /opt/gitlab/embedded/postgresql/9.6/bin/foo_three
676
677
678
679
680
          )
        )
      end

      it 'corrects symlinks to the correct location' do
681
        allow(FileUtils).to receive(:ln_sf).and_return(true)
682
        %w(foo_one foo_two foo_three).each do |pg_bin|
683
          expect(FileUtils).to receive(:ln_sf).with(
684
            "/opt/gitlab/embedded/postgresql/9.6/bin/#{pg_bin}",
685
686
687
            "/opt/gitlab/embedded/bin/#{pg_bin}"
          )
        end
688
        chef_run.ruby_block('Link postgresql bin files to the correct version').block.call
689
      end
690
691
692
693
694

      it 'does not warn the user that a restart is needed' do
        allow_any_instance_of(PgHelper).to receive(:is_running?).and_return(true)
        expect(chef_run).not_to run_ruby_block('warn pending postgresql restart')
      end
695
    end
696
697
698

    context 'old unused data version is present' do
      before do
699
        allow_any_instance_of(PgHelper).to receive(:database_version).and_return(PGVersion.new('9.2'))
700
701
702
703
        allow(File).to receive(:exists?).and_call_original
        allow(File).to receive(:exists?).with("/var/opt/gitlab/postgresql/data/PG_VERSION").and_return(true)
        allow(Dir).to receive(:glob).and_call_original
        allow(Dir).to receive(:glob).with("/opt/gitlab/embedded/postgresql/9.2*").and_return([])
704
705
        allow(Dir).to receive(:glob).with("/opt/gitlab/embedded/postgresql/9.6*").and_return(
          ['/opt/gitlab/embedded/postgresql/9.6']
706
        )
707
        allow(Dir).to receive(:glob).with("/opt/gitlab/embedded/postgresql/9.6/bin/*").and_return(
708
          %w(
709
710
711
            /opt/gitlab/embedded/postgresql/9.6/bin/foo_one
            /opt/gitlab/embedded/postgresql/9.6/bin/foo_two
            /opt/gitlab/embedded/postgresql/9.6/bin/foo_three
712
713
714
715
716
717
718
719
          )
        )
      end

      it 'corrects symlinks to the correct location' do
        allow(FileUtils).to receive(:ln_sf).and_return(true)
        %w(foo_one foo_two foo_three).each do |pg_bin|
          expect(FileUtils).to receive(:ln_sf).with(
720
            "/opt/gitlab/embedded/postgresql/9.6/bin/#{pg_bin}",
721
722
723
            "/opt/gitlab/embedded/bin/#{pg_bin}"
          )
        end
724
        chef_run.ruby_block('Link postgresql bin files to the correct version').block.call
725
726
727
728
729
      end
    end

    context 'the desired postgres version is missing' do
      before do
730
        allow_any_instance_of(PgHelper).to receive(:database_version).and_return(PGVersion.new('9.2.18'))
731
732
733
734
735
736
737
738
739
        allow(File).to receive(:exists?).and_call_original
        allow(File).to receive(:exists?).with("/var/opt/gitlab/postgresql/data/PG_VERSION").and_return(true)
        allow(Dir).to receive(:glob).and_call_original
        allow(Dir).to receive(:glob).with("/opt/gitlab/embedded/postgresql/9.2*").and_return([])
        allow(Dir).to receive(:glob).with("/opt/gitlab/embedded/postgresql/9.6*").and_return([])
      end

      it 'throws an error' do
        expect do
740
          chef_run.ruby_block('Link postgresql bin files to the correct version').block.call
741
742
743
        end.to raise_error(RuntimeError, /Could not find PostgreSQL binaries/)
      end
    end
744
  end
745

746
  context 'postgresql_user resource' do
747
748
749
750
    before do
      stub_gitlab_rb(
        {
          postgresql: {
751
752
            sql_user_password: 'fakepassword',
            sql_replication_password: 'fakepassword'
753
754
755
          }
        }
      )
756
757

      allow_any_instance_of(PgHelper).to receive(:is_standby?).and_return(false)
758
759
760
761
762
    end

    it 'should set a password for sql_user when sql_user_password is set' do
      expect(chef_run).to create_postgresql_user('gitlab').with(password: 'md5fakepassword')
    end
763
764

    it 'should create the gitlab_replicator user with replication permissions' do
765
766
767
768
      expect(chef_run).to create_postgresql_user('gitlab_replicator').with(
        options: %w(replication),
        password: 'md5fakepassword'
      )
769
    end
770
771
772

    context 'when database is a secondary' do
      before do
773
        allow_any_instance_of(PgHelper).to receive(:is_standby?).and_return(true)
774
        allow_any_instance_of(PgHelper).to receive(:replica?).and_return(true)
775
776
777
778
779
780
781
782
783
784
785
      end

      it 'should not create users' do
        expect(chef_run).not_to create_postgresql_user('gitlab')
        expect(chef_run).not_to create_postgresql_user('gitlab_replicator')
      end

      it 'should not activate pg_trgm' do
        expect(chef_run).not_to run_execute('enable pg_trgm extension')
      end
    end
786
  end
787

788
789
790
791
792
793
794
  context 'pg_hba.conf' do
    let(:pg_hba_conf) { '/var/opt/gitlab/postgresql/data/pg_hba.conf' }
    it 'creates a standard pg_hba.conf' do
      expect(chef_run).to render_file(pg_hba_conf)
        .with_content('local   all         all                               peer map=gitlab')
    end

795
796
797
798
799
800
801
802
803
804
805
    it 'prefers hostssl when configured in pg_hba.conf' do
      stub_gitlab_rb(
        postgresql: {
          hostssl: true,
          trust_auth_cidr_addresses: ['127.0.0.1/32']
        }
      )
      expect(chef_run).to render_file(pg_hba_conf)
        .with_content('hostssl    all         all         127.0.0.1/32           trust')
    end

806
807
808
    it 'adds users custom entries to pg_hba.conf' do
      stub_gitlab_rb(
        postgresql: {
809
810
          custom_pg_hba_entries: {
            foo: [
811
812
813
814
815
816
817
              {
                type: 'host',
                database: 'foo',
                user: 'bar',
                cidr: '127.0.0.1/32',
                method: 'trust'
              }
818
819
            ]
          }
820
821
822
823
824
        }
      )
      expect(chef_run).to render_file(pg_hba_conf)
        .with_content('host foo bar 127.0.0.1/32 trust')
    end
825
826
827
828

    it 'notifies postgresql reload' do
      allow_any_instance_of(OmnibusHelper).to receive(:should_notify?).and_call_original
      allow_any_instance_of(OmnibusHelper).to receive(:should_notify?).with('postgresql').and_return(true)
Ian Baum's avatar
Ian Baum committed
829
830
      allow_any_instance_of(OmnibusHelper).to receive(:service_dir_enabled?).and_call_original
      allow_any_instance_of(OmnibusHelper).to receive(:service_dir_enabled?).with('postgresql').and_return(true)
Ian Baum's avatar
Ian Baum committed
831
832
833
      postgresql_config = chef_run.postgresql_config('gitlab')
      expect(postgresql_config).to notify('execute[reload postgresql]').to(:run).immediately
      expect(postgresql_config).to notify('execute[start postgresql]').to(:run).immediately
834
    end
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861

    context 'cert authentication' do
      it 'is disabled by default' do
        expect(chef_run).to render_file(pg_hba_conf).with_content { |content|
          expect(content).to_not match(/cert$/)
        }
      end

      it 'can be enabled' do
        stub_gitlab_rb(
          postgresql: {
            cert_auth_addresses: {
              '1.2.3.4/32' => {
                database: 'fakedatabase',
                user: 'fakeuser'
              },
              'fakehostname' => {
                database: 'anotherfakedatabase',
                user: 'anotherfakeuser'
              },
            }
          }
        )
        expect(chef_run).to render_file(pg_hba_conf).with_content('hostssl fakedatabase fakeuser 1.2.3.4/32 cert')
        expect(chef_run).to render_file(pg_hba_conf).with_content('hostssl anotherfakedatabase anotherfakeuser fakehostname cert')
      end
    end
862
  end
863
864

  it 'creates sysctl files' do
865
    expect(chef_run).to create_gitlab_sysctl('kernel.shmmax').with_value(17179869184)
866
  end
867
end
868

Ian Baum's avatar
Ian Baum committed
869
RSpec.describe 'postgresql::bin' do
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
  let(:chef_run) { ChefSpec::SoloRunner.converge('gitlab::default') }

  before do
    allow(Gitlab). to receive(:[]).and_call_original
  end

  context 'when bundled postgresql is disabled' do
    before do
      stub_gitlab_rb(
        postgresql: {
          enable: false
        }
      )

      allow(File).to receive(:exist?).and_call_original
      allow(File).to receive(:exist?).with('/var/opt/gitlab/postgresql/data/PG_VERSION').and_return(false)

      allow_any_instance_of(PgHelper).to receive(:database_version).and_return(nil)
      version = double("PgHelper", major: 10, minor: 9)
      allow_any_instance_of(PgHelper).to receive(:version).and_return(version)
    end

    it 'still includes the postgresql::bin recipe' do
      expect(chef_run).to include_recipe('postgresql::bin')
    end

    # We do expect the ruby block to run, but nothing to be found
    it "doesn't link any files by default" do
      expect(FileUtils).to_not receive(:ln_sf)
    end

    context "with postgresql['version'] set" do
      before do
        stub_gitlab_rb(
          postgresql: {
            enable: false,
            version: '999'
          }
        )
        allow(Dir).to receive(:glob).and_call_original
        allow(Dir).to receive(:glob).with("/opt/gitlab/embedded/postgresql/999*").and_return(
          %w(
            /opt/gitlab/embedded/postgresql/999
          )
        )
        allow(Dir).to receive(:glob).with("/opt/gitlab/embedded/postgresql/999/bin/*").and_return(
          %w(
            /opt/gitlab/embedded/postgresql/999/bin/foo_one
            /opt/gitlab/embedded/postgresql/999/bin/foo_two
            /opt/gitlab/embedded/postgresql/999/bin/foo_three
          )
        )
      end

924
925
926
927
      it "doesn't print a warning with a valid postgresql version" do
        expect(chef_run).to_not run_ruby_block('check_postgresql_version')
      end

928
929
930
931
932
933
934
935
936
937
938
      it 'links the specified version' do
        allow(FileUtils).to receive(:ln_sf).and_return(true)
        %w(foo_one foo_two foo_three).each do |pg_bin|
          expect(FileUtils).to receive(:ln_sf).with(
            "/opt/gitlab/embedded/postgresql/999/bin/#{pg_bin}",
            "/opt/gitlab/embedded/bin/#{pg_bin}"
          )
        end
        chef_run.ruby_block('Link postgresql bin files to the correct version').block.call
      end
    end
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955

    context "with an invalid version in postgresql['version']" do
      before do
        stub_gitlab_rb(
          postgresql: {
            enable: false,
            version: '888'
          }
        )
        allow(Dir).to receive(:glob).and_call_original
        allow(Dir).to receive(:glob).with('/opt/gitlab/embedded/postgresql/888*').and_return([])
      end

      it 'should print a warning' do
        expect(chef_run).to run_ruby_block('check_postgresql_version')
      end
    end
956
957
  end
end
958

Ian Baum's avatar
Ian Baum committed
959
RSpec.describe 'postgresql dir and homedir' do
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
  let(:chef_run) { ChefSpec::SoloRunner.new(step_into: %w(runit_service postgresql_config)).converge('gitlab::default') }

  before do
    allow(Gitlab).to receive(:[]).and_call_original
  end

  context 'when using default values for directories' do
    it 'creates necessary directories' do
      expect(chef_run).to create_directory('/var/opt/gitlab/postgresql').with(owner: 'gitlab-psql', mode: '0755', recursive: true)
    end
  end

  context 'when using custom values for directories' do
    before do
      stub_gitlab_rb(postgresql: {
                       dir: '/mypgdir',
                       home: '/mypghomedir'
                     })
    end

    it 'creates necessary directories' do
      expect(chef_run).to create_directory('/mypgdir').with(owner: 'gitlab-psql', mode: '0755', recursive: true)
      expect(chef_run).to create_directory('/mypghomedir').with(owner: 'gitlab-psql', mode: '0755', recursive: true)
    end
  end
end