summaryrefslogtreecommitdiffstats
path: root/spec/unit/module.rb
blob: e79001ae130b385e47dd77135f12f069237ee1c9 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env ruby

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

describe Puppet::Module, " when building its search path" do
    include PuppetTest

    it "should fully qualify unqualified paths in the search path" do
        Puppet[:modulepath] = "something:/my/something"
        File.stubs(:directory?).returns(true)
        Puppet::Module.modulepath.should == [File.join(Dir.getwd, 'something'), "/my/something"]
    end

    it "should ignore paths that do not exist" do
        Puppet[:modulepath] = "/yes:/no"
        File.expects(:directory?).with("/yes").returns(true)
        File.expects(:directory?).with("/no").returns(false)
        Puppet::Module.modulepath.should == %w{/yes}
    end

    it "should prepend PUPPETLIB in search path when set" do
        Puppet[:modulepath] = "/my/mod:/other/mod"
        ENV["PUPPETLIB"] = "/env/mod:/myenv/mod"
        File.stubs(:directory?).returns(true)
        Puppet::Module.modulepath.should == %w{/env/mod /myenv/mod /my/mod /other/mod}
    end

    it "should use the environment-specific search path when a node environment is provided" do
        Puppet.settings.expects(:value).with(:modulepath, "myenv").returns("/mone:/mtwo")
        File.stubs(:directory?).returns(true)
        Puppet::Module.modulepath("myenv").should == %w{/mone /mtwo}
    end

    after do
        ENV["PUPPETLIB"] = nil
    end
end

describe Puppet::Module, " when searching for modules" do
    it "should find modules in the search path" do
        path = %w{/dir/path}
        Puppet::Module.stubs(:modulepath).returns(path)
        File.stubs(:directory?).returns(true)
        mod = Puppet::Module.find("mymod")
        mod.should be_an_instance_of(Puppet::Module)
        mod.path.should == "/dir/path/mymod"
    end

    it "should not search for fully qualified modules" do
        path = %w{/dir/path}
        Puppet::Module.expects(:modulepath).never
        File.expects(:directory?).never
        Puppet::Module.find("/mymod").should be_nil
    end

    it "should search for modules in the order specified in the search path" do
        Puppet[:modulepath] = "/one:/two:/three"
        Puppet::Module.stubs(:modulepath).returns %w{/one /two /three}
        File.expects(:directory?).with("/one/mod").returns(false)
        File.expects(:directory?).with("/two/mod").returns(true)
        File.expects(:directory?).with("/three/mod").never
        mod = Puppet::Module.find("mod")
        mod.path.should == "/two/mod"
    end

    it "should use a node environment if specified" do
        Puppet::Module.expects(:modulepath).with("myenv").returns([])
        Puppet::Module.find("mymod", "myenv")
    end
end

describe Puppet::Module, " when searching for templates" do
    it "should return fully-qualified templates directly" do
        Puppet::Module.expects(:modulepath).never
        Puppet::Module.find_template("/my/template").should == "/my/template"
    end

    it "should return the template from the first found module" do
        Puppet[:modulepath] = "/one:/two"
        File.stubs(:directory?).returns(true)
        Puppet::Module.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
    end
    
    it "should return the file in the templatedir if it exists" do
        Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
        Puppet[:modulepath] = "/one:/two"
        File.stubs(:directory?).returns(true)
        File.stubs(:exists?).returns(true)
        Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
    end

    it "should use the main templatedir if no module is found" do
        Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
        Puppet::Module.expects(:find).with("mymod", nil).returns(nil)
        Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
    end

    it "should return unqualified templates directly in the template dir" do
        Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
        Puppet::Module.expects(:find).never
        Puppet::Module.find_template("mytemplate").should == "/my/templates/mytemplate"
    end

    it "should use the environment templatedir if no module is found and an environment is specified" do
        Puppet.settings.expects(:value).with(:templatedir, "myenv").returns("/myenv/templates")
        Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil)
        Puppet::Module.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate"
    end

    it "should use the node environment if specified" do
        Puppet.settings.stubs(:value).returns.returns("/my/directory")
        Puppet.settings.expects(:value).with(:modulepath, "myenv").returns("/my/modules")
        File.stubs(:directory?).returns(true)
        Puppet::Module.find_template("mymod/envtemplate", "myenv").should == "/my/modules/mymod/templates/envtemplate"
    end

    after { Puppet.settings.clear }
