Skip to content

Commit 11fb01c

Browse files
author
Morgan Haskel
committed
Fix test issues
Remove the dependency on stdlib 4.x puppetlabs/puppetlabs-mysql#574 introduced, add some input validation, and improve test checks.
1 parent ab84a67 commit 11fb01c

2 files changed

Lines changed: 68 additions & 49 deletions

File tree

manifests/db.pp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
validate_re($ensure, '^(present|absent)$',
1717
"${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
1818
$table = "${dbname}.*"
19-
$sql_inputs = join(any2array($sql), ' ')
19+
20+
if !(is_array($sql) or is_string($sql)) {
21+
fail('$sql must be either a string or an array.')
22+
}
23+
24+
$sql_inputs = join([$sql], ' ')
2025

2126
include '::mysql::client'
2227

spec/defines/mysql_db_spec.rb

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,75 @@
11
require 'spec_helper'
22

33
describe 'mysql::db', :type => :define do
4-
let(:facts) {{ :osfamily => 'RedHat' }}
5-
let(:title) { 'test_db' }
6-
7-
let(:params) {
8-
{ 'user' => 'testuser',
9-
'password' => 'testpass',
10-
}
11-
}
12-
13-
it 'should report an error when ensure is not present or absent' do
14-
params.merge!({'ensure' => 'invalid_val'})
15-
expect { subject }.to raise_error(Puppet::Error,
16-
/invalid_val is not supported for ensure\. Allowed values are 'present' and 'absent'\./)
17-
end
4+
on_pe_supported_platforms(PLATFORMS).each do |pe_version,pe_platforms|
5+
pe_platforms.each do |pe_platform,facts|
6+
describe "on #{pe_version} #{pe_platform}" do
7+
let(:facts) { facts }
188

19-
it 'should not notify the import sql exec if no sql script was provided' do
20-
is_expected.to contain_mysql_database('test_db').without_notify
21-
end
9+
let(:title) { 'test_db' }
2210

23-
it 'should subscribe to database if sql script is given' do
24-
params.merge!({'sql' => 'test_sql'})
25-
is_expected.to contain_exec('test_db-import').with_subscribe('Mysql_database[test_db]')
26-
end
11+
let(:params) {
12+
{ 'user' => 'testuser',
13+
'password' => 'testpass',
14+
}
15+
}
2716

28-
it 'should only import sql script on creation if not enforcing' do
29-
params.merge!({'sql' => 'test_sql', 'enforce_sql' => false})
30-
is_expected.to contain_exec('test_db-import').with_refreshonly(true)
31-
end
17+
it 'should report an error when ensure is not present or absent' do
18+
params.merge!({'ensure' => 'invalid_val'})
19+
expect { subject }.to raise_error(Puppet::Error,
20+
/invalid_val is not supported for ensure\. Allowed values are 'present' and 'absent'\./)
21+
end
3222

33-
it 'should import sql script on creation if enforcing' do
34-
params.merge!({'sql' => 'test_sql', 'enforce_sql' => true})
35-
is_expected.to contain_exec('test_db-import').with_refreshonly(false)
36-
end
23+
it 'should not notify the import sql exec if no sql script was provided' do
24+
is_expected.to contain_mysql_database('test_db').without_notify
25+
end
3726

38-
it 'should import sql scripts when more than one is specified' do
39-
params.merge!({'sql' => ['test_sql', 'test_2_sql']})
40-
is_expected.to contain_exec('test_db-import').with_command('cat test_sql test_2_sql | mysql test_db')
41-
end
27+
it 'should subscribe to database if sql script is given' do
28+
params.merge!({'sql' => 'test_sql'})
29+
is_expected.to contain_exec('test_db-import').with_subscribe('Mysql_database[test_db]')
30+
end
4231

43-
it 'should not create database and database user' do
44-
params.merge!({'ensure' => 'absent', 'host' => 'localhost'})
45-
is_expected.to contain_mysql_database('test_db').with_ensure('absent')
46-
is_expected.to contain_mysql_user('testuser@localhost').with_ensure('absent')
47-
end
32+
it 'should only import sql script on creation if not enforcing' do
33+
params.merge!({'sql' => 'test_sql', 'enforce_sql' => false})
34+
is_expected.to contain_exec('test_db-import').with_refreshonly(true)
35+
end
4836

49-
it 'should create with an appropriate collate and charset' do
50-
params.merge!({'charset' => 'utf8', 'collate' => 'utf8_danish_ci'})
51-
is_expected.to contain_mysql_database('test_db').with({
52-
'charset' => 'utf8',
53-
'collate' => 'utf8_danish_ci',
54-
})
55-
end
37+
it 'should import sql script on creation if enforcing' do
38+
params.merge!({'sql' => 'test_sql', 'enforce_sql' => true})
39+
is_expected.to contain_exec('test_db-import').with_refreshonly(false)
40+
is_expected.to contain_exec('test_db-import').with_command("cat test_sql | mysql test_db")
41+
end
42+
43+
it 'should import sql scripts when more than one is specified' do
44+
params.merge!({'sql' => ['test_sql', 'test_2_sql']})
45+
is_expected.to contain_exec('test_db-import').with_command('cat test_sql test_2_sql | mysql test_db')
46+
end
47+
48+
it 'should report an error if sql isn\'t a string or an array' do
49+
params.merge!({'sql' => {'foo' => 'test_sql', 'bar' => 'test_2_sql'}})
50+
expect { subject }.to raise_error(Puppet::Error,
51+
/\$sql must be either a string or an array\./)
52+
end
53+
54+
it 'should not create database and database user' do
55+
params.merge!({'ensure' => 'absent', 'host' => 'localhost'})
56+
is_expected.to contain_mysql_database('test_db').with_ensure('absent')
57+
is_expected.to contain_mysql_user('testuser@localhost').with_ensure('absent')
58+
end
59+
60+
it 'should create with an appropriate collate and charset' do
61+
params.merge!({'charset' => 'utf8', 'collate' => 'utf8_danish_ci'})
62+
is_expected.to contain_mysql_database('test_db').with({
63+
'charset' => 'utf8',
64+
'collate' => 'utf8_danish_ci',
65+
})
66+
end
5667

57-
it 'should use dbname parameter as database name instead of name' do
58-
params.merge!({'dbname' => 'real_db'})
59-
is_expected.to contain_mysql_database('real_db')
68+
it 'should use dbname parameter as database name instead of name' do
69+
params.merge!({'dbname' => 'real_db'})
70+
is_expected.to contain_mysql_database('real_db')
71+
end
72+
end
73+
end
6074
end
6175
end

0 commit comments

Comments
 (0)