Skip to content

Commit 0cbf8d9

Browse files
authored
Merge pull request #75 from github/kpaulisse-integration-refactor
Integration refactors - exit codes, more information
2 parents f9b9d30 + b36bb52 commit 0cbf8d9

5 files changed

Lines changed: 48 additions & 45 deletions

File tree

lib/octocatalog-diff/cli.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ def self.cli(argv = ARGV, logger = Logger.new(STDERR), opts = {})
116116
printer_obj = OctocatalogDiff::Cli::Printer.new(options, logger)
117117
printer_obj.printer(diffs, catalog_diff.from.compilation_dir, catalog_diff.to.compilation_dir)
118118

119-
# Return the diff object if requested (generally for testing) or otherwise return exit code
120-
return diffs if opts[:RETURN_DIFFS]
119+
# Return the resulting diff object if requested (generally for testing) or otherwise return exit code
120+
return catalog_diff if opts[:INTEGRATION]
121121
diffs.any? ? EXITCODE_SUCCESS_WITH_DIFFS : EXITCODE_SUCCESS_NO_DIFFS
122122
end
123123

@@ -169,11 +169,8 @@ def self.catalog_only(logger, options)
169169
puts to_catalog.to_json
170170
end
171171

172-
return EXITCODE_SUCCESS_NO_DIFFS unless options[:RETURN_DIFFS] # For integration testing
173-
# :nocov:
174-
dummy_catalog = OctocatalogDiff::API::V1::Catalog.new(OctocatalogDiff::Catalog.new(backend: :noop))
175-
[dummy_catalog, to_catalog]
176-
# :nocov:
172+
return { exitcode: EXITCODE_SUCCESS_NO_DIFFS, to: to_catalog } if options[:INTEGRATION] # For integration testing
173+
EXITCODE_SUCCESS_NO_DIFFS
177174
end
178175

179176
# --bootstrap-then-exit command

spec/octocatalog-diff/integration/catalog_only_spec.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,36 @@
2626

2727
it 'should compile the catalog' do
2828
expect(@result[:exitcode]).not_to eq(-1), OctocatalogDiff::Integration.format_exception(@result)
29-
expect(@result[:exitcode]).to eq(2), "Runtime error: #{@result[:logs]}"
30-
end
31-
32-
it 'should set the from-catalog to a no-op catalog type' do
33-
pending 'catalog compilation failed' unless @result[:exitcode] == 2
34-
from_catalog = @result[:diffs][0]
35-
expect(from_catalog).to be_a_kind_of(OctocatalogDiff::API::V1::Catalog)
36-
expect(from_catalog.builder).to eq('OctocatalogDiff::Catalog::Noop')
29+
expect(@result[:exitcode]).to eq(0), "Runtime error: #{@result[:logs]}"
3730
end
3831

3932
it 'should set the to-catalog to a computed catalog type' do
40-
pending 'catalog compilation failed' unless @result[:exitcode] == 2
41-
to_catalog = @result[:diffs][1]
33+
pending 'catalog compilation failed' unless (@result[:exitcode]).zero?
34+
to_catalog = @result.to
4235
expect(to_catalog).to be_a_kind_of(OctocatalogDiff::API::V1::Catalog)
4336
expect(to_catalog.builder).to eq('OctocatalogDiff::Catalog::Computed')
4437
end
4538

4639
it 'should have log messages indicating catalog compilations' do
47-
pending 'catalog compilation failed' unless @result[:exitcode] == 2
40+
pending 'catalog compilation failed' unless (@result[:exitcode]).zero?
4841
logs = @result[:logs]
4942
expect(logs).to match(/Compiling catalog for rspec-node.github.net/)
5043
expect(logs).to match(/Initialized OctocatalogDiff::Catalog::Noop for from-catalog/)
5144
expect(logs).to match(/Initialized OctocatalogDiff::Catalog::Computed for to-catalog/)
5245
end
5346

