diff options
| author | Paul Berry <paul@puppetlabs.com> | 2010-09-02 18:10:37 -0700 |
|---|---|---|
| committer | Paul Berry <paul@puppetlabs.com> | 2010-09-02 18:37:28 -0700 |
| commit | 6b278503021c4404904f56ced6995d0fbfa5b8fe (patch) | |
| tree | 134f89786a38137a89dc6e846920d9dfc57e59c3 /spec/unit/dsl | |
| parent | 25048ecc40db746f7e88bb6c5e1fc4f2c0150a4f (diff) | |
| download | puppet-6b278503021c4404904f56ced6995d0fbfa5b8fe.tar.gz puppet-6b278503021c4404904f56ced6995d0fbfa5b8fe.tar.xz puppet-6b278503021c4404904f56ced6995d0fbfa5b8fe.zip | |
[#4657] Customer-supplied .rb files are not compatible with multiple environments or staleness check
Changed the resource type API to create AST objects rather than
directly instantiating resource types. This allows the same code
paths to be used to handle the results of parsing both .pp and .rb
files. This makes .rb files work properly in multiple environments,
because the types are now instantiated by code that is aware of which
environment the compilation is happening in. It also reduces the risk
of future changes breaking .rb file support.
Also, switched to using "instance_eval" rather than "require" to
evaluate the contents of the .rb file. This ensures that if the file
has to be recompiled (because it became stale), it will actually get
re-evaluated. As a side benefit, ResourceTypeAPI is now a class
rather than a mixin to Object, so its methods do not pollute the
global namespace.
To reduce the risk of customers coming to rely on implementation
details of the resource type API, changed its methods to return nil,
and removed methods from it that were misleadingly labeled as
"private".
Diffstat (limited to 'spec/unit/dsl')
| -rwxr-xr-x | spec/unit/dsl/resource_type_api_spec.rb | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/spec/unit/dsl/resource_type_api_spec.rb b/spec/unit/dsl/resource_type_api_spec.rb index 4b8ccf5a7..c9a5d272f 100755 --- a/spec/unit/dsl/resource_type_api_spec.rb +++ b/spec/unit/dsl/resource_type_api_spec.rb @@ -4,41 +4,51 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/dsl/resource_type_api' -class DSLAPITester - include Puppet::DSL::ResourceTypeAPI -end - describe Puppet::DSL::ResourceTypeAPI do - before do - @api = DSLAPITester.new + # Verify that the block creates a single AST node through the API, + # instantiate that AST node into a types, and return that type. + def test_api_call(&block) + main_object = Puppet::DSL::ResourceTypeAPI.new + main_object.instance_eval(&block) + created_ast_objects = main_object.instance_eval { @__created_ast_objects__ } + created_ast_objects.length.should == 1 + new_types = created_ast_objects[0].instantiate('') + new_types.length.should == 1 + new_types[0] + ensure + Thread.current[:ruby_file_parse_result] = nil end [:definition, :node, :hostclass].each do |type| method = type == :definition ? "define" : type it "should be able to create a #{type}" do - newtype = @api.send(method, "myname") + newtype = test_api_call { send(method, "myname").should == nil } newtype.should be_a(Puppet::Resource::Type) newtype.type.should == type end it "should use the provided name when creating a #{type}" do - newtype = @api.send(method, "myname") + newtype = test_api_call { send(method, "myname") } newtype.name.should == "myname" end unless type == :definition it "should pass in any provided options when creating a #{type}" do - newtype = @api.send(method, "myname", :line => 200) + newtype = test_api_call { send(method, "myname", :line => 200) } newtype.line.should == 200 end end - it "should set any provided block as the type's ruby code" - - it "should add the type to the current environment's known resource types" + it "should set any provided block as the type's ruby code" do + newtype = test_api_call { send(method, "myname") { 'method_result' } } + newtype.ruby_code.call.should == 'method_result' + end end describe "when creating a definition" do - it "should use the provided options to define valid arguments for the resource type" + it "should use the provided options to define valid arguments for the resource type" do + newtype = test_api_call { define("myname", :arg1, :arg2) } + newtype.arguments.should == { 'arg1' => nil, 'arg2' => nil } + end end end |
