File: create_generic.rb

package info (click to toggle)
ruby-rspec-puppet 4.0.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,444 kB
  • sloc: ruby: 6,377; makefile: 6
file content (380 lines) | stat: -rw-r--r-- 11,660 bytes parent folder | download
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# frozen_string_literal: true

require 'set'
require 'rspec-puppet/matchers/parameter_matcher'

module RSpec::Puppet
  module ManifestMatchers
    class CreateGeneric
      include RSpec::Puppet::Errors

      def initialize(*args, &block)
        @exp_resource_type = args.shift.to_s.gsub(/^(create|contain)_/, '')
        @args = args
        @block = block
        @referenced_type = referenced_type(@exp_resource_type)
        @title = args[0]

        @errors = []
        @expected_params = []
        @expected_undef_params = []
        @notifies = []
        @subscribes = []
        @requires = []
        @befores = []
      end

      def with(*args)
        params = args.shift
        @expected_params |= params.to_a
        self
      end

      def only_with(*args, &block)
        params = args.shift
        @expected_params_count = (@expected_params_count || 0) + params.compact.size
        with(params, &block)
      end

      def without(*args)
        params = args.shift
        @expected_undef_params |= Array(params)
        self
      end

      def that_notifies(resource)
        @notifies.concat(Array(resource))
        self
      end

      def that_subscribes_to(resource)
        @subscribes.concat(Array(resource))
        self
      end

      def that_requires(resource)
        @requires.concat(Array(resource))
        self
      end

      def that_comes_before(resource)
        @befores.concat(Array(resource))
        self
      end

      def method_missing(method, *args, &block)
        case method.to_s
        when /^with_/
          param = method.to_s.gsub(/^with_/, '')
          @expected_params << [param, args[0]]
          self
        when /^only_with_/
          param = method.to_s.gsub(/^only_with_/, '')
          @expected_params_count = (@expected_params_count || 0) + 1
          @expected_params << [param, args[0]]
          self
        when /^without_/
          param = method.to_s.gsub(/^without_/, '')
          @expected_undef_params << [param, args[0]]
          self
        else
          super
        end
      end

      def matches?(catalogue)
        ret = true
        @catalogue = catalogue.is_a?(Puppet::Resource::Catalog) ? catalogue : catalogue.call
        resource = @catalogue.resource(@referenced_type, @title)

        if resource.nil?
          false
        else
          RSpec::Puppet::Coverage.cover!(resource)
          rsrc_hsh = resource.to_hash
          if resource.respond_to?(:sensitive_parameters)
            resource.sensitive_parameters.each do |s_param|
              rsrc_hsh[s_param] = ::Puppet::Pops::Types::PSensitiveType::Sensitive.new(rsrc_hsh[s_param])
            end
          end

          namevar = if resource.builtin_type?
                      resource.resource_type.key_attributes.first.to_s
                    else
                      'name'
                    end

          if @expected_params.none? { |param| param.first.to_s == namevar } && rsrc_hsh.key?(namevar.to_sym)
            rsrc_hsh.delete(namevar.to_sym)
          end

          if @expected_params_count && rsrc_hsh.size != @expected_params_count
            ret = false
            (@errors ||= []) << "exactly #{@expected_params_count} parameters but the catalogue contains #{rsrc_hsh.size}"
          end

          check_params(rsrc_hsh, @expected_params, :should) if @expected_params.any?
          check_params(rsrc_hsh, @expected_undef_params, :not) if @expected_undef_params.any?
          check_befores(@catalogue, resource) if @befores.any?
          check_requires(@catalogue, resource) if @requires.any?
          check_notifies(@catalogue, resource) if @notifies.any?
          check_subscribes(@catalogue, resource) if @subscribes.any?

          @errors.empty?
        end
      end

      def failure_message
        "expected that the catalogue would contain #{@referenced_type}[#{@title}]#{errors}"
      end

      def failure_message_when_negated
        "expected that the catalogue would not contain #{@referenced_type}[#{@title}]#{errors}"
      end

      def description
        values = []
        value_str_prefix = 'with'

        values << "exactly #{@expected_params_count} parameters" if @expected_params_count

        values.concat(generate_param_list(@expected_params, :should)) if @expected_params.any?

        values.concat(generate_param_list(@expected_undef_params, :not)) if @expected_undef_params.any?

        if @notifies.any?
          value_str_prefix = 'that notifies'
          values = @notifies
        end

        if @subscribes.any?
          value_str_prefix = 'that subscribes to'
          values = @subscribes
        end

        if @requires.any?
          value_str_prefix = 'that requires'
          values = @requires
        end

        if @befores.any?
          value_str_prefix = 'that comes before'
          values = @befores
        end

        unless values.empty?
          value_str = if values.length == 1
                        " #{value_str_prefix} #{values.first}"
                      else
                        " #{value_str_prefix} #{values[0..-2].join(', ')} and #{values[-1]}"
                      end
        end

        "contain #{@referenced_type}[#{@title}]#{value_str}"
      end

      def diffable?
        true
      end

      def supports_block_expectations
        true
      end

      def supports_value_expectations
        true
      end

      def expected
        @errors.filter_map { |e| e.expected if e.respond_to?(:expected) }.join("\n\n")
      end

      def actual
        @errors.filter_map { |e| e.actual if e.respond_to?(:actual) }.join("\n\n")
      end

      private

      def referenced_type(type)
        type.split('__').map(&:capitalize).join('::')
      end

      def errors
        @errors.empty? ? '' : " with #{@errors.join(', and parameter ')}"
      end

      def generate_param_list(list, type)
        output = []
        list.each do |param, value|
          if value.nil?
            output << "#{param} #{type == :not ? 'un' : ''}defined"
          else
            a = type == :not ? '!' : '='
            b = value.is_a?(Regexp) ? '~' : '>'
            output << if (param.to_s == 'content') && value.is_a?(String)
                        "#{param} #{type == :not ? 'not ' : ''} supplied string"
                      else
                        "#{param} #{a}#{b} #{value.inspect}"
                      end
          end
        end
        output
      end

      def check_befores(_catalogue, resource)
        @befores.each do |ref|
          unless precedes?(resource, canonicalize_resource(ref))
            @errors << BeforeRelationshipError.new(resource.to_ref, ref)
          end
        end
      end

      def check_requires(_catalogue, resource)
        @requires.each do |ref|
          unless precedes?(canonicalize_resource(ref), resource)
            @errors << RequireRelationshipError.new(resource.to_ref, ref)
          end
        end
      end

      def check_notifies(_catalogue, resource)
        @notifies.each do |ref|
          unless notifies?(resource, canonicalize_resource(ref))
            @errors << NotifyRelationshipError.new(resource.to_ref, ref)
          end
        end
      end

      def check_subscribes(_catalogue, resource)
        @subscribes.each do |ref|
          unless notifies?(canonicalize_resource(ref), resource)
            @errors << SubscribeRelationshipError.new(resource.to_ref, ref)
          end
        end
      end

      def resource_ref(resource)
        resource.respond_to?(:to_ref) ? resource.to_ref : resource
      end

      def resource_from_ref(ref)
        ref.is_a?(Puppet::Resource) ? ref : @catalogue.resource(ref)
      end

      def canonicalize_resource(resource)
        res = resource_from_ref(resource_ref(resource))
        if res.nil?
          resource = Struct.new(:type, :title).new(*@catalogue.title_key_for_ref(resource)) if resource.is_a?(String)
          res = @catalogue.resource_keys.select do |type, _name|
            type == resource.type
          end.filter_map do |type, name|
            @catalogue.resource(type, name)
          end.find do |cat_res|
            cat_res.builtin_type? && cat_res.uniqueness_key.first == resource.title
          end
        end
        res
      end

      def canonicalize_resource_ref(ref)
        resource_ref(resource_from_ref(ref))
      end

      def relationship_refs(resource, type, visited = Set.new)
        resource = canonicalize_resource(resource)
        results = Set.new
        return results unless resource

        # guard to prevent infinite recursion
        return [canonicalize_resource_ref(resource)] if visited.include?(resource.object_id)

        visited << resource.object_id

        [resource[type]].flatten.compact.each do |r|
          results << canonicalize_resource_ref(r)
          results << relationship_refs(r, type, visited)

          res = canonicalize_resource(r)
          if res&.builtin_type?
            results << res.to_ref
            results << "#{res.type.to_s.capitalize}[#{res.uniqueness_key.first}]"
          end
        end

        # Add auto* (autorequire etc) if any
        if %i[before notify require subscribe].include?(type)
          func = :"eachauto#{type}"
          if resource.resource_type.respond_to?(func)
            resource.resource_type.send(func) do |t, b|
              Array(resource.to_ral.instance_eval(&b)).each do |dep|
                next if dep.nil?

                res = "#{t.to_s.capitalize}[#{dep}]"
                if (r = relationship_refs(res, type, visited))
                  results << res
                  results << r
                end
              end
            end
          end
        end

        results.flatten
      end

      def self_or_upstream(vertex)
        [vertex] + @catalogue.upstream_from_vertex(vertex).keys
      end

      def precedes?(first, second)
        return false if first.nil? || second.nil?

        self_or_upstream(first).each do |u|
          self_or_upstream(second).each do |v|
            before_refs = relationship_refs(u, :before) + relationship_refs(u, :notify)
            require_refs = relationship_refs(v, :require) + relationship_refs(u, :subscribe)

            if before_refs.include?(v.to_ref) || require_refs.include?(u.to_ref) || (before_refs & require_refs).any?
              return true
            end
          end
        end

        # Nothing found
        false
      end

      def notifies?(first, second)
        return false if first.nil? || second.nil?

        self_or_upstream(first).each do |u|
          self_or_upstream(second).each do |v|
            notify_refs = relationship_refs(u, :notify)
            subscribe_refs = relationship_refs(v, :subscribe)

            return true if notify_refs.include?(v.to_ref) || subscribe_refs.include?(u.to_ref)
          end
        end

        # Nothing found
        false
      end

      # @param resource [Hash<Symbol, Object>] The resource in the catalog
      # @param list [Array<String, Object>] The expected values of the resource
      # @param type [:should, :not] Whether the given parameters should/not match
      def check_params(resource, list, type)
        list.each do |param, value|
          param = param.to_sym

          if value.nil?
            @errors << "#{param} undefined but it is set to #{resource[param].inspect}" unless resource[param].nil?
          else
            m = ParameterMatcher.new(param, value, type)
            @errors.concat m.errors unless m.matches?(resource)
          end
        end
      end
    end
  end
end