Module: AugeasSpec::Fixtures

Defined in:
vendor_modules/augeasproviders_core/spec/lib/augeas_spec/fixtures.rb

Instance Method Summary collapse

Instance Method Details

#apply(*resources) ⇒ Object

Runs a particular resource via a catalog



18
19
20
21
22
23
24
25
# File 'vendor_modules/augeasproviders_core/spec/lib/augeas_spec/fixtures.rb', line 18

def apply(*resources)
  catalog = Puppet::Resource::Catalog.new
  catalog.host_config = false
  resources.each do |resource|
    catalog.add_resource resource
  end
  catalog.apply
end

#apply!(*resources) ⇒ Object

Runs a resource and checks for warnings and errors



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'vendor_modules/augeasproviders_core/spec/lib/augeas_spec/fixtures.rb', line 28

def apply!(*resources)
  txn = apply(*resources)

  # Check for warning+ log messages
  loglevels = Puppet::Util::Log.levels[3, 999]
  firstlogs = @logs.dup
  expect(@logs.select { |log| loglevels.include?(log.level) && log.message !~ %r{'modulepath' as a setting} }).to eq([])

  # Check for transaction success after, as it's less informative
  expect(txn.any_failed?).to be_nil

  # Run the exact same resources, but this time ensure there were absolutely
  # no changes (as seen by logs) to indicate if it was idempotent or not
  @logs.clear
  txn_idempotent = apply(*resources)
  loglevels = Puppet::Util::Log.levels[2, 999]
  againlogs = @logs.select { |log| loglevels.include? log.level }

  expect(againlogs).to eq([]), 'expected no change on second run (idempotence check)'
  expect(txn_idempotent.any_failed?).to be_nil, 'expected no change on second run (idempotence check), got a resource failure'

  @logs = firstlogs
  txn
end

#aug_fixture(name) ⇒ Object

Creates a temp file from a given fixture name Doesn't explicitly clean up the temp file as we can't evaluate a block with “let” or pass the path back via an “around” hook.



10
11
12
13
14
15
# File 'vendor_modules/augeasproviders_core/spec/lib/augeas_spec/fixtures.rb', line 10

def aug_fixture(name)
  tmp = Tempfile.new('target')
  tmp.write(File.read(my_fixture(name)))
  tmp.close
  tmp
end

#aug_open(file, lens) ⇒ Object

Open Augeas on a given file. Used for testing the results of running Puppet providers.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'vendor_modules/augeasproviders_core/spec/lib/augeas_spec/fixtures.rb', line 55

def aug_open(file, lens)
  aug = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD)
  begin
    aug.transform(
      lens: lens,
      name: lens.split('.')[0],
      incl: file,
      excl: []
    )
    aug.set('/augeas/context', "/files#{file}")
    aug.load!
    raise AugeasSpec::Error, "Augeas didn't load #{file}" if aug.match('.').empty?

    yield aug
  rescue Augeas::Error
    errors = []
    aug.match('/augeas//error').each do |errnode|
      aug.match("#{errnode}/*").each do |subnode|
        subvalue = aug.get(subnode)
        errors << "#{subnode} = #{subvalue}"
      end
    end
    raise AugeasSpec::Error, errors.join("\n")
  ensure
    aug.close
  end
end