|
2 | 2 |
|
3 | 3 | require_relative '../tests/spec_helper'
|
4 | 4 |
|
| 5 | +require 'json' |
5 | 6 | require 'open3'
|
6 | 7 | require 'shellwords'
|
7 | 8 |
|
|
28 | 29 | expect(File.executable?(script)).to eq(true), ls_l
|
29 | 30 | end
|
30 | 31 |
|
31 |
| - context 'with an invalid configuration file' do |
32 |
| - let(:cfg) { OctocatalogDiff::Spec.fixture_path('cli-configs/invalid.rb') } |
33 |
| - let(:env) { { 'OCTOCATALOG_DIFF_CONFIG_FILE' => cfg } } |
| 32 | + context 'config test' do |
| 33 | + context 'config file found' do |
| 34 | + it 'should display configuration settings and then exit 0' do |
| 35 | + env = { 'OCTOCATALOG_DIFF_CONFIG_FILE' => OctocatalogDiff::Spec.fixture_path('cli-configs/valid.rb') } |
| 36 | + argv = ['--config-test'] |
34 | 37 |
|
35 |
| - it 'should error with --config-test' do |
36 |
| - text, status = Open3.capture2e(env, "#{script} #{config_test}") |
37 |
| - expect(status.exitstatus).to eq(1), text |
38 |
| - expect(text).to match(%r{Loading octocatalog-diff configuration from .+/fixtures/cli-configs/invalid.rb}) |
39 |
| - expect(text).to match(/FATAL .+: Fizz Buzz/) |
| 38 | + cmdline = [script, argv].flatten.map { |x| Shellwords.escape(x) }.join(' ') |
| 39 | + stdout, stderr, status = Open3.capture3(env, cmdline) |
| 40 | + |
| 41 | + expect(status.exitstatus).to eq(0) |
| 42 | + expect(stdout).to eq('') |
| 43 | + expect(stderr).to match(%r{Loading octocatalog-diff configuration from .+/cli-configs/valid.rb}) |
| 44 | + expect(stderr).to match(/DEBUG -- : :header => \(Symbol\) :default/) |
| 45 | + expect(stderr).to match(/DEBUG -- : Loaded 3 settings from/) |
| 46 | + expect(stderr).to match(/INFO -- : Exiting now because --config-test was specified/) |
| 47 | + end |
40 | 48 | end
|
41 | 49 |
|
42 |
| - it 'should error with normal settings' do |
43 |
| - text, status = Open3.capture2e(env, "#{script} #{normal_settings}") |
44 |
| - expect(status.exitstatus).to eq(1), text |
45 |
| - expect(text).to match(%r{Loading octocatalog-diff configuration from .+/fixtures/cli-configs/invalid.rb}) |
46 |
| - expect(text).to match(/FATAL .+: Fizz Buzz/) |
| 50 | + context 'invalid config file' do |
| 51 | + it 'should raise error and exit 1' do |
| 52 | + env = { 'OCTOCATALOG_DIFF_CONFIG_FILE' => OctocatalogDiff::Spec.fixture_path('cli-configs/invalid.rb') } |
| 53 | + argv = ['--config-test'] |
| 54 | + |
| 55 | + cmdline = [script, argv].flatten.map { |x| Shellwords.escape(x) }.join(' ') |
| 56 | + stdout, stderr, status = Open3.capture3(env, cmdline) |
| 57 | + |
| 58 | + expect(status.exitstatus).to eq(1) |
| 59 | + expect(stdout).to eq('') |
| 60 | + expect(stderr).to match(%r{Loading octocatalog-diff configuration from .+/cli-configs/invalid.rb}) |
| 61 | + expect(stderr).to match(/FATAL -- : RuntimeError error with.+Fizz Buzz/) |
| 62 | + end |
47 | 63 | end
|
48 | 64 | end
|
49 | 65 |
|
50 |
| - context 'with a valid configuration file' do |
51 |
| - let(:cfg) { OctocatalogDiff::Spec.fixture_path('cli-configs/valid.rb') } |
52 |
| - let(:env) { { 'OCTOCATALOG_DIFF_CONFIG_FILE' => cfg } } |
53 |
| - |
54 |
| - it 'should print values with --config-test and exit' do |
55 |
| - text, status = Open3.capture2e(env, "#{script} #{config_test}") |
56 |
| - expect(status.exitstatus).to eq(0), text |
57 |
| - expect(text).to match(%r{Loading octocatalog-diff configuration from .+/fixtures/cli-configs/valid.rb}) |
58 |
| - expect(text).to match(/:header => \(Symbol\) :default/) |
59 |
| - expect(text).to match(%r{:hiera_config => \(String\) "config/hiera.yaml"}) |
60 |
| - expect(text).to match(/:hiera_path => \(String\) "hieradata"/) |
61 |
| - expect(text).to match(/Exiting now because --config-test was specified/) |
| 66 | + context 'normal' do |
| 67 | + context 'with valid catalogs that differ' do |
| 68 | + it 'should display output and exit 2' do |
| 69 | + env = { 'OCTOCATALOG_DIFF_CONFIG_FILE' => OctocatalogDiff::Spec.fixture_path('cli-configs/valid.rb') } |
| 70 | + argv = [ |
| 71 | + '--bootstrapped-to-dir', OctocatalogDiff::Spec.fixture_path('repos/default'), |
| 72 | + '--from-catalog', OctocatalogDiff::Spec.fixture_path('catalogs/tiny-catalog.json'), |
| 73 | + '--puppet-binary', OctocatalogDiff::Spec::PUPPET_BINARY, |
| 74 | + '--fact-file', OctocatalogDiff::Spec.fixture_path('facts/facts.json'), |
| 75 | + '-n', 'rspec-node.github.net', |
| 76 | + '--no-color' |
| 77 | + ] |
| 78 | + |
| 79 | + cmdline = [script, argv].flatten.map { |x| Shellwords.escape(x) }.join(' ') |
| 80 | + stdout, stderr, status = Open3.capture3(env, cmdline) |
| 81 | + |
| 82 | + expect(status.exitstatus).to eq(2), [stdout, stderr].join("\n") |
| 83 | + |
| 84 | + out_lines = stdout.split(/\n/) |
| 85 | + expect(out_lines).to include('diff origin/master/rspec-node.github.net current/rspec-node.github.net') |
| 86 | + expect(out_lines).to include('+ File[/root/.ssh]') |
| 87 | + expect(out_lines).to include('+ System::User[bob]') |
| 88 | + |
| 89 | + expect(stderr).not_to match(%r{Loading octocatalog-diff configuration from .+/cli-configs/valid.rb}) |
| 90 | + expect(stderr).not_to match(/DEBUG -- : :header => \(Symbol\) :default/) |
| 91 | + expect(stderr).not_to match(/DEBUG -- : Loaded 3 settings from/) |
| 92 | + expect(stderr).not_to match(/INFO -- : Exiting now because --config-test was specified/) |
| 93 | + expect(stderr).to match(/INFO -- : Catalogs compiled for rspec-node.github.net/) |
| 94 | + expect(stderr).to match(/INFO -- : Diffs computed for rspec-node.github.net/) |
| 95 | + expect(stderr).to match(/INFO -- : Note: you can use --display-detail-add/) |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + context 'writing output to JSON file' do |
| 100 | + before(:each) { @tempdir = Dir.mktmpdir } |
| 101 | + after(:each) { OctocatalogDiff::Spec.clean_up_tmpdir(@tempdir) } |
| 102 | + |
| 103 | + it 'should write JSON to an output file' do |
| 104 | + env = { 'OCTOCATALOG_DIFF_CONFIG_FILE' => OctocatalogDiff::Spec.fixture_path('cli-configs/valid.rb') } |
| 105 | + argv = [ |
| 106 | + '--to-catalog', OctocatalogDiff::Spec.fixture_path('catalogs/catalog-1.json'), |
| 107 | + '--from-catalog', OctocatalogDiff::Spec.fixture_path('catalogs/catalog-2.json'), |
| 108 | + '-o', File.join(@tempdir, 'output.json'), |
| 109 | + '--output-format', 'json', |
| 110 | + '-d' |
| 111 | + ] |
| 112 | + |
| 113 | + cmdline = [script, argv].flatten.map { |x| Shellwords.escape(x) }.join(' ') |
| 114 | + stdout, stderr, status = Open3.capture3(env, cmdline) |
| 115 | + |
| 116 | + expect(status.exitstatus).to eq(2), [stdout, stderr].join("\n") |
| 117 | + |
| 118 | + expect(stdout).to eq('') |
| 119 | + |
| 120 | + expect(stderr).not_to match(%r{Loading octocatalog-diff configuration from .+/cli-configs/valid.rb}) |
| 121 | + expect(stderr).not_to match(/DEBUG -- : :header => \(Symbol\) :default/) |
| 122 | + expect(stderr).not_to match(/DEBUG -- : Loaded 3 settings from/) |
| 123 | + expect(stderr).not_to match(/INFO -- : Exiting now because --config-test was specified/) |
| 124 | + expect(stderr).to match(/DEBUG -- : Initialized OctocatalogDiff::Catalog::JSON for from-catalog/) |
| 125 | + expect(stderr).to match(/Exiting hashdiff_initial; changes: 6, nested changes: 9/) |
| 126 | + |
| 127 | + j = JSON.parse(File.read(File.join(@tempdir, 'output.json'))) |
| 128 | + expect(j).to be_a_kind_of(Hash) |
| 129 | + answer = { |
| 130 | + 'diff_type' => '!', |
| 131 | + 'type' => 'Package', |
| 132 | + 'title' => 'rubygems1.8', |
| 133 | + 'structure' => ['parameters', 'old-parameter'], |
| 134 | + 'old_value' => nil, |
| 135 | + 'new_value' => 'old value', |
| 136 | + 'old_file' => '/environments/production/modules/ruby/manifests/system.pp', |
| 137 | + 'old_line' => 27, |
| 138 | + 'new_file' => '/environments/production/modules/ruby/manifests/system.pp', |
| 139 | + 'new_line' => 27, |
| 140 | + 'old_location' => { |
| 141 | + 'file' => '/environments/production/modules/ruby/manifests/system.pp', |
| 142 | + 'line' => 27 |
| 143 | + }, |
| 144 | + 'new_location' => { |
| 145 | + 'file' => '/environments/production/modules/ruby/manifests/system.pp', |
| 146 | + 'line' => 27 |
| 147 | + } |
| 148 | + } |
| 149 | + expect(j['diff']).to include(answer) |
| 150 | + expect(j['header']).to eq('diff origin/master/my.rspec.node current/my.rspec.node') |
| 151 | + end |
62 | 152 | end
|
63 | 153 |
|
64 |
| - it 'should print settings details and then invoke the main CLI' do |
65 |
| - text, status = Open3.capture2e(env, "#{script} #{normal_settings}") |
66 |
| - expect(status.exitstatus).to eq(2), text |
67 |
| - expect(text).to match(%r{Loading octocatalog-diff configuration from .+/fixtures/cli-configs/valid.rb}) |
68 |
| - expect(text).to match(%r{Loaded 3 settings from .+/fixtures/cli-configs/valid.rb}) |
69 |
| - expect(text).to match(/Initialized OctocatalogDiff::Catalog::Computed for to-catalog/) |
70 |
| - expect(text).to match(/Entering catdiff; catalog sizes: 2, 21/) |
71 |
| - expect(text).to match(/Entering filter_diffs_for_absent_files with 14 diffs/) |
72 |
| - expect(text).to match(/Added resources: 14/) |
73 |
| - expect(text).to match(/\+ Ssh_authorized_key\[root@6def27049c06f48eea8b8f37329f40799d07dc84\]/) |
| 154 | + context 'with no changes' do |
| 155 | + it 'should display output and exit 0' do |
| 156 | + env = { 'OCTOCATALOG_DIFF_CONFIG_FILE' => OctocatalogDiff::Spec.fixture_path('cli-configs/valid.rb') } |
| 157 | + argv = [ |
| 158 | + '--bootstrapped-to-dir', OctocatalogDiff::Spec.fixture_path('repos/tiny-repo'), |
| 159 | + '--from-catalog', OctocatalogDiff::Spec.fixture_path('catalogs/tiny-catalog.json'), |
| 160 | + '--puppet-binary', OctocatalogDiff::Spec::PUPPET_BINARY, |
| 161 | + '--fact-file', OctocatalogDiff::Spec.fixture_path('facts/facts.json'), |
| 162 | + '-n', 'rspec-node.github.net', |
| 163 | + '--no-color', |
| 164 | + '--no-hiera-config' |
| 165 | + ] |
| 166 | + |
| 167 | + cmdline = [script, argv].flatten.map { |x| Shellwords.escape(x) }.join(' ') |
| 168 | + stdout, stderr, status = Open3.capture3(env, cmdline) |
| 169 | + |
| 170 | + expect(status.exitstatus).to eq(0), [stdout, stderr].join("\n") |
| 171 | + |
| 172 | + expect(stdout).to eq('') |
| 173 | + |
| 174 | + expect(stderr).not_to match(%r{Loading octocatalog-diff configuration from .+/cli-configs/valid.rb}) |
| 175 | + expect(stderr).not_to match(/DEBUG -- : :header => \(Symbol\) :default/) |
| 176 | + expect(stderr).not_to match(/DEBUG -- : Loaded 3 settings from/) |
| 177 | + expect(stderr).not_to match(/INFO -- : Exiting now because --config-test was specified/) |
| 178 | + expect(stderr).to match(/INFO -- : Catalogs compiled for rspec-node.github.net/) |
| 179 | + expect(stderr).to match(/INFO -- : Diffs computed for rspec-node.github.net/) |
| 180 | + expect(stderr).to match(/INFO -- : No differences/) |
| 181 | + end |
| 182 | + end |
| 183 | + |
| 184 | + context 'when encountering an error in catalog compilation' do |
| 185 | + it 'should display error and exit 1' do |
| 186 | + env = { 'OCTOCATALOG_DIFF_CONFIG_FILE' => OctocatalogDiff::Spec.fixture_path('cli-configs/valid.rb') } |
| 187 | + argv = [ |
| 188 | + '--bootstrapped-to-dir', OctocatalogDiff::Spec.fixture_path('repos/failing-catalog'), |
| 189 | + '--from-catalog', OctocatalogDiff::Spec.fixture_path('catalogs/tiny-catalog.json'), |
| 190 | + '--puppet-binary', OctocatalogDiff::Spec::PUPPET_BINARY, |
| 191 | + '--fact-file', OctocatalogDiff::Spec.fixture_path('facts/facts.json'), |
| 192 | + '-n', 'rspec-node.github.net', |
| 193 | + '--no-color', |
| 194 | + '--no-hiera-config' |
| 195 | + ] |
| 196 | + |
| 197 | + cmdline = [script, argv].flatten.map { |x| Shellwords.escape(x) }.join(' ') |
| 198 | + stdout, stderr, status = Open3.capture3(env, cmdline) |
| 199 | + |
| 200 | + expect(status.exitstatus).to eq(1), [stdout, stderr].join("\n") |
| 201 | + |
| 202 | + expect(stdout).to eq('') |
| 203 | + |
| 204 | + expect(stderr).not_to match(%r{Loading octocatalog-diff configuration from .+/cli-configs/valid.rb}) |
| 205 | + expect(stderr).not_to match(/DEBUG -- : :header => \(Symbol\) :default/) |
| 206 | + expect(stderr).not_to match(/DEBUG -- : Loaded 3 settings from/) |
| 207 | + expect(stderr).not_to match(/INFO -- : Exiting now because --config-test was specified/) |
| 208 | + expect(stderr).to match(/WARN -- : Failed build_catalog for ./) |
| 209 | + expect(stderr).to match(/OctocatalogDiff::Errors::CatalogError/) |
| 210 | + expect(stderr).to match(/Could not find class (::)?this::module::does::not::exist/) |
| 211 | + end |
| 212 | + end |
| 213 | + |
| 214 | + context 'when encountering an error in usage' do |
| 215 | + it 'should display error and exit 1' do |
| 216 | + env = { 'OCTOCATALOG_DIFF_CONFIG_FILE' => OctocatalogDiff::Spec.fixture_path('cli-configs/valid.rb') } |
| 217 | + argv = [ |
| 218 | + '--bootstrapped-to-dir', OctocatalogDiff::Spec.fixture_path('repos/failing-catalog'), |
| 219 | + '--from-catalog', OctocatalogDiff::Spec.fixture_path('catalogs/tiny-catalog.json'), |
| 220 | + '--no-color', |
| 221 | + '--no-hiera-config' |
| 222 | + ] |
| 223 | + |
| 224 | + cmdline = [script, argv].flatten.map { |x| Shellwords.escape(x) }.join(' ') |
| 225 | + stdout, stderr, status = Open3.capture3(env, cmdline) |
| 226 | + |
| 227 | + expect(status.exitstatus).to eq(1), [stdout, stderr].join("\n") |
| 228 | + |
| 229 | + expect(stdout).to eq('') |
| 230 | + |
| 231 | + expect(stderr).not_to match(%r{Loading octocatalog-diff configuration from .+/cli-configs/valid.rb}) |
| 232 | + expect(stderr).not_to match(/DEBUG -- : :header => \(Symbol\) :default/) |
| 233 | + expect(stderr).not_to match(/DEBUG -- : Loaded 3 settings from/) |
| 234 | + expect(stderr).not_to match(/INFO -- : Exiting now because --config-test was specified/) |
| 235 | + expect(stderr).to match(/Catalog for 'to' \(.\) failed to compile with/) |
| 236 | + expect(stderr).to match(/ArgumentError: Unable to compute facts for node./) |
| 237 | + end |
74 | 238 | end
|
75 | 239 | end
|
76 | 240 | end
|
0 commit comments