Skip to content

Commit

Permalink
Refs #2709: Isolate/fix the no-fork fault
Browse files Browse the repository at this point in the history
* Share specific recipes code to application parent
* Update specs to passing, update specs to RSpec 3.
* Specs for set_specific_recipes, solo, client.
  • Loading branch information
AJ Christensen authored and btm committed Feb 10, 2015
1 parent 955bce9 commit 95be9a0
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 14 deletions.
30 changes: 19 additions & 11 deletions lib/chef/application.rb
Expand Up @@ -104,6 +104,12 @@ def load_config_file
Chef::Config.merge!(config)
end

def set_specific_recipes
Chef::Config[:specific_recipes] =
cli_arguments.map { |file| File.expand_path(file) } if
cli_arguments.respond_to?(:map)
end

# Initialize and configure the logger.
# === Loggers and Formatters
# In Chef 10.x and previous, the Logger was the primary/only way that Chef
Expand Down Expand Up @@ -200,16 +206,18 @@ def run_application

# Initializes Chef::Client instance and runs it
def run_chef_client(specific_recipes = [])
unless specific_recipes.respond_to?(:size)
raise ArgumentError, 'received non-Array like specific_recipes argument'
end

Chef::LocalMode.with_server_connectivity do
override_runlist = config[:override_runlist]
if specific_recipes.size > 0
override_runlist ||= []
end
override_runlist ||= [] if specific_recipes.size > 0
@chef_client = Chef::Client.new(
@chef_client_json,
:override_runlist => config[:override_runlist],
:specific_recipes => specific_recipes,
:runlist => config[:runlist]
override_runlist: override_runlist,
specific_recipes: specific_recipes,
runlist: config[:runlist]
)
@chef_client_json = nil

Expand Down Expand Up @@ -239,7 +247,7 @@ def run_with_graceful_exit_option
# Override the TERM signal.
trap('TERM') do
Chef::Log.debug("SIGTERM received during converge," +
" finishing converge to exit normally (send SIGINT to terminate immediately)")
" finishing converge to exit normally (send SIGINT to terminate immediately)")
end

@chef_client.run
Expand All @@ -253,7 +261,7 @@ def fork_chef_client
# TERM singal is received (exit gracefully)
trap('TERM') do
Chef::Log.debug("SIGTERM received during converge," +
" finishing converge to exit normally (send SIGINT to terminate immediately)")
" finishing converge to exit normally (send SIGINT to terminate immediately)")
end

client_solo = Chef::Config[:solo] ? "chef-solo" : "chef-client"
Expand Down Expand Up @@ -299,7 +307,7 @@ def apply_config(config_content, config_file_path)
def configure_http_proxy
if http_proxy = Chef::Config[:http_proxy]
http_proxy_string = configure_proxy("http", http_proxy,
Chef::Config[:http_proxy_user], Chef::Config[:http_proxy_pass])
Chef::Config[:http_proxy_user], Chef::Config[:http_proxy_pass])
env['http_proxy'] = http_proxy_string unless env['http_proxy']
env['HTTP_PROXY'] = http_proxy_string unless env['HTTP_PROXY']
end
Expand All @@ -309,7 +317,7 @@ def configure_http_proxy
def configure_https_proxy
if https_proxy = Chef::Config[:https_proxy]
https_proxy_string = configure_proxy("https", https_proxy,
Chef::Config[:https_proxy_user], Chef::Config[:https_proxy_pass])
Chef::Config[:https_proxy_user], Chef::Config[:https_proxy_pass])
env['https_proxy'] = https_proxy_string unless env['https_proxy']
env['HTTPS_PROXY'] = https_proxy_string unless env['HTTPS_PROXY']
end
Expand All @@ -319,7 +327,7 @@ def configure_https_proxy
def configure_ftp_proxy
if ftp_proxy = Chef::Config[:ftp_proxy]
ftp_proxy_string = configure_proxy("ftp", ftp_proxy,
Chef::Config[:ftp_proxy_user], Chef::Config[:ftp_proxy_pass])
Chef::Config[:ftp_proxy_user], Chef::Config[:ftp_proxy_pass])
env['ftp_proxy'] = ftp_proxy_string unless env['ftp_proxy']
env['FTP_PROXY'] = ftp_proxy_string unless env['FTP_PROXY']
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/application/client.rb
Expand Up @@ -264,7 +264,7 @@ def reconfigure

