~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to spec/unit/module.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mfrom: (1.1.8 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080726154345-c03m49twzxewdwjn
Tags: 0.24.5-2
* Fix puppetlast to work with 0.24.5
* Adjust logcheck to match against new log messages in 0.24.5
* Update standards version to 3.8.0 (no changes)
* Update changelog to reduce length of line to make lintian happy

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require File.dirname(__FILE__) + '/../spec_helper'
 
4
 
 
5
describe Puppet::Module, " when building its search path" do
 
6
    include PuppetTest
 
7
 
 
8
    it "should fully qualify unqualified paths in the search path" do
 
9
        Puppet[:modulepath] = "something:/my/something"
 
10
        File.stubs(:directory?).returns(true)
 
11
        Puppet::Module.modulepath.should == [File.join(Dir.getwd, 'something'), "/my/something"]
 
12
    end
 
13
 
 
14
    it "should ignore paths that do not exist" do
 
15
        Puppet[:modulepath] = "/yes:/no"
 
16
        File.expects(:directory?).with("/yes").returns(true)
 
17
        File.expects(:directory?).with("/no").returns(false)
 
18
        Puppet::Module.modulepath.should == %w{/yes}
 
19
    end
 
20
 
 
21
    it "should prepend PUPPETLIB in search path when set" do
 
22
        Puppet[:modulepath] = "/my/mod:/other/mod"
 
23
        ENV["PUPPETLIB"] = "/env/mod:/myenv/mod"
 
24
        File.stubs(:directory?).returns(true)
 
25
        Puppet::Module.modulepath.should == %w{/env/mod /myenv/mod /my/mod /other/mod}
 
26
    end
 
27
 
 
28
    it "should use the environment-specific search path when a node environment is provided" do
 
29
        Puppet.settings.expects(:value).with(:modulepath, "myenv").returns("/mone:/mtwo")
 
30
        File.stubs(:directory?).returns(true)
 
31
        Puppet::Module.modulepath("myenv").should == %w{/mone /mtwo}
 
32
    end
 
33
 
 
34
    after do
 
35
        ENV["PUPPETLIB"] = nil
 
36
    end
 
37
end
 
38
 
 
39
describe Puppet::Module, " when searching for modules" do
 
40
    it "should find modules in the search path" do
 
41
        path = %w{/dir/path}
 
42
        Puppet::Module.stubs(:modulepath).returns(path)
 
43
        File.stubs(:directory?).returns(true)
 
44
        mod = Puppet::Module.find("mymod")
 
45
        mod.should be_an_instance_of(Puppet::Module)
 
46
        mod.path.should == "/dir/path/mymod"
 
47
    end
 
48
 
 
49
    it "should not search for fully qualified modules" do
 
50
        path = %w{/dir/path}
 
51
        Puppet::Module.expects(:modulepath).never
 
52
        File.expects(:directory?).never
 
53
        Puppet::Module.find("/mymod").should be_nil
 
54
    end
 
55
 
 
56
    it "should search for modules in the order specified in the search path" do
 
57
        Puppet[:modulepath] = "/one:/two:/three"
 
58
        Puppet::Module.stubs(:modulepath).returns %w{/one /two /three}
 
59
        File.expects(:directory?).with("/one/mod").returns(false)
 
60
        File.expects(:directory?).with("/two/mod").returns(true)
 
61
        File.expects(:directory?).with("/three/mod").never
 
62
        mod = Puppet::Module.find("mod")
 
63
        mod.path.should == "/two/mod"
 
64
    end
 
65
 
 
66
    it "should use a node environment if specified" do
 
67
        Puppet::Module.expects(:modulepath).with("myenv").returns([])
 
68
        Puppet::Module.find("mymod", "myenv")
 
69
    end
 
70
end
 
71
 
 
72
describe Puppet::Module, " when searching for templates" do
 
73
    it "should return fully-qualified templates directly" do
 
74
        Puppet::Module.expects(:modulepath).never
 
75
        Puppet::Module.find_template("/my/template").should == "/my/template"
 
76
    end
 
77
 
 
78
    it "should return the template from the first found module" do
 
79
        Puppet[:modulepath] = "/one:/two"
 
80
        File.stubs(:directory?).returns(true)
 
81
        Puppet::Module.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
 
82
    end
 
83
    
 
84
    it "should return the file in the templatedir if it exists" do
 
85
        Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
 
86
        Puppet[:modulepath] = "/one:/two"
 
87
        File.stubs(:directory?).returns(true)
 
88
        File.stubs(:exists?).returns(true)
 
89
        Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
 
90
    end
 
91
 
 
92
    it "should use the main templatedir if no module is found" do
 
93
        Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
 
94
        Puppet::Module.expects(:find).with("mymod", nil).returns(nil)
 
95
        Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
 
96
    end
 
97
 
 
98
    it "should return unqualified templates directly in the template dir" do
 
99
        Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
 
100
        Puppet::Module.expects(:find).never
 
101
        Puppet::Module.find_template("mytemplate").should == "/my/templates/mytemplate"
 
102
    end
 
103
 
 
104
    it "should use the environment templatedir if no module is found and an environment is specified" do
 
105
        Puppet.settings.expects(:value).with(:templatedir, "myenv").returns("/myenv/templates")
 
106
        Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil)
 
107
        Puppet::Module.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate"
 
108
    end
 
109
 
 
110
    it "should use the node environment if specified" do
 
111
        Puppet.settings.stubs(:value).returns.returns("/my/directory")
 
