~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to spec/unit/parser/ast/selector.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
describe Puppet::Parser::AST::Selector do
 
6
    before :each do
 
7
        @scope = Puppet::Parser::Scope.new()
 
8
    end
 
9
 
 
10
    describe "when evaluating" do
 
11
 
 
12
        before :each do
 
13
            @param = stub 'param'
 
14
            @param.stubs(:safeevaluate).with(@scope).returns("value")
 
15
 
 
16
            @value1 = stub 'value1'
 
17
            @param1 = stub_everything 'param1'
 
18
            @param1.stubs(:safeevaluate).with(@scope).returns(@param1)
 
19
            @param1.stubs(:respond_to?).with(:downcase).returns(false)
 
20
            @value1.stubs(:param).returns(@param1)
 
21
            @value1.stubs(:value).returns(@value1)
 
22
 
 
23
            @value2 = stub 'value2'
 
24
            @param2 = stub_everything 'param2'
 
25
            @param2.stubs(:safeevaluate).with(@scope).returns(@param2)
 
26
            @param2.stubs(:respond_to?).with(:downcase).returns(false)
 
27
            @value2.stubs(:param).returns(@param2)
 
28
            @value2.stubs(:value).returns(@value2)
 
29
 
 
30
            @values = stub 'values', :instance_of? => true
 
31
            @values.stubs(:each).multiple_yields(@value1, @value2)
 
32
 
 
33
            @selector = Puppet::Parser::AST::Selector.new :param => @param, :values => @values
 
34
            @selector.stubs(:fail)
 
35
        end
 
36
 
 
37
        it "should evaluate param" do
 
38
            @param.expects(:safeevaluate).with(@scope)
 
39
 
 
40
            @selector.evaluate(@scope)
 
41
        end
 
42
 
 
43
        it "should downcase the evaluated param value if allowed" do
 
44
            Puppet.stubs(:[]).with(:casesensitive).returns(false)
 
45
            value = stub 'param'
 
46
            @param.stubs(:safeevaluate).with(@scope).returns(value)
 
47
 
 
48
            value.expects(:downcase)
 
49
 
 
50
            @selector.evaluate(@scope)
 
51
        end
 
52
 
 
53
        it "should scan each option" do
 
54
            @values.expects(:each).multiple_yields(@value1, @value2)
 
55
 
 
56
            @selector.evaluate(@scope)
 
57
        end
 
58
 
 
59
        describe "when scanning values" do
 
60
            it "should evaluate first matching option" do
 
61
                @param2.stubs(:evaluate_match).with { |*arg| arg[0] == "value" }.returns(true)
 
62
                @value2.expects(:safeevaluate).with(@scope)
 
63
 
 
64
                @selector.evaluate(@scope)
 
65
            end
 
66
 
 
67
            it "should return the first matching evaluated option" do
 
68
                @param2.stubs(:evaluate_match).with { |*arg| arg[0] == "value" }.returns(true)
 
69
                @value2.stubs(:safeevaluate).with(@scope).returns(:result)
 
70
 
 
71
                @selector.evaluate(@scope).should == :result
 
72
            end
 
73
 
 
74
            it "should evaluate the default option if none matched" do
 
75
                @param1.stubs(:is_a?).with(Puppet::Parser::AST::Default).returns(true)
 
76
                @value1.expects(:safeevaluate).with(@scope).returns(@param1)
 
77
 
 
78
                @selector.evaluate(@scope)
 
79
            end
 
80
 
 
81
            it "should return the default evaluated option if none matched" do
 
82
                result = stub 'result'
 
83
                @param1.stubs(:is_a?).with(Puppet::Parser::AST::Default).returns(true)
 
84
                @value1.stubs(:safeevaluate).returns(result)
 
85
 
 
86
                @selector.evaluate(@scope).should == result
 
87
            end
 
88
 
 
89
            it "should return nil if nothing matched" do
 
90
                @selector.evaluate(@scope).should be_nil
 
91
            end
 
92
 
 
93
            it "should delegate matching to evaluate_match" do
 
94
                @param1.expects(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }
 
95
 
 
96
                @selector.evaluate(@scope)
 
97
            end
 
98
 
 
99
            it "should transmit the sensitive parameter to evaluate_match" do
 
100
                Puppet.stubs(:[]).with(:casesensitive).returns(:sensitive)
 
101
                @param1.expects(:evaluate_match).with { |*arg| arg[2][:sensitive] == :sensitive }
 
102
 
 
103
                @selector.evaluate(@scope)
 
104
            end
 
105
 
 
106
            it "should transmit the AST file and line to evaluate_match" do
 
107
                @selector.file = :file
 
108
                @selector.line = :line
 
109
                @param1.expects(:evaluate_match).with { |*arg| arg[2][:file] == :file and arg[2][:line] == :line }
 
110
 
 
111
                @selector.evaluate(@scope)
 
112
            end
 
113
 
 
114
 
 
115
            it "should evaluate the matching param" do
 
116
                @param1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)
 
117
 
 
118
                @value1.expects(:safeevaluate).with(@scope)
 
119
 
 
120
                @selector.evaluate(@scope)
 
121
            end
 
122
 
 
123
            it "should return this evaluated option if it matches" do
 
124
                @param1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)
 
125
                @value1.stubs(:safeevaluate).with(@scope).returns(:result)
 
126
 
 
127
                @selector.evaluate(@scope).should == :result
 
128
            end
 
129
 
 
130
            it "should unset scope ephemeral variables after option evaluation" do
 
131
                @param1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)
 
132
                @value1.stubs(:safeevaluate).with(@scope).returns(:result)
 
133
 
 
134
                @scope.expects(:unset_ephemeral_var)
 
135
 
 
136
                @selector.evaluate(@scope)
 
137
            end
 
138
 
 
139
            it "should not leak ephemeral variables even if evaluation fails" do
 
140
                @param1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)
 
141
                @value1.stubs(:safeevaluate).with(@scope).raises
 
142
 
 
143
                @scope.expects(:unset_ephemeral_var)
 
144
 
 
145
                lambda { @selector.evaluate(@scope) }.should raise_error
 
146
            end
 
147
 
 
148
            it "should fail if there is no default" do
 
149
                @selector.expects(:fail)
 
150
 
 
151
                @selector.evaluate(@scope)
 
152
            end
 
153
        end
 
154
 
 
155
    end
 
156
end