File: wrapper_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 (139 lines) | stat: -rw-r--r-- 5,038 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
# frozen_string_literal: true

require 'spec_helper'
require 'puppet/resource_api/transport/wrapper'
require_relative '../../../fixtures/test_module/lib/puppet/transport/test_device'

RSpec.describe Puppet::ResourceApi::Transport::Wrapper, agent_test: true do
  describe '#initialize(name, url_or_config)' do
    context 'when called with a url' do
      context 'with a file:// prefix' do
        let(:url) { 'file:///etc/credentials' }

        it 'will not throw an error' do
          allow(File).to receive(:exist?).and_return(true)
          allow(Hocon).to receive(:load).and_call_original
          expect(Puppet::ResourceApi::Transport).to receive(:connect)
          expect(Hocon).to receive(:load).with('/etc/credentials', any_args).and_return('foo' => %w[a b], 'bar' => 2)
          expect { described_class.new('wibble', url) }.not_to raise_error
        end
      end

      context 'with an http:// prefix' do
        let(:url) { 'http://www.puppet.com' }

        it { expect { described_class.new('wibble', url) }.to raise_error RuntimeError, %r{Only file:/// URLs for configuration supported} }
      end
    end

    context 'when called with a config hash' do
      let(:config) { {} }

      it 'will use the configuration directly' do
        allow(Hocon).to receive(:load).and_call_original
        expect(Hocon).not_to receive(:load).with('/etc/credentials', any_args)
        expect(Puppet::ResourceApi::Transport).to receive(:connect)
        described_class.new('wibble', config)
      end
    end

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

    context 'when called with a transport class' do
      let(:transport) { Puppet::Transport::SomethingSomethingDarkside.new }
      let(:instance) { described_class.new('something_something_darkside', transport) }

      it 'will set the @transport class variable' do
        expect(instance.instance_variable_get(:@transport)).to eq(transport)
      end
    end
  end

  describe '#facts' do
    context 'when called' do
      let(:instance) { described_class.new('wibble', {}) }
      let(:context) { instance_double(Puppet::ResourceApi::PuppetContext, 'context') }
      let(:facts) { { 'foo' => 'bar' } }
      let(:transport) { instance_double(Puppet::Transport::TestDevice, 'transport') }

      it 'will return the facts provided by the transport' do
        allow(Puppet::ResourceApi::Transport).to receive(:connect).and_return(transport)
        allow(Puppet::ResourceApi::Transport).to receive(:list).and_return(schema: :dummy)
        allow(Puppet::ResourceApi::PuppetContext).to receive(:new).and_return(context)
        allow(transport).to receive(:facts).with(context).and_return(facts)

        expect(instance.facts).to eq(facts)
      end
    end
  end

  context 'when an unsupported method is called' do
    context 'when the transport can handle the method' do
      let(:instance) { described_class.new('wibble', {}) }
      let(:transport) { instance_double(Puppet::Transport::TestDevice, 'transport') }
      let(:context) { instance_double(Puppet::ResourceApi::PuppetContext, 'context') }

      it 'will return the facts provided by the transport' do
        allow(Puppet::ResourceApi::Transport).to receive(:connect).and_return(transport)
        expect(transport).to receive(:close)

        instance.close(context)
      end
    end

    context 'when the transport cannot handle the method' do
      let(:instance) { described_class.new('wibble', {}) }
      let(:transport) { instance_double(Puppet::Transport::TestDevice, 'transport') }

      it 'will raise a NoMethodError' do
        allow(Puppet::ResourceApi::Transport).to receive(:connect).and_return(transport)
        expect { instance.wibble }.to raise_error NoMethodError
      end
    end
  end

  context 'when a method is checked for' do
    let(:instance) { described_class.new('wibble', {}) }
    let(:transport) { instance_double(Puppet::Transport::TestDevice, 'transport') }

    before(:each) do
      allow(Puppet::ResourceApi::Transport).to receive(:connect).and_return(transport)
    end

    context 'when the transport does not support the function' do
      context 'when using respond_to?' do
        it 'will return false' do
          expect(instance.respond_to?(:wibble)).to eq(false)
        end
      end

      context 'when using method?' do
        it 'will return false' do
          expect { instance.method :wibble }.to raise_error NameError, %r{undefined method `wibble'}
        end
      end
    end

    context 'when the transport does support the function' do
      before(:each) do
        allow(transport).to receive(:close)
      end

      context 'when using respond_to?' do
        it 'will return true' do
          expect(instance.respond_to?(:close)).to eq(true)
        end
      end

      context 'when using method?' do
        it 'will return the method' do
          expect(instance.method(:close)).to be_a(Method)
        end
      end
    end
  end
end