end

describe Puppet::Module, " when searching for manifests when no module is found" do
    before do
        File.stubs(:find).returns(nil)
    end

    it "should not look for modules when paths are fully qualified" do
        Puppet.expects(:value).with(:modulepath).never
        file = "/fully/qualified/file.pp"
        Dir.stubs(:glob).with(file).returns([file])
        Puppet::Module.find_manifests(file)
    end

    it "should directly return fully qualified files" do
        file = "/fully/qualified/file.pp"
        Dir.stubs(:glob).with(file).returns([file])
        Puppet::Module.find_manifests(file).should == [file]
    end

    it "should match against provided fully qualified patterns" do
        pattern = "/fully/qualified/pattern/*"
        Dir.expects(:glob).with(pattern).returns(%w{my file list})
        Puppet::Module.find_manifests(pattern).should == %w{my file list}
    end

    it "should look for files relative to the current directory" do
        cwd = Dir.getwd
        Dir.expects(:glob).with("#{cwd}/foobar/init.pp").returns(["#{cwd}/foobar/init.pp"])
        Puppet::Module.find_manifests("foobar/init.pp").should == ["#{cwd}/foobar/init.pp"]
    end

    it "should only return files, not directories" do
        pattern = "/fully/qualified/pattern/*"
        file = "/my/file"
        dir = "/my/directory"
        Dir.expects(:glob).with(pattern).returns([file, dir])
        FileTest.expects(:directory?).with(file).returns(false)
        FileTest.expects(:directory?).with(dir).returns(true)
        Puppet::Module.find_manifests(pattern).should == [file]
    end
end

describe Puppet::Module, " when searching for manifests in a found module" do
    it "should return the manifests from the first found module" do
        Puppet[:modulepath] = "/one:/two"
        File.stubs(:directory?).returns(true)
        Dir.expects(:glob).with("/one/mymod/manifests/init.pp").returns(%w{/one/mymod/manifests/init.pp})
        Puppet::Module.find_manifests("mymod/init.pp").should == ["/one/mymod/manifests/init.pp"]
    end

    it "should use the node environment if specified" do
        Puppet.settings.expects(:value).with(:modulepath, "myenv").returns("/env/modules")
        File.stubs(:directory?).returns(true)
        Dir.expects(:glob).with("/env/modules/mymod/manifests/envmanifest.pp").returns(%w{/env/modules/mymod/manifests/envmanifest.pp})
        Puppet::Module.find_manifests("mymod/envmanifest.pp", :environment => "myenv").should == ["/env/modules/mymod/manifests/envmanifest.pp"]
    end

    it "should return all manifests matching the glob pattern" do
        Puppet.settings.expects(:value).with(:modulepath, nil).returns("/my/modules")
        File.stubs(:directory?).returns(true)
        Dir.expects(:glob).with("/my/modules/mymod/manifests/yay/*.pp").returns(%w{/one /two})
        Puppet::Module.find_manifests("mymod/yay/*.pp").should == %w{/one /two}
    end

    it "should not return directories" do
        Puppet.settings.expects(:value).with(:modulepath, nil).returns("/my/modules")
        File.stubs(:directory?).returns(true)
        Dir.expects(:glob).with("/my/modules/mymod/manifests/yay/*.pp").returns(%w{/one /two})
        FileTest.expects(:directory?).with("/one").returns false
        FileTest.expects(:directory?).with("/two").returns true
        Puppet::Module.find_manifests("mymod/yay/*.pp").should == %w{/one}
    end

    it "should default to the 'init.pp' file in the manifests directory" do
        Puppet.settings.expects(:value).with(:modulepath, nil).returns("/my/modules")
        File.stubs(:directory?).returns(true)
        Dir.expects(:glob).with("/my/modules/mymod/manifests/init.pp").returns(%w{my manifest})
        Puppet::Module.find_manifests("mymod").should == %w{my manifest}
    end

    after { Puppet.settings.clear }
end

describe Puppet::Module, " when returning files" do
    it "should return the path to the module's 'files' directory" do
        mod = Puppet::Module.send(:new, "mymod", "/my/mod")
        mod.files.should == "/my/mod/files"
    end
end