|
| 1 | +require 'spec_helper_acceptance' |
| 2 | + |
| 3 | +describe 'ini_setting resource' do |
| 4 | + after :all do |
| 5 | + shell("rm /tmp/*.ini", :acceptable_exit_codes => [0,1]) |
| 6 | + end |
| 7 | + |
| 8 | + shared_examples 'has_content' do |path,pp,content| |
| 9 | + before :all do |
| 10 | + shell("rm #{path}", :acceptable_exit_codes => [0,1]) |
| 11 | + end |
| 12 | + after :all do |
| 13 | + shell("cat #{path}", :acceptable_exit_codes => [0,1]) |
| 14 | + shell("rm #{path}", :acceptable_exit_codes => [0,1]) |
| 15 | + end |
| 16 | + |
| 17 | + it 'applies the manifest twice with no stderr' do |
| 18 | + expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("") |
| 19 | + expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("") |
| 20 | + end |
| 21 | + |
| 22 | + describe file(path) do |
| 23 | + it { should be_file } |
| 24 | + it { should contain(content) } |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + shared_examples 'has_error' do |path,pp,error| |
| 29 | + before :all do |
| 30 | + shell("rm #{path}", :acceptable_exit_codes => [0,1]) |
| 31 | + end |
| 32 | + after :all do |
| 33 | + shell("cat #{path}", :acceptable_exit_codes => [0,1]) |
| 34 | + shell("rm #{path}", :acceptable_exit_codes => [0,1]) |
| 35 | + end |
| 36 | + |
| 37 | + it 'applies the manifest and gets a failure message' do |
| 38 | + expect(apply_manifest(pp, :expect_failures => true).stderr).to match(error) |
| 39 | + end |
| 40 | + |
| 41 | + describe file(path) do |
| 42 | + it { should_not be_file } |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + describe 'ensure parameter' do |
| 47 | + context '=> present for global and section' do |
| 48 | + pp = <<-EOS |
| 49 | + ini_setting { 'ensure => present for section': |
| 50 | + ensure => present, |
| 51 | + path => '/tmp/ini_setting.ini', |
| 52 | + section => 'one', |
| 53 | + setting => 'two', |
| 54 | + value => 'three', |
| 55 | + } |
| 56 | + ini_setting { 'ensure => present for global': |
| 57 | + ensure => present, |
| 58 | + path => '/tmp/ini_setting.ini', |
| 59 | + section => '', |
| 60 | + setting => 'four', |
| 61 | + value => 'five', |
| 62 | + } |
| 63 | + EOS |
| 64 | + |
| 65 | + it 'applies the manifest twice with no stderr' do |
| 66 | + expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("") |
| 67 | + expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("") |
| 68 | + end |
| 69 | + |
| 70 | + describe file('/tmp/ini_setting.ini') do |
| 71 | + it { should be_file } |
| 72 | + it { should contain("four = five\n[one]\ntwo = three") } |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + context '=> absent for key/value' do |
| 77 | + before :all do |
| 78 | + shell('echo -e "four = five\n[one]\ntwo = three" > /tmp/ini_setting.ini') |
| 79 | + end |
| 80 | + |
| 81 | + pp = <<-EOS |
| 82 | + ini_setting { 'ensure => absent for key/value': |
| 83 | + ensure => absent, |
| 84 | + path => '/tmp/ini_setting.ini', |
| 85 | + section => 'one', |
| 86 | + setting => 'two', |
| 87 | + value => 'three', |
| 88 | + } |
| 89 | + EOS |
| 90 | + |
| 91 | + it 'applies the manifest twice with no stderr' do |
| 92 | + expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("") |
| 93 | + expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("") |
| 94 | + end |
| 95 | + |
| 96 | + describe file('/tmp/ini_setting.ini') do |
| 97 | + it { should be_file } |
| 98 | + it { should contain('four = five') } |
| 99 | + it { should contain('[one]') } |
| 100 | + it { should_not contain('two = three') } |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + context '=> absent for section', :pending => "cannot ensure absent on a section" do |
| 105 | + before :all do |
| 106 | + shell('echo -e "four = five\n[one]\ntwo = three" > /tmp/ini_setting.ini') |
| 107 | + end |
| 108 | + after :all do |
| 109 | + shell("cat /tmp/ini_setting.ini", :acceptable_exit_codes => [0,1]) |
| 110 | + shell("rm /tmp/ini_setting.ini", :acceptable_exit_codes => [0,1]) |
| 111 | + end |
| 112 | + |
| 113 | + pp = <<-EOS |
| 114 | + ini_setting { 'ensure => absent for section': |
| 115 | + ensure => absent, |
| 116 | + path => '/tmp/ini_setting.ini', |
| 117 | + section => 'one', |
| 118 | + } |
| 119 | + EOS |
| 120 | + |
| 121 | + it 'applies the manifest twice with no stderr' do |
| 122 | + expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("") |
| 123 | + expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("") |
| 124 | + end |
| 125 | + |
| 126 | + describe file('/tmp/ini_setting.ini') do |
| 127 | + it { should be_file } |
| 128 | + it { should contain('four = five') } |
| 129 | + it { should_not contain('[one]') } |
| 130 | + it { should_not contain('two = three') } |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + context '=> absent for global' do |
| 135 | + before :all do |
| 136 | + shell('echo -e "four = five\n[one]\ntwo = three" > /tmp/ini_setting.ini') |
| 137 | + end |
| 138 | + after :all do |
| 139 | + shell("cat /tmp/ini_setting.ini", :acceptable_exit_codes => [0,1]) |
| 140 | + shell("rm /tmp/ini_setting.ini", :acceptable_exit_codes => [0,1]) |
| 141 | + end |
| 142 | + |
| 143 | + pp = <<-EOS |
| 144 | + ini_setting { 'ensure => absent for global': |
| 145 | + ensure => absent, |
| 146 | + path => '/tmp/ini_setting.ini', |
| 147 | + section => '', |
| 148 | + setting => 'four', |
| 149 | + value => 'five', |
| 150 | + } |
| 151 | + EOS |
| 152 | + |
| 153 | + it 'applies the manifest twice with no stderr' do |
| 154 | + expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("") |
| 155 | + expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("") |
| 156 | + end |
| 157 | + |
| 158 | + describe file('/tmp/ini_setting.ini') do |
| 159 | + it { should be_file } |
| 160 | + it { should_not contain('four = five') } |
| 161 | + it { should contain('[one]') } |
| 162 | + it { should contain('two = three') } |
| 163 | + end |
| 164 | + end |
| 165 | + end |
| 166 | + |
| 167 | + describe 'section, setting, value parameters' do |
| 168 | + { |
| 169 | + "section => 'test', setting => 'foo', value => 'bar'," => "[test]\nfoo = bar", |
| 170 | + "section => 'more', setting => 'baz', value => 'quux'," => "[more]\nbaz = quux", |
| 171 | + "section => '', setting => 'top', value => 'level'," => "top = level", |
| 172 | + }.each do |parameter_list, content| |
| 173 | + context parameter_list do |
| 174 | + pp = <<-EOS |
| 175 | + ini_setting { "#{parameter_list}": |
| 176 | + ensure => present, |
| 177 | + path => '/tmp/ini_setting.ini', |
| 178 | + #{parameter_list} |
| 179 | + } |
| 180 | + EOS |
| 181 | + |
| 182 | + it_behaves_like 'has_content', '/tmp/ini_setting.ini', pp, content |
| 183 | + end |
| 184 | + end |
| 185 | + |
| 186 | + { |
| 187 | + "section => 'test'," => /setting is a required.+value is a required/, |
| 188 | + "setting => 'foo', value => 'bar'," => /section is a required/, |
| 189 | + "section => 'test', setting => 'foo'," => /value is a required/, |
| 190 | + "section => 'test', value => 'bar'," => /setting is a required/, |
| 191 | + "value => 'bar'," => /section is a required.+setting is a required/, |
| 192 | + "setting => 'foo'," => /section is a required.+value is a required/, |
| 193 | + }.each do |parameter_list, error| |
| 194 | + context parameter_list, :pending => 'no error checking yet' do |
| 195 | + pp = <<-EOS |
| 196 | + ini_setting { "#{parameter_list}": |
| 197 | + ensure => present, |
| 198 | + path => '/tmp/ini_setting.ini', |
| 199 | + #{parameter_list} |
| 200 | + } |
| 201 | + EOS |
| 202 | + |
| 203 | + it_behaves_like 'has_error', '/tmp/ini_setting.ini', pp, error |
| 204 | + end |
| 205 | + end |
| 206 | + end |
| 207 | + |
| 208 | + describe 'path parameter' do |
| 209 | + [ |
| 210 | + "/tmp/one.ini", |
| 211 | + "/tmp/two.ini", |
| 212 | + "/tmp/three.ini", |
| 213 | + ].each do |path| |
| 214 | + context "path => #{path}" do |
| 215 | + pp = <<-EOS |
| 216 | + ini_setting { 'path => #{path}': |
| 217 | + ensure => present, |
| 218 | + section => 'one', |
| 219 | + setting => 'two', |
| 220 | + value => 'three', |
| 221 | + path => '#{path}', |
| 222 | + } |
| 223 | + EOS |
| 224 | + |
| 225 | + it_behaves_like 'has_content', path, pp, "[one]\ntwo = three" |
| 226 | + end |
| 227 | + end |
| 228 | + |
| 229 | + context "path => foo" do |
| 230 | + pp = <<-EOS |
| 231 | + ini_setting { 'path => foo': |
| 232 | + ensure => present, |
| 233 | + section => 'one', |
| 234 | + setting => 'two', |
| 235 | + value => 'three', |
| 236 | + path => 'foo', |
| 237 | + } |
| 238 | + EOS |
| 239 | + |
| 240 | + it_behaves_like 'has_error', 'foo', pp, /must be fully qualified/ |
| 241 | + end |
| 242 | + end |
| 243 | + |
| 244 | + describe 'key_val_separator parameter' do |
| 245 | + { |
| 246 | + "" => "two = three", |
| 247 | + "key_val_separator => '='," => "two=three", |
| 248 | + "key_val_separator => ' = '," => "two = three", |
| 249 | + }.each do |parameter, content| |
| 250 | + context "with \"#{parameter}\" makes \"#{content}\"" do |
| 251 | + pp = <<-EOS |
| 252 | + ini_setting { "with #{parameter} makes #{content}": |
| 253 | + ensure => present, |
| 254 | + section => 'one', |
| 255 | + setting => 'two', |
| 256 | + value => 'three', |
| 257 | + path => '/tmp/key_val_separator.ini', |
| 258 | + #{parameter} |
| 259 | + } |
| 260 | + EOS |
| 261 | + |
| 262 | + it_behaves_like 'has_content', '/tmp/key_val_separator.ini', pp, content |
| 263 | + end |
| 264 | + end |
| 265 | + |
| 266 | + { |
| 267 | + "key_val_separator => ''," => /must contain exactly one/, |
| 268 | + "key_val_separator => ','," => /must contain exactly one/, |
| 269 | + "key_val_separator => ' '," => /must contain exactly one/, |
| 270 | + "key_val_separator => ' == '," => /must contain exactly one/, |
| 271 | + }.each do |parameter, error| |
| 272 | + context "with \"#{parameter}\" raises \"#{error}\"" do |
| 273 | + pp = <<-EOS |
| 274 | + ini_setting { "with #{parameter} raises #{error}": |
| 275 | + ensure => present, |
| 276 | + section => 'one', |
| 277 | + setting => 'two', |
| 278 | + value => 'three', |
| 279 | + path => '/tmp/key_val_separator.ini', |
| 280 | + #{parameter} |
| 281 | + } |
| 282 | + EOS |
| 283 | + |
| 284 | + it_behaves_like 'has_error', '/tmp/key_val_separator.ini', pp, error |
| 285 | + end |
| 286 | + end |
| 287 | + end |
| 288 | +end |
0 commit comments