summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2014-08-09 07:56:00 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2014-08-09 07:56:00 -0700
commitb62d7daf60afa96f62ec50c64290948c801253d8 (patch)
treefef584e7d41b03624e1e5234573c374530a7ea89 /spec
parente21909ae01533883e27c87b15783c3549fed0d9b (diff)
downloadchef-b62d7daf60afa96f62ec50c64290948c801253d8.tar.gz
add json pretty print tests to some knife show commands
Diffstat (limited to 'spec')
-rw-r--r--spec/unit/json_compat_spec.rb47
-rw-r--r--spec/unit/knife/client_show_spec.rb20
-rw-r--r--spec/unit/knife/data_bag_show_spec.rb37
-rw-r--r--spec/unit/knife/node_show_spec.rb47
-rw-r--r--spec/unit/knife/role_show_spec.rb59
5 files changed, 160 insertions, 50 deletions
diff --git a/spec/unit/json_compat_spec.rb b/spec/unit/json_compat_spec.rb
index cce31b0c7c..c0fb8be6ac 100644
--- a/spec/unit/json_compat_spec.rb
+++ b/spec/unit/json_compat_spec.rb
@@ -22,47 +22,62 @@ require 'chef/json_compat'
describe Chef::JSONCompat do
describe "with JSON containing an existing class" do
- let(:json){'{"json_class": "Chef::Role"}'}
+ let(:json) { '{"json_class": "Chef::Role"}' }
+
it "returns an instance of the class instead of a Hash" do
- Chef::JSONCompat.from_json(json).class.should eq Chef::Role
+ expect(Chef::JSONCompat.from_json(json).class).to eq Chef::Role
end
end
describe 'with JSON containing "Chef::Sandbox" as a json_class value' do
require 'chef/sandbox' # Only needed for this test
- let(:json){'{"json_class": "Chef::Sandbox", "arbitrary": "data"}'}
+
+ let(:json) { '{"json_class": "Chef::Sandbox", "arbitrary": "data"}' }
+
it "returns a Hash, because Chef::Sandbox is a dummy class" do
- Chef::JSONCompat.from_json(json).should eq({"json_class" => "Chef::Sandbox", "arbitrary" => "data"})
+ expect(Chef::JSONCompat.from_json(json)).to eq({"json_class" => "Chef::Sandbox", "arbitrary" => "data"})
end
end
- describe "with a file with 300 or less nested entries" do
- before(:all) do
- @json = IO.read(File.join(CHEF_SPEC_DATA, 'big_json.json'))
- @hash = Chef::JSONCompat.from_json(@json)
+ describe "when pretty printing an object that defines #to_json" do
+ class Foo
+ def to_json(*a)
+ {'foo' => 1234}.to_json(*a)
+ end
+ end
+
+ it "should work" do
+ f = Foo.new
+ expect(Chef::JSONCompat.to_json_pretty(f)).to eql("{\n \"foo\": 1234\n}")
end
+ end
+
+ describe "with a file with 300 or less nested entries" do
+ let(:json) { IO.read(File.join(CHEF_SPEC_DATA, 'big_json.json')) }
+ let(:hash) { Chef::JSONCompat.from_json(json) }
describe "when a big json file is loaded" do
it "should create a Hash from the file" do
- @hash.should be_kind_of(Hash)
+ expect(hash).to be_kind_of(Hash)
end
+
it "should has 'test' as a 300th nested value" do
- @hash['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key'].should == 'test'
+ expect(hash['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']).to eq('test')
end
end
end
+
describe "with a file with more than 300 nested entries" do
- before(:all) do
- @json = IO.read(File.join(CHEF_SPEC_DATA, 'big_json_plus_one.json'))
- @hash = Chef::JSONCompat.from_json(@json, {:max_nesting => 301})
- end
+ let(:json) { IO.read(File.join(CHEF_SPEC_DATA, 'big_json_plus_one.json')) }
+ let(:hash) { Chef::JSONCompat.from_json(json, {:max_nesting => 301}) }
describe "when a big json file is loaded" do
it "should create a Hash from the file" do
- @hash.should be_kind_of(Hash)
+ expect(hash).to be_kind_of(Hash)
end
+
it "should has 'test' as a 301st nested value" do
- @hash['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key'].should == 'test'
+ expect(hash['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']['key']).to eq('test')
end
end
end
diff --git a/spec/unit/knife/client_show_spec.rb b/spec/unit/knife/client_show_spec.rb
index b5c95db097..8404e8d019 100644
--- a/spec/unit/knife/client_show_spec.rb
+++ b/spec/unit/knife/client_show_spec.rb
@@ -27,16 +27,26 @@ describe Chef::Knife::ClientShow do
describe 'run' do
it 'should list the client' do
- Chef::ApiClient.should_receive(:load).with('adam').and_return(@client_mock)
- @knife.should_receive(:format_for_display).with(@client_mock)
+ expect(Chef::ApiClient).to receive(:load).with('adam').and_return(@client_mock)
+ expect(@knife).to receive(:format_for_display).with(@client_mock)
@knife.run
end
+ it 'should pretty print json' do
+ @knife.config[:format] = 'json'
+ @stdout = StringIO.new
+ allow(@knife.ui).to receive(:stdout).and_return(@stdout)
+ fake_client_contents = {"foo"=>"bar", "baz"=>"qux"}
+ expect(Chef::ApiClient).to receive(:load).with('adam').and_return(fake_client_contents)
+ @knife.run
+ expect(@stdout.string).to eql("{\n \"foo\": \"bar\",\n \"baz\": \"qux\"\n}\n")
+ 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)
+ expect(@knife).to receive(:show_usage)
+ expect(@knife.ui).to receive(:fatal)
+ expect { @knife.run }.to raise_error(SystemExit)
end
end
end
diff --git a/spec/unit/knife/data_bag_show_spec.rb b/spec/unit/knife/data_bag_show_spec.rb
index d8d1077d0d..4aa642fc4b 100644
--- a/spec/unit/knife/data_bag_show_spec.rb
+++ b/spec/unit/knife/data_bag_show_spec.rb
@@ -30,9 +30,9 @@ describe Chef::Knife::DataBagShow do
@knife = Chef::Knife::DataBagShow.new
@knife.config[:format] = 'json'
@rest = double("Chef::REST")
- @knife.stub(:rest).and_return(@rest)
+ allow(@knife).to receive(:rest).and_return(@rest)
@stdout = StringIO.new
- @knife.ui.stub(:stdout).and_return(@stdout)
+ allow(@knife.ui).to receive(:stdout).and_return(@stdout)
end
@@ -40,24 +40,33 @@ describe Chef::Knife::DataBagShow do
@knife.instance_variable_set(:@name_args, ['bag_o_data'])
data_bag_contents = { "baz"=>"http://localhost:4000/data/bag_o_data/baz",
"qux"=>"http://localhost:4000/data/bag_o_data/qux"}
- Chef::DataBag.should_receive(:load).and_return(data_bag_contents)
+ expect(Chef::DataBag).to receive(:load).and_return(data_bag_contents)
expected = %q|[
"baz",
"qux"
]|
@knife.run
- @stdout.string.strip.should == expected
+ expect(@stdout.string.strip).to eq(expected)
end
it "prints the contents of the data bag item when given a bag and item name" do
@knife.instance_variable_set(:@name_args, ['bag_o_data', 'an_item'])
data_item = Chef::DataBagItem.new.tap {|item| item.raw_data = {"id" => "an_item", "zsh" => "victory_through_tabbing"}}
- Chef::DataBagItem.should_receive(:load).with('bag_o_data', 'an_item').and_return(data_item)
+ expect(Chef::DataBagItem).to receive(:load).with('bag_o_data', 'an_item').and_return(data_item)
@knife.run
- Chef::JSONCompat.from_json(@stdout.string).should == data_item.raw_data
+ expect(Chef::JSONCompat.from_json(@stdout.string)).to eq(data_item.raw_data)
+ end
+
+ it "should pretty print the data bag contents" do
+ @knife.instance_variable_set(:@name_args, ['bag_o_data', 'an_item'])
+ data_item = Chef::DataBagItem.new.tap {|item| item.raw_data = {"id" => "an_item", "zsh" => "victory_through_tabbing"}}
+
+ expect(Chef::DataBagItem).to receive(:load).with('bag_o_data', 'an_item').and_return(data_item)
+ @knife.run
+ expect(@stdout.string).to eql("{\n \"id\": \"an_item\",\n \"zsh\": \"victory_through_tabbing\"\n}\n")
end
describe "encrypted data bag items" do
@@ -83,29 +92,29 @@ describe Chef::Knife::DataBagShow do
end
it "prints the decrypted contents of an item when given --secret" do
- @knife.stub(:config).and_return({:secret => @secret})
- Chef::EncryptedDataBagItem.should_receive(:load).
+ allow(@knife).to receive(:config).and_return({:secret => @secret})
+ expect(Chef::EncryptedDataBagItem).to receive(:load).
with('bag_name', 'item_name', @secret).
and_return(Chef::EncryptedDataBagItem.new(@enc_data, @secret))
@knife.run
- Chef::JSONCompat.from_json(@stdout.string).should == @plain_data
+ expect(Chef::JSONCompat.from_json(@stdout.string)).to eq(@plain_data)
end
it "prints the decrypted contents of an item when given --secret_file" do
- @knife.stub(:config).and_return({:secret_file => @secret_file.path})
- Chef::EncryptedDataBagItem.should_receive(:load).
+ allow(@knife).to receive(:config).and_return({:secret_file => @secret_file.path})
+ expect(Chef::EncryptedDataBagItem).to receive(:load).
with('bag_name', 'item_name', @secret).
and_return(Chef::EncryptedDataBagItem.new(@enc_data, @secret))
@knife.run
- Chef::JSONCompat.from_json(@stdout.string).should == @plain_data
+ expect(Chef::JSONCompat.from_json(@stdout.string)).to eq(@plain_data)
end
end
describe "command line parsing" do
it "prints help if given no arguments" do
@knife.instance_variable_set(:@name_args, [])
- lambda { @knife.run }.should raise_error(SystemExit)
- @stdout.string.should match(/^knife data bag show BAG \[ITEM\] \(options\)/)
+ expect { @knife.run }.to raise_error(SystemExit)
+ expect(@stdout.string).to match(/^knife data bag show BAG \[ITEM\] \(options\)/)
end
end
diff --git a/spec/unit/knife/node_show_spec.rb b/spec/unit/knife/node_show_spec.rb
index 72a269278e..4ecf2778e6 100644
--- a/spec/unit/knife/node_show_spec.rb
+++ b/spec/unit/knife/node_show_spec.rb
@@ -19,30 +19,47 @@
require 'spec_helper'
describe Chef::Knife::NodeShow do
+
+ let(:node) do
+ node = Chef::Node.new()
+ node.name("adam")
+ node.run_list = ['role[base]']
+ node
+ end
+
+ let(:knife) do
+ knife = Chef::Knife::NodeShow.new
+ knife.name_args = [ "adam" ]
+ knife
+ end
+
before(:each) do
Chef::Config[:node_name] = "webmonkey.example.com"
- @knife = Chef::Knife::NodeShow.new
- @knife.config = {
- :attribute => nil,
- :run_list => nil,
- :environment => nil
- }
- @knife.name_args = [ "adam" ]
- @knife.stub(:output).and_return(true)
- @node = Chef::Node.new()
- Chef::Node.stub(:load).and_return(@node)
end
describe "run" do
it "should load the node" do
- Chef::Node.should_receive(:load).with("adam").and_return(@node)
- @knife.run
+ expect(Chef::Node).to receive(:load).with("adam").and_return(node)
+ allow(knife).to receive(:output).and_return(true)
+ knife.run
end
it "should pretty print the node, formatted for display" do
- @knife.should_receive(:format_for_display).with(@node).and_return("poop")
- @knife.should_receive(:output).with("poop")
- @knife.run
+ knife.config[:format] = nil
+ stdout = StringIO.new
+ allow(knife.ui).to receive(:stdout).and_return(stdout)
+ allow(Chef::Node).to receive(:load).and_return(node)
+ knife.run
+ expect(stdout.string).to eql("Node Name: adam\nEnvironment: _default\nFQDN: \nIP: \nRun List: \nRoles: \nRecipes: \nPlatform: \nTags: \n")
+ end
+
+ it "should pretty print json" do
+ knife.config[:format] = 'json'
+ stdout = StringIO.new
+ allow(knife.ui).to receive(:stdout).and_return(stdout)
+ expect(Chef::Node).to receive(:load).with('adam').and_return(node)
+ knife.run
+ expect(stdout.string).to eql("{\n \"name\": \"adam\",\n \"chef_environment\": \"_default\",\n \"run_list\": [\n\n ],\n \"normal\": {\n }\n}\n")
end
end
end
diff --git a/spec/unit/knife/role_show_spec.rb b/spec/unit/knife/role_show_spec.rb
new file mode 100644
index 0000000000..b086136186
--- /dev/null
+++ b/spec/unit/knife/role_show_spec.rb
@@ -0,0 +1,59 @@
+#
+# Author:: Lamont Granquist (<lamont@getchef.com>)
+# Copyright:: Copyright (c) 2014 Lamont Granquist
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'spec_helper'
+
+describe Chef::Knife::RoleShow do
+ let(:role) { 'base' }
+
+ let(:knife) do
+ knife = Chef::Knife::RoleShow.new
+ knife.name_args = [ role ]
+ knife
+ end
+
+ let(:role_mock) { double('role_mock') }
+
+ describe 'run' do
+ it 'should list the role' do
+ expect(Chef::Role).to receive(:load).with('base').and_return(role_mock)
+ expect(knife).to receive(:format_for_display).with(role_mock)
+ knife.run
+ end
+
+ it 'should pretty print json' do
+ knife.config[:format] = 'json'
+ stdout = StringIO.new
+ allow(knife.ui).to receive(:stdout).and_return(stdout)
+ fake_role_contents = {"foo"=>"bar", "baz"=>"qux"}
+ expect(Chef::Role).to receive(:load).with('base').and_return(fake_role_contents)
+ knife.run
+ expect(stdout.string).to eql("{\n \"foo\": \"bar\",\n \"baz\": \"qux\"\n}\n")
+ end
+
+ context "without a role name" do
+ let(:role) { }
+
+ it 'should print usage and exit when a role name is not provided' do
+ expect(knife).to receive(:show_usage)
+ expect(knife.ui).to receive(:fatal)
+ expect { knife.run }.to raise_error(SystemExit)
+ end
+ end
+ end
+end