5447
it 'should produce a valid catalog' do
55-
pending 'catalog compilation failed' unless @result[:exitcode] == 2
56-
to_catalog = @result[:diffs][1]
48+
pending 'catalog compilation failed' unless (@result[:exitcode]).zero?
49+
to_catalog = @result.to
5750
expect(to_catalog.valid?).to eq(true)
5851
expect(to_catalog.to_h).to be_a_kind_of(Hash)
5952
expect(to_catalog.to_json).to be_a_kind_of(String)
6053
expect(to_catalog.error_message).to be(nil)
6154
end
6255

6356
it 'should produce the expected catalog' do
64-
pending 'catalog compilation failed' unless @result[:exitcode] == 2
65-
to_catalog = @result[:diffs][1]
57+
pending 'catalog compilation failed' unless @result.exitcode.zero?
58+
to_catalog = @result.to
6659

6760
param1 = { 'owner' => 'root', 'group' => 'root', 'mode' => '0644', 'content' => 'Testy McTesterson' }
6861
expect(to_catalog.resource(type: 'File', title: '/tmp/foo')['parameters']).to eq(param1)

spec/octocatalog-diff/integration/integration_helper.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,39 +113,40 @@ def self.integration(options = {})
113113
options[:from_puppet_binary] ||= OctocatalogDiff::Spec::PUPPET_BINARY
114114
options[:to_puppet_binary] ||= OctocatalogDiff::Spec::PUPPET_BINARY
115115
options[:parallel] = false if ENV['COVERAGE']
116+
options[:INTEGRATION] = true
116117

117118
# Run octocatalog-diff CLI method. Capture stdout and stderr using 'strio'.
118-
# Set options[:RETURN_DIFFS] so that the .cli method returns the JSON array
119-
# of differences instead of the exit code.
120119
logger, logger_string = OctocatalogDiff::Spec.setup_logger
121120
begin
122121
# Capture stdout to a variable
123122
old_out = $stdout
124123
stdout_strio = StringIO.new
125124
$stdout = stdout_strio
126125

127-
# Tell OctocatalogDiff::Cli.cli to return the JSON differences and not a numeric exit code
128-
# for a full catalog-diff.
129-
options[:RETURN_DIFFS] = true
130-
131126
# Run the OctocatalogDiff::Cli.cli and validate output format.
132127
result = OctocatalogDiff::Cli.cli(argv, logger, options)
133128
if result.is_a?(Fixnum)
134-
return {
135-
logs: logger_string.string,
136-
output: stdout_strio.string,
137-
exitcode: result
138-
}
129+
result = OpenStruct.new(exitcode: result, exception: nil, diffs: [], to: nil, from: nil)
130+
elsif result.is_a?(Hash)
131+
result = OpenStruct.new({ exitcode: nil, exception: nil, diffs: [], to: nil, from: nil }.merge(result))
132+
elsif !result.is_a?(OpenStruct)
133+
raise "Expected OpenStruct, got #{result.inspect} from OctocatalogDiff::Cli.cli!"
139134
end
140135

141-
raise "OctocatalogDiff::Cli.cli should return array, got #{result.inspect}" unless result.is_a?(Array)
136+
exitcode = if result.exitcode.nil?
137+
result.diffs.any? ? 2 : 0
138+
else
139+
result.exitcode
140+
end
142141

143-
# Return hash
144142
OpenStruct.new(
145143
logs: logger_string.string,
144+
log_messages: logger_string.string.split(/\n/).map { |x| OctocatalogDiff::Spec.strip_log_message(x) },
146145
output: stdout_strio.string,
147-
diffs: result,
148-
exitcode: result.any? ? 2 : 0,
146+
diffs: result.diffs,
147+
to: result.to,
148+
from: result.from,
149+
exitcode: exitcode,
149150
options: options
150151
)
151152
rescue => exc # Yes, rescue *everything*

spec/octocatalog-diff/integration/reference_validation_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def self.catalog_contains_resource(result, type, title)
4545
end
4646

4747
it 'should return the valid catalog' do
48-
expect(@result.exitcode).to eq(2), OctocatalogDiff::Integration.format_exception(@result)
48+
expect(@result.exitcode).to eq(0), OctocatalogDiff::Integration.format_exception(@result)
4949
end
5050
end
5151

@@ -55,15 +55,15 @@ def self.catalog_contains_resource(result, type, title)
5555
end
5656

