| 1 |
require 'spec_helper' |
| 2 |
require 'puppet/util/package' |
| 3 |
|
| 4 |
describe Puppet::Type.type(:augeas).provider(:augeas) do |
| 5 |
let(:resource) do |
| 6 |
Puppet::Type.type(:augeas).new( |
| 7 |
name: 'test', |
| 8 |
root: my_fixture_dir, |
| 9 |
provider: :augeas, |
| 10 |
) |
| 11 |
end |
| 12 |
|
| 13 |
let(:provider) do |
| 14 |
resource.provider |
| 15 |
end |
| 16 |
|
| 17 |
let(:logs) do |
| 18 |
# rubocop:disable RSpec/InstanceVariable |
| 19 |
@logs |
| 20 |
# rubocop:enable RSpec/InstanceVariable |
| 21 |
end |
| 22 |
|
| 23 |
after(:each) do |
| 24 |
provider.close_augeas |
| 25 |
end |
| 26 |
|
| 27 |
def my_fixture_dir |
| 28 |
File.expand_path(File.join(File.dirname(__FILE__), '../../../fixtures/unit/provider/augeas/augeas')) |
| 29 |
end |
| 30 |
|
| 31 |
def tmpfile(name) |
| 32 |
Puppet::FileSystem.expand_path(make_tmpname(name, nil).encode(Encoding::UTF_8), Dir.tmpdir) |
| 33 |
end |
| 34 |
|
| 35 |
# Copied from ruby 2.4 source |
| 36 |
def make_tmpname((prefix, suffix), n) |
| 37 |
prefix = (String.try_convert(prefix) || |
| 38 |
raise(ArgumentError, "unexpected prefix: #{prefix.inspect}")) |
| 39 |
suffix &&= (String.try_convert(suffix) || |
| 40 |
raise(ArgumentError, "unexpected suffix: #{suffix.inspect}")) |
| 41 |
t = Time.now.strftime('%Y%m%d') |
| 42 |
path = "#{prefix}#{t}-#{$PROCESS_ID}-#{rand(0x100000000).to_s(36)}".dup |
| 43 |
path << "-#{n}" if n |
| 44 |
path << suffix if suffix |
| 45 |
path |
| 46 |
end |
| 47 |
|
| 48 |
describe 'command parsing' do |
| 49 |
it 'ignores nil values when parsing commands' do |
| 50 |
commands = [nil, 'set Jar/Jar Binks'] |
| 51 |
tokens = provider.parse_commands(commands) |
| 52 |
expect(tokens.size).to eq(1) |
| 53 |
expect(tokens[0].size).to eq(3) |
| 54 |
expect(tokens[0][0]).to eq('set') |
| 55 |
expect(tokens[0][1]).to eq('Jar/Jar') |
| 56 |
expect(tokens[0][2]).to eq('Binks') |
| 57 |
end |
| 58 |
|
| 59 |
it 'breaks apart a single line into three tokens and clean up the context' do |
| 60 |
resource[:context] = '/context' |
| 61 |
tokens = provider.parse_commands('set Jar/Jar Binks') |
| 62 |
expect(tokens.size).to eq(1) |
| 63 |
expect(tokens[0].size).to eq(3) |
| 64 |
expect(tokens[0][0]).to eq('set') |
| 65 |
expect(tokens[0][1]).to eq('/context/Jar/Jar') |
| 66 |
expect(tokens[0][2]).to eq('Binks') |
| 67 |
end |
| 68 |
|
| 69 |
it 'breaks apart a multiple line into six tokens' do |
| 70 |
tokens = provider.parse_commands("set /Jar/Jar Binks\nrm anakin") |
| 71 |
expect(tokens.size).to eq(2) |
| 72 |
expect(tokens[0].size).to eq(3) |
| 73 |
expect(tokens[1].size).to eq(2) |
| 74 |
expect(tokens[0][0]).to eq('set') |
| 75 |
expect(tokens[0][1]).to eq('/Jar/Jar') |
| 76 |
expect(tokens[0][2]).to eq('Binks') |
| 77 |
expect(tokens[1][0]).to eq('rm') |
| 78 |
expect(tokens[1][1]).to eq('anakin') |
| 79 |
end |
| 80 |
|
| 81 |
it 'strips whitespace and ignore blank lines' do |
| 82 |
tokens = provider.parse_commands(" set /Jar/Jar Binks \t\n \n\n rm anakin ") |
| 83 |
expect(tokens.size).to eq(2) |
| 84 |
expect(tokens[0].size).to eq(3) |
| 85 |
expect(tokens[1].size).to eq(2) |
| 86 |
expect(tokens[0][0]).to eq('set') |
| 87 |
expect(tokens[0][1]).to eq('/Jar/Jar') |
| 88 |
expect(tokens[0][2]).to eq('Binks') |
| 89 |
expect(tokens[1][0]).to eq('rm') |
| 90 |
expect(tokens[1][1]).to eq('anakin') |
| 91 |
end |
| 92 |
|
| 93 |
it 'handles arrays' do |
| 94 |
resource[:context] = '/foo/' |
| 95 |
commands = ['set /Jar/Jar Binks', 'rm anakin'] |
| 96 |
tokens = provider.parse_commands(commands) |
| 97 |
expect(tokens.size).to eq(2) |
| 98 |
expect(tokens[0].size).to eq(3) |
| 99 |
expect(tokens[1].size).to eq(2) |
| 100 |
expect(tokens[0][0]).to eq('set') |
| 101 |
expect(tokens[0][1]).to eq('/Jar/Jar') |
| 102 |
expect(tokens[0][2]).to eq('Binks') |
| 103 |
expect(tokens[1][0]).to eq('rm') |
| 104 |
expect(tokens[1][1]).to eq('/foo/anakin') |
| 105 |
end |
| 106 |
|
| 107 |
# This is not supported in the new parsing class |
| 108 |
# it "should concat the last values" do |
| 109 |
# provider = provider_class.new |
| 110 |
# tokens = provider.parse_commands("set /Jar/Jar Binks is my copilot") |
| 111 |
# tokens.size.should == 1 |
| 112 |
# tokens[0].size.should == 3 |
| 113 |
# tokens[0][0].should == "set" |
| 114 |
# tokens[0][1].should == "/Jar/Jar" |
| 115 |
# tokens[0][2].should == "Binks is my copilot" |
| 116 |
# end |
| 117 |
|
| 118 |
it 'accepts spaces in the value and single ticks' do |
| 119 |
resource[:context] = '/foo/' |
| 120 |
tokens = provider.parse_commands("set JarJar 'Binks is my copilot'") |
| 121 |
expect(tokens.size).to eq(1) |
| 122 |
expect(tokens[0].size).to eq(3) |
| 123 |
expect(tokens[0][0]).to eq('set') |
| 124 |
expect(tokens[0][1]).to eq('/foo/JarJar') |
| 125 |
expect(tokens[0][2]).to eq('Binks is my copilot') |
| 126 |
end |
| 127 |
|
| 128 |
it 'accepts spaces in the value and double ticks' do |
| 129 |
resource[:context] = '/foo/' |
| 130 |
tokens = provider.parse_commands('set /JarJar "Binks is my copilot"') |
| 131 |
expect(tokens.size).to eq(1) |
| 132 |
expect(tokens[0].size).to eq(3) |
| 133 |
expect(tokens[0][0]).to eq('set') |
| 134 |
expect(tokens[0][1]).to eq('/JarJar') |
| 135 |
expect(tokens[0][2]).to eq('Binks is my copilot') |
| 136 |
end |
| 137 |
|
| 138 |
it 'accepts mixed ticks' do |
| 139 |
resource[:context] = '/foo/' |
| 140 |
tokens = provider.parse_commands('set JarJar "Some \'Test\'"') |
| 141 |
expect(tokens.size).to eq(1) |
| 142 |
expect(tokens[0].size).to eq(3) |
| 143 |
expect(tokens[0][0]).to eq('set') |
| 144 |
expect(tokens[0][1]).to eq('/foo/JarJar') |
| 145 |
expect(tokens[0][2]).to eq("Some \'Test\'") |
| 146 |
end |
| 147 |
|
| 148 |
it 'handles predicates with literals' do |
| 149 |
resource[:context] = '/foo/' |
| 150 |
tokens = provider.parse_commands("rm */*[module='pam_console.so']") |
| 151 |
expect(tokens).to eq([['rm', "/foo/*/*[module='pam_console.so']"]]) |
| 152 |
end |
| 153 |
|
| 154 |
it 'handles whitespace in predicates' do |
| 155 |
resource[:context] = '/foo/' |
| 156 |
tokens = provider.parse_commands("ins 42 before /files/etc/hosts/*/ipaddr[ . = '127.0.0.1' ]") |
| 157 |
expect(tokens).to eq([['ins', '42', 'before', "/files/etc/hosts/*/ipaddr[ . = '127.0.0.1' ]"]]) |
| 158 |
end |
| 159 |
|
| 160 |
it 'handles multiple predicates' do |
| 161 |
resource[:context] = '/foo/' |
| 162 |
tokens = provider.parse_commands("clear pam.d/*/*[module = 'system-auth'][type = 'account']") |
| 163 |
expect(tokens).to eq([['clear', "/foo/pam.d/*/*[module = 'system-auth'][type = 'account']"]]) |
| 164 |
end |
| 165 |
|
| 166 |
it 'handles nested predicates' do |
| 167 |
resource[:context] = '/foo/' |
| 168 |
args = ['clear', "/foo/pam.d/*/*[module[ ../type = 'type] = 'system-auth'][type[last()] = 'account']"] |
| 169 |
tokens = provider.parse_commands(args.join(' ')) |
| 170 |
expect(tokens).to eq([args]) |
| 171 |
end |
| 172 |
|
| 173 |
it 'handles escaped doublequotes in doublequoted string' do |
| 174 |
resource[:context] = '/foo/' |
| 175 |
tokens = provider.parse_commands("set /foo \"''\\\"''\"") |
| 176 |
expect(tokens).to eq([['set', '/foo', "''\"''"]]) |
| 177 |
end |
| 178 |
|
| 179 |
it 'preserves escaped single quotes in double quoted strings' do |
| 180 |
resource[:context] = '/foo/' |
| 181 |
tokens = provider.parse_commands("set /foo \"\\'\"") |
| 182 |
expect(tokens).to eq([['set', '/foo', "\\'"]]) |
| 183 |
end |
| 184 |
|
| 185 |
it 'allows escaped spaces and brackets in paths' do |
| 186 |
resource[:context] = '/foo/' |
| 187 |
args = ['set', '/white\\ space/\\[section', 'value'] |
| 188 |
tokens = provider.parse_commands(args.join(" \t ")) |
| 189 |
expect(tokens).to eq([args]) |
| 190 |
end |
| 191 |
|
| 192 |
it 'allows single quoted escaped spaces in paths' do |
| 193 |
resource[:context] = '/foo/' |
| 194 |
args = ['set', "'/white\\ space/key'", 'value'] |
| 195 |
tokens = provider.parse_commands(args.join(" \t ")) |
| 196 |
expect(tokens).to eq([['set', '/white\\ space/key', 'value']]) |
| 197 |
end |
| 198 |
|
| 199 |
it 'allows double quoted escaped spaces in paths' do |
| 200 |
resource[:context] = '/foo/' |
| 201 |
args = ['set', '"/white\\ space/key"', 'value'] |
| 202 |
tokens = provider.parse_commands(args.join(" \t ")) |
| 203 |
expect(tokens).to eq([['set', '/white\\ space/key', 'value']]) |
| 204 |
end |
| 205 |
|
| 206 |
it 'removes trailing slashes' do |
| 207 |
resource[:context] = '/foo/' |
| 208 |
tokens = provider.parse_commands('set foo/ bar') |
| 209 |
expect(tokens).to eq([['set', '/foo/foo', 'bar']]) |
| 210 |
end |
| 211 |
end |
| 212 |
|
| 213 |
describe 'get filters' do |
| 214 |
let(:augeas) { stub('augeas', get: 'value') } |
| 215 |
|
| 216 |
before(:each) do |
| 217 |
augeas.stubs('close') |
| 218 |
provider.aug = augeas |
| 219 |
end |
| 220 |
|
| 221 |
it 'returns false for a = nonmatch' do |
| 222 |
command = ['get', 'fake value', '==', 'value'] |
| 223 |
expect(provider.process_get(command)).to eq(true) |
| 224 |
end |
| 225 |
|
| 226 |
it 'returns true for a != match' do |
| 227 |
command = ['get', 'fake value', '!=', 'value'] |
| 228 |
expect(provider.process_get(command)).to eq(false) |
| 229 |
end |
| 230 |
|
| 231 |
it 'returns true for a =~ match' do |
| 232 |
command = ['get', 'fake value', '=~', 'val*'] |
| 233 |
expect(provider.process_get(command)).to eq(true) |
| 234 |
end |
| 235 |
|
| 236 |
it 'returns false for a == nonmatch' do |
| 237 |
command = ['get', 'fake value', '=~', 'num*'] |
| 238 |
expect(provider.process_get(command)).to eq(false) |
| 239 |
end |
| 240 |
end |
| 241 |
|
| 242 |
describe 'values filters' do |
| 243 |
let(:augeas) { stub('augeas', match: ['set', 'of', 'values']) } |
| 244 |
|
| 245 |
before(:each) do |
| 246 |
augeas.stubs(:get).returns('set').then.returns('of').then.returns('values') |
| 247 |
augeas.stubs('close') |
| 248 |
provider.aug = augeas |
| 249 |
end |
| 250 |
|
| 251 |
it 'returns true for includes match' do |
| 252 |
command = ['values', 'fake value', 'include values'] |
| 253 |
expect(provider.process_values(command)).to eq(true) |
| 254 |
end |
| 255 |
|
| 256 |
it 'returns false for includes non match' do |
| 257 |
command = ['values', 'fake value', 'include JarJar'] |
| 258 |
expect(provider.process_values(command)).to eq(false) |
| 259 |
end |
| 260 |
|
| 261 |
it 'returns true for not_include non match' do |
| 262 |
command = ['values', 'fake value', 'not_include JarJar'] |
| 263 |
expect(provider.process_values(command)).to eq(true) |
| 264 |
end |
| 265 |
|
| 266 |
it 'returns false for non_include match' do |
| 267 |
command = ['values', 'fake value', 'not_include values'] |
| 268 |
expect(provider.process_values(command)).to eq(false) |
| 269 |
end |
| 270 |
|
| 271 |
it 'returns true for an array match' do |
| 272 |
command = ['values', 'fake value', "== ['set', 'of', 'values']"] |
| 273 |
expect(provider.process_values(command)).to eq(true) |
| 274 |
end |
| 275 |
it 'returns true for an array match with double quotes and spaces' do |
| 276 |
command = ['values', 'fake value', '== [ "set" , "of" , "values" ] '] |
| 277 |
expect(provider.process_values(command)).to eq(true) |
| 278 |
end |
| 279 |
|
| 280 |
it 'returns true for an array match with internally escaped single quotes' do |
| 281 |
provider.aug.stubs(:match).returns(['set', "o'values", 'here']) |
| 282 |
provider.aug.stubs(:get).returns('set').then.returns("o'values").then.returns('here') |
| 283 |
command = ['values', 'fake value', "== [ 'set', 'o\\'values', 'here']"] |
| 284 |
expect(provider.process_values(command)).to eq(true) |
| 285 |
end |
| 286 |
|
| 287 |
it 'returns true for an array match with octal character sequences' do |
| 288 |
command = ['values', 'fake value', '== ["\\x73et", "of", "values"]'] |
| 289 |
expect(provider.process_values(command)).to eq(true) |
| 290 |
end |
| 291 |
|
| 292 |
it 'returns true for an array match with hex character sequences' do |
| 293 |
command = ['values', 'fake value', '== ["\\163et", "of", "values"]'] |
| 294 |
expect(provider.process_values(command)).to eq(true) |
| 295 |
end |
| 296 |
|
| 297 |
it 'returns true for an array match with short unicode escape sequences' do |
| 298 |
command = ['values', 'fake value', '== ["\\u0073et", "of", "values"]'] |
| 299 |
expect(provider.process_values(command)).to eq(true) |
| 300 |
end |
| 301 |
|
| 302 |
it 'returns true for an array match with single character long unicode escape sequences' do |
| 303 |
command = ['values', 'fake value', '== ["\\u{0073}et", "of", "values"]'] |
| 304 |
expect(provider.process_values(command)).to eq(true) |
| 305 |
end |
| 306 |
|
| 307 |
it 'returns true for an array match with multi-character long unicode escape sequences' do |
| 308 |
command = ['values', 'fake value', '== ["\\u{0073 0065 0074}", "of", "values"]'] |
| 309 |
expect(provider.process_values(command)).to eq(true) |
| 310 |
end |
| 311 |
|
| 312 |
it 'returns true for an array match with literal backslashes' do |
| 313 |
provider.aug.stubs(:match).returns(['set', 'o\\values', 'here']) |
| 314 |
provider.aug.stubs(:get).returns('set').then.returns('o\\values').then.returns('here') |
| 315 |
command = ['values', 'fake value', '== [ "set", "o\\\\values", "here"]'] |
| 316 |
expect(provider.process_values(command)).to eq(true) |
| 317 |
end |
| 318 |
|
| 319 |
it 'returns false for an array non match' do |
| 320 |
command = ['values', 'fake value', "== ['this', 'should', 'not', 'match']"] |
| 321 |
expect(provider.process_values(command)).to eq(false) |
| 322 |
end |
| 323 |
|
| 324 |
it 'returns false for an array match with noteq' do |
| 325 |
command = ['values', 'fake value', "!= ['set', 'of', 'values']"] |
| 326 |
expect(provider.process_values(command)).to eq(false) |
| 327 |
end |
| 328 |
|
| 329 |
it 'returns true for an array non match with noteq' do |
| 330 |
command = ['values', 'fake value', "!= ['this', 'should', 'not', 'match']"] |
| 331 |
expect(provider.process_values(command)).to eq(true) |
| 332 |
end |
| 333 |
|
| 334 |
it 'returns true for an array non match with double quotes and spaces' do |
| 335 |
command = ['values', 'fake value', '!= [ "this" , "should" ,"not", "match" ] '] |
| 336 |
expect(provider.process_values(command)).to eq(true) |
| 337 |
end |
| 338 |
|
| 339 |
it 'returns true for an empty array match' do |
| 340 |
provider.aug.stubs(:match).returns([]) |
| 341 |
provider.aug.stubs(:get) |
| 342 |
command = ['values', 'fake value', '== []'] |
| 343 |
expect(provider.process_values(command)).to eq(true) |
| 344 |
end |
| 345 |
end |
| 346 |
|
| 347 |
describe 'match filters' do |
| 348 |
let(:augeas) { stub('augeas', match: ['set', 'of', 'values']) } |
| 349 |
|
| 350 |
before(:each) do |
| 351 |
augeas.stubs('close') |
| 352 |
provider.aug = augeas |
| 353 |
end |
| 354 |
|
| 355 |
it 'returns true for size match' do |
| 356 |
command = ['match', 'fake value', 'size == 3'] |
| 357 |
expect(provider.process_match(command)).to eq(true) |
| 358 |
end |
| 359 |
|
| 360 |
it 'returns false for a size non match' do |
| 361 |
command = ['match', 'fake value', 'size < 3'] |
| 362 |
expect(provider.process_match(command)).to eq(false) |
| 363 |
end |
| 364 |
|
| 365 |
it 'returns true for includes match' do |
| 366 |
command = ['match', 'fake value', 'include values'] |
| 367 |
expect(provider.process_match(command)).to eq(true) |
| 368 |
end |
| 369 |
|
| 370 |
it 'returns false for includes non match' do |
| 371 |
command = ['match', 'fake value', 'include JarJar'] |
| 372 |
expect(provider.process_match(command)).to eq(false) |
| 373 |
end |
| 374 |
|
| 375 |
it 'returns true for not_includes non match' do |
| 376 |
command = ['match', 'fake value', 'not_include JarJar'] |
| 377 |
expect(provider.process_match(command)).to eq(true) |
| 378 |
end |
| 379 |
|
| 380 |
it 'returns false for not_includes match' do |
| 381 |
command = ['match', 'fake value', 'not_include values'] |
| 382 |
expect(provider.process_match(command)).to eq(false) |
| 383 |
end |
| 384 |
|
| 385 |
it 'returns true for an array match' do |
| 386 |
command = ['match', 'fake value', "== ['set', 'of', 'values']"] |
| 387 |
expect(provider.process_match(command)).to eq(true) |
| 388 |
end |
| 389 |
|
| 390 |
it 'returns true for an array match with double quotes and spaces' do |
| 391 |
command = ['match', 'fake value', '== [ "set" , "of" , "values" ] '] |
| 392 |
expect(provider.process_match(command)).to eq(true) |
| 393 |
end |
| 394 |
|
| 395 |
it 'returns false for an array non match' do |
| 396 |
command = ['match', 'fake value', "== ['this', 'should', 'not', 'match']"] |
| 397 |
expect(provider.process_match(command)).to eq(false) |
| 398 |
end |
| 399 |
|
| 400 |
it 'returns false for an array match with noteq' do |
| 401 |
command = ['match', 'fake value', "!= ['set', 'of', 'values']"] |
| 402 |
expect(provider.process_match(command)).to eq(false) |
| 403 |
end |
| 404 |
|
| 405 |
it 'returns true for an array non match with noteq' do |
| 406 |
command = ['match', 'fake value', "!= ['this', 'should', 'not', 'match']"] |
| 407 |
expect(provider.process_match(command)).to eq(true) |
| 408 |
end |
| 409 |
|
| 410 |
it 'returns true for an array non match with double quotes and spaces' do |
| 411 |
command = ['match', 'fake value', '!= [ "this" , "should" ,"not", "match" ] '] |
| 412 |
expect(provider.process_match(command)).to eq(true) |
| 413 |
end |
| 414 |
end |
| 415 |
|
| 416 |
describe 'need to run' do |
| 417 |
let(:augeas) { stub('augeas') } |
| 418 |
|
| 419 |
before(:each) do |
| 420 |
augeas.stubs('close') |
| 421 |
provider.aug = augeas |
| 422 |
|
| 423 |
# These tests pretend to be an earlier version so the provider doesn't |
| 424 |
# attempt to make the change in the need_to_run? method |
| 425 |
provider.stubs(:get_augeas_version).returns('0.3.5') |
| 426 |
end |
| 427 |
|
| 428 |
it 'handles no filters' do |
| 429 |
augeas.stubs('match').returns(['set', 'of', 'values']) |
| 430 |
expect(provider.need_to_run?).to eq(true) |
| 431 |
end |
| 432 |
|
| 433 |
it 'returns true when a get filter matches' do |
| 434 |
resource[:onlyif] = 'get path == value' |
| 435 |
augeas.stubs('get').returns('value') |
| 436 |
expect(provider.need_to_run?).to eq(true) |
| 437 |
end |
| 438 |
|
| 439 |
describe 'performing numeric comparisons (#22617)' do |
| 440 |
it 'returns true when a get string compare is true' do |
| 441 |
resource[:onlyif] = 'get bpath > a' |
| 442 |
augeas.stubs('get').returns('b') |
| 443 |
expect(provider.need_to_run?).to eq(true) |
| 444 |
end |
| 445 |
|
| 446 |
it 'returns false when a get string compare is false' do |
| 447 |
resource[:onlyif] = 'get a19path > a2' |
| 448 |
augeas.stubs('get').returns('a19') |
| 449 |
expect(provider.need_to_run?).to eq(false) |
| 450 |
end |
| 451 |
|
| 452 |
it 'returns true when a get int gt compare is true' do |
| 453 |
resource[:onlyif] = 'get path19 > 2' |
| 454 |
augeas.stubs('get').returns('19') |
| 455 |
expect(provider.need_to_run?).to eq(true) |
| 456 |
end |
| 457 |
|
| 458 |
it 'returns true when a get int ge compare is true' do |
| 459 |
resource[:onlyif] = 'get path19 >= 2' |
| 460 |
augeas.stubs('get').returns('19') |
| 461 |
expect(provider.need_to_run?).to eq(true) |
| 462 |
end |
| 463 |
|
| 464 |
it 'returns true when a get int lt compare is true' do |
| 465 |
resource[:onlyif] = 'get path2 < 19' |
| 466 |
augeas.stubs('get').returns('2') |
| 467 |
expect(provider.need_to_run?).to eq(true) |
| 468 |
end |
| 469 |
|
| 470 |
it 'returns false when a get int le compare is false' do |
| 471 |
resource[:onlyif] = 'get path39 <= 4' |
| 472 |
augeas.stubs('get').returns('39') |
| 473 |
expect(provider.need_to_run?).to eq(false) |
| 474 |
end |
| 475 |
end |
| 476 |
describe 'performing is_numeric checks (#22617)' do |
| 477 |
it 'returns false for nil' do |
| 478 |
expect(provider.is_numeric?(nil)).to eq(false) |
| 479 |
end |
| 480 |
it 'returns true for Integers' do |
| 481 |
expect(provider.is_numeric?(9)).to eq(true) |
| 482 |
end |
| 483 |
it 'returns true for numbers in Strings' do |
| 484 |
expect(provider.is_numeric?('9')).to eq(true) |
| 485 |
end |
| 486 |
it 'returns false for non-number Strings' do |
| 487 |
expect(provider.is_numeric?('x9')).to eq(false) |
| 488 |
end |
| 489 |
it 'returns false for other types' do |
| 490 |
expect(provider.is_numeric?([true])).to eq(false) |
| 491 |
end |
| 492 |
end |
| 493 |
|
| 494 |
it 'returns false when a get filter does not match' do |
| 495 |
resource[:onlyif] = 'get path == another value' |
| 496 |
augeas.stubs('get').returns('value') |
| 497 |
expect(provider.need_to_run?).to eq(false) |
| 498 |
end |
| 499 |
|
| 500 |
it 'returns true when a match filter matches' do |
| 501 |
resource[:onlyif] = 'match path size == 3' |
| 502 |
augeas.stubs('match').returns(['set', 'of', 'values']) |
| 503 |
expect(provider.need_to_run?).to eq(true) |
| 504 |
end |
| 505 |
|
| 506 |
it 'returns false when a match filter does not match' do |
| 507 |
resource[:onlyif] = 'match path size == 2' |
| 508 |
augeas.stubs('match').returns(['set', 'of', 'values']) |
| 509 |
expect(provider.need_to_run?).to eq(false) |
| 510 |
end |
| 511 |
|
| 512 |
# Now setting force to true |
| 513 |
it 'setting force should not change the above logic' do |
| 514 |
resource[:force] = true |
| 515 |
resource[:onlyif] = 'match path size == 2' |
| 516 |
augeas.stubs('match').returns(['set', 'of', 'values']) |
| 517 |
expect(provider.need_to_run?).to eq(false) |
| 518 |
end |
| 519 |
|
| 520 |
# Ticket 5211 testing |
| 521 |
it 'returns true when a size != the provided value' do |
| 522 |
resource[:onlyif] = 'match path size != 17' |
| 523 |
augeas.stubs('match').returns(['set', 'of', 'values']) |
| 524 |
expect(provider.need_to_run?).to eq(true) |
| 525 |
end |
| 526 |
|
| 527 |
# Ticket 5211 testing |
| 528 |
it 'returns false when a size does equal the provided value' do |
| 529 |
resource[:onlyif] = 'match path size != 3' |
| 530 |
augeas.stubs('match').returns(['set', 'of', 'values']) |
| 531 |
expect(provider.need_to_run?).to eq(false) |
| 532 |
end |
| 533 |
|
| 534 |
[true, false].product([true, false]) do |cfg, param| |
| 535 |
describe "and Puppet[:show_diff] is #{cfg} and show_diff => #{param}" do |
| 536 |
let(:file) { '/some/random/file' } |
| 537 |
|
| 538 |
before(:each) do |
| 539 |
Puppet[:show_diff] = cfg |
| 540 |
resource[:show_diff] = param |
| 541 |
|
| 542 |
resource[:root] = '' |
| 543 |
resource[:context] = '/files' |
| 544 |
resource[:changes] = ["set #{file}/foo bar"] |
| 545 |
|
| 546 |
File.stubs(:delete) |
| 547 |
provider.stubs(:get_augeas_version).returns('0.10.0') |
| 548 |
provider.stubs('diff').with(file.to_s, "#{file}.augnew").returns('diff') |
| 549 |
|
| 550 |
augeas.stubs(:set).returns(true) |
| 551 |
augeas.stubs(:save).returns(true) |
| 552 |
augeas.stubs(:match).with('/augeas/events/saved').returns(['/augeas/events/saved']) |
| 553 |
augeas.stubs(:get).with('/augeas/events/saved').returns("/files#{file}") |
| 554 |
augeas.stubs(:set).with('/augeas/save', 'newfile') |
| 555 |
end |
| 556 |
|
| 557 |
if cfg && param |
| 558 |
it 'displays a diff' do |
| 559 |
expect(provider).to be_need_to_run |
| 560 |
|
| 561 |
expect(logs[0].message).to eq("\ndiff") |
| 562 |
end |
| 563 |
else |
| 564 |
it 'does not display a diff' do |
| 565 |
expect(provider).to be_need_to_run |
| 566 |
|
| 567 |
expect(logs).to be_empty |
| 568 |
end |
| 569 |
end |
| 570 |
end |
| 571 |
end |
| 572 |
|
| 573 |
# Ticket 2728 (diff files) |
| 574 |
describe 'and configured to show diffs' do |
| 575 |
before(:each) do |
| 576 |
Puppet[:show_diff] = true |
| 577 |
resource[:show_diff] = true |
| 578 |
|
| 579 |
resource[:root] = '' |
| 580 |
provider.stubs(:get_augeas_version).returns('0.10.0') |
| 581 |
augeas.stubs(:set).returns(true) |
| 582 |
augeas.stubs(:save).returns(true) |
| 583 |
end |
| 584 |
|
| 585 |
it 'displays a diff when a single file is shown to have been changed' do |
| 586 |
file = '/etc/hosts' |
| 587 |
File.stubs(:delete) |
| 588 |
|
| 589 |
resource[:loglevel] = 'crit' |
| 590 |
resource[:context] = '/files' |
| 591 |
resource[:changes] = ["set #{file}/foo bar"] |
| 592 |
|
| 593 |
augeas.stubs(:match).with('/augeas/events/saved').returns(['/augeas/events/saved']) |
| 594 |
augeas.stubs(:get).with('/augeas/events/saved').returns("/files#{file}") |
| 595 |
augeas.expects(:set).with('/augeas/save', 'newfile') |
| 596 |
provider.expects('diff').with(file.to_s, "#{file}.augnew").returns('diff') |
| 597 |
|
| 598 |
expect(provider).to be_need_to_run |
| 599 |
|
| 600 |
expect(logs[0].message).to eq("\ndiff") |
| 601 |
expect(logs[0].level).to eq(:crit) |
| 602 |
end |
| 603 |
|
| 604 |
it 'displays a diff for each file that is changed when changing many files' do |
| 605 |
file1 = '/etc/hosts' |
| 606 |
file2 = '/etc/resolv.conf' |
| 607 |
File.stubs(:delete) |
| 608 |
|
| 609 |
resource[:context] = '/files' |
| 610 |
resource[:changes] = ["set #{file1}/foo bar", "set #{file2}/baz biz"] |
| 611 |
|
| 612 |
augeas.stubs(:match).with('/augeas/events/saved').returns(['/augeas/events/saved[1]', '/augeas/events/saved[2]']) |
| 613 |
augeas.stubs(:get).with('/augeas/events/saved[1]').returns("/files#{file1}") |
| 614 |
augeas.stubs(:get).with('/augeas/events/saved[2]').returns("/files#{file2}") |
| 615 |
augeas.expects(:set).with('/augeas/save', 'newfile') |
| 616 |
provider.expects(:diff).with(file1.to_s, "#{file1}.augnew").returns("diff #{file1}") |
| 617 |
provider.expects(:diff).with(file2.to_s, "#{file2}.augnew").returns("diff #{file2}") |
| 618 |
|
| 619 |
expect(provider).to be_need_to_run |
| 620 |
|
| 621 |
expect(logs.map(&:message)).to include("\ndiff #{file1}", "\ndiff #{file2}") |
| 622 |
expect(logs.map(&:level)).to eq([:notice, :notice]) |
| 623 |
end |
| 624 |
|
| 625 |
describe 'and resource[:root] is set' do |
| 626 |
it 'calls diff when a file is shown to have been changed' do |
| 627 |
root = '/tmp/foo' |
| 628 |
file = '/etc/hosts' |
| 629 |
File.stubs(:delete) |
| 630 |
|
| 631 |
resource[:context] = '/files' |
| 632 |
resource[:changes] = ["set #{file}/foo bar"] |
| 633 |
resource[:root] = root |
| 634 |
|
| 635 |
augeas.stubs(:match).with('/augeas/events/saved').returns(['/augeas/events/saved']) |
| 636 |
augeas.stubs(:get).with('/augeas/events/saved').returns("/files#{file}") |
| 637 |
augeas.expects(:set).with('/augeas/save', 'newfile') |
| 638 |
provider.expects(:diff).with("#{root}#{file}", "#{root}#{file}.augnew").returns('diff') |
| 639 |
|
| 640 |
expect(provider).to be_need_to_run |
| 641 |
|
| 642 |
expect(logs[0].message).to eq("\ndiff") |
| 643 |
expect(logs[0].level).to eq(:notice) |
| 644 |
end |
| 645 |
end |
| 646 |
|
| 647 |
it 'does not call diff if no files change' do |
| 648 |
file = '/etc/hosts' |
| 649 |
|
| 650 |
resource[:context] = '/files' |
| 651 |
resource[:changes] = ["set #{file}/foo bar"] |
| 652 |
|
| 653 |
augeas.stubs(:match).with('/augeas/events/saved').returns([]) |
| 654 |
augeas.expects(:set).with('/augeas/save', 'newfile') |
| 655 |
augeas.expects(:get).with('/augeas/events/saved').never |
| 656 |
augeas.expects(:close) |
| 657 |
|
| 658 |
provider.expects(:diff).never |
| 659 |
expect(provider).not_to be_need_to_run |
| 660 |
end |
| 661 |
|
| 662 |
it 'cleanups the .augnew file' do |
| 663 |
file = '/etc/hosts' |
| 664 |
|
| 665 |
resource[:context] = '/files' |
| 666 |
resource[:changes] = ["set #{file}/foo bar"] |
| 667 |
|
| 668 |
augeas.stubs(:match).with('/augeas/events/saved').returns(['/augeas/events/saved']) |
| 669 |
augeas.stubs(:get).with('/augeas/events/saved').returns("/files#{file}") |
| 670 |
augeas.expects(:set).with('/augeas/save', 'newfile') |
| 671 |
augeas.expects(:close) |
| 672 |
|
| 673 |
File.expects(:delete).with(file + '.augnew') |
| 674 |
|
| 675 |
provider.expects(:diff).with(file.to_s, "#{file}.augnew").returns('') |
| 676 |
expect(provider).to be_need_to_run |
| 677 |
end |
| 678 |
|
| 679 |
# Workaround for Augeas bug #264 which reports filenames twice |
| 680 |
it 'handles duplicate /augeas/events/saved filenames' do |
| 681 |
file = '/etc/hosts' |
| 682 |
|
| 683 |
resource[:context] = '/files' |
| 684 |
resource[:changes] = ["set #{file}/foo bar"] |
| 685 |
|
| 686 |
augeas.stubs(:match).with('/augeas/events/saved').returns(['/augeas/events/saved[1]', '/augeas/events/saved[2]']) |
| 687 |
augeas.stubs(:get).with('/augeas/events/saved[1]').returns("/files#{file}") |
| 688 |
augeas.stubs(:get).with('/augeas/events/saved[2]').returns("/files#{file}") |
| 689 |
augeas.expects(:set).with('/augeas/save', 'newfile') |
| 690 |
augeas.expects(:close) |
| 691 |
|
| 692 |
File.expects(:delete).with(file + '.augnew').once |
| 693 |
|
| 694 |
provider.expects(:diff).with(file.to_s, "#{file}.augnew").returns('').once |
| 695 |
expect(provider).to be_need_to_run |
| 696 |
end |
| 697 |
|
| 698 |
it 'fails with an error if saving fails' do |
| 699 |
file = '/etc/hosts' |
| 700 |
|
| 701 |
resource[:context] = '/files' |
| 702 |
resource[:changes] = ["set #{file}/foo bar"] |
| 703 |
|
| 704 |
augeas.stubs(:save).returns(false) |
| 705 |
augeas.stubs(:match).with('/augeas/events/saved').returns([]) |
| 706 |
augeas.expects(:close) |
| 707 |
|
| 708 |
provider.expects(:diff).never |
| 709 |
provider.expects(:print_put_errors) |
| 710 |
expect { provider.need_to_run? }.to raise_error(Puppet::Error) |
| 711 |
end |
| 712 |
end |
| 713 |
end |
| 714 |
|
| 715 |
describe 'augeas execution integration' do |
| 716 |
let(:augeas) { stub('augeas', :load) } |
| 717 |
|
| 718 |
before(:each) do |
| 719 |
augeas.stubs('close') |
| 720 |
augeas.stubs(:match).with('/augeas/events/saved').returns([]) |
| 721 |
|
| 722 |
provider.aug = augeas |
| 723 |
provider.stubs(:get_augeas_version).returns('0.3.5') |
| 724 |
end |
| 725 |
|
| 726 |
it 'handles set commands' do |
| 727 |
resource[:changes] = 'set JarJar Binks' |
| 728 |
resource[:context] = '/some/path/' |
| 729 |
augeas.expects(:set).with('/some/path/JarJar', 'Binks').returns(true) |
| 730 |
augeas.expects(:save).returns(true) |
| 731 |
augeas.expects(:close) |
| 732 |
expect(provider.execute_changes).to eq(:executed) |
| 733 |
end |
| 734 |
|
| 735 |
it 'handles rm commands' do |
| 736 |
resource[:changes] = 'rm /Jar/Jar' |
| 737 |
augeas.expects(:rm).with('/Jar/Jar') |
| 738 |
augeas.expects(:save).returns(true) |
| 739 |
augeas.expects(:close) |
| 740 |
expect(provider.execute_changes).to eq(:executed) |
| 741 |
end |
| 742 |
|
| 743 |
it 'handles remove commands' do |
| 744 |
resource[:changes] = 'remove /Jar/Jar' |
| 745 |
augeas.expects(:rm).with('/Jar/Jar') |
| 746 |
augeas.expects(:save).returns(true) |
| 747 |
augeas.expects(:close) |
| 748 |
expect(provider.execute_changes).to eq(:executed) |
| 749 |
end |
| 750 |
|
| 751 |
it 'handles clear commands' do |
| 752 |
resource[:changes] = 'clear Jar/Jar' |
| 753 |
resource[:context] = '/foo/' |
| 754 |
augeas.expects(:clear).with('/foo/Jar/Jar').returns(true) |
| 755 |
augeas.expects(:save).returns(true) |
| 756 |
augeas.expects(:close) |
| 757 |
expect(provider.execute_changes).to eq(:executed) |
| 758 |
end |
| 759 |
|
| 760 |
describe 'touch command' do |
| 761 |
it 'clears missing path' do |
| 762 |
resource[:changes] = 'touch Jar/Jar' |
| 763 |
resource[:context] = '/foo/' |
| 764 |
augeas.expects(:match).with('/foo/Jar/Jar').returns([]) |
| 765 |
augeas.expects(:clear).with('/foo/Jar/Jar').returns(true) |
| 766 |
augeas.expects(:save).returns(true) |
| 767 |
augeas.expects(:close) |
| 768 |
expect(provider.execute_changes).to eq(:executed) |
| 769 |
end |
| 770 |
|
| 771 |
it 'does not change on existing path' do |
| 772 |
resource[:changes] = 'touch Jar/Jar' |
| 773 |
resource[:context] = '/foo/' |
| 774 |
augeas.expects(:match).with('/foo/Jar/Jar').returns(['/foo/Jar/Jar']) |
| 775 |
augeas.expects(:clear).never |
| 776 |
augeas.expects(:save).returns(true) |
| 777 |
augeas.expects(:close) |
| 778 |
expect(provider.execute_changes).to eq(:executed) |
| 779 |
end |
| 780 |
end |
| 781 |
|
| 782 |
it 'handles ins commands with before' do |
| 783 |
resource[:changes] = 'ins Binks before Jar/Jar' |
| 784 |
resource[:context] = '/foo' |
| 785 |
augeas.expects(:insert).with('/foo/Jar/Jar', 'Binks', true) |
| 786 |
augeas.expects(:save).returns(true) |
| 787 |
augeas.expects(:close) |
| 788 |
expect(provider.execute_changes).to eq(:executed) |
| 789 |
end |
| 790 |
|
| 791 |
it 'handles ins commands with after' do |
| 792 |
resource[:changes] = 'ins Binks after /Jar/Jar' |
| 793 |
resource[:context] = '/foo' |
| 794 |
augeas.expects(:insert).with('/Jar/Jar', 'Binks', false) |
| 795 |
augeas.expects(:save).returns(true) |
| 796 |
augeas.expects(:close) |
| 797 |
expect(provider.execute_changes).to eq(:executed) |
| 798 |
end |
| 799 |
|
| 800 |
it 'handles ins with no context' do |
| 801 |
resource[:changes] = 'ins Binks after /Jar/Jar' |
| 802 |
augeas.expects(:insert).with('/Jar/Jar', 'Binks', false) |
| 803 |
augeas.expects(:save).returns(true) |
| 804 |
augeas.expects(:close) |
| 805 |
expect(provider.execute_changes).to eq(:executed) |
| 806 |
end |
| 807 |
|
| 808 |
it 'handles multiple commands' do |
| 809 |
resource[:changes] = ['ins Binks after /Jar/Jar', 'clear Jar/Jar'] |
| 810 |
resource[:context] = '/foo/' |
| 811 |
augeas.expects(:insert).with('/Jar/Jar', 'Binks', false) |
| 812 |
augeas.expects(:clear).with('/foo/Jar/Jar').returns(true) |
| 813 |
augeas.expects(:save).returns(true) |
| 814 |
augeas.expects(:close) |
| 815 |
expect(provider.execute_changes).to eq(:executed) |
| 816 |
end |
| 817 |
|
| 818 |
it 'handles defvar commands' do |
| 819 |
resource[:changes] = 'defvar myjar Jar/Jar' |
| 820 |
resource[:context] = '/foo/' |
| 821 |
augeas.expects(:defvar).with('myjar', '/foo/Jar/Jar').returns(true) |
| 822 |
augeas.expects(:save).returns(true) |
| 823 |
augeas.expects(:close) |
| 824 |
expect(provider.execute_changes).to eq(:executed) |
| 825 |
end |
| 826 |
|
| 827 |
it 'passes through augeas variables without context' do |
| 828 |
resource[:changes] = ['defvar myjar Jar/Jar', 'set $myjar/Binks 1'] |
| 829 |
resource[:context] = '/foo/' |
| 830 |
augeas.expects(:defvar).with('myjar', '/foo/Jar/Jar').returns(true) |
| 831 |
# this is the important bit, shouldn't be /foo/$myjar/Binks |
| 832 |
augeas.expects(:set).with('$myjar/Binks', '1').returns(true) |
| 833 |
augeas.expects(:save).returns(true) |
| 834 |
augeas.expects(:close) |
| 835 |
expect(provider.execute_changes).to eq(:executed) |
| 836 |
end |
| 837 |
|
| 838 |
it 'handles defnode commands' do |
| 839 |
resource[:changes] = 'defnode newjar Jar/Jar[last()+1] Binks' |
| 840 |
resource[:context] = '/foo/' |
| 841 |
augeas.expects(:defnode).with('newjar', '/foo/Jar/Jar[last()+1]', 'Binks').returns(true) |
| 842 |
augeas.expects(:save).returns(true) |
| 843 |
augeas.expects(:close) |
| 844 |
expect(provider.execute_changes).to eq(:executed) |
| 845 |
end |
| 846 |
|
| 847 |
it 'handles mv commands' do |
| 848 |
resource[:changes] = 'mv Jar/Jar Binks' |
| 849 |
resource[:context] = '/foo/' |
| 850 |
augeas.expects(:mv).with('/foo/Jar/Jar', '/foo/Binks').returns(true) |
| 851 |
augeas.expects(:save).returns(true) |
| 852 |
augeas.expects(:close) |
| 853 |
expect(provider.execute_changes).to eq(:executed) |
| 854 |
end |
| 855 |
|
| 856 |
it 'handles rename commands' do |
| 857 |
resource[:changes] = 'rename Jar/Jar Binks' |
| 858 |
resource[:context] = '/foo/' |
| 859 |
augeas.expects(:rename).with('/foo/Jar/Jar', 'Binks').returns(true) |
| 860 |
augeas.expects(:save).returns(true) |
| 861 |
augeas.expects(:close) |
| 862 |
expect(provider.execute_changes).to eq(:executed) |
| 863 |
end |
| 864 |
|
| 865 |
it 'handles setm commands' do |
| 866 |
resource[:changes] = ['set test[1]/Jar/Jar Foo', 'set test[2]/Jar/Jar Bar', 'setm test Jar/Jar Binks'] |
| 867 |
resource[:context] = '/foo/' |
| 868 |
augeas.expects(:respond_to?).with('setm').returns(true) |
| 869 |
augeas.expects(:set).with('/foo/test[1]/Jar/Jar', 'Foo').returns(true) |
| 870 |
augeas.expects(:set).with('/foo/test[2]/Jar/Jar', 'Bar').returns(true) |
| 871 |
augeas.expects(:setm).with('/foo/test', 'Jar/Jar', 'Binks').returns(true) |
| 872 |
augeas.expects(:save).returns(true) |
| 873 |
augeas.expects(:close) |
| 874 |
expect(provider.execute_changes).to eq(:executed) |
| 875 |
end |
| 876 |
|
| 877 |
it 'throws error if setm command not supported' do |
| 878 |
resource[:changes] = ['set test[1]/Jar/Jar Foo', 'set test[2]/Jar/Jar Bar', 'setm test Jar/Jar Binks'] |
| 879 |
resource[:context] = '/foo/' |
| 880 |
augeas.expects(:respond_to?).with('setm').returns(false) |
| 881 |
augeas.expects(:set).with('/foo/test[1]/Jar/Jar', 'Foo').returns(true) |
| 882 |
augeas.expects(:set).with('/foo/test[2]/Jar/Jar', 'Bar').returns(true) |
| 883 |
expect { provider.execute_changes }.to raise_error RuntimeError, %r{command 'setm' not supported} |
| 884 |
end |
| 885 |
|
| 886 |
it 'handles clearm commands' do |
| 887 |
resource[:changes] = ['set test[1]/Jar/Jar Foo', 'set test[2]/Jar/Jar Bar', 'clearm test Jar/Jar'] |
| 888 |
resource[:context] = '/foo/' |
| 889 |
augeas.expects(:respond_to?).with('clearm').returns(true) |
| 890 |
augeas.expects(:set).with('/foo/test[1]/Jar/Jar', 'Foo').returns(true) |
| 891 |
augeas.expects(:set).with('/foo/test[2]/Jar/Jar', 'Bar').returns(true) |
| 892 |
augeas.expects(:clearm).with('/foo/test', 'Jar/Jar').returns(true) |
| 893 |
augeas.expects(:save).returns(true) |
| 894 |
augeas.expects(:close) |
| 895 |
expect(provider.execute_changes).to eq(:executed) |
| 896 |
end |
| 897 |
|
| 898 |
it 'throws error if clearm command not supported' do |
| 899 |
resource[:changes] = ['set test[1]/Jar/Jar Foo', 'set test[2]/Jar/Jar Bar', 'clearm test Jar/Jar'] |
| 900 |
resource[:context] = '/foo/' |
| 901 |
augeas.expects(:respond_to?).with('clearm').returns(false) |
| 902 |
augeas.expects(:set).with('/foo/test[1]/Jar/Jar', 'Foo').returns(true) |
| 903 |
augeas.expects(:set).with('/foo/test[2]/Jar/Jar', 'Bar').returns(true) |
| 904 |
expect { provider.execute_changes }.to raise_error(RuntimeError, %r{command 'clearm' not supported}) |
| 905 |
end |
| 906 |
|
| 907 |
it 'throws error if saving failed' do |
| 908 |
resource[:changes] = ['set test[1]/Jar/Jar Foo', 'set test[2]/Jar/Jar Bar', 'clearm test Jar/Jar'] |
| 909 |
resource[:context] = '/foo/' |
| 910 |
augeas.expects(:respond_to?).with('clearm').returns(true) |
| 911 |
augeas.expects(:set).with('/foo/test[1]/Jar/Jar', 'Foo').returns(true) |
| 912 |
augeas.expects(:set).with('/foo/test[2]/Jar/Jar', 'Bar').returns(true) |
| 913 |
augeas.expects(:clearm).with('/foo/test', 'Jar/Jar').returns(true) |
| 914 |
augeas.expects(:save).returns(false) |
| 915 |
provider.expects(:print_put_errors) |
| 916 |
augeas.expects(:match).returns([]) |
| 917 |
expect { provider.execute_changes }.to raise_error(Puppet::Error) |
| 918 |
end |
| 919 |
end |
| 920 |
|
| 921 |
describe 'when making changes', if: Puppet.features.augeas? do |
| 922 |
it "does not clobber the file if it's a symlink" do |
| 923 |
Puppet::Util::Storage.stubs(:store) |
| 924 |
|
| 925 |
link = tmpfile('link') |
| 926 |
target = tmpfile('target') |
| 927 |
FileUtils.touch(target) |
| 928 |
Puppet::FileSystem.symlink(target, link) |
| 929 |
|
| 930 |
resource = Puppet::Type.type(:augeas).new( |
| 931 |
name: 'test', |
| 932 |
incl: link, |
| 933 |
lens: 'Sshd.lns', |
| 934 |
changes: 'set PermitRootLogin no', |
| 935 |
) |
| 936 |
|
| 937 |
catalog = Puppet::Resource::Catalog.new |
| 938 |
catalog.add_resource resource |
| 939 |
|
| 940 |
catalog.apply |
| 941 |
|
| 942 |
expect(File.ftype(link)).to eq('link') |
| 943 |
expect(Puppet::FileSystem.readlink(link)).to eq(target) |
| 944 |
expect(File.read(target)).to match(%r{PermitRootLogin no}) |
| 945 |
end |
| 946 |
end |
| 947 |
|
| 948 |
describe 'load/save failure reporting' do |
| 949 |
let(:augeas) { stub('augeas') } |
| 950 |
|
| 951 |
before(:each) do |
| 952 |
augeas.stubs('close') |
| 953 |
provider.aug = augeas |
| 954 |
end |
| 955 |
|
| 956 |
describe 'should find load errors' do |
| 957 |
before(:each) do |
| 958 |
augeas.expects(:match).with('/augeas//error').returns(['/augeas/files/foo/error']) |
| 959 |
augeas.expects(:match).with('/augeas/files/foo/error/*').returns(['/augeas/files/foo/error/path', '/augeas/files/foo/error/message']) |
| 960 |
augeas.expects(:get).with('/augeas/files/foo/error').returns('some_failure') |
| 961 |
augeas.expects(:get).with('/augeas/files/foo/error/path').returns('/foo') |
| 962 |
augeas.expects(:get).with('/augeas/files/foo/error/message').returns('Failed to...') |
| 963 |
end |
| 964 |
|
| 965 |
it 'and output only to debug when no path supplied' do |
| 966 |
provider.expects(:debug).times(5) |
| 967 |
provider.expects(:warning).never |
| 968 |
provider.print_load_errors(nil) |
| 969 |
end |
| 970 |
|
| 971 |
it 'and output a warning and to debug when path supplied' do |
| 972 |
augeas.expects(:match).with('/augeas/files/foo//error').returns(['/augeas/files/foo/error']) |
| 973 |
provider.expects(:warning).once |
| 974 |
provider.expects(:debug).times(4) |
| 975 |
provider.print_load_errors('/augeas/files/foo//error') |
| 976 |
end |
| 977 |
|
| 978 |
it "and output only to debug when path doesn't match" do |
| 979 |
augeas.expects(:match).with('/augeas/files/foo//error').returns([]) |
| 980 |
provider.expects(:warning).never |
| 981 |
provider.expects(:debug).times(5) |
| 982 |
provider.print_load_errors('/augeas/files/foo//error') |
| 983 |
end |
| 984 |
end |
| 985 |
|
| 986 |
it 'finds load errors from lenses' do |
| 987 |
augeas.expects(:match).with('/augeas//error').twice.returns(['/augeas/load/Xfm/error']) |
| 988 |
augeas.expects(:match).with('/augeas/load/Xfm/error/*').returns([]) |
| 989 |
augeas.expects(:get).with('/augeas/load/Xfm/error').returns(['Could not find lens php.aug']) |
| 990 |
provider.expects(:warning).once |
| 991 |
provider.expects(:debug).twice |
| 992 |
provider.print_load_errors('/augeas//error') |
| 993 |
end |
| 994 |
|
| 995 |
it 'finds save errors and output to debug' do |
| 996 |
augeas.expects(:match).with("/augeas//error[. = 'put_failed']").returns(['/augeas/files/foo/error']) |
| 997 |
augeas.expects(:match).with('/augeas/files/foo/error/*').returns(['/augeas/files/foo/error/path', '/augeas/files/foo/error/message']) |
| 998 |
augeas.expects(:get).with('/augeas/files/foo/error').returns('some_failure') |
| 999 |
augeas.expects(:get).with('/augeas/files/foo/error/path').returns('/foo') |
| 1000 |
augeas.expects(:get).with('/augeas/files/foo/error/message').returns('Failed to...') |
| 1001 |
provider.expects(:debug).times(5) |
| 1002 |
provider.print_put_errors |
| 1003 |
end |
| 1004 |
end |
| 1005 |
|
| 1006 |
# Run initialisation tests of the real Augeas library to test our open_augeas |
| 1007 |
# method. This relies on Augeas and ruby-augeas on the host to be |
| 1008 |
# functioning. |
| 1009 |
describe 'augeas lib initialisation', if: Puppet.features.augeas? do |
| 1010 |
# Expect lenses for fstab and hosts |
| 1011 |
it 'has loaded standard files by default' do |
| 1012 |
aug = provider.open_augeas |
| 1013 |
expect(aug).not_to eq(nil) |
| 1014 |
expect(aug.match('/files/etc/fstab')).to eq(['/files/etc/fstab']) |
| 1015 |
expect(aug.match('/files/etc/hosts')).to eq(['/files/etc/hosts']) |
| 1016 |
expect(aug.match('/files/etc/test')).to eq([]) |
| 1017 |
end |
| 1018 |
|
| 1019 |
it 'reports load errors to debug only' do |
| 1020 |
provider.expects(:print_load_errors).with(nil) |
| 1021 |
aug = provider.open_augeas |
| 1022 |
expect(aug).not_to eq(nil) |
| 1023 |
end |
| 1024 |
|
| 1025 |
# Only the file specified should be loaded |
| 1026 |
it 'loads one file if incl/lens used' do |
| 1027 |
resource[:incl] = '/etc/hosts' |
| 1028 |
resource[:lens] = 'Hosts.lns' |
| 1029 |
|
| 1030 |
provider.expects(:print_load_errors).with('/augeas//error') |
| 1031 |
aug = provider.open_augeas |
| 1032 |
expect(aug).not_to eq(nil) |
| 1033 |
expect(aug.match('/files/etc/fstab')).to eq([]) |
| 1034 |
expect(aug.match('/files/etc/hosts')).to eq(['/files/etc/hosts']) |
| 1035 |
expect(aug.match('/files/etc/test')).to eq([]) |
| 1036 |
end |
| 1037 |
|
| 1038 |
it 'alsoes load lenses from load_path' do |
| 1039 |
resource[:load_path] = my_fixture_dir |
| 1040 |
|
| 1041 |
aug = provider.open_augeas |
| 1042 |
expect(aug).not_to eq(nil) |
| 1043 |
expect(aug.match('/files/etc/fstab')).to eq(['/files/etc/fstab']) |
| 1044 |
expect(aug.match('/files/etc/hosts')).to eq(['/files/etc/hosts']) |
| 1045 |
expect(aug.match('/files/etc/test')).to eq(['/files/etc/test']) |
| 1046 |
end |
| 1047 |
|
| 1048 |
it "alsoes load lenses from pluginsync'd path" do |
| 1049 |
Puppet[:libdir] = my_fixture_dir |
| 1050 |
|
| 1051 |
aug = provider.open_augeas |
| 1052 |
expect(aug).not_to eq(nil) |
| 1053 |
expect(aug.match('/files/etc/fstab')).to eq(['/files/etc/fstab']) |
| 1054 |
expect(aug.match('/files/etc/hosts')).to eq(['/files/etc/hosts']) |
| 1055 |
expect(aug.match('/files/etc/test')).to eq(['/files/etc/test']) |
| 1056 |
end |
| 1057 |
|
| 1058 |
# Optimisations added for Augeas 0.8.2 or higher is available, see #7285 |
| 1059 |
describe '>= 0.8.2 optimisations', if: Puppet.features.augeas? && Facter.value(:augeasversion) && Puppet::Util::Package.versioncmp(Facter.value(:augeasversion), '0.8.2') >= 0 do |
| 1060 |
it 'onlies load one file if relevant context given' do |
| 1061 |
resource[:context] = '/files/etc/fstab' |
| 1062 |
|
| 1063 |
provider.expects(:print_load_errors).with('/augeas/files/etc/fstab//error') |
| 1064 |
aug = provider.open_augeas |
| 1065 |
expect(aug).not_to eq(nil) |
| 1066 |
expect(aug.match('/files/etc/fstab')).to eq(['/files/etc/fstab']) |
| 1067 |
expect(aug.match('/files/etc/hosts')).to eq([]) |
| 1068 |
end |
| 1069 |
|
| 1070 |
it 'onlies load one lens from load_path if context given' do |
| 1071 |
resource[:context] = '/files/etc/test' |
| 1072 |
resource[:load_path] = my_fixture_dir |
| 1073 |
|
| 1074 |
provider.expects(:print_load_errors).with('/augeas/files/etc/test//error') |
| 1075 |
aug = provider.open_augeas |
| 1076 |
expect(aug).not_to eq(nil) |
| 1077 |
expect(aug.match('/files/etc/fstab')).to eq([]) |
| 1078 |
expect(aug.match('/files/etc/hosts')).to eq([]) |
| 1079 |
expect(aug.match('/files/etc/test')).to eq(['/files/etc/test']) |
| 1080 |
end |
| 1081 |
|
| 1082 |
it "loads standard files if context isn't specific" do |
| 1083 |
resource[:context] = '/files/etc' |
| 1084 |
|
| 1085 |
provider.expects(:print_load_errors).with(nil) |
| 1086 |
aug = provider.open_augeas |
| 1087 |
expect(aug).not_to eq(nil) |
| 1088 |
expect(aug.match('/files/etc/fstab')).to eq(['/files/etc/fstab']) |
| 1089 |
expect(aug.match('/files/etc/hosts')).to eq(['/files/etc/hosts']) |
| 1090 |
end |
| 1091 |
|
| 1092 |
it 'does not optimise if the context is a complex path' do |
| 1093 |
resource[:context] = "/files/*[label()='etc']" |
| 1094 |
|
| 1095 |
provider.expects(:print_load_errors).with(nil) |
| 1096 |
aug = provider.open_augeas |
| 1097 |
expect(aug).not_to eq(nil) |
| 1098 |
expect(aug.match('/files/etc/fstab')).to eq(['/files/etc/fstab']) |
| 1099 |
expect(aug.match('/files/etc/hosts')).to eq(['/files/etc/hosts']) |
| 1100 |
end |
| 1101 |
end |
| 1102 |
end |
| 1103 |
|
| 1104 |
describe 'get_load_path' do |
| 1105 |
it 'offers no load_path by default' do |
| 1106 |
expect(provider.get_load_path(resource)).to eq('') |
| 1107 |
end |
| 1108 |
|
| 1109 |
it 'offers one path from load_path' do |
| 1110 |
resource[:load_path] = '/foo' |
| 1111 |
expect(provider.get_load_path(resource)).to eq('/foo') |
| 1112 |
end |
| 1113 |
|
| 1114 |
it 'offers multiple colon-separated paths from load_path' do |
| 1115 |
resource[:load_path] = '/foo:/bar:/baz' |
| 1116 |
expect(provider.get_load_path(resource)).to eq('/foo:/bar:/baz') |
| 1117 |
end |
| 1118 |
|
| 1119 |
it 'offers multiple paths in array from load_path' do |
| 1120 |
resource[:load_path] = ['/foo', '/bar', '/baz'] |
| 1121 |
expect(provider.get_load_path(resource)).to eq('/foo:/bar:/baz') |
| 1122 |
end |
| 1123 |
|
| 1124 |
context 'when running application is agent' do |
| 1125 |
before(:each) do |
| 1126 |
Puppet[:libdir] = my_fixture_dir |
| 1127 |
Puppet.run_mode.stubs(:name).returns(:agent) |
| 1128 |
end |
| 1129 |
|
| 1130 |
it 'offers pluginsync augeas/lenses subdir' do |
| 1131 |
expect(provider.get_load_path(resource)).to eq("#{my_fixture_dir}/augeas/lenses") |
| 1132 |
end |
| 1133 |
|
| 1134 |
it 'offers both pluginsync and load_path paths' do |
| 1135 |
resource[:load_path] = ['/foo', '/bar', '/baz'] |
| 1136 |
expect(provider.get_load_path(resource)).to eq("/foo:/bar:/baz:#{my_fixture_dir}/augeas/lenses") |
| 1137 |
end |
| 1138 |
end |
| 1139 |
|
| 1140 |
context 'when running application is not agent' do |
| 1141 |
before(:each) do |
| 1142 |
Puppet.run_mode.stubs(:name).returns(:user) |
| 1143 |
|
| 1144 |
env = Puppet::Node::Environment.create('root', ['/modules/foobar']) |
| 1145 |
Puppet.stubs(:lookup).returns(env) |
| 1146 |
env.stubs(:each_plugin_directory).yields('/modules/foobar') |
| 1147 |
|
| 1148 |
resource[:load_path] = ['/foo', '/bar', '/baz'] |
| 1149 |
end |
| 1150 |
|
| 1151 |
it 'offers both load_path and module lenses path when available' do |
| 1152 |
File.stubs(:exist?).with('/modules/foobar/augeas/lenses').returns(true) |
| 1153 |
expect(provider.get_load_path(resource)).to eq('/foo:/bar:/baz:/modules/foobar/augeas/lenses') |
| 1154 |
end |
| 1155 |
|
| 1156 |
it 'offers only load_path if module lenses path is not available' do |
| 1157 |
File.stubs(:exist?).with('/modules/foobar/augeas/lenses').returns(false) |
| 1158 |
expect(provider.get_load_path(resource)).to eq('/foo:/bar:/baz') |
| 1159 |
end |
| 1160 |
end |
| 1161 |
end |
| 1162 |
end |