From c89be5578a673ce523a0289b34c078b3011db1e7 Mon Sep 17 00:00:00 2001 From: aliasgar16 Date: Mon, 25 Jul 2016 17:01:25 +0530 Subject: [PATCH 1/2] Added validation when spot-wait-mode option is given by user on CLI and spot-price option is not given. --- lib/chef/knife/ec2_server_create.rb | 5 +++ spec/unit/ec2_server_create_spec.rb | 60 +++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/lib/chef/knife/ec2_server_create.rb b/lib/chef/knife/ec2_server_create.rb index 9bc8f7ea..73ccfe7a 100644 --- a/lib/chef/knife/ec2_server_create.rb +++ b/lib/chef/knife/ec2_server_create.rb @@ -902,6 +902,11 @@ def validate! exit 1 end + if locate_config_value(:spot_price).nil? && locate_config_value(:spot_wait_mode).downcase != 'prompt' + ui.error('spot-wait-mode option works only with the spot-price option.') + exit 1 + end + end def tags diff --git a/spec/unit/ec2_server_create_spec.rb b/spec/unit/ec2_server_create_spec.rb index 4c88de76..1f94a93a 100644 --- a/spec/unit/ec2_server_create_spec.rb +++ b/spec/unit/ec2_server_create_spec.rb @@ -173,6 +173,66 @@ expect(@new_ec2_server).to receive(:wait_for).and_return(true) @knife_ec2_create.run end + + context 'spot-wait-mode option' do + context 'when spot-price is not given' do + context 'spot-wait-mode option is not given' do + before do + @knife_ec2_create.config.delete(:spot_price) + end + + it 'does not raise error' do + expect(@knife_ec2_create.ui).to_not receive(:error).with( + 'spot-wait-mode option works only with the spot-price option.' + ) + expect { @knife_ec2_create.validate! }.to_not raise_error + end + end + + context 'spot-wait-mode option is given' do + before do + @knife_ec2_create.config.delete(:spot_price) + @knife_ec2_create.config[:spot_wait_mode] = 'wait' + end + + it 'raises error' do + expect(@knife_ec2_create.ui).to receive(:error).with( + 'spot-wait-mode option works only with the spot-price option.' + ) + expect { @knife_ec2_create.validate! }.to raise_error(SystemExit) + end + end + end + + context 'when spot-price is given' do + context 'spot-wait-mode option is not given' do + before do + @knife_ec2_create.config[:spot_price] = 0.001 + end + + it 'does not raise error' do + expect(@knife_ec2_create.ui).to_not receive(:error).with( + 'spot-wait-mode option works only with the spot-price option.' + ) + expect { @knife_ec2_create.validate! }.to_not raise_error + end + end + + context 'spot-wait-mode option is given' do + before do + @knife_ec2_create.config[:spot_price] = 0.001 + @knife_ec2_create.config[:spot_wait_mode] = 'exit' + end + + it 'does not raise error' do + expect(@knife_ec2_create.ui).to_not receive(:error).with( + 'spot-wait-mode option works only with the spot-price option.' + ) + expect { @knife_ec2_create.validate! }.to_not raise_error + end + end + end + end end describe "run" do From e538f1087b01cbdae1e180936ba447c712ee38e2 Mon Sep 17 00:00:00 2001 From: aliasgar16 Date: Tue, 26 Jul 2016 10:13:09 +0530 Subject: [PATCH 2/2] Modified validation message for spot-wait-mode option. --- lib/chef/knife/ec2_server_create.rb | 2 +- spec/unit/ec2_server_create_spec.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/chef/knife/ec2_server_create.rb b/lib/chef/knife/ec2_server_create.rb index 73ccfe7a..2e812934 100644 --- a/lib/chef/knife/ec2_server_create.rb +++ b/lib/chef/knife/ec2_server_create.rb @@ -903,7 +903,7 @@ def validate! end if locate_config_value(:spot_price).nil? && locate_config_value(:spot_wait_mode).downcase != 'prompt' - ui.error('spot-wait-mode option works only with the spot-price option.') + ui.error('spot-wait-mode option requires that a spot-price option is set.') exit 1 end diff --git a/spec/unit/ec2_server_create_spec.rb b/spec/unit/ec2_server_create_spec.rb index 1f94a93a..fc3679f1 100644 --- a/spec/unit/ec2_server_create_spec.rb +++ b/spec/unit/ec2_server_create_spec.rb @@ -183,7 +183,7 @@ it 'does not raise error' do expect(@knife_ec2_create.ui).to_not receive(:error).with( - 'spot-wait-mode option works only with the spot-price option.' + 'spot-wait-mode option requires that a spot-price option is set.' ) expect { @knife_ec2_create.validate! }.to_not raise_error end @@ -197,7 +197,7 @@ it 'raises error' do expect(@knife_ec2_create.ui).to receive(:error).with( - 'spot-wait-mode option works only with the spot-price option.' + 'spot-wait-mode option requires that a spot-price option is set.' ) expect { @knife_ec2_create.validate! }.to raise_error(SystemExit) end @@ -212,7 +212,7 @@ it 'does not raise error' do expect(@knife_ec2_create.ui).to_not receive(:error).with( - 'spot-wait-mode option works only with the spot-price option.' + 'spot-wait-mode option requires that a spot-price option is set.' ) expect { @knife_ec2_create.validate! }.to_not raise_error end @@ -226,7 +226,7 @@ it 'does not raise error' do expect(@knife_ec2_create.ui).to_not receive(:error).with( - 'spot-wait-mode option works only with the spot-price option.' + 'spot-wait-mode option requires that a spot-price option is set.' ) expect { @knife_ec2_create.validate! }.to_not raise_error end