Skip to content

Commit

Permalink
Honor proxy env vars.
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire McQuin committed Aug 10, 2015
1 parent e95bc2a commit 46965e4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/kitchen/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,22 @@ def validate_config!
# @param code [String] the shell code to be wrapped
# @return [String] wrapped shell code
# @api private
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def wrap_shell_code(code)
env = []
if config[:http_proxy]
env << shell_env_var("http_proxy", config[:http_proxy])
env << shell_env_var("HTTP_PROXY", config[:http_proxy])
else
env << shell_env_var("http_proxy", ENV["http_proxy"]) if ENV["http_proxy"]
env << shell_env_var("HTTP_PROXY", ENV["HTTP_PROXY"]) if ENV["HTTP_PROXY"]
end
if config[:https_proxy]
env << shell_env_var("https_proxy", config[:https_proxy])
env << shell_env_var("HTTPS_PROXY", config[:https_proxy])
else
env << shell_env_var("https_proxy", ENV["https_proxy"]) if ENV["https_proxy"]
env << shell_env_var("HTTPS_PROXY", ENV["HTTPS_PROXY"]) if ENV["HTTPS_PROXY"]
end
if powershell_shell?
env.join("\n").concat("\n").concat(code)
Expand Down
11 changes: 8 additions & 3 deletions lib/kitchen/driver/ssh_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,21 @@ def build_ssh_args(state)
end

# Adds http and https proxy environment variables to a command, if set
# in configuration data.
# in configuration data or on local workstation.
#
# @param cmd [String] command string
# @return [String] command string
# @api private
# rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
def env_cmd(cmd)
return if cmd.nil?
env = "env"
env << " http_proxy=#{config[:http_proxy]}" if config[:http_proxy]
env << " https_proxy=#{config[:https_proxy]}" if config[:https_proxy]
http_proxy = config[:http_proxy] || ENV["http_proxy"] ||
ENV["HTTP_PROXY"]
https_proxy = config[:https_proxy] || ENV["https_proxy"] ||
ENV["HTTPS_PROXY"]
env << " http_proxy=#{http_proxy}" if http_proxy
env << " https_proxy=#{https_proxy}" if https_proxy

env == "env" ? cmd : "#{env} #{cmd}"
end
Expand Down
59 changes: 59 additions & 0 deletions spec/kitchen/configurable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,17 @@ class SubclassDefaults < StaticDefaults

let(:cmd) { subject.send(:wrap_shell_code, "mkdir foo") }

before do
@original_env = ENV.to_hash
ENV.replace("http_proxy" => nil, "HTTP_PROXY" => nil,
"https_proxy" => nil, "HTTPS_PROXY" => nil)
end

after do
ENV.clear
ENV.replace(@original_env)
end

describe "for bourne shells" do

before { platform.stubs(:shell_type).returns("bourne") }
Expand Down Expand Up @@ -778,6 +789,32 @@ class SubclassDefaults < StaticDefaults
'
CODE
end

it "exports http_proxy & HTTP_PROXY from workstation when :http_proxy isn't set" do
ENV["http_proxy"] = "http://proxy"
ENV["HTTP_PROXY"] = "http://proxy"

cmd.must_equal(outdent!(<<-CODE.chomp))
sh -c '
http_proxy="http://proxy"; export http_proxy
HTTP_PROXY="http://proxy"; export HTTP_PROXY
mkdir foo
'
CODE
end

it "exports https_proxy & HTTPS_PROXY from workstation when :https_proxy isn't set" do
ENV["https_proxy"] = "https://proxy"
ENV["HTTPS_PROXY"] = "https://proxy"

cmd.must_equal(outdent!(<<-CODE.chomp))
sh -c '
https_proxy="https://proxy"; export https_proxy
HTTPS_PROXY="https://proxy"; export HTTPS_PROXY
mkdir foo
'
CODE
end
end

describe "for powershell shells" do
Expand Down Expand Up @@ -820,6 +857,28 @@ class SubclassDefaults < StaticDefaults
mkdir foo
CODE
end

it "exports http_proxy & HTTP_PROXY from workstation when :http_proxy isn't set" do
ENV["http_proxy"] = "http://proxy"
ENV["HTTP_PROXY"] = "http://proxy"

cmd.must_equal(outdent!(<<-CODE.chomp))
$env:http_proxy = "http://proxy"
$env:HTTP_PROXY = "http://proxy"
mkdir foo
CODE
end

it "exports https_proxy & HTTPS_PROXY from workstation when :https_proxy isn't set" do
ENV["https_proxy"] = "https://proxy"
ENV["HTTPS_PROXY"] = "https://proxy"

cmd.must_equal(outdent!(<<-CODE.chomp))
$env:https_proxy = "https://proxy"
$env:HTTPS_PROXY = "https://proxy"
mkdir foo
CODE
end
end
end

Expand Down
29 changes: 29 additions & 0 deletions spec/kitchen/driver/ssh_base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,16 @@ def self.constructs_an_ssh_connection
provisioner.stubs(:[]).with(:root_path).returns("/rooty")
FakeFS.activate!
FileUtils.mkdir_p("/tmp")
@original_env = ENV.to_hash
ENV.replace("http_proxy" => nil, "HTTP_PROXY" => nil,
"https_proxy" => nil, "HTTPS_PROXY" => nil)
end

after do
FakeFS.deactivate!
FakeFS::FileSystem.clear
ENV.clear
ENV.replace(@original_env)
end

constructs_an_ssh_connection
Expand Down Expand Up @@ -333,6 +338,18 @@ def self.constructs_an_ssh_connection
cmd
end

it "invokes the #install_command with ENV[\"http_proxy\"] set" do
ENV["http_proxy"] = "http://proxy"
transport.stubs(:connection).yields(connection)
if running_tests_on_windows?
connection.expects(:execute).
with("env http_proxy=http://proxy HTTP_PROXY=http://proxy install")
else
connection.expects(:execute).with("env http_proxy=http://proxy install")
end
cmd
end

it "invokes the #install_command with :https_proxy set in config" do
config[:https_proxy] = "https://proxy"
transport.stubs(:connection).yields(connection)
Expand All @@ -341,6 +358,18 @@ def self.constructs_an_ssh_connection
cmd
end

it "invokes the #install_command with ENV[\"https_proxy\"] set" do
ENV["https_proxy"] = "https://proxy"
transport.stubs(:connection).yields(connection)
if running_tests_on_windows?
connection.expects(:execute).
with("env https_proxy=https://proxy HTTPS_PROXY=https://proxy install")
else
connection.expects(:execute).with("env https_proxy=https://proxy install")
end
cmd
end

it "invokes the #install_command with :http_proxy & :https_proxy set" do
config[:http_proxy] = "http://proxy"
config[:https_proxy] = "https://proxy"
Expand Down

0 comments on commit 46965e4

Please sign in to comment.