forked from puppetlabs/puppetlabs-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_backup_spec.rb
187 lines (168 loc) · 5.77 KB
/
mysql_backup_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
require 'spec_helper_acceptance'
require 'puppet'
require 'puppet/util/package'
describe 'mysql::server::backup class' do
def pre_run
apply_manifest("class { 'mysql::server': root_password => 'password' }", :catch_failures => true)
@mysql_version = (on default, 'mysql --version').output.chomp.match(/\d+\.\d+\.\d+/)[0]
end
def version_is_greater_than(version)
return Puppet::Util::Package.versioncmp(@mysql_version, version) > 0
end
context 'should work with no errors' do
it 'when configuring mysql backups' do
pp = <<-EOS
class { 'mysql::server': root_password => 'password' }
mysql::db { [
'backup1',
'backup2'
]:
user => 'backup',
password => 'secret',
}
class { 'mysql::server::backup':
backupuser => 'myuser',
backuppassword => 'mypassword',
backupdir => '/tmp/backups',
backupcompress => true,
postscript => [
'rm -rf /var/tmp/mysqlbackups',
'rm -f /var/tmp/mysqlbackups.done',
'cp -r /tmp/backups /var/tmp/mysqlbackups',
'touch /var/tmp/mysqlbackups.done',
],
execpath => '/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin',
}
EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_failures => true)
end
end
describe 'mysqlbackup.sh' do
it 'should run mysqlbackup.sh with no errors' do
shell("/usr/local/sbin/mysqlbackup.sh") do |r|
expect(r.stderr).to eq("")
end
end
it 'should dump all databases to single file' do
shell('ls -l /tmp/backups/mysql_backup_*-*.sql.bz2 | wc -l') do |r|
expect(r.stdout).to match(/1/)
expect(r.exit_code).to be_zero
end
end
context 'should create one file per database per run' do
it 'executes mysqlbackup.sh a second time' do
shell('sleep 1')
shell('/usr/local/sbin/mysqlbackup.sh')
end
it 'creates at least one backup tarball' do
shell('ls -l /tmp/backups/mysql_backup_*-*.sql.bz2 | wc -l') do |r|
expect(r.stdout).to match(/2/)
expect(r.exit_code).to be_zero
end
end
end
end
context 'with one file per database' do
context 'should work with no errors' do
it 'when configuring mysql backups' do
pp = <<-EOS
class { 'mysql::server': root_password => 'password' }
mysql::db { [
'backup1',
'backup2'
]:
user => 'backup',
password => 'secret',
}
class { 'mysql::server::backup':
backupuser => 'myuser',
backuppassword => 'mypassword',
backupdir => '/tmp/backups',
backupcompress => true,
file_per_database => true,
postscript => [
'rm -rf /var/tmp/mysqlbackups',
'rm -f /var/tmp/mysqlbackups.done',
'cp -r /tmp/backups /var/tmp/mysqlbackups',
'touch /var/tmp/mysqlbackups.done',
],
execpath => '/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin',
}
EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_failures => true)
end
end
describe 'mysqlbackup.sh' do
it 'should run mysqlbackup.sh with no errors without root credentials' do
shell("HOME=/tmp/dontreadrootcredentials /usr/local/sbin/mysqlbackup.sh") do |r|
expect(r.stderr).to eq("")
end
end
it 'should create one file per database' do
['backup1', 'backup2'].each do |database|
shell("ls -l /tmp/backups/mysql_backup_#{database}_*-*.sql.bz2 | wc -l") do |r|
expect(r.stdout).to match(/1/)
expect(r.exit_code).to be_zero
end
end
end
context 'should create one file per database per run' do
it 'executes mysqlbackup.sh a second time' do
shell('sleep 1')
shell('HOME=/tmp/dontreadrootcredentials /usr/local/sbin/mysqlbackup.sh')
end
it 'has one file per database per run' do
['backup1', 'backup2'].each do |database|
shell("ls -l /tmp/backups/mysql_backup_#{database}_*-*.sql.bz2 | wc -l") do |r|
expect(r.stdout).to match(/2/)
expect(r.exit_code).to be_zero
end
end
end
end
end
end
context 'with triggers and routines' do
it 'when configuring mysql backups with triggers and routines' do
pre_run
pp = <<-EOS
class { 'mysql::server': root_password => 'password' }
mysql::db { [
'backup1',
'backup2'
]:
user => 'backup',
password => 'secret',
}
package { 'bzip2':
ensure => present,
}
class { 'mysql::server::backup':
backupuser => 'myuser',
backuppassword => 'mypassword',
backupdir => '/tmp/backups',
backupcompress => true,
file_per_database => true,
include_triggers => #{version_is_greater_than('5.1.5')},
include_routines => true,
postscript => [
'rm -rf /var/tmp/mysqlbackups',
'rm -f /var/tmp/mysqlbackups.done',
'cp -r /tmp/backups /var/tmp/mysqlbackups',
'touch /var/tmp/mysqlbackups.done',
],
execpath => '/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin',
require => Package['bzip2'],
}
EOS
apply_manifest(pp, :catch_failures => true)
end
it 'should run mysqlbackup.sh with no errors' do
shell("/usr/local/sbin/mysqlbackup.sh") do |r|
expect(r.stderr).to eq("")
end
end
end
end