Project

General

Profile

« Previous | Next » 

Revision 1ce43f91

Added by Ohad Levy almost 12 years ago

  • ID 1ce43f91fee51a7539cd3751799fe0d3d560d743

Export arguments of parameterized classes

Use puppet/parser for a first class analysis of the class definitions.
Using regexes would have been a nightmare.

Exports a "params" sub-object whose keys are the parameter names and
values are a best-effort convertion from AST leaves to native ruby
types, before being exported as native JSON values.

Compatible with Puppet 2.6 and 2.7, at least.

Tests:
- Manifest must now parse in order to extract classes from it,
hence the fix in the test.
- Functional tests

View differences:

test/puppet_class_test.rb
class PuppetClassTest < Test::Unit::TestCase
def setup
Puppet::Node::Environment.clear
end
def test_should_have_a_logger
assert_respond_to Proxy::Puppet, :logger
end
......
manifest = <<-EOF
class foreman::install {
include 'x::y'
}
EOF
klasses = Proxy::Puppet::PuppetClass.scan_manifest(manifest)
assert_kind_of Array, klasses
......
klasses = Proxy::Puppet::PuppetClass.scan_manifest(manifest)
assert klasses.empty?
end
def test_should_find_multiple_class_in_a_manifest
def test_should_find_multiple_class_in_a_manifest
manifest = <<-EOF
class foreman::install {
include 'x::y'
......
klasses = Proxy::Puppet::PuppetClass.scan_manifest(manifest)
assert_kind_of Array, klasses
assert_equal 2, klasses.size
klasses.sort! { |k1,k2| k1.name <=> k2.name }
klass = klasses.first
assert_equal "install", klass.name
assert_equal "foreman", klass.module
klass = klasses.last
assert_equal "params", klass.name
assert_equal "foreman", klass.module
end
end
def test_should_scan_a_dir
klasses = Proxy::Puppet::PuppetClass.scan_directory('/tmp/no_such_dir')
......
assert klasses.empty?
end
def test_should_extract_parameters__no_param_parenthesis
manifest = <<-EOF
class foreman::install {
}
EOF
klasses = Proxy::Puppet::PuppetClass.scan_manifest(manifest)
assert_kind_of Array, klasses
assert_equal 1, klasses.size
klass = klasses.first
assert_equal({}, klass.params)
end
def test_should_extract_parameters__empty_param_parenthesis
manifest = <<-EOF
class foreman::install () {
}
EOF
klasses = Proxy::Puppet::PuppetClass.scan_manifest(manifest)
assert_kind_of Array, klasses
assert_equal 1, klasses.size
klass = klasses.first
assert_equal({}, klass.params)
end
def test_should_extract_parameters__single_param_no_value
manifest = <<-EOF
class foreman::install ($mandatory) {
}
EOF
klasses = Proxy::Puppet::PuppetClass.scan_manifest(manifest)
assert_kind_of Array, klasses
assert_equal 1, klasses.size
klass = klasses.first
assert_equal({'mandatory' => nil}, klass.params)
end
def test_should_extract_parameters__type_coverage
# Note that all keys are string in Puppet
manifest = <<-EOF
class foreman::install (
$mandatory,
$emptyString = '',
$emptyStringDq = "",
$string = "foo",
$integer = 42,
$float = 3.14,
$array = ['', "", "foo", 42, 3.14],
$hash = { unquoted => '', "quoted" => "", 42 => "integer", 3.14 => "float", '' => 'empty' },
$complex = { array => ['','foo',42,3.14], hash => {foo=>"bar"}, mixed => [{foo=>bar},{bar=>"baz"}] }
) {
}
EOF
klasses = Proxy::Puppet::PuppetClass.scan_manifest(manifest)
assert_kind_of Array, klasses
assert_equal 1, klasses.size
klass = klasses.first
assert_equal({
'mandatory' => nil,
'emptyString' => '',
'emptyStringDq' => '',
'string' => 'foo',
'integer' => 42,
'float' => 3.14,
'array' => ['', '', 'foo', 42, 3.14],
# All keys must be strings
'hash' => { 'unquoted' => '', 'quoted' => '', '42' => 'integer', '3.14' => 'float', '' => 'empty' },
'complex' => { 'array' => ['','foo',42,3.14], 'hash' => {'foo'=>'bar'}, 'mixed' => [{'foo'=>'bar'},{'bar'=>'baz'}] }
}, klass.params)
end
#TODO add scans to a real puppet directory with modules
end

Also available in: Unified diff