summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/functions/require_spec.rb
blob: 692b353057039a39f9d786ef87772fd91085053a (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
#!/usr/bin/env rspec
require 'spec_helper'

describe "the require function" do
  before :all do
    Puppet::Parser::Functions.autoloader.loadall
  end

  before :each do
    @catalog = stub 'catalog'
    @compiler = stub 'compiler', :catalog => @catalog, :environment => nil

    @scope = Puppet::Parser::Scope.new
    @scope.stubs(:findresource)
    @scope.stubs(:compiler).returns(@compiler)
    @klass = stub 'class', :name => "myclass"
    @scope.stubs(:find_hostclass).returns(@klass)

    @resource = Puppet::Parser::Resource.new(:file, "/my/file", :scope => @scope, :source => "source")
    @resource.stubs(:metaparam_compatibility_mode?).returns false
    @scope.stubs(:resource).returns @resource
  end

  it "should exist" do
    Puppet::Parser::Functions.function("require").should == "function_require"
  end

  it "should delegate to the 'include' puppet function" do
    @scope.expects(:function_include).with("myclass")

    @scope.function_require("myclass")
  end

  it "should set the 'require' prarameter on the resource to a resource reference" do
    @scope.stubs(:function_include)
    @scope.function_require("myclass")

    @resource["require"].should be_instance_of(Array)
    @resource["require"][0].should be_instance_of(Puppet::Resource)
  end

  it "should verify the 'include' function is loaded" do
    Puppet::Parser::Functions.expects(:function).with(:include).returns(:function_include)
    @scope.stubs(:function_include)
    @scope.function_require("myclass")
  end

  it "should include the class but not add a dependency if used on a client not at least version 0.25" do
    @resource.expects(:metaparam_compatibility_mode?).returns true
    @scope.expects(:warning)
    @resource.expects(:set_parameter).never
    @scope.expects(:function_include)

    @scope.function_require("myclass")
  end

  it "should lookup the absolute class path" do
    @scope.stubs(:function_include)

    @scope.expects(:find_hostclass).with("myclass").returns(@klass)
    @klass.expects(:name).returns("myclass")

    @scope.function_require("myclass")
  end

  it "should append the required class to the require parameter" do
    @scope.stubs(:function_include)

    one = Puppet::Resource.new(:file, "/one")
    @resource[:require] = one
    @scope.function_require("myclass")

    @resource[:require].should be_include(one)
    @resource[:require].detect { |r| r.to_s == "Class[Myclass]" }.should be_instance_of(Puppet::Resource)
  end
end