newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / spec / unit / puppet / provider / postgresql_replication_slot / ruby_spec.rb
1 require 'spec_helper'
2
3 type = Puppet::Type.type(:postgresql_replication_slot)
4 describe type.provider(:ruby) do
5   let(:name) { 'standby' }
6   let(:resource) do
7     type.new({ :name => name, :provider => :ruby }.merge attributes)
8   end
9
10   let(:sql_instances) do
11     "abc |        | physical  |        |          | t      |      |              | 0/3000420
12 def |        | physical  |        |          | t      |      |              | 0/3000420\n"
13   end
14
15   class SuccessStatus
16     def success?
17       true
18     end
19   end
20   let(:success_status) { SuccessStatus.new }
21
22   class FailStatus
23     def success?
24       false
25     end
26   end
27   let(:fail_status) { FailStatus.new }
28
29   let(:provider) { resource.provider }
30
31   context 'when listing instances' do
32     let(:attributes) do { } end
33
34     it 'should list instances' do
35       provider.class.expects(:run_command).with(
36         ['psql', '-t', '-c', 'SELECT * FROM pg_replication_slots;'],
37         'postgres', 'postgres').returns([sql_instances, nil])
38       instances = provider.class.instances
39       expect(instances.size).to eq 2
40       expect(instances[0].name).to eq 'abc'
41       expect(instances[1].name).to eq 'def'
42     end
43   end
44
45   context 'when creating slot' do
46     let(:attributes) do { :ensure => 'present' } end
47
48     context 'when creation works' do
49       it 'should call psql and succeed' do
50         provider.class.expects(:run_command).with(
51           ['psql', '-t', '-c', "SELECT * FROM pg_create_physical_replication_slot('standby');"],
52           'postgres', 'postgres').returns([nil, success_status])
53
54         expect { provider.create }.not_to raise_error
55       end
56     end
57
58     context 'when creation fails' do
59       it 'should call psql and fail' do
60         provider.class.expects(:run_command).with(
61           ['psql', '-t', '-c', "SELECT * FROM pg_create_physical_replication_slot('standby');"],
62           'postgres', 'postgres').returns([nil, fail_status])
63
64         expect { provider.create }.to raise_error(Puppet::Error, /Failed to create replication slot standby:/)
65       end
66     end
67   end
68
69   context 'when destroying slot' do
70     let(:attributes) do { :ensure => 'absent' } end
71
72     context 'when destruction works' do
73       it 'should call psql and succeed' do
74         provider.class.expects(:run_command).with(
75           ['psql', '-t', '-c', "SELECT pg_drop_replication_slot('standby');"],
76           'postgres', 'postgres').returns([nil, success_status])
77
78         expect { provider.destroy }.not_to raise_error
79       end
80     end
81
82     context 'when destruction fails' do
83       it 'should call psql and fail' do
84         provider.class.expects(:run_command).with(
85           ['psql', '-t', '-c', "SELECT pg_drop_replication_slot('standby');"],
86           'postgres', 'postgres').returns([nil, fail_status])
87
88         expect { provider.destroy }.to raise_error(Puppet::Error, /Failed to destroy replication slot standby:/)
89       end
90     end
91   end
92 end