~nvalcarcel/ubuntu/lucid/puppet/fix-546677

« back to all changes in this revision

Viewing changes to spec/unit/parser/files.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

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
require 'puppet/parser/files'
 
6
 
 
7
describe Puppet::Parser::Files do
 
8
    it "should have a method for finding a template" do
 
9
        Puppet::Parser::Files.should respond_to(:find_template)
 
10
    end
 
11
 
 
12
    it "should have a method for finding manifests" do
 
13
        Puppet::Parser::Files.should respond_to(:find_manifests)
 
14
    end
 
15
 
 
16
    describe "when searching for templates" do
 
17
        it "should return fully-qualified templates directly" do
 
18
            Puppet::Parser::Files.expects(:modulepath).never
 
19
            Puppet::Parser::Files.find_template("/my/template").should == "/my/template"
 
20
        end
 
21
 
 
22
        it "should return the template from the first found module" do
 
23
            mod = mock 'module'
 
24
            Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
 
25
 
 
26
            mod.expects(:template).returns("/one/mymod/templates/mytemplate")
 
27
            Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
 
28
        end
 
29
 
 
30
        it "should return the file in the templatedir if it exists" do
 
31
            Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
 
32
            Puppet[:modulepath] = "/one:/two"
 
33
            File.stubs(:directory?).returns(true)
 
34
            FileTest.stubs(:exist?).returns(true)
 
35
            Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
 
36
        end
 
37
 
 
38
        it "should not raise an error if no valid templatedir exists and the template exists in a module" do
 
39
            mod = mock 'module'
 
40
            Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
 
41
 
 
42
            mod.expects(:template).returns("/one/mymod/templates/mytemplate")
 
43
            Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(nil)
 
44
 
 
45
            Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
 
46
        end
 
47
 
 
48
        it "should return unqualified templates if they exist in the template dir" do
 
49
            FileTest.stubs(:exist?).returns true
 
50
            Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
 
51
            Puppet::Parser::Files.find_template("mytemplate").should == "/my/templates/mytemplate"
 
52
        end
 
53
 
 
54
        it "should only return templates if they actually exist" do
 
55
            FileTest.expects(:exist?).with("/my/templates/mytemplate").returns true
 
56
            Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
 
57
            Puppet::Parser::Files.find_template("mytemplate").should == "/my/templates/mytemplate"
 
58
        end
 
59
 
 
60
        it "should return nil when asked for a template that doesn't exist" do
 
61
            FileTest.expects(:exist?).with("/my/templates/mytemplate").returns false
 
62
            Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
 
63
            Puppet::Parser::Files.find_template("mytemplate").should be_nil
 
64
        end
 
65
 
 
66
        it "should search in the template directories before modules" do
 
67
            FileTest.stubs(:exist?).returns true
 
68
            Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
 
69
            Puppet::Module.expects(:find).never
 
70
            Puppet::Parser::Files.find_template("mytemplate")
 
71
        end
 
72
 
 
73
        it "should accept relative templatedirs" do
 
74
            FileTest.stubs(:exist?).returns true
 
75
            Puppet[:templatedir] = "my/templates"
 
76
            File.expects(:directory?).with(File.join(Dir.getwd,"my/templates")).returns(true)
 
77
            Puppet::Parser::Files.find_template("mytemplate").should == File.join(Dir.getwd,"my/templates/mytemplate")
 
78
        end
 
79
 
 
80
        it "should use the environment templatedir if no module is found and an environment is specified" do
 
81
            FileTest.stubs(:exist?).returns true
 
82
            Puppet::Parser::Files.stubs(:templatepath).with("myenv").returns(["/myenv/templates"])
 
83
            Puppet::Parser::Files.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate"
 
84
        end
 
85
 
 
86
        it "should use first dir from environment templatedir if no module is found and an environment is specified" do
 
87
            FileTest.stubs(:exist?).returns true
 
88
            Puppet::Parser::Files.stubs(:templatepath).with("myenv").returns(["/myenv/templates", "/two/templates"])
 
89
            Puppet::Parser::Files.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate"
 
90
        end
 
91
 
 
92
        it "should use a valid dir when templatedir is a path for unqualified templates and the first dir contains template" do
 
93
            Puppet::Parser::Files.stubs(:templatepath).returns(["/one/templates", "/two/templates"])
 
94
            FileTest.expects(:exist?).with("/one/templates/mytemplate").returns(true)
 
95
            Puppet::Parser::Files.find_template("mytemplate").should == "/one/templates/mytemplate"
 
96
        end
 
97
 
 
98
        it "should use a valid dir when templatedir is a path for unqualified templates and only second dir contains template" do
 
99
            Puppet::Parser::Files.stubs(:templatepath).returns(["/one/templates", "/two/templates"])
 