5757
it 'should return the valid catalog' do
58-
expect(@result.exitcode).to eq(2)
58+
expect(@result.exitcode).to eq(0)
5959
end
6060

6161
it 'should not raise any exceptions' do
6262
expect(@result.exception).to be_nil, OctocatalogDiff::Integration.format_exception(@result)
6363
end
6464

6565
it 'should contain representative resources' do
66-
pending 'Catalog failed' unless @result.exitcode == 2
66+
pending 'Catalog failed' unless @result.exitcode.zero?
6767
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'File', '/tmp/test-main')).to eq(true)
6868
end
6969
end
@@ -75,15 +75,15 @@ def self.catalog_contains_resource(result, type, title)
7575
end
7676

7777
it 'should succeed' do
78-
expect(@result.exitcode).to eq(2)
78+
expect(@result.exitcode).to eq(0)
7979
end
8080

8181
it 'should not raise any exceptions' do
8282
expect(@result.exception).to be_nil, OctocatalogDiff::Integration.format_exception(@result)
8383
end
8484

8585
it 'should contain representative resources' do
86-
pending 'Catalog failed' unless @result.exitcode == 2
86+
pending 'Catalog failed' unless @result.exitcode.zero?
8787
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'Exec', 'subscribe caller 1')).to eq(true)
8888
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'Exec', 'subscribe target')).to eq(true)
8989
end
@@ -179,7 +179,7 @@ def self.catalog_contains_resource(result, type, title)
179179
end
180180

181181
it 'should succeed' do
182-
expect(@result.exitcode).to eq(2), OctocatalogDiff::Integration.format_exception(@result)
182+
expect(@result.exitcode).to eq(0), OctocatalogDiff::Integration.format_exception(@result)
183183
end
184184

185185
it 'should not raise error' do
@@ -195,15 +195,15 @@ def self.catalog_contains_resource(result, type, title)
195195
end
196196

197197
it 'should succeed' do
198-
expect(@result.exitcode).to eq(2)
198+
expect(@result.exitcode).to eq(0)
199199
end
200200

201201
it 'should not raise any exceptions' do
202202
expect(@result.exception).to be_nil, OctocatalogDiff::Integration.format_exception(@result)
203203
end
204204

205205
it 'should contain representative resources' do
206-
pending 'Catalog failed' unless @result.exitcode == 2
206+
pending 'Catalog failed' unless @result.exitcode.zero?
207207
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'Exec', 'before alias caller')).to eq(true)
208208
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'Exec', 'before alias target')).to eq(true)
209209
expect(OctocatalogDiff::Spec.catalog_contains_resource(@result, 'Exec', 'the before alias target')).to eq(true)

spec/octocatalog-diff/tests/spec_helper.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ def self.setup_logger
217217
# remove the file and line locations. This takes the JSON result from catalog-diff and returns
218218
# a cleaned-up array of diffs.
219219
def self.remove_file_and_line(diff)
220+
if diff.is_a?(OctocatalogDiff::API::V1::Diff)
221+
result = diff.to_h.dup
222+
%w(new_location old_location new_line old_line new_file old_file).each { |x| result.delete(x.to_sym) }
223+
return result
224+
end
220225
if diff.is_a?(Hash)
221226
result = diff.dup
222227
%w(new_location old_location new_line old_line new_file old_file).each { |x| result.delete(x) }
@@ -228,6 +233,13 @@ def self.remove_file_and_line(diff)
228233
obj.map { |x| x[0] =~ /^[\-\+]$/ ? x[0..2] : x[0..3] }
229234
end
230235

236+
# Strip off timestamps and other extraneous content from log messages so that matching
237+
# of individual elements can be done via string and not regexp.
238+
def self.strip_log_message(message)
239+
return message unless message.strip =~ /\A\w,\s*\[[^\]]+\]\s+(\w+)\s*--\s*:(.+)/
240+
"#{Regexp.last_match(1)} - #{Regexp.last_match(2).strip}"
241+
end
242+
231243
# Get the Puppet version from the Puppet binary
232244
def self.puppet_version
233245
require require_path('util/puppetversion')

0 commit comments

Comments
 (0)