~bkerensa/ubuntu/raring/puppet/new-upstream-release

« back to all changes in this revision

Viewing changes to spec/unit/interface/action_manager_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-25 01:00:37 UTC
  • mfrom: (1.1.24 upstream) (3.1.25 sid)
  • Revision ID: james.westby@ubuntu.com-20110725010037-875vuxs10eboqgw3
Tags: 2.7.1-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/puppetmaster-passenger.postinst: Use cacrl instead of hostcrl to
    set the location of the CRL in apache2 configuration. Fix apache2
    configuration on upgrade as well (LP: #641001)
  - move all puppet dependencies to puppet-common since all the code
    actually located in puppet-common.
  - move libagueas from a recommend to a dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env rspec
 
2
require 'spec_helper'
 
3
 
 
4
# This is entirely an internal class for Interface, so we have to load it instead of our class.
 
5
require 'puppet/interface'
 
6
require 'puppet/face'
 
7
 
 
8
class ActionManagerTester
 
9
  include Puppet::Interface::ActionManager
 
10
end
 
11
 
 
12
describe Puppet::Interface::ActionManager do
 
13
  subject { ActionManagerTester.new }
 
14
 
 
15
  describe "when included in a class" do
 
16
    it "should be able to define an action" do
 
17
      subject.action(:foo) do
 
18
        when_invoked { |options| "something "}
 
19
      end
 
20
    end
 
21
 
 
22
    it "should be able to define a 'script' style action" do
 
23
      subject.script :bar do |options|
 
24
        "a bar is where beer is found"
 
25
      end
 
26
    end
 
27
 
 
28
    it "should be able to list defined actions" do
 
29
      subject.action(:foo) do
 
30
        when_invoked { |options| "something" }
 
31
      end
 
32
      subject.action(:bar) do
 
33
        when_invoked { |options| "something" }
 
34
      end
 
35
 
 
36
      subject.actions.should =~ [:foo, :bar]
 
37
    end
 
38
 
 
39
    it "should list 'script' actions" do
 
40
      subject.script :foo do |options| "foo" end
 
41
      subject.actions.should =~ [:foo]
 
42
    end
 
43
 
 
44
    it "should list both script and normal actions" do
 
45
      subject.action :foo do
 
46
        when_invoked do |options| "foo" end
 
47
      end
 
48
      subject.script :bar do |options| "a bar is where beer is found" end
 
49
 
 
50
      subject.actions.should =~ [:foo, :bar]
 
51
    end
 
52
 
 
53
    it "should be able to indicate when an action is defined" do
 
54
      subject.action(:foo) do
 
55
        when_invoked { |options| "something" }
 
56
      end
 
57
 
 
58
      subject.should be_action(:foo)
 
59
    end
 
60
 
 
61
    it "should indicate an action is defined for script actions" do
 
62
      subject.script :foo do |options| "foo" end
 
63
      subject.should be_action :foo
 
64
    end
 
65
 
 
66
    it "should correctly treat action names specified as strings" do
 
67
      subject.action(:foo) do
 
68
        when_invoked { |options| "something" }
 
69
      end
 
70
 
 
71
      subject.should be_action("foo")
 
72
    end
 
73
  end
 
74
 
 
75
  describe "when used to extend a class" do
 
76
    subject { Class.new.extend(Puppet::Interface::ActionManager) }
 
77
 
 
78
    it "should be able to define an action" do
 
79
      subject.action(:foo) do
 
80
        when_invoked { |options| "something "}
 
81
      end
 
82
    end
 
83
 
 
84
    it "should be able to list defined actions" do
 
85
      subject.action(:foo) do
 
86
        when_invoked { |options| "something" }
 
87
      end
 
88
      subject.action(:bar) do
 
89
        when_invoked { |options| "something" }
 
90
      end
 
91
 
 
92
      subject.actions.should include(:bar)
 
93
      subject.actions.should include(:foo)
 
94
    end
 
95
 
 
96
    it "should be able to indicate when an action is defined" do
 
97
      subject.action(:foo) { when_invoked do |options| true end }
 
98
      subject.should be_action(:foo)
 
99
    end
 
100
  end
 
101
 
 
102
  describe "when used both at the class and instance level" do
 
103
    before do
 
104
      @klass = Class.new do
 
105
        include Puppet::Interface::ActionManager
 
106
        extend Puppet::Interface::ActionManager
 
107
        def __invoke_decorations(*args) true end
 
108
        def options() [] end
 
109
      end
 
110
      @instance = @klass.new
 
111
    end
 
112
 
 
113
    it "should be able to define an action at the class level" do
 
114
      @klass.action(:foo) do
 
115
        when_invoked { |options| "something "}
 
116
      end
 
117
    end
 
118
 
 
119
    it "should create an instance method when an action is defined at the class level" do
 
120
      @klass.action(:foo) do
 
121
        when_invoked { |options| "something" }
 
122
      end
 
123
      @instance.foo.should == "something"
 
124
    end
 
125
 
 
126
    it "should be able to define an action at the instance level" do
 
127
      @instance.action(:foo) do
 
128
        when_invoked { |options| "something "}
 
129
      end
 
130
    end
 
131
 
 
132
    it "should create an instance method when an action is defined at the instance level" do
 
133
      @instance.action(:foo) do
 
134
        when_invoked { |options| "something" }
 
135
      end
 
136
      @instance.foo.should == "something"
 
137
    end
 
138
 
 
139
    it "should be able to list actions defined at the class level" do
 
140
      @klass.action(:foo) do
 
141
        when_invoked { |options| "something" }
 
142
      end
 
143
      @klass.action(:bar) do
 
144
        when_invoked { |options| "something" }
 
145
      end
 
146
 
 
147
      @klass.actions.should include(:bar)
 
148
      @klass.actions.should include(:foo)
 
149
    end
 
150
 
 
151
    it "should be able to list actions defined at the instance level" do
 
152
      @instance.action(:foo) do
 
153
        when_invoked { |options| "something" }
 
154
      end
 
155
      @instance.action(:bar) do
 
156
        when_invoked { |options| "something" }
 
157
      end
 
158
 
 
159
      @instance.actions.should include(:bar)
 
160
      @instance.actions.should include(:foo)
 
161
    end
 
162
 
 
163
    it "should be able to list actions defined at both instance and class level" do
 
164
      @klass.action(:foo) do
 
165
        when_invoked { |options| "something" }
 
166
      end
 
167
      @instance.action(:bar) do
 
168
        when_invoked { |options| "something" }
 
169
      end
 
170
 
 
171
      @instance.actions.should include(:bar)
 
172
      @instance.actions.should include(:foo)
 
173
    end
 
174
 
 
175
    it "should be able to indicate when an action is defined at the class level" do
 
176
      @klass.action(:foo) do
 
177
        when_invoked { |options| "something" }
 
178
      end
 
179
      @instance.should be_action(:foo)
 
180
    end
 
181
 
 
182
    it "should be able to indicate when an action is defined at the instance level" do
 
183
      @klass.action(:foo) do
 
184
        when_invoked { |options| "something" }
 
185
      end
 
186
      @instance.should be_action(:foo)
 
187
    end
 
188
 
 
189
    context "with actions defined in superclass" do
 
190
      before :each do
 
191
        @subclass = Class.new(@klass)
 
192
        @instance = @subclass.new
 
193
 
 
194
        @klass.action(:parent) do
 
195
          when_invoked { |options| "a" }
 
196
        end
 
197
        @subclass.action(:sub) do
 
198
          when_invoked { |options| "a" }
 
199
        end
 
200
        @instance.action(:instance) do
 
201
          when_invoked { |options| "a" }
 
202
        end
 
203
      end
 
204
 
 
205
      it "should list actions defined in superclasses" do
 
206
        @instance.should be_action(:parent)
 
207
        @instance.should be_action(:sub)
 
208
        @instance.should be_action(:instance)
 
209
      end
 
210
 
 
211
      it "should list inherited actions" do
 
212
        @instance.actions.should =~ [:instance, :parent, :sub]
 
213
      end
 
214
 
 
215
      it "should not duplicate instance actions after fetching them (#7699)" do
 
216
        @instance.actions.should =~ [:instance, :parent, :sub]
 
217
        @instance.get_action(:instance)
 
218
        @instance.actions.should =~ [:instance, :parent, :sub]
 
219
      end
 
220
 
 
221
      it "should not duplicate subclass actions after fetching them (#7699)" do
 
222
        @instance.actions.should =~ [:instance, :parent, :sub]
 
223
        @instance.get_action(:sub)
 
224
        @instance.actions.should =~ [:instance, :parent, :sub]
 
225
      end
 
226
 
 
227
      it "should not duplicate superclass actions after fetching them (#7699)" do
 
228
        @instance.actions.should =~ [:instance, :parent, :sub]
 
229
        @instance.get_action(:parent)
 
230
        @instance.actions.should =~ [:instance, :parent, :sub]
 
231
      end
 
232
    end
 
233
 
 
234
    it "should create an instance method when an action is defined in a superclass" do
 
235
      @subclass = Class.new(@klass)
 
236
      @instance = @subclass.new
 
237
 
 
238
      @klass.action(:foo) do
 
239
        when_invoked { |options| "something" }
 
240
      end
 
241
      @instance.foo.should == "something"
 
242
    end
 
243
  end
 
244
 
 
245
  describe "#action" do
 
246
    it 'should add an action' do
 
247
      subject.action(:foo) { when_invoked do |options| true end }
 
248
      subject.get_action(:foo).should be_a Puppet::Interface::Action
 
249
    end
 
250
 
 
251
    it 'should support default actions' do
 
252
      subject.action(:foo) { when_invoked do |options| true end; default }
 
253
      subject.get_default_action.should == subject.get_action(:foo)
 
254
    end
 
255
 
 
256
    it 'should not support more than one default action' do
 
257
      subject.action(:foo) { when_invoked do |options| true end; default }
 
258
      expect { subject.action(:bar) {
 
259
          when_invoked do |options| true end
 
260
          default
 
261
        }
 
262
      }.should raise_error /cannot both be default/
 
263
    end
 
264
  end
 
265
 
 
266
  describe "#get_action" do
 
267
    let :parent_class do
 
268
      parent_class = Class.new(Puppet::Interface)
 
269
      parent_class.action(:foo) { when_invoked do |options| true end }
 
270
      parent_class
 
271
    end
 
272
 
 
273
    it "should check that we can find inherited actions when we are a class" do
 
274
      Class.new(parent_class).get_action(:foo).name.should == :foo
 
275
    end
 
276
 
 
277
    it "should check that we can find inherited actions when we are an instance" do
 
278
      instance = parent_class.new(:foo, '0.0.0')
 
279
      instance.get_action(:foo).name.should == :foo
 
280
    end
 
281
  end
 
282
end