summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/api_client.rb5
-rw-r--r--lib/chef/knife/client_reregister.rb9
-rw-r--r--spec/unit/knife/client_reregister_spec.rb45
3 files changed, 33 insertions, 26 deletions
diff --git a/lib/chef/api_client.rb b/lib/chef/api_client.rb
index 6c962ec945..efb855b27a 100644
--- a/lib/chef/api_client.rb
+++ b/lib/chef/api_client.rb
@@ -193,6 +193,11 @@ class Chef
"client[#{@name}]"
end
+ def inspect
+ "Chef::ApiClient name:'#{name}' admin:'#{admin.inspect}'" +
+ "public_key:'#{public_key}' private_key:#{private_key}"
+ end
+
end
end
diff --git a/lib/chef/knife/client_reregister.rb b/lib/chef/knife/client_reregister.rb
index 73a93ec31d..666fd09fd2 100644
--- a/lib/chef/knife/client_reregister.rb
+++ b/lib/chef/knife/client_reregister.rb
@@ -43,14 +43,15 @@ class Chef
exit 1
end
- client = Chef::ApiClient.load(@client_name)
- key = client.save(new_key=true)
+ client = Chef::ApiClient.reregister(@client_name)
+ Chef::Log.debug("Updated client data: #{client.inspect}")
+ key = client.private_key
if config[:file]
File.open(config[:file], "w") do |f|
- f.print(key['private_key'])
+ f.print(key)
end
else
- ui.msg key['private_key']
+ ui.msg key
end
end
end
diff --git a/spec/unit/knife/client_reregister_spec.rb b/spec/unit/knife/client_reregister_spec.rb
index 0d284c0f58..d9fc8ec180 100644
--- a/spec/unit/knife/client_reregister_spec.rb
+++ b/spec/unit/knife/client_reregister_spec.rb
@@ -22,40 +22,41 @@ describe Chef::Knife::ClientReregister do
before(:each) do
@knife = Chef::Knife::ClientReregister.new
@knife.name_args = [ 'adam' ]
- @client_mock = mock('client_mock')
- @client_mock.stub!(:save).and_return({ 'private_key' => 'foo_key' })
- Chef::ApiClient.stub!(:load).and_return(@client_mock)
+ @client_mock = mock('client_mock', :private_key => "foo_key")
@stdout = StringIO.new
@knife.ui.stub!(:stdout).and_return(@stdout)
end
- describe 'run' do
- it 'should load and save the client' do
- Chef::ApiClient.should_receive(:load).with('adam').and_return(@client_mock)
- @client_mock.should_receive(:save).with(true).and_return({'private_key' => 'foo_key'})
- @knife.run
- end
-
- it 'should output the private key' do
- @knife.run
- @stdout.string.should match /foo_key/
+ context "when no client name is given on the command line" do
+ before do
+ @knife.name_args = []
end
it 'should print usage and exit when a client name is not provided' do
- @knife.name_args = []
@knife.should_receive(:show_usage)
@knife.ui.should_receive(:fatal)
lambda { @knife.run }.should raise_error(SystemExit)
end
+ end
+
+ context 'when not configured for file output' do
+ it 'reregisters the client and prints the key' do
+ Chef::ApiClient.should_receive(:reregister).with('adam').and_return(@client_mock)
+ @knife.run
+ @stdout.string.should match( /foo_key/ )
+ end
+ end
- describe 'with -f or --file' do
- it 'should write the private key to a file' do
- @knife.config[:file] = '/tmp/monkeypants'
- filehandle = mock('Filehandle')
- filehandle.should_receive(:print).with('foo_key')
- File.should_receive(:open).with('/tmp/monkeypants', 'w').and_yield(filehandle)
- @knife.run
- end
+ context 'when configured for file output' do
+ it 'should write the private key to a file' do
+ Chef::ApiClient.should_receive(:reregister).with('adam').and_return(@client_mock)
+
+ @knife.config[:file] = '/tmp/monkeypants'
+ filehandle = StringIO.new
+ File.should_receive(:open).with('/tmp/monkeypants', 'w').and_yield(filehandle)
+ @knife.run
+ filehandle.string.should == "foo_key"
end
end
+
end