Class: CucumberPuppet

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber-puppet/puppet.rb,
lib/cucumber-puppet.rb

Overview

A class for accessing Puppet’s internal state regarding a certain node or class.

Defined Under Namespace

Modules: Helper

Constant Summary collapse

VERSION =
[version[:major], version[:minor], version[:patch], version[:build]].compact.join('.')

Instance Method Summary collapse

Constructor Details

#initializeCucumberPuppet

Returns a new instance of CucumberPuppet.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cucumber-puppet/puppet.rb', line 6

def initialize
  # resources' alias metaparameter
  @aliases = {}

  # default puppet configuration
  @puppetcfg = {
    'confdir' => "/etc/puppet",
    'manifest' => "/etc/puppet/manifests/site.pp",
  }

  # default facts
  @facts = {
    'architecture' => "",
    'domain' => "no.domain",
    'environment' => "production",
    'hostname' => "testnode",
    'lsbdistcodename' => "",
    'network_eth0' => "127.0.0.0",
    'operatingsystem' => "",
  }

  Puppet::Util::Log.newdestination(:console)
  Puppet::Util::Log.level = :notice
end

Instance Method Details

#catalog_resourcesObject

Returns an Array with the catalog’s Puppet::Resource objects.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cucumber-puppet/puppet.rb', line 86

def catalog_resources
  # This method exists to supply a common interface to the puppet catalog
  # for different versions of puppet.
  @catalog.resources.map do |r|
    if r.is_a?(String)
      # puppet 0.25 and older
      resource(r)
    elsif r.is_a?(Puppet::Resource)
      # puppet 2.6 and newer
      r
    else
      raise "Unknown resource object #{r.class}"
    end
  end
end

#compile_catalog(node = nil) ⇒ Object

Compile catalog for configured testnode.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cucumber-puppet/puppet.rb', line 49

def compile_catalog( node = nil )
  Puppet.settings.handlearg("--confdir", @puppetcfg['confdir'])
  Puppet.parse_config
  # reset confdir in case it got overwritten
  @puppetcfg.each do |option,value|
    Puppet.settings.handlearg("--#{option}", value)
  end

  unless node.is_a?(Puppet::Node)
    node = Puppet::Node.new(@facts['hostname'], :classes => @klass)
    node.merge(@facts)
  end

  # Compile our catalog
  begin
    @catalog = Puppet::Resource::Catalog.indirection.find(node.name, :use_node => node)
  rescue NameError
    @catalog = Puppet::Node::Catalog.find(node.name, :use_node => node)
  end

  # XXX could not find this in puppet
  catalog_resources.each do |resource|
    next unless resource[:alias]
    resource[:alias].each do |a|
      # "foo" -> "Package[foo]"
      @aliases["#{resource.type}[#{a}]"] = resource
    end
  end
end

#debugObject

Set Puppet’s log level to ‘debug’.



32
33
34
# File 'lib/cucumber-puppet/puppet.rb', line 32

def debug
  Puppet::Util::Log.level = :debug
end

#klass=(klass) ⇒ Object

Define Puppet classes to include in a feature’s testnode.



37
38
39
40
41
42
43
44
45
46
# File 'lib/cucumber-puppet/puppet.rb', line 37

def klass=(klass)
  if klass.class == String
    # XXX sth like klass.split(/,/) and remove whitespace
    @klass = klass.to_a
  elsif klass.class == Hash
    @klass = klass
  else
    raise "unsupported class #{klass.class} for klass."
  end
end

#resource(title) ⇒ Object

Returns an Object with the given title from catalog, taking aliases into account.



81
82
83
# File 'lib/cucumber-puppet/puppet.rb', line 81

def resource(title)
  @catalog.resource(title) || @aliases[title]
end