Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate the backend configuration #91

Merged
merged 1 commit into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/train.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def self.target_config(config = nil) # rubocop:disable Metrics/AbcSize

return conf if conf[:target].to_s.empty?

# split up the target's host/scheme configuration
uri = URI.parse(conf[:target].to_s)
unless uri.host.nil? and uri.scheme.nil?
conf[:backend] ||= uri.scheme
Expand All @@ -79,6 +80,24 @@ def self.target_config(config = nil) # rubocop:disable Metrics/AbcSize
conf
end

def self.validate_backend(conf, default = :local)
return default if conf.nil?
res = conf[:backend]
return res if !res.nil?

if !conf[:target].nil?
fail Train::UserError, 'Cannot determine backend from target '\
"configuration #{conf[:target].inspect}. Valid example: ssh://192.168.0.1."
end

if !conf[:host].nil?
fail Train::UserError, 'Host configured, but no backend was provided. Please '\
'specify how you want to connect. Valid example: ssh://192.168.0.1.'
end

conf[:backend] = default
end

def self.group_keys_and_keyfiles(conf)
# in case the user specified a key-file, register it that way
# we will clear the list of keys and put keys and key_files separately
Expand Down
24 changes: 24 additions & 0 deletions test/unit/train_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,28 @@
res.must_equal nu
end
end

describe '#validate_backend' do
it 'just returns the backend if it is provided' do
x = rand
Train.validate_backend({ backend: x }).must_equal x
end

it 'returns the local backend if nothing was provided' do
Train.validate_backend({}).must_equal :local
end

it 'returns the default backend if nothing was provided' do
x = rand
Train.validate_backend({}, x).must_equal x
end

it 'fails if no backend was given but a target is provided' do
proc { Train.validate_backend({ target: rand }) }.must_raise Train::UserError
end

it 'fails if no backend was given but a host is provided' do
proc { Train.validate_backend({ host: rand }) }.must_raise Train::UserError
end
end
end