File: transport_defaults_spec.rb

package info (click to toggle)
ruby-puppet-resource-api 1.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,232 kB
  • sloc: ruby: 9,573; sh: 4; makefile: 2
file content (107 lines) | stat: -rw-r--r-- 3,798 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
# frozen_string_literal: true

require 'spec_helper'
require 'tempfile'
require 'open3'

RSpec.describe 'a transport' do
  let(:common_args) { '--verbose --trace --debug --strict=error --modulepath spec/fixtures' }

  before(:all) do
    FileUtils.mkdir_p(File.expand_path('~/.puppetlabs/opt/puppet/cache/devices/the_node/state'))
  end

  describe 'using `puppet device`' do
    let(:common_args) { super() + ' --target the_node' }
    let(:device_conf) { Tempfile.new('device.conf') }
    let(:device_conf_content) do
      <<DEVICE_CONF
[the_node]
type test_device_default
url  file://#{device_credentials.path}
DEVICE_CONF
    end
    let(:device_credentials) { Tempfile.new('credentials.txt') }

    def is_device_apply_supported?
      Gem::Version.new(Puppet::PUPPETVERSION) >= Gem::Version.new('5.3.6') && Gem::Version.new(Puppet::PUPPETVERSION) != Gem::Version.new('5.4.0')
    end

    before(:each) do
      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?
      device_conf.write(device_conf_content)
      device_conf.close

      device_credentials.write(device_credentials_content)
      device_credentials.close
    end

    after(:each) do
      device_conf.unlink
      device_credentials.unlink
    end

    context 'when all values are supplied' do
      let(:device_credentials_content) do
        <<DEVICE_CREDS
{
  username: foo
  default_string: other
  optional_default: bar
  array_default: [z]
}
DEVICE_CREDS
      end

      it 'does not use default values' do
        Tempfile.create('apply_success') do |f|
          f.write 'notify { "foo": }'
          f.close

          stdout_str, status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path}  --apply #{f.path}")
          expect(status.exitstatus).to eq 0
          expect(stdout_str).not_to match %r{Value type mismatch}
          expect(stdout_str).not_to match %r{Error}

          expect(stdout_str).to match %r{transport connection_info:}
          expect(stdout_str).to match %r{:username=>"foo"}
          expect(stdout_str).to match %r{:default_string=>"other"}
          expect(stdout_str).to match %r{:optional_default=>"bar"}
          expect(stdout_str).to match %r{:array_default=>\["z"\]}
        end
      end
    end

    context 'when no values supplied for parameters with defaults' do
      let(:device_credentials_content) do
        <<DEVICE_CREDS
{
  username: foo
}
DEVICE_CREDS
      end

      it 'uses the defaults specified' do
        Tempfile.create('apply_success') do |f|
          f.write 'notify { "foo": }'
          f.close

          stdout_str, status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path}  --apply #{f.path}")
          expect(stdout_str).not_to match %r{Value type mismatch}
          expect(stdout_str).not_to match %r{Error}

          expect(stdout_str).to match %r{Debug: test_device_default: Using default value for attribute: default_string, value: "default_value"}
          expect(stdout_str).to match %r{Debug: test_device_default: Using default value for attribute: optional_default, value: "another_default_value"}
          expect(stdout_str).to match %r{Debug: test_device_default: Using default value for attribute: array_default, value: \["a", "b"\]}
          expect(stdout_str).to match %r{transport connection_info:}
          expect(stdout_str).to match %r{:username=>"foo"}
          expect(stdout_str).to match %r{:default_string=>"default_value"}
          expect(stdout_str).to match %r{:optional_default=>"another_default_value"}
          expect(stdout_str).to match %r{:array_default=>\["a", "b"\]}

          expect(status.exitstatus).to eq 0
        end
      end
    end
  end
end