File: action_manager_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (254 lines) | stat: -rw-r--r-- 7,734 bytes parent folder | download | duplicates (3)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
require 'spec_helper'

require 'puppet/interface'

class ActionManagerTester
  include Puppet::Interface::ActionManager
end

describe Puppet::Interface::ActionManager do
  subject { ActionManagerTester.new }

  describe "when included in a class" do
    it "should be able to define an action" do
      subject.action(:foo) do
        when_invoked { |options| "something "}
      end
    end

    it "should be able to list defined actions" do
      subject.action(:foo) do
        when_invoked { |options| "something" }
      end
      subject.action(:bar) do
        when_invoked { |options| "something" }
      end

      expect(subject.actions).to match_array([:foo, :bar])
    end

    it "should be able to indicate when an action is defined" do
      subject.action(:foo) do
        when_invoked { |options| "something" }
      end

      expect(subject).to be_action(:foo)
    end

    it "should correctly treat action names specified as strings" do
      subject.action(:foo) do
        when_invoked { |options| "something" }
      end

      expect(subject).to be_action("foo")
    end
  end

  describe "when used to extend a class" do
    subject { Class.new.extend(Puppet::Interface::ActionManager) }

    it "should be able to define an action" do
      subject.action(:foo) do
        when_invoked { |options| "something "}
      end
    end

    it "should be able to list defined actions" do
      subject.action(:foo) do
        when_invoked { |options| "something" }
      end
      subject.action(:bar) do
        when_invoked { |options| "something" }
      end

      expect(subject.actions).to include(:bar)
      expect(subject.actions).to include(:foo)
    end

    it "should be able to indicate when an action is defined" do
      subject.action(:foo) { when_invoked do |options| true end }
      expect(subject).to be_action(:foo)
    end
  end

  describe "when used both at the class and instance level" do
    before do
      @klass = Class.new do
        include Puppet::Interface::ActionManager
        extend Puppet::Interface::ActionManager
        def __invoke_decorations(*args) true end
        def options() [] end
      end
      @instance = @klass.new
    end

    it "should be able to define an action at the class level" do
      @klass.action(:foo) do
        when_invoked { |options| "something "}
      end
    end

    it "should create an instance method when an action is defined at the class level" do
      @klass.action(:foo) do
        when_invoked { |options| "something" }
      end
      expect(@instance.foo).to eq("something")
    end

    it "should be able to define an action at the instance level" do
      @instance.action(:foo) do
        when_invoked { |options| "something "}
      end
    end

    it "should create an instance method when an action is defined at the instance level" do
      @instance.action(:foo) do
        when_invoked { |options| "something" }
      end
      expect(@instance.foo).to eq("something")
    end

    it "should be able to list actions defined at the class level" do
      @klass.action(:foo) do
        when_invoked { |options| "something" }
      end
      @klass.action(:bar) do
        when_invoked { |options| "something" }
      end

      expect(@klass.actions).to include(:bar)
      expect(@klass.actions).to include(:foo)
    end

    it "should be able to list actions defined at the instance level" do
      @instance.action(:foo) do
        when_invoked { |options| "something" }
      end
      @instance.action(:bar) do
        when_invoked { |options| "something" }
      end

      expect(@instance.actions).to include(:bar)
      expect(@instance.actions).to include(:foo)
    end

    it "should be able to list actions defined at both instance and class level" do
      @klass.action(:foo) do
        when_invoked { |options| "something" }
      end
      @instance.action(:bar) do
        when_invoked { |options| "something" }
      end

      expect(@instance.actions).to include(:bar)
      expect(@instance.actions).to include(:foo)
    end

    it "should be able to indicate when an action is defined at the class level" do
      @klass.action(:foo) do
        when_invoked { |options| "something" }
      end
      expect(@instance).to be_action(:foo)
    end

    it "should be able to indicate when an action is defined at the instance level" do
      @klass.action(:foo) do
        when_invoked { |options| "something" }
      end
      expect(@instance).to be_action(:foo)
    end

    context "with actions defined in superclass" do
      before :each do
        @subclass = Class.new(@klass)
        @instance = @subclass.new

        @klass.action(:parent) do
          when_invoked { |options| "a" }
        end
        @subclass.action(:sub) do
          when_invoked { |options| "a" }
        end
        @instance.action(:instance) do
          when_invoked { |options| "a" }
        end
      end

      it "should list actions defined in superclasses" do
        expect(@instance).to be_action(:parent)
        expect(@instance).to be_action(:sub)
        expect(@instance).to be_action(:instance)
      end

      it "should list inherited actions" do
        expect(@instance.actions).to match_array([:instance, :parent, :sub])
      end

      it "should not duplicate instance actions after fetching them (#7699)" do
        expect(@instance.actions).to match_array([:instance, :parent, :sub])
        @instance.get_action(:instance)
        expect(@instance.actions).to match_array([:instance, :parent, :sub])
      end

      it "should not duplicate subclass actions after fetching them (#7699)" do
        expect(@instance.actions).to match_array([:instance, :parent, :sub])
        @instance.get_action(:sub)
        expect(@instance.actions).to match_array([:instance, :parent, :sub])
      end

      it "should not duplicate superclass actions after fetching them (#7699)" do
        expect(@instance.actions).to match_array([:instance, :parent, :sub])
        @instance.get_action(:parent)
        expect(@instance.actions).to match_array([:instance, :parent, :sub])
      end
    end

    it "should create an instance method when an action is defined in a superclass" do
      @subclass = Class.new(@klass)
      @instance = @subclass.new

      @klass.action(:foo) do
        when_invoked { |options| "something" }
      end
      expect(@instance.foo).to eq("something")
    end
  end

  describe "#action" do
    it 'should add an action' do
      subject.action(:foo) { when_invoked do |options| true end }
      expect(subject.get_action(:foo)).to be_a Puppet::Interface::Action
    end

    it 'should support default actions' do
      subject.action(:foo) { when_invoked do |options| true end; default }
      expect(subject.get_default_action).to eq(subject.get_action(:foo))
    end

    it 'should not support more than one default action' do
      subject.action(:foo) { when_invoked do |options| true end; default }
      expect { subject.action(:bar) {
          when_invoked do |options| true end
          default
        }
      }.to raise_error(/cannot both be default/)
    end
  end

  describe "#get_action" do
    let :parent_class do
      parent_class = Class.new(Puppet::Interface)
      parent_class.action(:foo) { when_invoked do |options| true end }
      parent_class
    end

    it "should check that we can find inherited actions when we are a class" do
      expect(Class.new(parent_class).get_action(:foo).name).to eq(:foo)
    end

    it "should check that we can find inherited actions when we are an instance" do
      instance = parent_class.new(:foo, '0.0.0')
      expect(instance.get_action(:foo).name).to eq(:foo)
    end
  end
end