File: capability_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 (415 lines) | stat: -rw-r--r-- 13,138 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
require 'spec_helper'
require 'puppet_spec/compiler'

describe 'Capability types' do
  include PuppetSpec::Compiler
  let(:env) { Puppet::Node::Environment.create(:testing, []) }
  let(:node) { Puppet::Node.new('test', :environment => env) }
  let(:loaders) { Puppet::Pops::Loaders.new(env) }

  before(:each) do
    allow_any_instance_of(Puppet::Parser::Compiler).to receive(:loaders).and_return(loaders)
  end

  around :each do |example|
    Puppet.override(:loaders => loaders, :current_environment => env) do
      Puppet::Type.newtype :cap, :is_capability => true do
        newparam :name
        newparam :host
      end
      example.run
      Puppet::Type.rmtype(:cap)
    end
  end

  context 'annotations' do
    it "adds a blueprint for a produced resource" do
      catalog = compile_to_catalog(<<-MANIFEST, node)
      define test($hostname) {
        notify { "hostname ${hostname}":}
      }

      Test produces Cap {
        host => $hostname
      }
    MANIFEST

      krt = catalog.environment_instance.known_resource_types
      type = krt.definition(:test)
      expect(type.produces).to be_instance_of(Array)
      prd = type.produces.first

      expect(prd).to be_instance_of(Hash)
      expect(prd[:capability]).to eq("Cap")
      expect(prd[:mappings]).to be_instance_of(Hash)
      expect(prd[:mappings]["host"]).to be_instance_of(Puppet::Parser::AST::PopsBridge::Expression)
    end

    it "adds a blueprint for a consumed resource" do
      catalog = compile_to_catalog(<<-MANIFEST, node)
      define test($hostname) {
        notify { "hostname ${hostname}":}
      }

      Test consumes Cap {
        host => $hostname
      }
    MANIFEST

      krt = catalog.environment_instance.known_resource_types
      type = krt.definition(:test)
      expect(type.produces).to be_instance_of(Array)
      cns = type.consumes.first

      expect(cns).to be_instance_of(Hash)
      expect(cns[:capability]).to eq("Cap")
      expect(cns[:mappings]).to be_instance_of(Hash)
      expect(cns[:mappings]["host"]).to be_instance_of(Puppet::Parser::AST::PopsBridge::Expression)
    end

    it 'can place define and consumes/produces in separate manifests' do
      parse_results = []
      parser = Puppet::Parser::ParserFactory.parser

      parser.string = <<-MANIFEST
        define test($hostname) {
          notify { "hostname ${hostname}":}
        }
      MANIFEST
      parse_results << parser.parse

      parser.string = <<-MANIFEST
        Test consumes Cap {
          host => $hostname
        }
      MANIFEST
      parse_results << parser.parse

      main = Puppet::Parser::AST::Hostclass.new('', :code => Puppet::Parser::ParserFactory.code_merger.concatenate(parse_results))
      allow_any_instance_of(Puppet::Node::Environment).to receive(:perform_initial_import).and_return(main)

      type = compile_to_catalog(nil).environment_instance.known_resource_types.definition(:test)
      expect(type.produces).to be_instance_of(Array)
      cns = type.consumes.first

      expect(cns).to be_instance_of(Hash)
      expect(cns[:capability]).to eq('Cap')
      expect(cns[:mappings]).to be_instance_of(Hash)
      expect(cns[:mappings]['host']).to be_instance_of(Puppet::Parser::AST::PopsBridge::Expression)
    end

    it 'can place use a qualified name for defines that produces capabilities' do
      parse_results = []
      parser = Puppet::Parser::ParserFactory.parser

      parser.string = <<-MANIFEST
        class mod {
          define test($hostname) {
            notify { "hostname ${hostname}":}
          }
        }
        include mod
      MANIFEST
      parse_results << parser.parse

      parser.string = <<-MANIFEST
        Mod::Test consumes Cap {
          host => $hostname
        }
      MANIFEST
      parse_results << parser.parse

      main = Puppet::Parser::AST::Hostclass.new('', :code => Puppet::Parser::ParserFactory.code_merger.concatenate(parse_results))
     allow_any_instance_of(Puppet::Node::Environment).to receive(:perform_initial_import).and_return(main)

      type = compile_to_catalog(nil).environment_instance.known_resource_types.definition('Mod::Test')
      expect(type.produces).to be_instance_of(Array)
      cns = type.consumes.first

      expect(cns).to be_instance_of(Hash)
      expect(cns[:capability]).to eq('Cap')
      expect(cns[:mappings]).to be_instance_of(Hash)
      expect(cns[:mappings]['host']).to be_instance_of(Puppet::Parser::AST::PopsBridge::Expression)
    end

    it "does not allow operator '+>' in a mapping" do
      expect do
      compile_to_catalog(<<-MANIFEST, node)
        define test($hostname) {
          notify { "hostname ${hostname}":}
        }

        Test consumes Cap {
          host +> $hostname
        }
      MANIFEST
      end.to raise_error(Puppet::ParseErrorWithIssue, /Illegal \+> operation.*This operator can not be used in a Capability Mapping/)
    end

    it "does not allow operator '*=>' in a mapping" do
      expect do
        compile_to_catalog(<<-MANIFEST, node)
        define test($hostname) {
          notify { "hostname ${hostname}":}
        }

        Test consumes Cap {
          *=> { host => $hostname }
        }
        MANIFEST
      end.to raise_error(Puppet::ParseError, /The operator '\* =>' in a Capability Mapping is not supported/)
    end

    it "does not allow 'before' relationship to capability mapping" do
      expect do
        compile_to_catalog(<<-MANIFEST, node)
        define test() {
          notify { "hello":}
        }

        Test consumes Cap {}

        test { one: before => Cap[cap] }
        MANIFEST
      end.to raise_error(Puppet::Error, /'before' is not a valid relationship to a capability/)
    end

    ["produces", "consumes"].each do |kw|
      it "creates an error when #{kw} references nonexistent type" do
        manifest = <<-MANIFEST
        Test #{kw} Cap {
          host => $hostname
        }
      MANIFEST

        expect {
          compile_to_catalog(manifest, node)
        }.to raise_error(Puppet::Error,
                         /#{kw} clause references nonexistent type Test/)
      end
    end
  end

  context 'exporting a capability' do
    it "does not add produced resources that are not exported" do
      manifest = <<-MANIFEST
define test($hostname) {
  notify { "hostname ${hostname}":}
}

Test produces Cap {
  host => $hostname
}

test { one: hostname => "ahost" }
    MANIFEST
      catalog = compile_to_catalog(manifest, node)
      expect(catalog.resource("Test[one]")).to be_instance_of(Puppet::Resource)
      expect(catalog.resource_keys.find { |type, _| type == "Cap" }).to be_nil
    end

    it "adds produced resources that are exported" do
      manifest = <<-MANIFEST
define test($hostname) {
  notify { "hostname ${hostname}":}
}

# The $hostname in the produces clause does not refer to this variable,
# instead, it referes to the hostname property of the Test resource
# that is producing the Cap
$hostname = "other_host"

Test produces Cap {
  host => $hostname
}

test { one: hostname => "ahost", export => Cap[two] }
    MANIFEST
      catalog = compile_to_catalog(manifest, node)
      expect(catalog.resource("Test[one]")).to be_instance_of(Puppet::Resource)

      caps = catalog.resource_keys.select { |type, _| type == "Cap" }
      expect(caps.size).to eq(1)

      cap = catalog.resource("Cap[two]")
      expect(cap).to be_instance_of(Puppet::Resource)
      expect(cap["require"]).to eq("Test[one]")
      expect(cap["host"]).to eq("ahost")
      expect(cap.resource_type).to eq(Puppet::Type::Cap)
      expect(cap.tags.any? { |t| t == 'producer:testing' }).to eq(true)
    end
  end

  context 'consuming a capability' do
    def make_catalog(instance)
      manifest = <<-MANIFEST
      define test($hostname = nohost) {
        notify { "hostname ${hostname}":}
      }

      Test consumes Cap {
        hostname => $host
      }
    MANIFEST
      compile_to_catalog(manifest + instance, node)
    end

    def mock_cap_finding
      cap = Puppet::Resource.new("Cap", "two")
      cap["host"] = "ahost"
      expect(Puppet::Resource::CapabilityFinder).to receive(:find).and_return(cap)
      cap
    end

    it "does not fetch a consumed resource when consume metaparam not set" do
      expect(Puppet::Resource::CapabilityFinder).not_to receive(:find)
      catalog = make_catalog("test { one: }")
      expect(catalog.resource_keys.find { |type, _| type == "Cap" }).to be_nil
      expect(catalog.resource("Test", "one")["hostname"]).to eq("nohost")
    end

    it "sets hostname from consumed capability" do
      cap = mock_cap_finding
      catalog = make_catalog("test { one: consume => Cap[two] }")
      expect(catalog.resource("Cap[two]")).to eq(cap)
      expect(catalog.resource("Cap[two]")["host"]).to eq("ahost")
      expect(catalog.resource("Test", "one")["hostname"]).to eq("ahost")
    end

    it "does not override explicit hostname property when consuming" do
      cap = mock_cap_finding
      catalog = make_catalog("test { one: hostname => other_host, consume => Cap[two] }")
      expect(catalog.resource("Cap[two]")).to eq(cap)
      expect(catalog.resource("Cap[two]")["host"]).to eq("ahost")
      expect(catalog.resource("Test", "one")["hostname"]).to eq("other_host")
    end

    it "fetches required capability" do
      cap = mock_cap_finding
      catalog = make_catalog("test { one: require => Cap[two] }")
      expect(catalog.resource("Cap[two]")).to eq(cap)
      expect(catalog.resource("Cap[two]")["host"]).to eq("ahost")
      expect(catalog.resource("Test", "one")["hostname"]).to eq("nohost")
    end

    ['export', 'consume'].each do |metaparam|

      it "validates that #{metaparam} metaparameter rejects values that are not resources" do
        expect { make_catalog("test { one: #{metaparam} => 'hello' }") }.to raise_error(Puppet::Error, /not a resource/)
      end

      it "validates that #{metaparam} metaparameter rejects resources that are not capability resources" do
        expect { make_catalog("notify{hello:} test { one: #{metaparam} => Notify[hello] }") }.to raise_error(Puppet::Error, /not a capability resource/)
      end
    end

    context 'producing/consuming resources' do

      let(:ral) do
        compile_to_ral(<<-MANIFEST, node)
  define producer() {
    notify { "producer":}
  }

  define consumer() {
    notify { $title:}
  }

  Producer produces Cap {}

  Consumer consumes Cap {}

  producer {x: export => Cap[cap]}
  consumer {x: consume => Cap[cap]}
  consumer {y: require => Cap[cap]}
        MANIFEST
      end

      let(:graph) do
        graph = Puppet::Graph::RelationshipGraph.new(Puppet::Graph::SequentialPrioritizer.new)
        graph.populate_from(ral)
        graph
      end

      let(:capability) { ral.resource('Cap[cap]') }

      it 'the produced resource depends on the producer' do
        expect(graph.dependencies(capability).map {|d| d.to_s }).to include('Producer[x]')
      end

      it 'the consumer depends on the consumed resource' do
        expect(graph.dependents(capability).map {|d| d.to_s }).to include('Consumer[x]')
      end

      it 'the consumer depends on the required resource' do
        expect(graph.dependents(capability).map {|d| d.to_s }).to include('Consumer[y]')
      end
    end

    context 'producing/consuming resources to/from classes' do

      let(:ral) do
        compile_to_ral(<<-MANIFEST, node)
  define test($hostname) {
    notify { $hostname:}
  }

  class producer($host) {
    notify { p: }
  }

  class consumer($host) {
    test { c: hostname => $host }
  }

  Class[producer] produces Cap {}

  Class[consumer] consumes Cap {}

  class { producer: host => 'produced.host', export => Cap[one]}
  class { consumer: consume => Cap[one]}
        MANIFEST
      end

      let(:graph) do
        graph = Puppet::Graph::RelationshipGraph.new(Puppet::Graph::SequentialPrioritizer.new)
        graph.populate_from(ral)
        graph
      end

      let(:capability) { ral.resource('Cap[one]') }

      it 'the produced resource depends on the producer' do
        expect(graph.dependencies(capability).map {|d| d.to_s }).to include('Class[Producer]')
      end

      it 'the consumer depends on the consumed resource' do
        expect(graph.dependents(capability).map {|d| d.to_s }).to include('Class[Consumer]')
      end

      it 'resource in the consumer class gets values from producer via the capability resource' do
        expect(graph.dependents(capability).map {|d| d.to_s }).to include('Notify[produced.host]')
      end
    end
  end

  context 'and aliased resources' do
    let(:drive) { Puppet.features.microsoft_windows? ? 'C:' : '' }
    let(:code) { <<-PUPPET }
      $dir='#{drive}/tmp/test'
      $same_dir='#{drive}/tmp/test/'

      file {$dir:
        ensure => directory
      }

      file { $same_dir:
        ensure => directory
      }
    PUPPET

    it 'fails if a resource is defined and then redefined using name that results in the same alias' do
      expect { compile_to_ral(code) }.to raise_error(/resource \["File", "#{drive}\/tmp\/test"\] already declared/)
    end
  end
end