Class: Puppet::PuppetSpecInitializer

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetlabs_spec_helper/puppet_spec_helper.rb

Class Method Summary collapse

Class Method Details

.initialize_via_fallback_compatibility(config) ⇒ Object

This method is for initializing puppet state for testing for older versions of puppet that do not support the new TestHelper API. As you can see, this involves explicitly modifying global variables, directly manipulating Puppet’s Settings singleton object, and other fun implementation details that code external to puppet should really never know about.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/puppetlabs_spec_helper/puppet_spec_helper.rb', line 74

def self.initialize_via_fallback_compatibility(config)
  config.before :all do
    # nothing to do for now
  end

  config.after :all do
    # nothing to do for now
  end

  config.before :each do
    # these globals are set by Application
    $puppet_application_mode = nil
    $puppet_application_name = nil

    # REVISIT: I think this conceals other bad tests, but I don't have time to
    # fully diagnose those right now.  When you read this, please come tell me
    # I suck for letting this float. --daniel 2011-04-21
    Signal.stubs(:trap)

    # Set the confdir and vardir to gibberish so that tests
    # have to be correctly mocked.
    Puppet[:confdir] = "/dev/null"
    Puppet[:vardir] = "/dev/null"

    # Avoid opening ports to the outside world
    Puppet.settings[:bindaddress] = "127.0.0.1"
  end

  config.after :each do
    Puppet.settings.clear

    Puppet::Node::Environment.clear
    Puppet::Util::Storage.clear
    Puppet::Util::ExecutionStub.reset if Puppet::Util.constants.include? "ExecutionStub"

    PuppetlabsSpec::Files.cleanup
  end
end

.initialize_via_testhelper(config) ⇒ Object

This method uses the “new”/preferred approach of delegating all of the test state initialization to puppet itself, via Puppet::Test::TestHelper API. This should be fairly future-proof as long as that API doesn’t change, which it hopefully will not need to.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/puppetlabs_spec_helper/puppet_spec_helper.rb', line 44

def self.initialize_via_testhelper(config)
  begin
    Puppet::Test::TestHelper.initialize
  rescue NoMethodError
    Puppet::Test::TestHelper.before_each_test
  end

  # connect rspec hooks to TestHelper methods.
  config.before :all do
    Puppet::Test::TestHelper.before_all_tests
  end

  config.after :all do
    Puppet::Test::TestHelper.after_all_tests
  end

  config.before :each do
    Puppet::Test::TestHelper.before_each_test
  end

  config.after :each do
    Puppet::Test::TestHelper.after_each_test
  end
end