This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
rds_spec.rb
162 lines (124 loc) · 4.26 KB
/
rds_spec.rb
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
162
require 'spec_helper'
require 'securerandom'
require 'ipaddr'
require 'faraday'
require 'timeout'
require 'net/http'
require 'pg'
require 'rubygems'
require 'net/http'
require 'test_utils'
describe 'hetzner-rds-postgres' do
# snippet:test_volume_helper
def remove_data_volume
`docker volume rm -f rds_rds-data`
end
def remove_backup_volume
`docker volume rm -f rds_rds-backup`
end
# /snippet:test_volume_helper
def wait_for_server_start(service)
wait_while {
!@compose.logs(service).include? 'database system is ready to accept connections'
}
host, port = @compose.address(service, 5432)
wait_while {
!is_port_open?(host, port)
}
sleep 5
end
def clean_start(service)
@compose.force_shutdown
remove_data_volume
remove_backup_volume
@compose.up(service, detached: true)
host, port = @compose.address(service, 5432)
wait_while {
!@compose.logs(service).include? 'backup command end: completed successfully'
}
wait_while {
!is_port_open?(host, port)
}
return host, port
end
before(:all) do
@compose ||= ComposeWrapper.new('rds/docker-compose.yml')
end
after(:all) do
#@compose.dump_logs
@compose.force_shutdown
end
it 'can connect with user test1 to database test1' do
@compose.up('rds-test1-no-instance-id', detached: true)
wait_while {
!@compose.logs('rds-test1-no-instance-id').include? 'DB_INSTANCE_ID not set or empty, exiting'
}
end
it 'can connect with user test1 to database test1' do
host, port = clean_start('rds-test1')
conn = PG::Connection.new(host, port, '', '', 'test1', 'test1', 'password1')
conn.exec('SELECT version();')
conn.close
end
it "does not allow empty passwords" do
err = assert_raises PG::ConnectionBad do
host, port = clean_start('rds-test1-no-password')
conn = PG::Connection.new(host, port, '', '', 'test1', 'test1', ' ')
conn.exec('SELECT version();')
conn.close
end
end
it 'does not allow empty passwords' do
end
it 'keeps data after restart' do
host, port = clean_start('rds-test1')
conn = PG::Connection.new(host, port, '', '', 'test1', 'test1', 'password1')
conn.exec('CREATE TABLE pets (id SERIAL PRIMARY KEY, name VARCHAR(64) NOT NULL);')
petname = SecureRandom.uuid
conn.exec("INSERT INTO pets (name) VALUES ('#{petname}');")
pets = conn.exec("SELECT * FROM pets;").map { |row| row['name'] }
assert_includes(pets, petname)
conn.close
@compose.kill('rds-test1')
wait_while {
!@compose.logs('rds-test1').include? 'database system is shut down'
}
@compose.rm('rds-test1', force: true)
@compose.up('rds-test1', detached: true)
host, port = @compose.address('rds-test1', 5432)
wait_for_server_start('rds-test1')
conn = PG::Connection.new(host, port, '', '', 'test1', 'test1', 'password1')
pets = conn.exec("SELECT * FROM pets;").map { |row| row['name'] }
assert_includes(pets, petname)
end
it 'restores latest data from backup' do
# snippet:test_restore_setup
host, port = clean_start('rds-test1')
conn = PG::Connection.new(host, port, '', '', 'test1', 'test1', 'password1')
conn.exec('CREATE TABLE pets (id SERIAL PRIMARY KEY, name VARCHAR(64) NOT NULL);')
petname = SecureRandom.uuid
conn.exec("INSERT INTO pets (name) VALUES ('#{petname}');")
pets = conn.exec("SELECT * FROM pets;").map { |row| row['name'] }
assert_includes(pets, petname)
conn.close
# /snippet:test_restore_setup
# snippet:test_restore_destroy
@compose.exec('rds-test1', '/rds/bin/backup.sh')
# stopping instance and remove data volume
@compose.kill('rds-test1')
wait_while {
!@compose.logs('rds-test1').include? 'database system is shut down'
}
@compose.rm('rds-test1', force: true)
remove_data_volume
# /snippet:test_restore_destroy
# snippet:test_restore_verify
@compose.up('rds-test1', detached: true)
host, port = @compose.address('rds-test1', 5432)
wait_for_server_start('rds-test1')
conn = PG::Connection.new(host, port, '', '', 'test1', 'test1', 'password1')
pets = conn.exec("SELECT * FROM pets;").map { |row| row['name'] }
assert_includes(pets, petname)
# /snippet:test_restore_verify
end
end