|
17 | 17 | 'mysql_exec_path' => '' }
|
18 | 18 | end
|
19 | 19 |
|
| 20 | + let(:sql) { [ '/tmp/test.sql' ] } |
| 21 | + |
20 | 22 | it 'does not notify the import sql exec if no sql script was provided' do
|
21 | 23 | is_expected.to contain_mysql_database('test_db').without_notify
|
22 | 24 | end
|
23 | 25 |
|
24 | 26 | it 'subscribes to database if sql script is given' do
|
25 |
| - params['sql'] = 'test_sql' |
| 27 | + params['sql'] = sql |
26 | 28 | is_expected.to contain_mysql_database('test_db')
|
27 | 29 | is_expected.to contain_exec('test_db-import').with_subscribe('Mysql_database[test_db]')
|
28 | 30 | end
|
29 | 31 |
|
30 | 32 | 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) |
32 | 34 | is_expected.to contain_exec('test_db-import').with_refreshonly(true)
|
33 | 35 | end
|
34 | 36 |
|
35 | 37 | it 'imports sql script on creation' do
|
36 |
| - params.merge!('sql' => 'test_sql', 'enforce_sql' => true) |
| 38 | + params.merge!('sql' => sql, 'enforce_sql' => true) |
37 | 39 | # ' if enforcing #refreshonly'
|
38 | 40 | is_expected.to contain_exec('test_db-import').with_refreshonly(false)
|
39 | 41 | # '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') |
41 | 43 | end
|
42 | 44 |
|
43 | 45 | it 'imports sql script with custom command on creation ' do
|
44 |
| - params.merge!('sql' => 'test_sql', 'enforce_sql' => true, 'import_cat_cmd' => 'zcat') |
| 46 | + params.merge!('sql' => sql, 'enforce_sql' => true, 'use_zcat' => true) |
45 | 47 | # if enforcing #refreshonly
|
46 | 48 | is_expected.to contain_exec('test_db-import').with_refreshonly(false)
|
47 | 49 | # 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') |
49 | 51 | end
|
50 | 52 |
|
51 | 53 | 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') |
54 | 56 | end
|
55 | 57 |
|
56 | 58 | it 'does not create database' do
|
|
79 | 81 | params['grant_options'] = ['GRANT']
|
80 | 82 | is_expected.to contain_mysql_grant('testuser@localhost/test_db.*').with_options(['GRANT'])
|
81 | 83 | 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 |
82 | 139 | end
|
83 | 140 | end
|
84 | 141 | end
|
0 commit comments