summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsersut <serdar@opscode.com>2014-03-17 21:51:31 -0700
committersersut <serdar@opscode.com>2014-03-17 21:51:31 -0700
commit59a7c6f303918c2b986842e7549909fbf1803c8e (patch)
treeeffc0eb7331402e9902a0f9d904bcbd16e8f4cf7
parenta023f8d1de419ba9c756f666e349897c81cecc17 (diff)
downloadchef-59a7c6f303918c2b986842e7549909fbf1803c8e.tar.gz
Specs for --delete-validators options.
-rw-r--r--lib/chef/knife/client_bulk_delete.rb10
-rw-r--r--spec/unit/knife/client_bulk_delete_spec.rb159
2 files changed, 126 insertions, 43 deletions
diff --git a/lib/chef/knife/client_bulk_delete.rb b/lib/chef/knife/client_bulk_delete.rb
index ef3764882e..25bce4300b 100644
--- a/lib/chef/knife/client_bulk_delete.rb
+++ b/lib/chef/knife/client_bulk_delete.rb
@@ -27,9 +27,9 @@ class Chef
require 'chef/json_compat'
end
- option :force,
- :short => "-f",
- :long => "--force-validators",
+ option :delete_validators,
+ :short => "-D",
+ :long => "--delete-validators",
:description => "Force deletion of clients if they're validators"
banner "knife client bulk delete REGEX (options)"
@@ -59,10 +59,10 @@ class Chef
end
unless validators_to_delete.empty?
- unless config[:force]
+ unless config[:delete_validators]
ui.msg("Following clients are validators and will not be deleted.")
print_clients(validators_to_delete)
- ui.msg("You must specify --force-validators to delete the validator clients")
+ ui.msg("You must specify --delete-validators to delete the validator clients")
else
ui.msg("The following validators will be deleted:")
print_clients(validators_to_delete)
diff --git a/spec/unit/knife/client_bulk_delete_spec.rb b/spec/unit/knife/client_bulk_delete_spec.rb
index bedd4911c5..90b306290b 100644
--- a/spec/unit/knife/client_bulk_delete_spec.rb
+++ b/spec/unit/knife/client_bulk_delete_spec.rb
@@ -19,60 +19,143 @@
require 'spec_helper'
describe Chef::Knife::ClientBulkDelete do
- before(:each) do
- Chef::Log.logger = Logger.new(StringIO.new)
-
- Chef::Config[:node_name] = "webmonkey.example.com"
- @knife = Chef::Knife::ClientBulkDelete.new
- @knife.name_args = ["."]
- @stdout = StringIO.new
- @knife.ui.stub(:stdout).and_return(@stdout)
- @knife.ui.stub(:confirm).and_return(true)
- @clients = Hash.new
- %w{tim dan stephen}.each do |client_name|
+ let(:stdout_io) { StringIO.new }
+ let(:stdout) {stdout_io.string}
+
+ let(:knife) {
+ k = Chef::Knife::ClientBulkDelete.new
+ k.name_args = name_args
+ k.config = option_args
+ k.ui.stub(:stdout).and_return(stdout_io)
+ k.ui.stub(:confirm).and_return(knife_confirm)
+ k
+ }
+
+ let(:name_args) { [ "." ] }
+ let(:option_args) { {} }
+
+ let(:knife_confirm) { true }
+
+ let(:nonvalidator_client_names) { %w{tim dan stephen} }
+ let(:nonvalidator_clients) {
+ clients = Hash.new
+
+ nonvalidator_client_names.each do |client_name|
client = Chef::ApiClient.new()
client.name(client_name)
client.stub(:destroy).and_return(true)
- @clients[client_name] = client
+ clients[client_name] = client
+ end
+
+ clients
+ }
+
+ let(:validator_client_names) { %w{myorg-validator} }
+ let(:validator_clients) {
+ clients = Hash.new
+
+ validator_client_names.each do |validator_client_name|
+ validator_client = Chef::ApiClient.new()
+ validator_client.name(validator_client_name)
+ validator_client.stub(:validator).and_return(true)
+ validator_client.stub(:destroy).and_return(true)
+ clients[validator_client_name] = validator_client
end
- Chef::ApiClient.stub(:list).and_return(@clients)
+
+ clients
+ }
+
+ let(:client_names) { nonvalidator_client_names + validator_client_names}
+ let(:clients) {
+ nonvalidator_clients.merge(validator_clients)
+ }
+
+ before(:each) do
+ Chef::ApiClient.stub(:list).and_return(clients)
end
describe "run" do
+ describe "without a regex" do
+ let(:name_args) { [ ] }
- it "should get the list of the clients" do
- Chef::ApiClient.should_receive(:list).and_return(@clients)
- @knife.run
+ it "should exit if the regex is not provided" do
+ lambda { knife.run }.should raise_error(SystemExit)
+ end
end
- it "should print the clients you are about to delete" do
- @knife.run
- @stdout.string.should match(/#{@knife.ui.list(@clients.keys.sort, :columns_down)}/)
- end
+ describe "with any clients" do
+ it "should get the list of the clients" do
+ Chef::ApiClient.should_receive(:list)
+ knife.run
+ end
- it "should confirm you really want to delete them" do
- @knife.ui.should_receive(:confirm)
- @knife.run
- end
+ it "should print the name of the clients" do
+ knife.run
+ client_names.each do |client_name|
+ stdout.should include(client_name)
+ end
+ end
- it "should delete each client" do
- @clients.each_value do |c|
- c.should_receive(:destroy)
+ it "should confirm you really want to delete them" do
+ knife.ui.should_receive(:confirm)
+ knife.run
end
- @knife.run
- end
- it "should only delete clients that match the regex" do
- @knife.name_args = ["tim"]
- @clients["tim"].should_receive(:destroy)
- @clients["stephen"].should_not_receive(:destroy)
- @clients["dan"].should_not_receive(:destroy)
- @knife.run
+ describe "without --delete-validators" do
+ it "should mention that validator clients wont be deleted" do
+ knife.run
+ stdout.should include("Following clients are validators and will not be deleted.")
+ info = stdout.lines.index "Following clients are validators and will not be deleted.\n"
+ val = stdout.lines.index "myorg-validator\n"
+ (val > info).should be_true
+ end
+
+ it "should only delete nonvalidator clients" do
+ nonvalidator_clients.each_value do |c|
+ c.should_receive(:destroy)
+ end
+
+ validator_clients.each_value do |c|
+ c.should_not_receive(:destroy)
+ end
+
+ knife.run
+ end
+ end
+
+ describe "with --delete-validators" do
+ let(:option_args) { {:delete_validators => true} }
+
+ it "should mention that validator clients will be deleted" do
+ knife.run
+ stdout.should include("The following validators will be deleted")
+ end
+
+ it "should confirm twice" do
+ knife.ui.should_receive(:confirm).twice
+ knife.run
+ end
+
+ it "should delete all clients" do
+ clients.each_value do |c|
+ c.should_receive(:destroy)
+ end
+
+ knife.run
+ end
+ end
end
- it "should exit if the regex is not provided" do
- @knife.name_args = []
- lambda { @knife.run }.should raise_error(SystemExit)
+ describe "with some clients" do
+ let(:name_args) { [ "^ti" ] }
+
+ it "should only delete clients that match the regex" do
+ clients["tim"].should_receive(:destroy)
+ clients["stephen"].should_not_receive(:destroy)
+ clients["dan"].should_not_receive(:destroy)
+ clients["myorg-validator"].should_not_receive(:destroy)
+ knife.run
+ end
end
end
end