|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'tempfile' |
| 5 | +require 'open3' |
| 6 | + |
| 7 | +RSpec.describe 'a transport' do |
| 8 | + let(:common_args) { '--verbose --trace --debug --strict=error --modulepath spec/fixtures' } |
| 9 | + |
| 10 | + before(:all) do |
| 11 | + FileUtils.mkdir_p(File.expand_path('~/.puppetlabs/opt/puppet/cache/devices/the_node/state')) |
| 12 | + end |
| 13 | + |
| 14 | + describe 'using `puppet device`' do |
| 15 | + let(:common_args) { super() + ' --target the_node' } |
| 16 | + let(:device_conf) { Tempfile.new('device.conf') } |
| 17 | + let(:device_conf_content) do |
| 18 | + <<DEVICE_CONF |
| 19 | +[the_node] |
| 20 | +type test_device_default |
| 21 | +url file://#{device_credentials.path} |
| 22 | +DEVICE_CONF |
| 23 | + end |
| 24 | + let(:device_credentials) { Tempfile.new('credentials.txt') } |
| 25 | + |
| 26 | + def is_device_apply_supported? |
| 27 | + Gem::Version.new(Puppet::PUPPETVERSION) >= Gem::Version.new('5.3.6') && Gem::Version.new(Puppet::PUPPETVERSION) != Gem::Version.new('5.4.0') |
| 28 | + end |
| 29 | + |
| 30 | + before(:each) do |
| 31 | + skip "No device --apply in puppet before v5.3.6 nor in v5.4.0 (v#{Puppet::PUPPETVERSION} is installed)" unless is_device_apply_supported? |
| 32 | + device_conf.write(device_conf_content) |
| 33 | + device_conf.close |
| 34 | + |
| 35 | + device_credentials.write(device_credentials_content) |
| 36 | + device_credentials.close |
| 37 | + end |
| 38 | + |
| 39 | + after(:each) do |
| 40 | + device_conf.unlink |
| 41 | + device_credentials.unlink |
| 42 | + end |
| 43 | + |
| 44 | + context 'when all values are supplied' do |
| 45 | + let(:device_credentials_content) do |
| 46 | + <<DEVICE_CREDS |
| 47 | +{ |
| 48 | + username: foo |
| 49 | + default_string: other |
| 50 | + optional_default: bar |
| 51 | + array_default: [z] |
| 52 | +} |
| 53 | +DEVICE_CREDS |
| 54 | + end |
| 55 | + |
| 56 | + it 'does not use default values' do |
| 57 | + Tempfile.create('apply_success') do |f| |
| 58 | + f.write 'notify { "foo": }' |
| 59 | + f.close |
| 60 | + |
| 61 | + stdout_str, status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") |
| 62 | + expect(status.exitstatus).to eq 0 |
| 63 | + expect(stdout_str).not_to match %r{Value type mismatch} |
| 64 | + expect(stdout_str).not_to match %r{Error} |
| 65 | + |
| 66 | + expect(stdout_str).to match %r{transport connection_info:} |
| 67 | + expect(stdout_str).to match %r{:username=>"foo"} |
| 68 | + expect(stdout_str).to match %r{:default_string=>"other"} |
| 69 | + expect(stdout_str).to match %r{:optional_default=>"bar"} |
| 70 | + expect(stdout_str).to match %r{:array_default=>\["z"\]} |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context 'when no values supplied for parameters with defaults' do |
| 76 | + let(:device_credentials_content) do |
| 77 | + <<DEVICE_CREDS |
| 78 | +{ |
| 79 | + username: foo |
| 80 | +} |
| 81 | +DEVICE_CREDS |
| 82 | + end |
| 83 | + |
| 84 | + it 'uses the defaults specified' do |
| 85 | + Tempfile.create('apply_success') do |f| |
| 86 | + f.write 'notify { "foo": }' |
| 87 | + f.close |
| 88 | + |
| 89 | + stdout_str, status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") |
| 90 | + expect(stdout_str).not_to match %r{Value type mismatch} |
| 91 | + expect(stdout_str).not_to match %r{Error} |
| 92 | + |
| 93 | + expect(stdout_str).to match %r{Debug: test_device_default: Using default value for attribute: default_string, value: "default_value"} |
| 94 | + expect(stdout_str).to match %r{Debug: test_device_default: Using default value for attribute: optional_default, value: "another_default_value"} |
| 95 | + expect(stdout_str).to match %r{Debug: test_device_default: Using default value for attribute: array_default, value: \["a", "b"\]} |
| 96 | + expect(stdout_str).to match %r{transport connection_info:} |
| 97 | + expect(stdout_str).to match %r{:username=>"foo"} |
| 98 | + expect(stdout_str).to match %r{:default_string=>"default_value"} |
| 99 | + expect(stdout_str).to match %r{:optional_default=>"another_default_value"} |
| 100 | + expect(stdout_str).to match %r{:array_default=>\["a", "b"\]} |
| 101 | + |
| 102 | + expect(status.exitstatus).to eq 0 |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments