File: transport_spec.rb

package info (click to toggle)
ruby-puppet-resource-api 1.8.16-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,228 kB
  • sloc: ruby: 9,519; sh: 4; makefile: 2
file content (385 lines) | stat: -rw-r--r-- 15,202 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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Puppet::ResourceApi::Transport do
  def change_environment(name = nil)
    environment = class_double(Puppet::Node::Environment)

    if name.nil?
      allow(Puppet).to receive(:respond_to?).with(:lookup).and_return(false)
    else
      allow(Puppet).to receive(:respond_to?).with(:lookup).and_return(true)
    end

    allow(Puppet).to receive(:lookup).with(:current_environment).and_return(environment)

    # allow clean up scripts to run unhindered
    allow(Puppet).to receive(:lookup).with(:root_environment).and_call_original
    allow(Puppet).to receive(:lookup).with(:environments).and_call_original

    allow(environment).to receive(:name).and_return(name)
  end

  let(:strict_level) { :error }

  before(:each) do
    # set default to strictest setting
    # by default Puppet runs at warning level
    Puppet.settings[:strict] = strict_level
    # Enable debug logging
    Puppet.debug = true
  end

  describe '#register(schema)' do
    describe 'validation checks' do
      it { expect { described_class.register([]) }.to raise_error(Puppet::DevError, %r{requires a hash as schema}) }
      it { expect { described_class.register({}) }.to raise_error(Puppet::DevError, %r{requires a `:name`}) }
      it { expect { described_class.register(name: 'no connection info', desc: 'some description') }.to raise_error(Puppet::DevError, %r{requires `:connection_info`}) }
      it { expect { described_class.register(name: 'no description') }.to raise_error(Puppet::DevError, %r{requires `:desc`}) }
      it {
        expect {
          described_class.register(name: 'no hash connection_info',
                                   desc: 'some description',
                                   connection_info: [])
        } .to raise_error(Puppet::DevError, %r{`:connection_info` must be a hash, not})
      }
      it {
        expect(described_class.register(name: 'no array connection_info_order',
                                        desc: 'some description',
                                        connection_info: {}).definition).to have_key(:connection_info_order)
      }
      it {
        expect(described_class.register(name: 'no array connection_info_order',
                                        desc: 'some description',
                                        connection_info: {}).definition[:connection_info_order]).to eq []
      }
      it {
        expect {
          described_class.register(name: 'no array connection_info_order',
                                   desc: 'some description',
                                   connection_info: {},
                                   connection_info_order: {})
        }.to raise_error(Puppet::DevError, %r{`:connection_info_order` must be an array, not})
      }
    end

    context 'when registering a minimal transport' do
      let(:schema) { { name: 'minimal', desc: 'a  minimal connection', connection_info: {} } }

      it { expect { described_class.register(schema) }.not_to raise_error }

      context 'when re-registering a transport' do
        it {
          described_class.register(schema)
          expect { described_class.register(schema) }.to raise_error(Puppet::DevError, %r{`minimal` is already registered})
        }
      end
    end

    context 'when registering a transport' do
      let(:schema) do
        {
          name: 'a_remote_thing',
          desc: 'basic transport',
          connection_info: {
            host: {
              type: 'String',
              desc: 'the host ip address or hostname',
            },
            user: {
              type: 'String',
              desc: 'the user to connect as',
            },
            password: {
              type: 'String',
              sensitive: true,
              desc: 'the password to make the connection',
            },
          },
        }
      end

      context 'when a environment is available' do
        before(:each) { change_environment('production') }

        it 'adds to the transports register' do
          expect { described_class.register(schema) }.not_to raise_error
        end
      end

      context 'when no environment is available' do
        before(:each) { change_environment(nil) }

        it 'adds to the transports register' do
          expect { described_class.register(schema) }.not_to raise_error
        end
      end
    end

    context 'when registering a transport with a bad type' do
      let(:schema) do
        {
          name: 'a_bad_thing',
          desc: 'basic transport',
          connection_info: {
            host: {
              type: 'garbage',
              desc: 'the host ip address or hostname',
            },
          },
        }
      end

      it {
        expect { described_class.register(schema) }.to raise_error(
          Puppet::DevError, %r{<garbage> is not a valid type specification}
        )
      }
    end
  end

  describe '#list' do
    subject { described_class.list }

    context 'with no transports registered' do
      it { is_expected.to eq({}) }
    end

    context 'with a transport registered' do
      let(:schema) do
        {
          name: 'test_target',
          desc: 'a basic transport',
          connection_info: {
            host: {
              type: 'String',
              desc: 'the host ip address or hostname',
            },
          },
        }
      end

      before(:each) do
        described_class.register(schema)
      end

      it { expect(described_class.list['test_target'].definition).to eq schema }
      it 'returns a new object' do
        expect(described_class.list['test_target'].definition.object_id).not_to eq schema.object_id
      end
    end
  end

  describe '#connect(name, connection_info)', agent_test: true do
    let(:name) { 'test_target' }
    let(:schema) do
      {
        name: 'test_target',
        desc: 'a basic transport',
        connection_info: {
          host: {
            type: 'String',
            desc: 'the host ip address or hostname',
          },
        },
      }
    end

    context 'when the transport file does not exist' do
      it 'throws a LoadError' do
        expect(described_class).to receive(:validate).with(name, host: 'example.com')
        expect { described_class.connect(name, host: 'example.com') }.to raise_error LoadError, %r{(no such file to load|cannot load such file) -- puppet/transport/test_target}
      end
    end

    context 'when the transport file does exist' do
      context 'with an incorrectly defined transport' do
        it 'throws a NameError' do
          described_class.register(schema)

          expect(described_class).to receive(:validate).with(name, host: 'example.com')
          expect(described_class).to receive(:require).with('puppet/transport/test_target')
          expect { described_class.connect(name, host: 'example.com') }.to raise_error NameError,
                                                                                       %r{uninitialized constant (Puppet::Transport|TestTarget)}
        end
      end

      context 'with a correctly defined transport' do
        let(:test_target) { double('Puppet::Transport::TestTarget') } # rubocop:disable RSpec/VerifiedDoubles
        let(:context) { instance_double(Puppet::ResourceApi::PuppetContext, 'context') }

        it 'loads initiates the class successfully' do
          described_class.register(schema)

          allow(described_class).to receive(:require).with('puppet/resource_api/puppet_context').and_call_original
          expect(described_class).to receive(:require).with('puppet/transport/test_target')
          expect(described_class).to receive(:validate).with(name, host: 'example.com')
          expect(Puppet::ResourceApi::PuppetContext).to receive(:new).with(kind_of(Puppet::ResourceApi::TransportSchemaDef)).and_return(context)

          stub_const('Puppet::Transport::TestTarget', test_target)
          expect(test_target).to receive(:new).with(context, host: 'example.com')

          described_class.connect(name, host: 'example.com')
        end
      end
    end
  end

  describe '#inject_device(name, transport)' do
    let(:device_name) { 'wibble' }
    let(:transport) { instance_double(Puppet::Transport::Wibble, 'transport') }
    let(:wrapper) { instance_double(Puppet::ResourceApi::Transport::Wrapper, 'wrapper') }

    before(:each) do
      module Puppet::Transport
        class Wibble; end
      end
    end

    after(:each) do
      Puppet::Util::NetworkDevice.instance_variable_set(:@current, nil)
    end

    context 'when puppet has set_device' do
      it 'wraps the transport and calls set_device within NetworkDevice' do
        expect(Puppet::ResourceApi::Transport::Wrapper).to receive(:new).with(device_name, transport).and_return(wrapper)
        allow(Puppet::Util::NetworkDevice).to receive(:respond_to?).with(:set_device).and_return(true)
        expect(Puppet::Util::NetworkDevice).to receive(:set_device).with(device_name, wrapper)

        described_class.inject_device(device_name, transport)
      end
    end

    context 'when puppet does not have set_device' do
      it 'wraps the transport and sets it as current in NetworkDevice' do
        expect(Puppet::ResourceApi::Transport::Wrapper).to receive(:new).with(device_name, transport).and_return(wrapper)
        expect(Puppet::Util::NetworkDevice).to receive(:respond_to?).with(:set_device).and_return(false)

        described_class.inject_device(device_name, transport)

        expect(Puppet::Util::NetworkDevice.current).to eq(wrapper)
      end
    end
  end

  describe '#validate(name, connection_info)', agent_test: true do
    context 'when the transport does not exist' do
      it { expect { described_class.send(:validate, 'wibble', {}) }.to raise_error LoadError, %r{(no such file to load|cannot load such file) -- puppet/transport/schema/wibble} }
    end

    context 'when the transport being validated has not be registered' do
      it 'will throw an unregistered error message' do
        expect(described_class).to receive(:require).with('puppet/transport/schema/wibble')
        expect { described_class.send(:validate, 'wibble', {}) }.to raise_error Puppet::DevError, %r{ not registered with }
      end
    end

    context 'with a registered transport' do
      let(:attributes) { {} }
      let(:schema) { { name: 'validate', desc: 'a  minimal connection', connection_info: attributes } }
      let(:schema_def) { instance_double('Puppet::ResourceApi::TransportSchemaDef', 'schema_def') }
      let(:context) { instance_double(Puppet::ResourceApi::PuppetContext, 'context') }

      before(:each) do
        allow(Puppet::ResourceApi::TransportSchemaDef).to receive(:new).with(schema).and_return(schema_def)
        allow(schema_def).to receive(:attributes).with(no_args).and_return(attributes)
        allow(schema_def).to receive(:name).with(no_args).and_return(schema[:name])
        allow(described_class).to receive(:get_context).with('validate').and_return(context)

        described_class.register(schema)
      end

      it 'validates the connection_info' do
        expect(described_class).not_to receive(:require).with('puppet/transport/schema/validate')
        expect(schema_def).to receive(:check_schema).with({}, kind_of(String)).and_return(nil)
        expect(schema_def).to receive(:validate).with({}).and_return(nil)

        described_class.send :validate, 'validate', {}
      end

      context 'when validating bolt _target information' do
        let(:attributes) { { host: {}, foo: {} } }

        it 'cleans the connection_info' do
          expect(schema_def).to receive(:check_schema).with({ host: 'host value', foo: 'foo value' }, kind_of(String)).and_return(nil)
          expect(schema_def).to receive(:validate).with(host: 'host value', foo: 'foo value').and_return(nil)

          expect(context).to receive(:debug).with('Discarding bolt metaparameter: query')
          expect(context).to receive(:debug).with('Discarding bolt metaparameter: remote-transport')
          expect(context).to receive(:debug).with('Discarding bolt metaparameter: remote-reserved')
          expect(context).to receive(:info).with('Discarding superfluous bolt attribute: user')
          expect(context).to receive(:warning).with('Discarding unknown attribute: bar')
          described_class.send :validate, 'validate', 'remote-transport': 'validate',
                                                      host: 'host value',
                                                      foo: 'foo value',
                                                      user: 'superfluous bolt value',
                                                      query: 'metaparameter value',
                                                      'remote-reserved': 'reserved value',
                                                      bar: 'unknown attribute'
        end
      end

      context 'when applying defaults' do
        let(:attributes) { { host: { default: 'example.com' }, port: { default: 443 } } }

        it 'sets defaults in the connection info' do
          expect(schema_def).to receive(:check_schema).with({ host: 'host value', port: 443 }, kind_of(String)).and_return(nil)
          expect(schema_def).to receive(:validate).with(host: 'host value', port: 443).and_return(nil)

          expect(context).to receive(:debug).with('Using default value for attribute: port, value: 443')
          described_class.send :validate, 'validate', host: 'host value'
        end
      end
    end
  end

  describe '#wrap_sensitive(name, connection_info)' do
    let(:schema) do
      {
        name: 'sensitive_transport',
        desc: 'a  secret',
        connection_info: {
          secret: {
            type:      'String',
            desc:      'A secret to protect.',
            sensitive:  true,
          },
        },
      }
    end
    let(:schema_def) { instance_double('Puppet::ResourceApi::TransportSchemaDef', 'schema_def') }

    before(:each) do
      allow(Puppet::ResourceApi::TransportSchemaDef).to receive(:new).with(schema).and_return(schema_def)
      described_class.register(schema)
    end

    context 'when the connection info contains a `Sensitive` type' do
      let(:connection_info) do
        {
          secret: 'sup3r_secret_str1ng',
        }
      end

      it 'wraps the value in a PSensitiveType' do
        allow(schema_def).to receive(:definition).and_return(schema)

        conn_info = described_class.send :wrap_sensitive, 'sensitive_transport', connection_info
        expect(conn_info[:secret]).to be_a(Puppet::Pops::Types::PSensitiveType::Sensitive)
        expect(conn_info[:secret].unwrap).to eq('sup3r_secret_str1ng')
      end
    end

    context 'when the connection info does not contain a `Sensitive` type' do
      let(:connection_info) { {} }

      it 'wraps the value in a PSensitiveType' do
        allow(schema_def).to receive(:definition).and_return(schema)

        conn_info = described_class.send :wrap_sensitive, 'sensitive_transport', connection_info
        expect(conn_info[:secret]).to be_nil
      end
    end
  end
end