100
            FileTest.expects(:exist?).with("/one/templates/mytemplate").returns(false)
 
101
            FileTest.expects(:exist?).with("/two/templates/mytemplate").returns(true)
 
102
            Puppet::Parser::Files.find_template("mytemplate").should == "/two/templates/mytemplate"
 
103
        end
 
104
 
 
105
        it "should use the node environment if specified" do
 
106
            mod = mock 'module'
 
107
            Puppet::Node::Environment.new("myenv").expects(:module).with("mymod").returns mod
 
108
 
 
109
            mod.expects(:template).returns("/my/modules/mymod/templates/envtemplate")
 
110
 
 
111
            Puppet::Parser::Files.find_template("mymod/envtemplate", "myenv").should == "/my/modules/mymod/templates/envtemplate"
 
112
        end
 
113
 
 
114
        it "should return nil if no template can be found" do
 
115
            Puppet::Parser::Files.find_template("foomod/envtemplate", "myenv").should be_nil
 
116
        end
 
117
 
 
118
        after { Puppet.settings.clear }
 
119
    end
 
120
 
 
121
    describe "when searching for manifests" do
 
122
        it "should ignore invalid modules" do
 
123
            mod = mock 'module'
 
124
            Puppet::Node::Environment.new.expects(:module).with("mymod").raises Puppet::Module::InvalidName
 
125
            Puppet.expects(:value).with(:modulepath).never
 
126
            Dir.stubs(:glob).returns %w{foo}
 
127
 
 
128
            Puppet::Parser::Files.find_manifests("mymod/init.pp").should == %w{foo}
 
129
        end
 
130
    end
 
131
 
 
132
    describe "when searching for manifests when no module is found" do
 
133
        before do
 
134
            File.stubs(:find).returns(nil)
 
135
        end
 
136
 
 
137
        it "should not look for modules when paths are fully qualified" do
 
138
            Puppet.expects(:value).with(:modulepath).never
 
139
            file = "/fully/qualified/file.pp"
 
140
            Dir.stubs(:glob).with(file).returns([file])
 
141
            Puppet::Parser::Files.find_manifests(file)
 
142
        end
 
143
 
 
144
        it "should directly return fully qualified files" do
 
145
            file = "/fully/qualified/file.pp"
 
146
            Dir.stubs(:glob).with(file).returns([file])
 
147
            Puppet::Parser::Files.find_manifests(file).should == [file]
 
148
        end
 
149
 
 
150
        it "should match against provided fully qualified patterns" do
 
151
            pattern = "/fully/qualified/pattern/*"
 
152
            Dir.expects(:glob).with(pattern).returns(%w{my file list})
 
153
            Puppet::Parser::Files.find_manifests(pattern).should == %w{my file list}
 
154
        end
 
155
 
 
156
        it "should look for files relative to the current directory" do
 
157
            cwd = Dir.getwd
 
158
            Dir.expects(:glob).with("#{cwd}/foobar/init.pp").returns(["#{cwd}/foobar/init.pp"])
 
159
            Puppet::Parser::Files.find_manifests("foobar/init.pp").should == ["#{cwd}/foobar/init.pp"]
 
160
        end
 
161
 
 
162
        it "should only return files, not directories" do
 
163
            pattern = "/fully/qualified/pattern/*"
 
164
            file = "/my/file"
 
165
            dir = "/my/directory"
 
166
            Dir.expects(:glob).with(pattern).returns([file, dir])
 
167
            FileTest.expects(:directory?).with(file).returns(false)
 
168
            FileTest.expects(:directory?).with(dir).returns(true)
 
169
            Puppet::Parser::Files.find_manifests(pattern).should == [file]
 
170
        end
 
171
    end
 
172
 
 
173
    describe "when searching for manifests in a found module" do
 
174
        it "should return the manifests from the first found module" do
 
175
            mod = mock 'module'
 
176
            Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
 
177
            mod.expects(:match_manifests).with("init.pp").returns(%w{/one/mymod/manifests/init.pp})
 
178
            Puppet::Parser::Files.find_manifests("mymod/init.pp").should == ["/one/mymod/manifests/init.pp"]
 
179
        end
 
180
 
 
181
        it "should use the node environment if specified" do
 
182
            mod = mock 'module'
 
183
            Puppet::Node::Environment.new("myenv").expects(:module).with("mymod").returns mod
 
184
            mod.expects(:match_manifests).with("init.pp").returns(%w{/one/mymod/manifests/init.pp})
 
185
            Puppet::Parser::Files.find_manifests("mymod/init.pp", :environment => "myenv").should == ["/one/mymod/manifests/init.pp"]
 
186
        end
 
187
 
 
188
        after { Puppet.settings.clear }
 
189
    end
 
190
end