Skip to content

Commit 211de4f

Browse files
committed
Add spec tests
This commit adds spec tests for the the changes made in the previous commit.
1 parent 547483f commit 211de4f

File tree

1 file changed

+66
-9
lines changed

1 file changed

+66
-9
lines changed

spec/defines/mysql_db_spec.rb

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,42 @@
1717
'mysql_exec_path' => '' }
1818
end
1919

20+
let(:sql) { [ '/tmp/test.sql' ] }
21+
2022
it 'does not notify the import sql exec if no sql script was provided' do
2123
is_expected.to contain_mysql_database('test_db').without_notify
2224
end
2325

2426
it 'subscribes to database if sql script is given' do
25-
params['sql'] = 'test_sql'
27+
params['sql'] = sql
2628
is_expected.to contain_mysql_database('test_db')
2729
is_expected.to contain_exec('test_db-import').with_subscribe('Mysql_database[test_db]')
2830
end
2931

3032
it 'onlies import sql script on creation if not enforcing' do
31-
params.merge!('sql' => 'test_sql', 'enforce_sql' => false)
33+
params.merge!('sql' => sql, 'enforce_sql' => false)
3234
is_expected.to contain_exec('test_db-import').with_refreshonly(true)
3335
end
3436

3537
it 'imports sql script on creation' do
36-
params.merge!('sql' => 'test_sql', 'enforce_sql' => true)
38+
params.merge!('sql' => sql, 'enforce_sql' => true)
3739
# ' if enforcing #refreshonly'
3840
is_expected.to contain_exec('test_db-import').with_refreshonly(false)
3941
# 'if enforcing #command'
40-
is_expected.to contain_exec('test_db-import').with_command('cat test_sql | mysql test_db')
42+
is_expected.to contain_exec('test_db-import').with_command('cat /tmp/test.sql | mysql test_db')
4143
end
4244

43-
it 'imports sql script with custom command on creation ' do
44-
params.merge!('sql' => 'test_sql', 'enforce_sql' => true, 'import_cat_cmd' => 'zcat')
45+
it 'imports sql script with custom command on creation' do
46+
params.merge!('sql' => sql, 'enforce_sql' => true, 'import_cat_cmd' => 'zcat')
4547
# if enforcing #refreshonly
4648
is_expected.to contain_exec('test_db-import').with_refreshonly(false)
4749
# if enforcing #command
48-
is_expected.to contain_exec('test_db-import').with_command('zcat test_sql | mysql test_db')
50+
is_expected.to contain_exec('test_db-import').with_command('zcat /tmp/test.sql | mysql test_db')
4951
end
5052

5153
it 'imports sql scripts when more than one is specified' do
52-
params['sql'] = ['test_sql', 'test_2_sql']
53-
is_expected.to contain_exec('test_db-import').with_command('cat test_sql test_2_sql | mysql test_db')
54+
params['sql'] = ['/tmp/test.sql', '/tmp/test_2.sql']
55+
is_expected.to contain_exec('test_db-import').with_command('cat /tmp/test.sql /tmp/test_2.sql | mysql test_db')
5456
end
5557

5658
it 'does not create database' do
@@ -79,6 +81,61 @@
7981
params['grant_options'] = ['GRANT']
8082
is_expected.to contain_mysql_grant('testuser@localhost/test_db.*').with_options(['GRANT'])
8183
end
84+
85+
# Invalid file paths
86+
[
87+
'|| ls -la ||',
88+
'|| touch /tmp/foo.txt ||',
89+
'/tmp/foo.txt;echo',
90+
'myPath;',
91+
'\\myPath\\',
92+
'//myPath has spaces//',
93+
'/',
94+
].each do |path|
95+
it "fails when provided '#{path}' as a value to the 'sql' parameter" do
96+
params['sql'] = [path]
97+
is_expected.to raise_error(Puppet::PreformattedError, %r{The file '#{Regexp.escape(path)}' is invalid. A a valid file path is expected.})
98+
end
99+
end
100+
101+
# Valid file paths
102+
[
103+
'/tmp/test.txt',
104+
'/tmp/.test',
105+
'/foo.test',
106+
].each do |path|
107+
it "succeeds when provided '#{path}' as a value to the 'sql' parameter" do
108+
params['sql'] = [path]
109+
is_expected.to contain_exec('test_db-import').with_command("cat #{path} | mysql test_db")
110+
end
111+
end
112+
113+
# Invalid database names
114+
[
115+
'test db',
116+
'test_db;',
117+
'test/db',
118+
'|| ls -la ||',
119+
'|| touch /tmp/foo.txt ||',
120+
].each do |name|
121+
it "fails when provided '#{name}' as a value to the 'name' parameter" do
122+
params['name'] = name
123+
is_expected.to raise_error(Puppet::PreformattedError, %r{The database name '#{name}' is invalid.})
124+
end
125+
end
126+
127+
# Valid database names
128+
[
129+
'test_db',
130+
'testdb',
131+
'test-db',
132+
'TESTDB',
133+
].each do |name|
134+
it "succeeds when the provided '#{name}' as a value to the 'dbname' parameter" do
135+
params['dbname'] = name
136+
is_expected.to contain_mysql_database(name)
137+
end
138+
end
82139
end
83140
end
84141
end

0 commit comments

Comments
 (0)