summaryrefslogtreecommitdiffstats
path: root/spec/unit/dsl/resource_api.rb
blob: 11ea5005232ba1cb532948d51e3abbde76d3de51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/dsl/resource_api'

class DSLResourceAPITester
    include Puppet::DSL::ResourceAPI
end

describe Puppet::DSL::ResourceAPI do
    before do
        @resource = Puppet::Parser::Resource.new(:type => :mytype, :title => "myresource", :scope => mock("scope"), :source => mock("source"))
        class << @resource
            include Puppet::DSL::ResourceAPI
        end
    end

    it "should include the resource type collection helper" do
        Puppet::DSL::ResourceAPI.ancestors.should be_include(Puppet::Resource::TypeCollectionHelper)
    end

    it "should be able to set all of its parameters as instance variables" do
        @resource["foo"] = "myval"
        @resource.set_instance_variables
        @resource.instance_variable_get("@foo").should == "myval"
    end

    describe "when calling a function" do
        it "should return false if the function does not exist" do
            Puppet::Parser::Functions.expects(:function).with("myfunc").returns nil
            @resource.call_function("myfunc", "foo").should be_false
        end

        it "should use the scope the call the provided function with the provided arguments and return the results" do
            scope = stub 'scope'
            @resource.stubs(:scope).returns scope
            Puppet::Parser::Functions.expects(:function).with("myfunc").returns "myfunc_method"

            scope.expects(:myfunc_method).with("one", "two")
            @resource.call_function("myfunc", ["one", "two"])
        end
    end

    describe "when determining if a provided name is a valid type" do
        it "should be valid if it's :class" do
            @resource.should be_valid_type(:class)
        end

        it "should be valid if it's :node" do
            @resource.should be_valid_type(:node)
        end

        it "should be valid if it's a builtin type" do
            Puppet::Type.expects(:type).with(:mytype).returns "whatever"
            @resource.should be_valid_type(:mytype)
        end

        it "should be valid if it's a defined resource type in the environment's known resource types" do
            collection = stub 'collection'
            @resource.stubs(:known_resource_types).returns collection
            collection.expects(:definition).with(:mytype).returns "whatever"
            @resource.should be_valid_type(:mytype)
        end

        it "should not be valid unless it's a node, class, builtin type, or defined resource" do
            collection = stub 'collection'
            @resource.stubs(:known_resource_types).returns collection
            collection.expects(:definition).returns nil
            Puppet::Type.expects(:type).returns nil
            @resource.should_not be_valid_type(:mytype)
        end
    end

    describe "when creating a resource" do
        before do
            @resource.scope.stubs(:source).returns stub("source")
            @resource.scope.stubs(:compiler).returns stub("compiler", :add_resource => nil)
            @created_resource = Puppet::Parser::Resource.new(:title => "eh", :type => 'yay', :scope => @resource.scope)
        end

        it "should create and return a resource of the type specified" do
            Puppet::Parser::Resource.expects(:new).with { |args| args[:type] == "mytype" }.returns @created_resource
            @resource.create_resource("mytype", "myname", {:foo => "bar"}).should == [@created_resource]
        end

        it "should use the name from the first element of the provided argument array" do
            Puppet::Parser::Resource.expects(:new).with { |args| args[:title] == "myname" }.returns @created_resource
            @resource.create_resource("mytype", "myname", {:foo => "bar"})
        end

        it "should create multiple resources if the first element of the argument array is an array" do
            second_resource = Puppet::Parser::Resource.new(:title => "eh", :type => 'yay', :scope => @resource.scope)
            Puppet::Parser::Resource.expects(:new).with { |args| args[:title] == "first" }.returns @created_resource
            Puppet::Parser::Resource.expects(:new).with { |args| args[:title] == "second" }.returns @created_resource
            @resource.create_resource("mytype", ["first", "second"], {:foo => "bar"})
        end

        it "should provide its scope as the scope" do
            Puppet::Parser::Resource.expects(:new).with { |args| args[:scope] == @resource.scope }.returns @created_resource
            @resource.create_resource("mytype", "myname", {:foo => "bar"})
        end

        it "should set each provided argument as a parameter on the created resource" do
            result = @resource.create_resource("mytype", "myname", {"foo" => "bar", "biz" => "baz"}).shift
            result["foo"].should == "bar"
            result["biz"].should == "baz"
        end

        it "should add the resource to the scope's copmiler" do
            Puppet::Parser::Resource.expects(:new).returns @created_resource
            @resource.scope.compiler.expects(:add_resource).with(@resource.scope, @created_resource)
            @resource.create_resource("mytype", "myname", {:foo => "bar"})
        end

        it "should fail if the resource parameters are not a hash" do
            lambda { @resource.create_resource("mytype", "myname", %w{foo bar}) }.should raise_error(ArgumentError)
        end
    end

    describe "when an unknown method is called" do
        it "should create a resource if the method name is a valid type" do
            @resource.expects(:valid_type?).with(:mytype).returns true
            @resource.expects(:create_resource).with(:mytype, "myname", {:foo => "bar"}).returns true

            @resource.mytype("myname", :foo => "bar")
        end

        it "should call any function whose name matches the undefined method if the name is not a valid type" do
            @resource.expects(:valid_type?).with(:myfunc).returns false
            @resource.expects(:create_resource).never

            Puppet::Parser::Functions.expects(:function).with(:myfunc).returns true

            @resource.expects(:call_function).with(:myfunc, %w{foo bar})

            @resource.myfunc("foo", "bar")
        end

        it "should raise a method missing error if the method is neither a type nor a function" do
            @resource.expects(:valid_type?).with(:myfunc).returns false
            @resource.expects(:create_resource).never

            Puppet::Parser::Functions.expects(:function).with(:myfunc).returns false

            @resource.expects(:call_function).never

            lambda { @resource.myfunc("foo", "bar") }.should raise_error(NoMethodError)
        end
    end
end