Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #73 from deepak/cli_validation_for_cook_subcommand

add CLI validation for knife cook
  • Loading branch information...
commit 212af845c50c97c9d6b63f04a03316dbaa3eb396 2 parents 94e9991 + c2da04c
@matschaffer authored
View
10 lib/chef/knife/cook.rb
@@ -20,6 +20,8 @@ class Cook < Knife
include KnifeSolo::KitchenCommand
include KnifeSolo::Tools
+ class WrongCookError < KnifeSolo::KnifeSoloError; end
+
banner "knife cook [user@]hostname [json] (options)"
option :skip_chef_check,
@@ -43,6 +45,7 @@ class Cook < Knife
:description => "Only run syntax checks - do not run Chef"
def run
+ validate_params!
super
check_syntax unless config[:skip_syntax_check]
return if config[:syntax_check_only]
@@ -116,7 +119,7 @@ def check_chef_version
BASH
raise "Couldn't find Chef #{CHEF_VERSION_CONSTRAINT} on #{host}. Please run `#{$0} prepare #{ssh_args}` to ensure Chef is installed and up to date." unless result.success?
end
-
+
def cook
logging_arg = "-l debug" if config[:verbosity] > 0
@@ -126,6 +129,11 @@ def cook
#{logging_arg}
BASH
end
+
+ def validate_params!
+ validate_first_cli_arg_is_a_hostname!(WrongCookError)
+ end
+
end
end
end
View
5 lib/chef/knife/prepare.rb
@@ -46,10 +46,9 @@ def operating_system
end
def validate_params!
- unless @name_args.first =~ /\A.+\@.+\z/
- raise WrongPrepareError.new "need to pass a [user@]hostname as the first argument"
- end
+ validate_first_cli_arg_is_a_hostname!(WrongPrepareError)
end
+
end
end
end
View
18 lib/knife-solo/kitchen_command.rb
@@ -27,9 +27,25 @@ def run
def required_files_present?
KitchenCommand.all_requirements.inject(true) do |m, f|
check = File.exists?(f)
- Chef::Log.warn "#{f} is a required file/directory" unless check
+ warn_for_required_file(f) unless check
m && check
end
end
+
+ def warn_for_required_file(file)
+ Chef::Log.warn "#{file} is a required file/directory"
+ end
+
+ def first_cli_arg_is_a_hostname?
+ @name_args.first =~ /\A.+\@.+\z/
+ end
+
+ def validate_first_cli_arg_is_a_hostname!(error_class)
+ unless first_cli_arg_is_a_hostname?
+ puts opt_parser.help
+ raise error_class.new "need to pass atleast a [user@]hostname as the first argument"
+ end
+ end
+
end
end
View
18 test/cook_test.rb
@@ -75,10 +75,28 @@ def setup
f << "this is a blatant ruby syntax error."
end
end
+ @clean_kitchen = '/tmp/kitchen'
+ end
+
+ def test_barks_without_atleast_a_hostname
+ kitchen(@clean_kitchen).run
+
+ Dir.chdir(@clean_kitchen) do
+ run_command = command
+
+ assert_raises Chef::Knife::Cook::WrongCookError do
+ run_command.run
+ end
+
+ `knife cook`
+ exit_status = $?
+ assert exit_status != 0, "exit status #{exit_status} should be non-zero for failure"
+ end
end
def teardown
FileUtils.rm_r("/tmp/cook_kitchen_test")
+ FileUtils.rm_rf(@clean_kitchen)
end
def command(*args)
View
17 test/prepare_test.rb
@@ -69,6 +69,23 @@ def test_run_calls_bootstrap
end
end
+ def test_barks_without_atleast_a_hostname
+ @kitchen = '/tmp/nodes'
+ Chef::Knife::Kitchen.new([@kitchen]).run
+
+ Dir.chdir(@kitchen) do
+ run_command = command
+
+ assert_raises Chef::Knife::Prepare::WrongPrepareError do
+ run_command.run
+ end
+
+ `knife prepare`
+ exit_status = $?
+ assert exit_status != 0, "exit status #{exit_status} should be non-zero for failure"
+ end
+ end
+
def teardown
FileUtils.rm_r("/tmp/nodes")
end
Please sign in to comment.
Something went wrong with that request. Please try again.