Skip to content

Commit e3eb6a7

Browse files
author
Kevin Paulisse
committed
Merge branch 'kpaulisse-puppet-version-check-environment' into kpaulisse-octocatalog-diff-0-5-4
2 parents b6488b5 + e2909b2 commit e3eb6a7

4 files changed

Lines changed: 82 additions & 81 deletions

File tree

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.3
1+
0.5.4

doc/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
| Version | Date | Description / Changes |
44
| ------- | ---- | ----------- |
5+
| 0.5.4 | 2016-11-03 | https://github.com/github/octocatalog-diff/pull/16: environment running `puppet --version` |
56
| 0.5.3 | 2016-10-31 | https://github.com/github/octocatalog-diff/pull/10: facts terminus optimization |
67
| 0.5.2 | - | Unreleased internal version |
78
| 0.5.1 | 2016-10-20 | Initial release |

lib/octocatalog-diff/util/puppetversion.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ def self.puppet_version(puppet)
1515
raise ArgumentError, 'Puppet binary was not supplied' if puppet.nil?
1616
raise Errno::ENOENT, "Puppet binary #{puppet} doesn't exist" unless File.file?(puppet)
1717
cmdline = [Shellwords.escape(puppet), '--version'].join(' ')
18-
output, _code = Open3.capture2e(cmdline)
19-
return Regexp.last_match(1) if output =~ /^([\d\.]+)\s*$/
20-
raise "Unable to determine Puppet version: #{output}"
18+
19+
# This is the environment provided to the puppet command.
20+
env = {
21+
'HOME' => ENV['HOME'],
22+
'PATH' => ENV['PATH'],
23+
'PWD' => File.dirname(puppet)
24+
}
25+
out, err, _status = Open3.capture3(env, cmdline, unsetenv_others: true, chdir: env['PWD'])
26+
return Regexp.last_match(1) if out =~ /^([\d\.]+)\s*$/
27+
raise "Unable to determine Puppet version: #{out} #{err}"
2128
end
2229
end
2330
end

spec/octocatalog-diff/tests/catalog/computed_spec.rb

Lines changed: 70 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -239,92 +239,85 @@
239239
end
240240

241241
describe '#puppet_version' do
242-
before(:all) do
243-
@temp_puppet = Tempfile.new('puppet')
244-
@temp_puppet.write "#!/bin/bash\n"
245-
@temp_puppet.write "if [ \"$FAKE_PUPPET_VERSION\" == \"oops\" ]; then echo \"oops\" >&2; exit 1; fi\n"
246-
@temp_puppet.write "echo $FAKE_PUPPET_VERSION\n"
247-
@temp_puppet.close
248-
FileUtils.chmod 0o755, @temp_puppet.path
249-
end
242+
context 'with working Puppet version' do
243+
before(:all) do
244+
@temp_puppet = Tempfile.new('puppet')
245+
@temp_puppet.write "#!/bin/bash\n"
246+
@temp_puppet.write "echo '3.8.7'\n"
247+
@temp_puppet.close
248+
FileUtils.chmod 0o755, @temp_puppet.path
249+
end
250250

251-
after(:all) do
252-
@temp_puppet.unlink
253-
end
251+
after(:all) do
252+
@temp_puppet.unlink
253+
end
254254

255-
after(:each) do
256-
ENV.delete('FAKE_PUPPET_VERSION')
255+
it 'should return a puppet version upon success' do
256+
opts = {
257+
basedir: '/',
258+
node: 'foonode',
259+
puppet_binary: @temp_puppet.path,
260+
puppet_command: @temp_puppet.path,
261+
fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
262+
branch: '.'
263+
}
264+
catalog = OctocatalogDiff::Catalog::Computed.new(opts)
265+
catalog.build
266+
expect(catalog.puppet_version).to eq('3.8.7')
267+
end
257268
end
258269

