From 85a47df5479a00e91ccb7aab89e238f802119baa Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Sat, 31 Aug 2013 07:09:26 +0000 Subject: [PATCH] Use SSH user name configured for a given target: When a user is configured for the targeted host name, use it for SSH authentification instead of the name of current user logged in. * Modify Remote#user_name to use configured user name. --- features/ssh/config.feature | 22 ++++++++++++++++++++++ features/steps/etc_steps.rb | 5 +++++ features/steps/ssh_steps.rb | 6 ++++++ lib/producer/core/remote.rb | 2 +- spec/producer/core/remote_spec.rb | 18 ++++++++++++++++-- 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 features/ssh/config.feature create mode 100644 features/steps/etc_steps.rb create mode 100644 features/steps/ssh_steps.rb diff --git a/features/ssh/config.feature b/features/ssh/config.feature new file mode 100644 index 0000000..2d0d5d7 --- /dev/null +++ b/features/ssh/config.feature @@ -0,0 +1,22 @@ +Feature: SSH settings + + Background: + Given a recipe with: + """ + target 'some_host.example' + + puts env.remote.user_name + """ + + Scenario: uses current user login name as SSH user name by default + When I successfully execute the recipe + Then the output must contain my current login name + + Scenario: uses configured SSH user name for a given host + Given an SSH config with: + """ + Host some_host.example + User some_user + """ + When I successfully execute the recipe + Then the output must contain "some_user" diff --git a/features/steps/etc_steps.rb b/features/steps/etc_steps.rb new file mode 100644 index 0000000..79e7d07 --- /dev/null +++ b/features/steps/etc_steps.rb @@ -0,0 +1,5 @@ +# FIXME: our monkey patch currently prevent us from using `must' in step +# definitions. +Then(/^the output should contain my current login name$/) do + assert_partial_output(Etc.getlogin, all_output) +end diff --git a/features/steps/ssh_steps.rb b/features/steps/ssh_steps.rb new file mode 100644 index 0000000..564b457 --- /dev/null +++ b/features/steps/ssh_steps.rb @@ -0,0 +1,6 @@ +# FIXME: current home directory shouldn't be changed here, maybe we should use +# a tag for features needing a fake home directory. +Given(/^an SSH config with:$/) do |config| + ENV['HOME'] = File.expand_path current_dir + write_file '.ssh/config', config +end diff --git a/lib/producer/core/remote.rb b/lib/producer/core/remote.rb index f7073b9..425f25d 100644 --- a/lib/producer/core/remote.rb +++ b/lib/producer/core/remote.rb @@ -19,7 +19,7 @@ module Producer end def user_name - Etc.getlogin + config[:user] || Etc.getlogin end def fs diff --git a/spec/producer/core/remote_spec.rb b/spec/producer/core/remote_spec.rb index b41bf25..5dd8f95 100644 --- a/spec/producer/core/remote_spec.rb +++ b/spec/producer/core/remote_spec.rb @@ -48,8 +48,22 @@ module Producer::Core end describe '#user_name' do - it 'returns the name of the user currently logged in' do - expect(remote.user_name).to eq Etc.getlogin + context 'ssh config has an entry for user' do + let(:config_user_name) { 'my_user_name' } + + before do + allow(Net::SSH::Config).to receive(:for) { { user: config_user_name } } + end + + it 'returns the configured value' do + expect(remote.user_name).to eq config_user_name + end + end + + context 'ssh config has no entry for user' do + it 'returns the name of the user currently logged in' do + expect(remote.user_name).to eq Etc.getlogin + end end end