spec/chef/knife/server_backup_spec.rb in knife-server-1.1.0 vs spec/chef/knife/server_backup_spec.rb in knife-server-1.2.0

- old
+ new

@@ -1,5 +1,6 @@ +# -*- encoding: utf-8 -*- # # Author:: Fletcher Nichol (<fnichol@nichol.ca>) # Copyright:: Copyright (c) 2012 Fletcher Nichol # License:: Apache License, Version 2.0 # @@ -14,25 +15,25 @@ # 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 'chef/knife/server_backup' -require 'fakefs/spec_helpers' -require 'timecop' +require "chef/knife/server_backup" +require "fakefs/spec_helpers" +require "timecop" describe Chef::Knife::ServerBackup do include FakeFS::SpecHelpers before do Chef::Log.logger = Logger.new(StringIO.new) @knife = Chef::Knife::ServerBackup.new @stdout = StringIO.new - @knife.ui.stub!(:stdout).and_return(@stdout) - @knife.ui.stub(:msg) + allow(@knife.ui).to receive(:stdout).and_return(@stdout) + allow(@knife.ui).to receive(:msg) @stderr = StringIO.new - @knife.ui.stub!(:stderr).and_return(@stderr) + allow(@knife.ui).to receive(:stderr).and_return(@stderr) @knife.config[:backup_dir] = "/baks" Chef::Config[:chef_server_url] = "https://chef.example.com:9876" end @@ -48,11 +49,11 @@ it "defaults the backup dir to <backup_dir>/<server_name>_<time>" do Timecop.freeze(Time.utc(2012, 1, 2, 3, 4, 5)) do @knife.config[:backup_dir] = nil Chef::Config[:file_backup_path] = "/da/bomb" - @knife.backup_dir.should eq( + expect(@knife.backup_dir).to eq( "/da/bomb/chef.example.com_20120102T030405-0000") end end end @@ -62,154 +63,167 @@ let(:env_list) { Hash["myenv" => "http://pancakes/envs/myenv"] } let(:data_bag_list) { Hash["mybag" => "http://pancakes/bags/mybag"] } let(:data_bag_item_list) { Hash["myitem" => "http://p/bags/mybag/myitem"] } before do - Chef::Node.stub(:list) { node_list } - Chef::Node.stub(:load).with("mynode") { stub_node("mynode") } - Chef::Role.stub(:list) { role_list } - Chef::Role.stub(:load).with("myrole") { stub_role("myrole") } - Chef::Environment.stub(:list) { env_list } - Chef::Environment.stub(:load).with("myenv") { stub_env("myenv") } - Chef::DataBag.stub(:list) { data_bag_list } - Chef::DataBag.stub(:load).with("mybag") { data_bag_item_list } - Chef::DataBagItem.stub(:load). - with("mybag", "myitem") { stub_bag_item("mybag", "myitem")} + allow(Chef::Node).to receive(:list) { node_list } + allow(Chef::Node).to receive(:load).with("mynode") { stub_node("mynode") } + allow(Chef::Role).to receive(:list) { role_list } + allow(Chef::Role).to receive(:load).with("myrole") { stub_role("myrole") } + allow(Chef::Environment).to receive(:list) { env_list } + allow(Chef::Environment).to receive(:load). + with("myenv") { stub_env("myenv") } + allow(Chef::DataBag).to receive(:list) { data_bag_list } + allow(Chef::DataBag).to receive(:load). + with("mybag") { data_bag_item_list } + allow(Chef::DataBagItem).to receive(:load). + with("mybag", "myitem") { stub_bag_item("mybag", "myitem") } end it "exits if component type is invalid" do - @knife.name_args = %w{nodes toasterovens} + @knife.name_args = %w[nodes toasterovens] - lambda { @knife.run }.should raise_error SystemExit + expect { @knife.run }.to raise_error SystemExit end context "for nodes" do - before { @knife.name_args = %w{nodes} } + before { @knife.name_args = %w[nodes] } it "creates the backup nodes dir" do @knife.run - File.directory?(["/baks", "nodes"].join("/")).should be_true + expect(File.directory?(["/baks", "nodes"].join("/"))).to be_truthy end it "sends a message to the ui" do - @knife.ui.should_receive(:msg).with(/mynode/) + expect(@knife.ui).to receive(:msg).with(/mynode/) @knife.run end it "writes out each node to a json file" do @knife.run json_str = File.open("/baks/nodes/mynode.json", "rb") { |f| f.read } json = JSON.parse(json_str, :create_additions => false) - json["name"].should eq("mynode") + expect(json["name"]).to eq("mynode") end end context "for roles" do - before { @knife.name_args = %w{roles} } + before { @knife.name_args = %w[roles] } it "creates the backup roles dir" do @knife.run + dir = File.join("/baks", "roles") - File.directory?(["/baks", "roles"].join("/")).should be_true + expect(File.directory?(dir)).to be_truthy end it "sends a message to the ui" do - @knife.ui.should_receive(:msg).with(/myrole/) + expect(@knife.ui).to receive(:msg).with(/myrole/) @knife.run end it "writes out each role to a json file" do @knife.run json_str = File.open("/baks/roles/myrole.json", "rb") { |f| f.read } json = JSON.parse(json_str, :create_additions => false) - json["name"].should eq("myrole") + expect(json["name"]).to eq("myrole") end end context "for environments" do - before { @knife.name_args = %w{environments} } + before { @knife.name_args = %w[environments] } it "creates the backup environments dir" do @knife.run + dir = File.join("/baks", "environments") - File.directory?(["/baks", "environments"].join("/")).should be_true + expect(File.directory?(dir)).to be_truthy end it "sends a message to the ui" do - @knife.ui.should_receive(:msg).with(/myenv/) + expect(@knife.ui).to receive(:msg).with(/myenv/) @knife.run end it "writes out each environment to a json file" do @knife.run - json_str = File.open("/baks/environments/myenv.json", "rb") { |f| f.read } + json_str = File.open("/baks/environments/myenv.json", "rb") do |f| + f.read + end json = JSON.parse(json_str, :create_additions => false) - json["name"].should eq("myenv") + expect(json["name"]).to eq("myenv") end it "skips the _default environment" do - Chef::Environment.stub(:list) { Hash["_default" => "http://url"] } - Chef::Environment.stub(:load).with("_default") { stub_env("_default") } + allow(Chef::Environment).to receive(:list) do + Hash["_default" => "http://url"] + end + allow(Chef::Environment).to receive(:load).with("_default") do + stub_env("_default") + end @knife.run - File.exists?("/baks/environments/_default.json").should_not be_true + expect(File.exist?("/baks/environments/_default.json")).to_not be_truthy end end context "for data_bags" do - before { @knife.name_args = %w{data_bags} } + before { @knife.name_args = %w[data_bags] } it "creates the backup data_bags dir" do @knife.run + dir = File.join("/baks", "data_bags") - File.directory?(["/baks", "data_bags"].join("/")).should be_true + expect(File.directory?(dir)).to be_truthy end it "sends messages to the ui" do - @knife.ui.should_receive(:msg).with(/myitem/) + expect(@knife.ui).to receive(:msg).with(/myitem/) @knife.run end it "writes out each data bag item to a json file" do @knife.run - json_str = File.open("/baks/data_bags/mybag/myitem.json", "rb") { |f| f.read } + json_str = File.open("/baks/data_bags/mybag/myitem.json", "rb") do |f| + f.read + end json = JSON.parse(json_str, :create_additions => false) - json["name"].should eq("data_bag_item_mybag_myitem") + expect(json["name"]).to eq("data_bag_item_mybag_myitem") end end context "for all" do it "writes a node file" do @knife.run - File.exists?("/baks/nodes/mynode.json").should be_true + expect(File.exist?("/baks/nodes/mynode.json")).to be_truthy end it "writes a role file" do @knife.run - File.exists?("/baks/roles/myrole.json").should be_true + expect(File.exist?("/baks/roles/myrole.json")).to be_truthy end it "writes an environment file" do @knife.run - File.exists?("/baks/environments/myenv.json").should be_true + expect(File.exist?("/baks/environments/myenv.json")).to be_truthy end it "writes a data bag item file" do @knife.run - File.exists?("/baks/data_bags/mybag/myitem.json").should be_true + expect(File.exist?("/baks/data_bags/mybag/myitem.json")).to be_truthy end end end private