112
        Puppet.settings.expects(:value).with(:modulepath, "myenv").returns("/my/modules")
 
113
        File.stubs(:directory?).returns(true)
 
114
        Puppet::Module.find_template("mymod/envtemplate", "myenv").should == "/my/modules/mymod/templates/envtemplate"
 
115
    end
 
116
 
 
117
    after { Puppet.settings.clear }
 
118
end
 
119
 
 
120
describe Puppet::Module, " when searching for manifests when no module is found" do
 
121
    before do
 
122
        File.stubs(:find).returns(nil)
 
123
    end
 
124
 
 
125
    it "should not look for modules when paths are fully qualified" do
 
126
        Puppet.expects(:value).with(:modulepath).never
 
127
        file = "/fully/qualified/file.pp"
 
128
        Dir.stubs(:glob).with(file).returns([file])
 
129
        Puppet::Module.find_manifests(file)
 
130
    end
 
131
 
 
132
    it "should directly return fully qualified files" do
 
133
        file = "/fully/qualified/file.pp"
 
134
        Dir.stubs(:glob).with(file).returns([file])
 
135
        Puppet::Module.find_manifests(file).should == [file]
 
136
    end
 
137
 
 
138
    it "should match against provided fully qualified patterns" do
 
139
        pattern = "/fully/qualified/pattern/*"
 
140
        Dir.expects(:glob).with(pattern).returns(%w{my file list})
 
141
        Puppet::Module.find_manifests(pattern).should == %w{my file list}
 
142
    end
 
143
 
 
144
    it "should look for files relative to the current directory" do
 
145
        cwd = Dir.getwd
 
146
        Dir.expects(:glob).with("#{cwd}/foobar/init.pp").returns(["#{cwd}/foobar/init.pp"])
 
147
        Puppet::Module.find_manifests("foobar/init.pp").should == ["#{cwd}/foobar/init.pp"]
 
148
    end
 
149
 
 
150
    it "should only return files, not directories" do
 
151
        pattern = "/fully/qualified/pattern/*"
 
152
        file = "/my/file"
 
153
        dir = "/my/directory"
 
154
        Dir.expects(:glob).with(pattern).returns([file, dir])
 
155
        FileTest.expects(:directory?).with(file).returns(false)
 
156
        FileTest.expects(:directory?).with(dir).returns(true)
 
157
        Puppet::Module.find_manifests(pattern).should == [file]
 
158
    end
 
159
end
 
160
 
 
161
describe Puppet::Module, " when searching for manifests in a found module" do
 
162
    it "should return the manifests from the first found module" do
 
163
        Puppet[:modulepath] = "/one:/two"
 
164
        File.stubs(:directory?).returns(true)
 
165
        Dir.expects(:glob).with("/one/mymod/manifests/init.pp").returns(%w{/one/mymod/manifests/init.pp})
 
166
        Puppet::Module.find_manifests("mymod/init.pp").should == ["/one/mymod/manifests/init.pp"]
 
167
    end
 
168
 
 
169
    it "should use the node environment if specified" do
 
170
        Puppet.settings.expects(:value).with(:modulepath, "myenv").returns("/env/modules")
 
171
        File.stubs(:directory?).returns(true)
 
172
        Dir.expects(:glob).with("/env/modules/mymod/manifests/envmanifest.pp").returns(%w{/env/modules/mymod/manifests/envmanifest.pp})
 
173
        Puppet::Module.find_manifests("mymod/envmanifest.pp", :environment => "myenv").should == ["/env/modules/mymod/manifests/envmanifest.pp"]
 
174
    end
 
175
 
 
176
    it "should return all manifests matching the glob pattern" do
 
177
        Puppet.settings.expects(:value).with(:modulepath, nil).returns("/my/modules")
 
178
        File.stubs(:directory?).returns(true)
 
179
        Dir.expects(:glob).with("/my/modules/mymod/manifests/yay/*.pp").returns(%w{/one /two})
 
180
        Puppet::Module.find_manifests("mymod/yay/*.pp").should == %w{/one /two}
 
181
    end
 
182
 
 
183
    it "should not return directories" do
 
184
        Puppet.settings.expects(:value).with(:modulepath, nil).returns("/my/modules")
 
185
        File.stubs(:directory?).returns(true)
 
186
        Dir.expects(:glob).with("/my/modules/mymod/manifests/yay/*.pp").returns(%w{/one /two})
 
187
        FileTest.expects(:directory?).with("/one").returns false
 
188
        FileTest.expects(:directory?).with("/two").returns true
 
189
        Puppet::Module.find_manifests("mymod/yay/*.pp").should == %w{/one}
 
190
    end
 
191
 
 
192
    it "should default to the 'init.pp' file in the manifests directory" do
 
193
        Puppet.settings.expects(:value).with(:modulepath, nil).returns("/my/modules")
 
194
        File.stubs(:directory?).returns(true)
 
195
        Dir.expects(:glob).with("/my/modules/mymod/manifests/init.pp").returns(%w{my manifest})
 
196
        Puppet::Module.find_manifests("mymod").should == %w{my manifest}
 
197
    end
 
198
 
 
199
    after { Puppet.settings.clear }
 
200
end
 
201
 
 
202
describe Puppet::Module, " when returning files" do
 
203
    it "should return the path to the module's 'files' directory" do
 
204
        mod = Puppet::Module.send(:new, "mymod", "/my/mod")
 
205
        mod.files.should == "/my/mod/files"
 
206
    end
 
207
end