File: exec_spec.rb

package info (click to toggle)
r10k 5.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,228 kB
  • sloc: ruby: 18,180; makefile: 10; sh: 1
file content (81 lines) | stat: -rw-r--r-- 2,901 bytes parent folder | download | duplicates (3)
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
require 'spec_helper'
require 'r10k/source'
require 'json'
require 'yaml'

describe R10K::Source::Exec do

  let(:environments_hash) do
    {
      'production' => {
        'remote'  => 'https://git.example.com/puppet/control-repo.git',
        'ref'     => 'release-141',
        'modules' => {
          'puppetlabs-stdlib' => '6.1.0',
          'puppetlabs-ntp' => '8.1.0',
          'example-myapp1' => {
            'git' => 'https://git.example.com/puppet/example-myapp1.git',
            'ref' => 'v1.3.0'
          }
        }
      },
      'development' => {
        'remote'  => 'https://git.example.com/puppet/control-repo.git',
        'ref'     => 'master',
        'modules' => {
          'puppetlabs-stdlib' => '6.1.0',
          'puppetlabs-ntp' => '8.1.0',
          'example-myapp1' => {
            'git' => 'https://git.example.com/puppet/example-myapp1.git',
            'ref' => 'v1.3.1'
          }
        }
      }
    }
  end

  describe 'initialize' do
    context 'with a valid command' do
      context 'that produces valid output' do
        it 'accepts json' do
          allow_any_instance_of(R10K::Util::Subprocess)
            .to receive(:execute)
            .and_return(double('result', stdout: environments_hash.to_json))

          source = described_class.new('execsource', '/some/nonexistent/dir', command: '/path/to/command')
          expect(source.environments.map(&:name)).to contain_exactly('production', 'development')
        end

        it 'accepts yaml' do
          allow_any_instance_of(R10K::Util::Subprocess)
            .to receive(:execute)
            .and_return(double('result', stdout: environments_hash.to_yaml))

          source = described_class.new('execsource', '/some/nonexistent/dir', command: '/path/to/command')
          expect(source.environments.map(&:name)).to contain_exactly('production', 'development')
        end

      end

      context 'that produces invalid output' do
        it 'raises an error for non-json, non-yaml data' do
          allow_any_instance_of(R10K::Util::Subprocess)
            .to receive(:execute)
            .and_return(double('result', stdout: "one:\ntwo\n"))

          source = described_class.new('execsource', '/some/nonexistent/dir', command: '/path/to/command')
          expect { source.environments }.to raise_error(/Error parsing command output/)
        end

        it 'raises an error for yaml data that is not a hash' do
          allow_any_instance_of(R10K::Util::Subprocess)
            .to receive(:execute)
            .and_return(double('result', stdout: "[one, two]"))

          source = described_class.new('execsource', '/some/nonexistent/dir', command: '/path/to/command')
          expect { source.environments }.to raise_error(R10K::Error, /Environment source execsource.*did not return valid environment data.*one.*two.*/m)
        end
      end
    end
  end
end