summaryrefslogtreecommitdiffstats
path: root/spec/unit/node/environment.rb
blob: 20889f071995216ee4d4d860116ef381b03fca18 (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
#!/usr/bin/env ruby

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

require 'puppet/node/environment'
require 'puppet/util/execution'

describe Puppet::Node::Environment do
    it "should use the default environment if no name is provided while initializing an environment" do
        Puppet.settings.expects(:value).with(:environment).returns("one")
        Puppet::Node::Environment.new().name.should == :one
    end

    it "should treat environment instances as singletons" do
        Puppet::Node::Environment.new("one").should equal(Puppet::Node::Environment.new("one"))
    end

    it "should treat an environment specified as names or strings as equivalent" do
        Puppet::Node::Environment.new(:one).should equal(Puppet::Node::Environment.new("one"))
    end

    it "should consider its module path to be the environment-specific modulepath setting" do
        FileTest.stubs(:directory?).returns true
        env = Puppet::Node::Environment.new("testing")
        module_path = %w{/one /two}.join(File::PATH_SEPARATOR)
        env.expects(:[]).with(:modulepath).returns module_path

        env.modulepath.should == %w{/one /two}
    end

    it "should prefix the value of the 'PUPPETLIB' environment variable to the module path if present" do
        FileTest.stubs(:directory?).returns true
        Puppet::Util::Execution.withenv("PUPPETLIB" => %w{/l1 /l2}.join(File::PATH_SEPARATOR)) do
            env = Puppet::Node::Environment.new("testing")
            module_path = %w{/one /two}.join(File::PATH_SEPARATOR)
            env.expects(:[]).with(:modulepath).returns module_path

            env.modulepath.should == %w{/l1 /l2 /one /two}
        end
    end

    it "should not return non-directories in the module path" do
        env = Puppet::Node::Environment.new("testing")
        module_path = %w{/one /two}.join(File::PATH_SEPARATOR)
        env.expects(:[]).with(:modulepath).returns module_path

        FileTest.expects(:directory?).with("/one").returns true
        FileTest.expects(:directory?).with("/two").returns false

        env.modulepath.should == %w{/one}
    end

    it "should use the current working directory to fully-qualify unqualified paths" do
        FileTest.stubs(:directory?).returns true
        env = Puppet::Node::Environment.new("testing")
        module_path = %w{/one two}.join(File::PATH_SEPARATOR)
        env.expects(:[]).with(:modulepath).returns module_path

        two = File.join(Dir.getwd, "two")
        env.modulepath.should == ["/one", two]
    end

    describe "when modeling a specific environment" do
        it "should have a method for returning the environment name" do
            Puppet::Node::Environment.new("testing").name.should == :testing
        end

        it "should provide an array-like accessor method for returning any environment-specific setting" do
            env = Puppet::Node::Environment.new("testing")
            env.should respond_to(:[])
        end

        it "should ask the Puppet settings instance for the setting qualified with the environment name" do
            Puppet.settings.expects(:value).with("myvar", :testing).returns("myval")
            env = Puppet::Node::Environment.new("testing")
            env["myvar"].should == "myval"
        end

        it "should be able to return its modules" do
            Puppet::Node::Environment.new("testing").should respond_to(:modules)
        end

        it "should return each module from the environment-specific module path when asked for its modules" do
            env = Puppet::Node::Environment.new("testing")
            module_path = %w{/one /two}.join(File::PATH_SEPARATOR)
            env.expects(:modulepath).returns module_path

            Puppet::Module.expects(:each_module).with(module_path).multiple_yields("mod1", "mod2")

            env.modules.should == %w{mod1 mod2}
        end

        it "should be able to return an individual module by matching the module name" do
            env = Puppet::Node::Environment.new("testing")
            module_path = %w{/one /two}.join(File::PATH_SEPARATOR)
            env.expects(:modulepath).returns module_path

            one = stub 'one', :name => 'one'
            two = stub 'two', :name => 'two'
            Puppet::Module.expects(:each_module).with(module_path).multiple_yields(one, two)

            env.module("two").should equal(two)
        end

        it "should return nil if asked for a module that is not in its path" do
            env = Puppet::Node::Environment.new("testing")
            module_path = %w{/one /two}.join(File::PATH_SEPARATOR)
            env.expects(:modulepath).returns module_path

            one = stub 'one', :name => 'one'
            two = stub 'two', :name => 'two'
            Puppet::Module.expects(:each_module).with(module_path).multiple_yields(one, two)

            env.module("three").should be_nil
        end
    end
end