|
1 | 1 | require 'spec_helper' |
2 | 2 |
|
3 | 3 | RSpec.describe Puppet::ResourceApi::Transport do |
| 4 | + def change_environment(name = nil) |
| 5 | + environment = class_double(Puppet::Node::Environment) |
| 6 | + |
| 7 | + if name.nil? |
| 8 | + allow(Puppet).to receive(:respond_to?).and_return(false) |
| 9 | + else |
| 10 | + allow(Puppet).to receive(:respond_to?).and_return(true) |
| 11 | + end |
| 12 | + |
| 13 | + allow(Puppet).to receive(:lookup).with(:current_environment).and_return(environment) |
| 14 | + |
| 15 | + # allow clean up scripts to run unhindered |
| 16 | + allow(Puppet).to receive(:lookup).with(:root_environment).and_call_original |
| 17 | + allow(Puppet).to receive(:lookup).with(:environments).and_call_original |
| 18 | + |
| 19 | + allow(environment).to receive(:name).and_return(name) |
| 20 | + end |
| 21 | + |
4 | 22 | let(:strict_level) { :error } |
5 | 23 |
|
6 | 24 | before(:each) do |
|
60 | 78 | }, |
61 | 79 | } |
62 | 80 | end |
| 81 | + let(:schema2) do |
| 82 | + { |
| 83 | + name: 'schema2', |
| 84 | + desc: 'basic transport', |
| 85 | + connection_info: { |
| 86 | + host: { |
| 87 | + type: 'String', |
| 88 | + desc: 'the host ip address or hostname', |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | + end |
| 93 | + let(:schema3) do |
| 94 | + { |
| 95 | + name: 'schema3', |
| 96 | + desc: 'basic transport', |
| 97 | + connection_info: { |
| 98 | + user: { |
| 99 | + type: 'String', |
| 100 | + desc: 'the user to connect as', |
| 101 | + }, |
| 102 | + password: { |
| 103 | + type: 'String', |
| 104 | + sensitive: true, |
| 105 | + desc: 'the password to make the connection', |
| 106 | + }, |
| 107 | + }, |
| 108 | + } |
| 109 | + end |
63 | 110 |
|
64 | 111 | it 'adds to the transports register' do |
65 | 112 | expect { described_class.register(schema) }.not_to raise_error |
66 | 113 | end |
| 114 | + |
| 115 | + context 'when a transports are added to multiple environments' do |
| 116 | + it 'will record the schemas in the correct structure' do |
| 117 | + change_environment(:wibble) |
| 118 | + described_class.register(schema) |
| 119 | + expect(described_class.instance_variable_get(:@transports)).to be_key(:wibble) |
| 120 | + expect(described_class.instance_variable_get(:@transports)[:wibble][schema[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) |
| 121 | + expect(described_class.instance_variable_get(:@transports)[:wibble][schema[:name]].definition).to eq(schema) |
| 122 | + |
| 123 | + change_environment(:foo) |
| 124 | + described_class.register(schema) |
| 125 | + described_class.register(schema2) |
| 126 | + expect(described_class.instance_variable_get(:@transports)).to be_key(:foo) |
| 127 | + expect(described_class.instance_variable_get(:@transports)[:foo][schema[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) |
| 128 | + expect(described_class.instance_variable_get(:@transports)[:foo][schema[:name]].definition).to eq(schema) |
| 129 | + expect(described_class.instance_variable_get(:@transports)[:foo][schema2[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) |
| 130 | + expect(described_class.instance_variable_get(:@transports)[:foo][schema2[:name]].definition).to eq(schema2) |
| 131 | + |
| 132 | + change_environment(:bar) |
| 133 | + described_class.register(schema3) |
| 134 | + expect(described_class.instance_variable_get(:@transports)).to be_key(:bar) |
| 135 | + expect(described_class.instance_variable_get(:@transports)[:bar][schema3[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) |
| 136 | + expect(described_class.instance_variable_get(:@transports)[:bar][schema3[:name]].definition).to eq(schema3) |
| 137 | + end |
| 138 | + end |
67 | 139 | end |
68 | 140 |
|
69 | 141 | context 'when registering a transport with a bad type' do |
|
176 | 248 | end |
177 | 249 |
|
178 | 250 | describe '#validate(name, connection_info)', agent_test: true do |
| 251 | + context 'when the transport does not exist' do |
| 252 | + 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} } |
| 253 | + end |
| 254 | + |
179 | 255 | context 'when the transport being validated has not be registered' do |
180 | | - it { expect { described_class.validate('wibble', {}) }.to raise_error LoadError, %r{(no such file to load|cannot load such file) -- puppet/transport/schema/wibble} } |
| 256 | + it 'will throw an unregistered error message' do |
| 257 | + expect(described_class).to receive(:require).with('puppet/transport/schema/wibble') |
| 258 | + expect { described_class.send(:validate, 'wibble', {}) }.to raise_error Puppet::DevError, %r{ not registered with } |
| 259 | + end |
181 | 260 | end |
182 | 261 |
|
183 | 262 | context 'when the transport being validated has been registered' do |
|
189 | 268 |
|
190 | 269 | described_class.register(schema) |
191 | 270 |
|
| 271 | + expect(described_class).to receive(:require).with('puppet/transport/schema/validate') |
192 | 272 | expect(schema_def).to receive(:check_schema).with('connection_info').and_return(nil) |
193 | 273 | expect(schema_def).to receive(:validate).with('connection_info').and_return(nil) |
194 | 274 |
|
195 | | - described_class.validate('validate', 'connection_info') |
| 275 | + described_class.send :validate, 'validate', 'connection_info' |
| 276 | + end |
| 277 | + end |
| 278 | + end |
| 279 | + |
| 280 | + describe '#init_transports' do |
| 281 | + context 'when there is not a current_environment' do |
| 282 | + it 'will return the default transport environment name' do |
| 283 | + change_environment |
| 284 | + |
| 285 | + described_class.send :init_transports |
| 286 | + |
| 287 | + expect(described_class.instance_variable_get(:@environment)).to eq(:transports_default) |
| 288 | + end |
| 289 | + end |
| 290 | + |
| 291 | + context 'when there is a current_environment' do |
| 292 | + it 'will return the set environment name' do |
| 293 | + change_environment(:something) |
| 294 | + |
| 295 | + described_class.send :init_transports |
| 296 | + |
| 297 | + expect(described_class.instance_variable_get(:@environment)).to eq(:something) |
196 | 298 | end |
197 | 299 | end |
198 | 300 | end |
|
0 commit comments