raise Chef::Exceptions::PIDFileLockfileMatch if Chef::Util::PathHelper.paths_eql? (Chef::Config[:pid_file] || '' ), (Chef::Config[:lockfile] || '')

Chef::Config[:specific_recipes] = cli_arguments.map { |file| File.expand_path(file) }
set_specific_recipes

Chef::Config[:chef_server_url] = config[:chef_server_url] if config.has_key? :chef_server_url

Expand Down
2 changes: 2 additions & 0 deletions lib/chef/application/solo.rb
Expand Up @@ -189,6 +189,8 @@ def initialize
def reconfigure
super

set_specific_recipes

Chef::Config[:solo] = true

if Chef::Config[:daemonize]
Expand Down
9 changes: 7 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -83,8 +83,13 @@ module Shell

OHAI_SYSTEM = Ohai::System.new
OHAI_SYSTEM.all_plugins("platform")
TEST_PLATFORM = OHAI_SYSTEM["platform"].dup.freeze
TEST_PLATFORM_VERSION = OHAI_SYSTEM["platform_version"].dup.freeze

TEST_PLATFORM =
(OHAI_SYSTEM['platform'] ||
'unknown_test_platform').dup.freeze
TEST_PLATFORM_VERSION =
(OHAI_SYSTEM['platform_version'] ||
'unknown_platform_version').dup.freeze

RSpec.configure do |config|
config.include(Matchers)
Expand Down
7 changes: 7 additions & 0 deletions spec/unit/application/client_spec.rb
Expand Up @@ -42,6 +42,13 @@
ARGV.replace(@original_argv)
end

describe 'parse cli_arguments' do
it 'should call set_specific_recipes' do
expect(app).to receive(:set_specific_recipes).and_return(true)
app.reconfigure
end
end

describe "when configured to not fork the client process" do
before do
Chef::Config[:client_fork] = false
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/application/solo_spec.rb
Expand Up @@ -34,6 +34,11 @@
end

describe "configuring the application" do
it 'should call set_specific_recipes' do
expect(app).to receive(:set_specific_recipes)
app.reconfigure
end

it "should set solo mode to true" do
app.reconfigure
expect(Chef::Config[:solo]).to be_truthy
Expand Down
35 changes: 35 additions & 0 deletions spec/unit/application_spec.rb
Expand Up @@ -56,6 +56,11 @@
expect(@app).to receive(:configure_proxy_environment_variables).and_return(true)
@app.reconfigure
end

it 'should not receive set_specific_recipes' do
expect(@app).to_not receive(:set_specific_recipes)
@app.reconfigure
end
end

describe Chef::Application do
Expand Down Expand Up @@ -473,6 +478,36 @@ def configure_proxy_environment_variables_stubs
end
end

describe 'run_chef_client' do
context 'with an application' do
let(:app) { Chef::Application.new }

context 'when called with an invalid argument' do
before do
allow(app).to receive(:fork_chef_client).and_return(true)
allow(app).to receive(:run_with_graceful_exit_option).and_return(true)
end

it 'should raise an argument error detailing the problem' do
specific_recipes_regexp = Regexp.new 'received non-Array like specific_recipes argument'
expect { app.run_chef_client(nil) }.to raise_error(ArgumentError, specific_recipes_regexp)
end
end

context 'when called with an Array-like argument (#size)' do
before do
allow(app).to receive(:fork_chef_client).and_return(true)
allow(app).to receive(:run_with_graceful_exit_option).and_return(true)
end

it 'should be cool' do
expect { app.run_chef_client([]) }.not_to raise_error
end
end
end

end

describe "configuration errors" do
before do
expect(Process).to receive(:exit)
Expand Down

0 comments on commit 95be9a0

Please sign in to comment.