update code to support more simultaneous versions

This commit is intended to resolve some of the issues
related with create_resources support for
multiple Puppet versions.

Basically, create_resources often needs to be an
external dependency if it's used in modules, so that
those modules can work with 2.6.x.

In this case, it needs to be added to the fixtures.yaml
file. Since there is no way to specify that
certain dependant modules should only be added for
certain versions of Puppet, then create_resources will
even override the native one for versions >= 2.7.0
in all unit tests.

For this reason, the module needs to be able to
simultaneously support the targeted versions:
2.6.x ,2.7.x, 3.0.x, and 3.1.x.

This commit does the following:
   - back ports all 2.7.x changes to create_resources
(so that it will work with 3.0)
   - modifies tests, unit tests so they pass with
     all targeted versions.
   - update Rakefile and spec_helper to use
     puppetlabs_spec_helper

of coarse, there is a bigger question of if I should
even be doing this, it has several pitfalls:
  - modules will be tested with an external create_resoures
    which may not be in sync with the one in core
  - I would recommend that modules not pull in
    create_resources as a dep in their module file,
    which means
       1. it becomes an extra step for 2.6.x
       2. module's are not tested and used consistenly
This commit is contained in:
Dan Bode
2013-03-03 12:08:57 -08:00
parent 28584b0ed1
commit 63c405d1a9
5 changed files with 94 additions and 109 deletions

View File

@@ -3,33 +3,43 @@ require 'spec_helper'
describe 'function for dynamically creating resources' do
def get_scope
@topscope = Puppet::Parser::Scope.new
# This is necessary so we don't try to use the compiler to discover our parent.
@topscope.parent = nil
@scope = Puppet::Parser::Scope.new
@scope.compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("floppy", :environment => 'production'))
def setup_scope
@compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("floppy", :environment => 'production'))
if Puppet.version =~ /^3\./
@scope = Puppet::Parser::Scope.new(@compiler)
else
@scope = Puppet::Parser::Scope.new(:compiler => @compiler)
end
@topscope = @topscope
@scope.parent = @topscope
@compiler = @scope.compiler
end
before :each do
get_scope
Puppet::Parser::Functions.function(:create_resources)
end
it "should exist" do
Puppet::Parser::Functions.function(:create_resources).should == "function_create_resources"
end
it 'should require two arguments' do
lambda { @scope.function_create_resources(['foo']) }.should raise_error(ArgumentError, 'create_resources(): wrong number of arguments (1; must be 2)')
describe 'basic tests' do
before :each do
setup_scope
end
it "should exist" do
Puppet::Parser::Functions.function(:create_resources).should == "function_create_resources"
end
it 'should require two arguments' do
lambda { @scope.function_create_resources(['foo']) }.should raise_error(ArgumentError, 'create_resources(): wrong number of arguments (1; must be 2 or 3)')
end
end
describe 'when creating native types' do
before :each do
Puppet[:code]='notify{test:}'
get_scope
@scope.resource=Puppet::Parser::Resource.new('class', 't', :scope => @scope)
Puppet[:code]=
'
class t {}
notify{test:}
'
setup_scope
end
it 'empty hash should not cause resources to be added' do
@scope.function_create_resources(['file', {}])
@compiler.catalog.resources.size == 1
end
@@ -39,34 +49,24 @@ describe 'function for dynamically creating resources' do
end
it 'should accept multiple types' do
type_hash = {}
type_hash['foo'] = {'message' => 'one'}
type_hash['bar'] = {'message' => 'two'}
type_hash['notify'] = {'message' => 'one'}
type_hash['user'] = {'home' => true}
@scope.function_create_resources(['notify', type_hash])
@compiler.catalog.resource(:notify, "foo")['message'].should == 'one'
@compiler.catalog.resource(:notify, "bar")['message'].should == 'two'
@compiler.catalog.resource(:notify, "notify")['message'].should == 'one'
@compiler.catalog.resource(:notify, "user")['home'].should == true
end
it 'should fail to add non-existing type' do
lambda { @scope.function_create_resources(['foo', {}]) }.should raise_error(ArgumentError, 'could not create resource of unknown type foo')
lambda {
@scope.function_create_resources(['fooper', {}]) }.should raise_error(ArgumentError, 'could not create resource of unknown type fooper')
end
it 'should be able to add edges' do
@scope.function_create_resources(['notify', {'foo'=>{'require' => 'Notify[test]'}}])
@scope.compiler.compile
rg = @scope.compiler.catalog.to_ral.relationship_graph
test = rg.vertices.find { |v| v.title == 'test' }
foo = rg.vertices.find { |v| v.title == 'foo' }
test.should be
foo.should be
rg.path_between(test,foo).should be
end
end
describe 'when dynamically creating resource types' do
before :each do
before :each do
Puppet[:code]=
'define foo($one){notify{$name: message => $one}}
'
class t {}
define foo($one){notify{$name: message => $one}}
notify{test:}
'
get_scope
@scope.resource=Puppet::Parser::Resource.new('class', 't', :scope => @scope)
setup_scope
Puppet::Parser::Functions.function(:create_resources)
end
it 'should be able to create defined resoure types' do
@@ -78,7 +78,7 @@ notify{test:}
end
it 'should fail if defines are missing params' do
@scope.function_create_resources(['foo', {'blah'=>{}}])
lambda { @scope.compiler.compile }.should raise_error(Puppet::ParseError, 'Must pass one to Foo[blah] at line 1')
lambda { @scope.compiler.compile }.should raise_error(Puppet::ParseError, /Must pass one/)
end
it 'should be able to add multiple defines' do
hash = {}
@@ -91,27 +91,16 @@ notify{test:}
@compiler.catalog.resource(:notify, "blah")['message'].should == 'two'
@compiler.catalog.resource(:notify, "blaz")['message'].should == 'three'
end
it 'should be able to add edges' do
@scope.function_create_resources(['foo', {'blah'=>{'one'=>'two', 'require' => 'Notify[test]'}}])
@scope.compiler.compile
rg = @scope.compiler.catalog.to_ral.relationship_graph
test = rg.vertices.find { |v| v.title == 'test' }
blah = rg.vertices.find { |v| v.title == 'blah' }
test.should be
blah.should be
# (Yoda speak like we do)
rg.path_between(test,blah).should be
@compiler.catalog.resource(:notify, "blah")['message'].should == 'two'
end
end
describe 'when creating classes' do
before :each do
Puppet[:code]=
'class bar($one){notify{test: message => $one}}
'
class t {}
class bar($one){notify{test: message => $one}}
notify{tester:}
'
get_scope
@scope.resource=Puppet::Parser::Resource.new('class', 't', :scope => @scope)
setup_scope
Puppet::Parser::Functions.function(:create_resources)
end
it 'should be able to create classes' do
@@ -123,15 +112,5 @@ notify{tester:}
it 'should fail to create non-existing classes' do
lambda { @scope.function_create_resources(['class', {'blah'=>{'one'=>'two'}}]) }.should raise_error(ArgumentError ,'could not find hostclass blah')
end
it 'should be able to add edges' do
@scope.function_create_resources(['class', {'bar'=>{'one'=>'two', 'require' => 'Notify[tester]'}}])
@scope.compiler.compile
rg = @scope.compiler.catalog.to_ral.relationship_graph
test = rg.vertices.find { |v| v.title == 'test' }
tester = rg.vertices.find { |v| v.title == 'tester' }
test.should be
tester.should be
rg.path_between(tester,test).should be
end
end
end