Skip to content

Commit be5e316

Browse files
authored
Merge pull request #60 from github/kpaulisse-script-integration
Integration tests for scripts
2 parents c329a87 + 4be8c67 commit be5e316

File tree

4 files changed

+261
-36
lines changed

4 files changed

+261
-36
lines changed

bin/octocatalog-diff

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ end
2121
config_test = ARGV.include?('--config-test')
2222

2323
logger = Logger.new(STDERR)
24+
logger.level = Logger::INFO
2425
logger.level = Logger::DEBUG if config_test
2526

2627
options = OctocatalogDiff::API::V1.config(logger: logger, test: config_test)

lib/octocatalog-diff/catalog/computed.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def cleanup_checkout_dir(checkout_dir, logger)
9393
# a parallel environment. Trap and ignore the errors here if we don't care about them.
9494
begin
9595
FileUtils.remove_entry_secure checkout_dir
96-
rescue Errno::ENOTEMPTY, Errno::ENOENT => exc
9796
# :nocov:
97+
rescue Errno::ENOTEMPTY, Errno::ENOENT => exc
9898
logger.debug "cleanup_checkout_dir(#{checkout_dir}) logged #{exc.class} - this can be ignored"
9999
# :nocov:
100100
end

spec/octocatalog-diff/integration/cli_spec.rb

Lines changed: 199 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_relative '../tests/spec_helper'
44

5+
require 'json'
56
require 'open3'
67
require 'shellwords'
78

@@ -28,49 +29,212 @@
2829
expect(File.executable?(script)).to eq(true), ls_l
2930
end
3031

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']
3437

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
4048
end
4149

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
4763
end
4864
end
4965

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
62152
end
63153

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
74238
end
75239
end
76240
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../tests/spec_helper'
4+
5+
require 'open3'
6+
require 'stringio'
7+
8+
describe 'examples/octocatalog-diff.cfg.rb' do
9+
let(:script) { File.expand_path('../../../examples/octocatalog-diff.cfg.rb', File.dirname(__FILE__)) }
10+
let(:ls_l) { Open3.capture2e("ls -l '#{script}'").first }
11+
12+
it 'should exist' do
13+
expect(File.file?(script)).to eq(true), ls_l
14+
end
15+
16+
it 'should not raise errors when loaded' do
17+
load script
18+
end
19+
20+
it 'should create OctocatalogDiff::Config namespace and .config method' do
21+
k = Kernel.const_get('OctocatalogDiff::Config')
22+
expect(k.to_s).to eq('OctocatalogDiff::Config')
23+
end
24+
25+
it 'should return a hash from the .config method' do
26+
result = OctocatalogDiff::Config.config
27+
expect(result).to be_a_kind_of(Hash)
28+
end
29+
end
30+
31+
describe 'examples/api/v1/catalog-builder-local-files.rb' do
32+
let(:script) { File.expand_path('../../../examples/api/v1/catalog-builder-local-files.rb', File.dirname(__FILE__)) }
33+
let(:ls_l) { Open3.capture2e("ls -l '#{script}'").first }
34+
35+
it 'should exist' do
36+
expect(File.file?(script)).to eq(true), ls_l
37+
end
38+
39+
context 'executing' do
40+
before(:each) do
41+
@stdout_obj = StringIO.new
42+
@old_stdout = $stdout
43+
$stdout = @stdout_obj
44+
end
45+
46+
after(:each) do
47+
$stdout = @old_stdout
48+
end
49+
50+
it 'should compile and run' do
51+
load script
52+
output = @stdout_obj.string.split("\n")
53+
expect(output).to include('Object returned from OctocatalogDiff::API::V1.catalog is: OctocatalogDiff::API::V1::Catalog')
54+
expect(output).to include('The catalog is valid.')
55+
expect(output).to include('The catalog was built using OctocatalogDiff::Catalog::Computed')
56+
expect(output).to include('- System::User - bob')
57+
expect(output).to include('The resources are equal!')
58+
end
59+
end
60+
end

0 commit comments

Comments
 (0)