File: resource_api_spec.rb

package info (click to toggle)
ruby-puppet-resource-api 1.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,232 kB
  • sloc: ruby: 9,573; sh: 4; makefile: 2
file content (277 lines) | stat: -rw-r--r-- 10,014 bytes parent folder | download | duplicates (2)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Resource API integrated tests:' do
  context 'when running in a Type' do
    subject(:type) { Puppet::Type.type(:integration) }

    let(:definition) do
      {
        name: 'integration',
        title_patterns: [
          {
            pattern: %r{^(?<name>.*[^-])-(?<name2>.*)$},
            desc: 'Where the name and the name2 are provided with a hyphen separator',
          },
          {
            pattern: %r{^(?<name>.*)$},
            desc: 'Where only the name is provided',
          },
        ],
        attributes: {
          name: {
            type: 'String',
            behaviour: :namevar,
            desc: 'the title',
          },
          name2: {
            type: 'String',
            behaviour: :namevar,
            desc: 'the other title',
          },
          string: {
            type: 'String',
            desc: 'a string attribute',
            default: 'default value',
          },
          boolean: {
            type: 'Boolean',
            desc: 'a boolean attribute',
          },
          integer: {
            type: 'Integer',
            desc: 'an integer attribute',
          },
          float: {
            type: 'Float',
            desc: 'a floating point attribute',
          },
          ensure: {
            type: 'Enum[present, absent]',
            desc: 'a ensure attribute',
          },
          variant_pattern: {
            type: 'Variant[Pattern[/\A(0x)?[0-9a-fA-F]{8}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{16}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{40}\Z/]]',
            desc: 'a pattern attribute',
          },
          url: {
            type: 'Pattern[/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/]',
            desc: 'a hkp or http(s) url attribute',
          },
          sensitive: {
            type: 'Sensitive[String]',
            desc: 'A sensitive string, like a password',
          },
          string_array: {
            type: 'Array[String]',
            desc: 'An attribute to exercise Array handling.',
          },
          variant_array: {
            type: 'Variant[Array[String], String]',
            desc: 'an array, or a string',
          },
          array_of_arrays: {
            type: 'Array[Array[String]]',
            desc: 'an array of arrays',
          },
          array_from_hell: {
            type: 'Array[Variant[Array[String], String]]',
            desc: 'an array of weird things',
          },
          optional_string_array: {
            type: 'Optional[Array[String]]',
            desc: 'An optional attribute to exercise Array handling.',
          },
          optional_string: {
            type: 'Optional[String]',
            desc: 'a optional string attribute',
          },
          string_param: {
            type: 'String',
            desc: 'a string parameter',
            default: 'default value',
            behaviour: :parameter,
          },
          boolean_param: {
            type: 'Boolean',
            desc: 'a boolean parameter',
            behaviour: :parameter,
          },
          integer_param: {
            type: 'Integer',
            desc: 'an integer parameter',
            behaviour: :parameter,
          },
          float_param: {
            type: 'Float',
            desc: 'a floating point parameter',
            behaviour: :parameter,
          },
          ensure_param: {
            type: 'Enum[present, absent]',
            desc: 'a ensure parameter',
            behaviour: :parameter,
          },
          variant_pattern_param: {
            type: 'Variant[Pattern[/\A(0x)?[0-9a-fA-F]{8}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{16}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{40}\Z/]]',
            desc: 'a pattern parameter',
            behaviour: :parameter,
          },
          url_param: {
            type: 'Pattern[/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/]',
            desc: 'a hkp or http(s) url parameter',
            behaviour: :parameter,
          },
          string_array_param: {
            type: 'Array[String]',
            desc: 'A parameter to exercise Array handling.',
            behaviour: :parameter,
          },
          optional_string_param: {
            type: 'Optional[String]',
            desc: 'a optional string parameter',
            behaviour: :parameter,
          },
          string_ro: {
            type: 'String',
            desc: 'a string readonly',
            default: 'default value',
            behaviour: :read_only,
          },
          boolean_ro: {
            type: 'Boolean',
            desc: 'a boolean readonly',
            behaviour: :read_only,
          },
          integer_ro: {
            type: 'Integer',
            desc: 'an integer readonly',
            behaviour: :read_only,
          },
          float_ro: {
            type: 'Float',
            desc: 'a floating point readonly',
            behaviour: :read_only,
          },
          ensure_ro: {
            type: 'Enum[present, absent]',
            desc: 'a ensure readonly',
            behaviour: :read_only,
          },
          variant_pattern_ro: {
            type: 'Variant[Pattern[/\A(0x)?[0-9a-fA-F]{8}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{16}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{40}\Z/]]',
            desc: 'a pattern readonly',
            behaviour: :read_only,
          },
          url_ro: {
            type: 'Pattern[/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/]',
            desc: 'a hkp or http(s) url readonly',
            behaviour: :read_only,
          },
          string_array_ro: {
            type: 'Array[String]',
            desc: 'A readonly parameter to exercise Array handling.',
            behaviour: :read_only,
          },
          optional_string_ro: {
            type: 'Optional[String]',
            desc: 'a optional string readonly',
            behaviour: :read_only,
          },
        },
      }
    end
    let(:provider_class) do
      # bring setter into scope for class
      s = setter
      Class.new do
        def get(_context)
          [
            {
              name: 'foo',
              name2: 'bar',
              title: 'foo-bar',
            },
            {
              name: 'foo2',
              name2: 'bar2',
              title: 'foo2-bar2',
            },
          ]
        end

        attr_reader :last_changes
        define_method(:set) do |context, changes|
          @last_changes = changes
          s.call(context, changes)
        end
      end
    end
    let(:setter) do
      proc { |_context, _changes| }
    end

    before(:each) do
      stub_const('Puppet::Provider::Integration', Module.new)
      stub_const('Puppet::Provider::Integration::Integration', provider_class)
      Puppet::ResourceApi.register_type(definition)
    end

    context 'with a instance of the type' do
      # the catalog here is required to mock out the catalog used in
      # https://github.com/puppetlabs/puppet/blob/fb96f5115b0a7033ee6624173a7c3be8e0d1f572/lib/puppet/type.rb#L1416-L1433
      # to have a quick check of the working of metaparams here, without having to stand up
      # a complete catalog.
      # See spec/acceptance/metaparam_spec.rb for more in-depth testing with a full puppet apply
      let(:catalog) { instance_double('Puppet::Resource::Catalog', 'catalog') }
      let(:instance) do
        type.new(name: 'somename', name2: 'othername', ensure: 'present', boolean: true, integer: 15, float: 1.23,
                 variant_pattern: '0x1234ABCD', url: 'http://www.google.com', sensitive: Puppet::Pops::Types::PSensitiveType::Sensitive.new('a password'),
                 string_array: %w[a b c],
                 variant_array: 'not_an_array', array_of_arrays: [%w[a b c], %w[d e f]],
                 array_from_hell: ['a', %w[subb subc], 'd'],
                 boolean_param: false, integer_param: 99, float_param: 3.21, ensure_param: 'present',
                 variant_pattern_param: '1234ABCD', url_param:  'http://www.puppet.com',
                 string_array_param: %w[d e f], alias: 'bar', audit: 'all', loglevel: 'crit', noop: false,
                 tag: %w[a b c], catalog: catalog)
      end

      before(:each) do
        allow(catalog).to receive(:resource).and_return nil
        allow(catalog).to receive(:alias).and_return nil
        allow(catalog).to receive(:host_config?).and_return true
      end

      it('flushes') { expect { instance.flush }.not_to raise_exception }

      describe '.to_resource' do
        it { expect(instance.to_resource).to be_a Puppet::ResourceApi::ResourceShim }

        describe 'its title' do
          it { expect(instance.to_resource.title).to eq 'somename' }
        end

        it 'can serialize to json' do
          expect({ 'resources' => [instance.to_resource] }.to_json).to eq '{"resources":[{"somename":{"name":"somename","name2":"othername","ensure":"absent","boolean_param":false,"integer_param":99,"float_param":3.21,"ensure_param":"present","variant_pattern_param":"1234ABCD","url_param":"http://www.puppet.com","string_array_param":"d","e":"f","string_param":"default value"}}]}' # rubocop:disable Metrics/LineLength
        end
      end

      it('ensure is reported as a symbol') { expect(instance[:ensure]).to be_a Symbol }
      it('can set ensure to :present') { expect { instance[:ensure] = :present }.not_to raise_error }
      it('can set ensure to :absent') { expect { instance[:ensure] = :absent }.not_to raise_error }

      context 'when updating encounters an error' do
        let(:setter) do
          proc do |context, _changes|
            context.updating('the update message') do
              raise StandardError, 'the error message'
            end
          end
        end

        it('doesn\'t flush') { expect { instance.flush }.to raise_exception(StandardError, %r{Execution encountered an error}) }
      end
    end
  end
end