259-
it 'should return a puppet version upon success' do
260-
ENV['FAKE_PUPPET_VERSION'] = '3.8.7'
261-
opts = {
262-
basedir: '/',
263-
node: 'foonode',
264-
pass_env_vars: ['FAKE_PUPPET_VERSION'],
265-
puppet_binary: @temp_puppet.path,
266-
puppet_command: @temp_puppet.path,
267-
fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
268-
branch: '.'
269-
}
270-
catalog = OctocatalogDiff::Catalog::Computed.new(opts)
271-
catalog.build
272-
expect(catalog.puppet_version).to eq('3.8.7')
273-
end
270+
context 'with failing Puppet version' do
271+
before(:all) do
272+
@temp_puppet = Tempfile.new('puppet')
273+
@temp_puppet.write "#!/bin/bash\n"
274+
@temp_puppet.write "echo 1>&2 'something failed horribly'\n"
275+
@temp_puppet.write "exit 1\n"
276+
@temp_puppet.close
277+
FileUtils.chmod 0o755, @temp_puppet.path
278+
end
274279

275-
it 'should raise an error if the version number is empty' do
276-
opts = {
277-
basedir: '/',
278-
node: 'foonode',
279-
puppet_binary: @temp_puppet.path,
280-
puppet_command: @temp_puppet.path,
281-
fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
282-
branch: '.'
283-
}
284-
expect { OctocatalogDiff::Catalog::Computed.new(opts).build }
285-
.to raise_error(RuntimeError, /Unable to determine Puppet version/)
286-
end
280+
after(:all) do
281+
@temp_puppet.unlink
282+
end
287283

288-
it 'should raise an error if the puppet binary is nil' do
289-
opts = {
290-
basedir: '/',
291-
node: 'foonode',
292-
puppet_command: @temp_puppet.path,
293-
fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
294-
branch: '.'
295-
}
296-
expect { OctocatalogDiff::Catalog::Computed.new(opts).build }.to raise_error(ArgumentError)
284+
it 'should raise an error if the version number is empty' do
285+
opts = {
286+
basedir: '/',
287+
node: 'foonode',
288+
puppet_binary: @temp_puppet.path,
289+
puppet_command: @temp_puppet.path,
290+
fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
291+
branch: '.'
292+
}
293+
expect { OctocatalogDiff::Catalog::Computed.new(opts).build }
294+
.to raise_error(RuntimeError, /Unable to determine Puppet version/)
295+
end
297296
end
297+
end
298298

299-
it 'should raise an error if the puppet binary does not exist' do
300-
ENV['FAKE_PUPPET_VERSION'] = 'chicken'
301-
opts = {
302-
basedir: '/',
303-
node: 'foonode',
304-
pass_env_vars: ['FAKE_PUPPET_VERSION'],
305-
puppet_binary: '/alsdfklafjasfkljafjafkjsdflaksfjasdfjadsfjadsf',
306-
puppet_command: @temp_puppet.path,
307-
fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
308-
branch: '.'
309-
}
310-
expect { OctocatalogDiff::Catalog::Computed.new(opts).build }
311-
.to raise_error(Errno::ENOENT)
312-
end
299+
it 'should raise an error if the puppet binary is nil' do
300+
opts = {
301+
basedir: '/',
302+
node: 'foonode',
303+
puppet_command: '/alsdfklafjasfkljafjafkjsdflaksfjasdfjadsfjadsf',
304+
fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
305+
branch: '.'
306+
}
307+
expect { OctocatalogDiff::Catalog::Computed.new(opts).build }.to raise_error(ArgumentError)
308+
end
313309

314-
it 'should raise an error if the puppet binary returns a nonzero exit code' do
315-
ENV['FAKE_PUPPET_VERSION'] = 'oops'
316-
opts = {
317-
basedir: '/',
318-
node: 'foonode',
319-
pass_env_vars: ['FAKE_PUPPET_VERSION'],
320-
puppet_binary: @temp_puppet.path,
321-
puppet_command: @temp_puppet.path,
322-
fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
323-
branch: '.'
324-
}
325-
expect { OctocatalogDiff::Catalog::Computed.new(opts).build }
326-
.to raise_error(RuntimeError, /Unable to determine Puppet version/)
327-
end
310+
it 'should raise an error if the puppet binary does not exist' do
311+
opts = {
312+
basedir: '/',
313+
node: 'foonode',
314+
puppet_binary: '/alsdfklafjasfkljafjafkjsdflaksfjasdfjadsfjadsf',
315+
puppet_command: '/alsdfklafjasfkljafjafkjsdflaksfjasdfjadsfjadsf',
316+
fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
317+
branch: '.'
318+
}
319+
expect { OctocatalogDiff::Catalog::Computed.new(opts).build }
320+
.to raise_error(Errno::ENOENT)
328321
end
329322
end
330323

0 commit comments

Comments
 (0)