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 CucumberPuppet object.



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

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.



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

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.



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
78
79
# File 'lib/cucumber-puppet/puppet.rb', line 51

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’.



34
35
36
# File 'lib/cucumber-puppet/puppet.rb', line 34

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

#klass=(klass) ⇒ Object

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



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

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.



83
84
85
# File 'lib/cucumber-puppet/puppet.rb', line 83

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