Skip to content

Commit

Permalink
Merge pull request #1342 from h0tw1r3/docker-package-testing
Browse files Browse the repository at this point in the history
Support package testing with Docker
  • Loading branch information
david22swan authored Apr 23, 2024
2 parents 9ad6e78 + 770abf8 commit 55fad57
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 64 deletions.
3 changes: 2 additions & 1 deletion package-testing/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ source ENV.fetch('GEM_SOURCE', nil) || 'https://rubygems.org'

gem 'beaker', '~> 4.39'
gem 'beaker-abs', '~> 0.11.0'
gem 'beaker-hostgenerator', '~> 2.2.0'
gem 'beaker-docker', '~> 2'
gem 'beaker-hostgenerator', '~> 2.11.0'
gem 'beaker-puppet', '= 1.29.0'
gem 'beaker-rspec', '= 7.1.0'
gem 'beaker-vmpooler', '= 1.4.0'
Expand Down
2 changes: 1 addition & 1 deletion package-testing/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace :acceptance do

puts "Generating beaker hosts using TEST_TARGET value #{test_target}"

cli = BeakerHostGenerator::CLI.new(["#{test_target}{type=foss}", '--disable-default-role', '--hypervisor', 'abs'])
cli = BeakerHostGenerator::CLI.new(["#{test_target}{type=foss}", '--disable-default-role', '--hypervisor', ENV.fetch('BEAKER_HYPERVISOR', 'abs')])

File.open('acceptance_hosts.yml', 'w') do |hosts_file|
hosts_file.print(cli.execute)
Expand Down
42 changes: 19 additions & 23 deletions package-testing/spec/package/support/serverspec_monkeypatch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class BeakerExec
old_build_command = instance_method(:build_command)

define_method(:build_command) do |cmd|
if get_config(:cwd)
cmd = cmd.shelljoin if cmd.is_a? Array
cmd = "cd #{get_config(:cwd)} && #{cmd}"
end

prepend_env(old_build_command.bind_call(self, cmd))
end

Expand All @@ -57,36 +62,27 @@ def unescape(string)
end

def prepend_env(cmd)
_, env, shell, command = cmd.match(/\Aenv (.+?)? (\S+) -c (.+)\Z/).to_a

output = if get_config(:run_as)
["sudo -u #{get_config(:run_as)}"]
else
['env']
end
_, orig_env, orig_cmd = cmd.match(/\A(?:env (.+) )?(\S+(?: -i)?(?: -l)? -c .+)\Z/).to_a

env = [orig_env].compact
(get_config(:env) || {}).each do |k, v|
output << %(#{k}="#{v}")
env << %(#{k}="#{v}")
end
output << env
new_cmd = if get_config(:cwd)
"'cd #{get_config(:cwd).shellescape} && #{unescape(command)}'"
else
"'#{unescape(command)}'"
end
output << '-i --' if get_config(:run_as)

# sudo on osx 10.11 behaves strangely when processing arguments and does
# not preserve quoted arguments, so we have to quote twice for this corner
# case.
output << if Specinfra.get_working_node.platform.start_with?('osx-10.11-') && get_config(:run_as)
"#{shell} -c \"#{new_cmd}\""
command = if env.empty?
orig_cmd
else
"#{shell} -c #{new_cmd}"
"env #{env.join(' ')} #{orig_cmd}"
end

$stderr.puts(output.join(' ')) if ENV.key?('BEAKER_debug')
output.join(' ')
output = if get_config(:run_as)
"su -l #{get_config(:run_as)} -c #{command.shellescape}"
else
command
end

$stderr.puts(output) if ENV.key?('BEAKER_debug')
output
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions package-testing/spec/package/unprivileged_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
on(host, "mkdir #{homedir} && chown #{uid}:#{gid} #{homedir}") unless directory_exists_on(host, homedir)
end
end

on(host, 'sed -i -e \'s/\srequiretty/!requiretty/g\' /etc/sudoers') if file_exists_on(host, '/etc/sudoers')
end
end

Expand Down
18 changes: 0 additions & 18 deletions spec/acceptance/remove_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,6 @@
end
end

RSpec.shared_context 'with a fake answer file' do |initial_content = nil|
before(:all) do
fake_answer_file = Tempfile.new('mock_answers.json')
unless initial_content.nil?
require 'json'
fake_answer_file.binmode
fake_answer_file.write(JSON.pretty_generate(initial_content))
end
fake_answer_file.close
ENV['PDK_ANSWER_FILE'] = fake_answer_file.path
end

after(:all) do
FileUtils.rm_f(ENV.fetch('PDK_ANSWER_FILE', nil)) # Need actual file calls here
ENV.delete('PDK_ANSWER_FILE')
end
end

context 'when run outside of a module' do
describe command('pdk remove config') do
its(:exit_status) { is_expected.not_to eq 0 }
Expand Down
18 changes: 0 additions & 18 deletions spec/acceptance/set_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,6 @@
end
end

RSpec.shared_context 'with a fake answer file' do |initial_content = nil|
before(:all) do
fake_answer_file = Tempfile.new('mock_answers.json')
unless initial_content.nil?
require 'json'
fake_answer_file.binmode
fake_answer_file.write(JSON.pretty_generate(initial_content))
end
fake_answer_file.close
ENV['PDK_ANSWER_FILE'] = fake_answer_file.path
end

after(:all) do
FileUtils.rm_f(ENV.fetch('PDK_ANSWER_FILE', nil)) # Need actual file calls here
ENV.delete('PDK_ANSWER_FILE')
end
end

context 'when run outside of a module' do
describe command('pdk set config') do
its(:exit_status) { is_expected.not_to eq 0 }
Expand Down
17 changes: 17 additions & 0 deletions spec/acceptance/support/with_a_fake_answer_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
RSpec.shared_context 'with a fake answer file' do |initial_content = nil|
before(:all) do
fake_answer_file = Tempfile.new('mock_answers.json')
unless initial_content.nil?
require 'json'
fake_answer_file.binmode
fake_answer_file.write(JSON.pretty_generate(initial_content))
end
fake_answer_file.close
ENV['PDK_ANSWER_FILE'] = fake_answer_file.path
end

after(:all) do
FileUtils.rm_f(ENV.fetch('PDK_ANSWER_FILE', nil)) # Need actual file calls here
ENV.delete('PDK_ANSWER_FILE')
end
end
2 changes: 1 addition & 1 deletion spec/acceptance/test_unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
include_context 'with a fake TTY'

shared_context 'with spec file' do |filename, content|
around(:all) do |example|
around do |example|
path = File.join('spec', 'unit', filename)
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') { |f| f.puts content }
Expand Down

0 comments on commit 55fad57

Please sign in to comment.