From 6634e9a3ed20d8ca097a497c9a72965974fc6857 Mon Sep 17 00:00:00 2001 From: ameyavarade Date: Wed, 11 Jun 2014 18:15:44 +0530 Subject: [PATCH 1/2] Fixed rspec deprecation warnings for unit tests --- spec/unit/openstack_flavor_list_spec.rb | 29 ++++-- spec/unit/openstack_group_list_spec.rb | 29 +++++- spec/unit/openstack_image_list_spec.rb | 27 +++++- spec/unit/openstack_network_list_spec.rb | 28 +++++- spec/unit/openstack_server_create_spec.rb | 112 ++++++++++++---------- spec/unit/openstack_server_delete_spec.rb | 28 +++++- spec/unit/openstack_server_list_spec.rb | 29 ++++-- spec/unit/openstack_server_show_spec.rb | 27 +++++- spec/unit/openstack_service_spec.rb | 36 +++---- 9 files changed, 245 insertions(+), 100 deletions(-) diff --git a/spec/unit/openstack_flavor_list_spec.rb b/spec/unit/openstack_flavor_list_spec.rb index 63862601..93152186 100644 --- a/spec/unit/openstack_flavor_list_spec.rb +++ b/spec/unit/openstack_flavor_list_spec.rb @@ -1,3 +1,22 @@ +# +# Author:: Mukta Aphale () +# Author:: Siddheshwar More () +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_flavor_list' require 'chef/knife/cloud/openstack_service' @@ -13,7 +32,7 @@ Chef::Config[:knife][:openstack_username] = "testuser" Chef::Config[:knife][:openstack_password] = "testpassword" Chef::Config[:knife][:openstack_auth_url] = "tsturl" - instance.stub(:exit) + allow(instance).to receive(:exit) end it "validate openstack mandatory options" do @@ -22,22 +41,20 @@ it "raise error on openstack_username missing" do Chef::Config[:knife].delete(:openstack_username) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Username' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_password missing" do Chef::Config[:knife].delete(:openstack_password) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Password' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_auth_url missing" do Chef::Config[:knife].delete(:openstack_auth_url) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end end - - end diff --git a/spec/unit/openstack_group_list_spec.rb b/spec/unit/openstack_group_list_spec.rb index c5292454..b70a3dec 100644 --- a/spec/unit/openstack_group_list_spec.rb +++ b/spec/unit/openstack_group_list_spec.rb @@ -1,3 +1,22 @@ +# +# Author:: Mukta Aphale () +# Author:: Siddheshwar More () +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_group_list' require 'chef/knife/cloud/openstack_service' @@ -13,7 +32,7 @@ Chef::Config[:knife][:openstack_username] = "testuser" Chef::Config[:knife][:openstack_password] = "testpassword" Chef::Config[:knife][:openstack_auth_url] = "tsturl" - instance.stub(:exit) + allow(instance).to receive(:exit) end it "validate openstack mandatory options" do @@ -22,19 +41,19 @@ it "raise error on openstack_username missing" do Chef::Config[:knife].delete(:openstack_username) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Username' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_password missing" do Chef::Config[:knife].delete(:openstack_password) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Password' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_auth_url missing" do Chef::Config[:knife].delete(:openstack_auth_url) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end end @@ -45,7 +64,7 @@ end it "returns group list" do - instance.ui.should_receive(:list).with(["Name", "Protocol", "From", "To", "CIDR", "Description", "Unrestricted", "tcp", "636", "636", "0.0.0.0/0", "testdescription"],:uneven_columns_across, 6) + expect(instance.ui).to receive(:list).with(["Name", "Protocol", "From", "To", "CIDR", "Description", "Unrestricted", "tcp", "636", "636", "0.0.0.0/0", "testdescription"],:uneven_columns_across, 6) instance.list(@security_groups) end end diff --git a/spec/unit/openstack_image_list_spec.rb b/spec/unit/openstack_image_list_spec.rb index a5bb9a0c..f12b95a2 100644 --- a/spec/unit/openstack_image_list_spec.rb +++ b/spec/unit/openstack_image_list_spec.rb @@ -1,3 +1,22 @@ +# +# Author:: Mukta Aphale () +# Author:: Siddheshwar More () +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_image_list' require 'chef/knife/cloud/openstack_service' @@ -13,7 +32,7 @@ Chef::Config[:knife][:openstack_username] = "testuser" Chef::Config[:knife][:openstack_password] = "testpassword" Chef::Config[:knife][:openstack_auth_url] = "tsturl" - instance.stub(:exit) + allow(instance).to receive(:exit) end it "validate openstack mandatory options" do @@ -22,19 +41,19 @@ it "raise error on openstack_username missing" do Chef::Config[:knife].delete(:openstack_username) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Username' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_password missing" do Chef::Config[:knife].delete(:openstack_password) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Password' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_auth_url missing" do Chef::Config[:knife].delete(:openstack_auth_url) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end end diff --git a/spec/unit/openstack_network_list_spec.rb b/spec/unit/openstack_network_list_spec.rb index d25b44b7..8c54b625 100644 --- a/spec/unit/openstack_network_list_spec.rb +++ b/spec/unit/openstack_network_list_spec.rb @@ -1,3 +1,21 @@ +# +# Author:: Prabhu Das () +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_network_list' require 'chef/knife/cloud/openstack_service' @@ -13,7 +31,7 @@ Chef::Config[:knife][:openstack_username] = "testuser" Chef::Config[:knife][:openstack_password] = "testpassword" Chef::Config[:knife][:openstack_auth_url] = "tsturl" - instance.stub(:exit) + allow(instance).to receive(:exit) end it "validate openstack mandatory options" do @@ -22,19 +40,19 @@ it "raise error on openstack_username missing" do Chef::Config[:knife].delete(:openstack_username) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Username' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_password missing" do Chef::Config[:knife].delete(:openstack_password) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Password' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_auth_url missing" do Chef::Config[:knife].delete(:openstack_auth_url) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end end @@ -42,7 +60,7 @@ context "query_resource" do it "returns the networks using the fog service." do instance.service = double - instance.service.should_receive(:list_networks) + expect(instance.service).to receive(:list_networks) instance.query_resource end end diff --git a/spec/unit/openstack_server_create_spec.rb b/spec/unit/openstack_server_create_spec.rb index e77c9275..534bf254 100644 --- a/spec/unit/openstack_server_create_spec.rb +++ b/spec/unit/openstack_server_create_spec.rb @@ -1,7 +1,23 @@ +# # Author:: Prabhu Das () # Author:: Mukta Aphale () # Author:: Siddheshwar More () +# Author:: Ameya Varade () # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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 File.expand_path('../../spec_helper', __FILE__) require 'chef/knife/openstack_server_create' @@ -29,7 +45,7 @@ describe "#validate_params!" do before(:each) do @instance = Chef::Knife::Cloud::OpenstackServerCreate.new - @instance.ui.stub(:error) + allow(@instance.ui).to receive(:error) Chef::Config[:knife][:bootstrap_protocol] = "ssh" Chef::Config[:knife][:identity_file] = "identity_file" Chef::Config[:knife][:image_os_type] = "linux" @@ -56,7 +72,7 @@ describe "#before_exec_command" do before(:each) do @instance = Chef::Knife::Cloud::OpenstackServerCreate.new - @instance.ui.stub(:error) + allow(@instance.ui).to receive(:error) @instance.config[:chef_node_name] = "chef_node_name" Chef::Config[:knife][:image] = "image" Chef::Config[:knife][:flavor] = "flavor" @@ -64,7 +80,7 @@ Chef::Config[:knife][:server_create_timeout] = "server_create_timeout" Chef::Config[:knife][:openstack_ssh_key_id] = "openstack_ssh_key" Chef::Config[:knife][:network_ids] = "test_network_id" - Chef::Config[:knife][:network_ids].stub(:map).and_return(Chef::Config[:knife][:network_ids]) + allow(Chef::Config[:knife][:network_ids]).to receive(:map).and_return(Chef::Config[:knife][:network_ids]) Chef::Config[:knife][:metadata] = "foo=bar" end @@ -81,15 +97,15 @@ @instance.service = double allow(@instance.service).to receive(:get_image).and_return(get_mock_resource('image')) allow(@instance.service).to receive(:get_flavor).and_return(get_mock_resource('flavor')) - @instance.service.should_receive(:create_server_dependencies) + expect(@instance.service).to receive(:create_server_dependencies) @instance.before_exec_command - @instance.create_options[:server_def][:name].should == @instance.config[:chef_node_name] - @instance.create_options[:server_def][:image_ref].should == Chef::Config[:knife][:image] - @instance.create_options[:server_def][:security_groups].should == Chef::Config[:knife][:openstack_security_groups] - @instance.create_options[:server_def][:flavor_ref].should == Chef::Config[:knife][:flavor] - @instance.create_options[:server_def][:nics].should == Chef::Config[:knife][:network_ids] - @instance.create_options[:server_def][:metadata].should == Chef::Config[:knife][:metadata] - @instance.create_options[:server_create_timeout].should == Chef::Config[:knife][:server_create_timeout] + expect(@instance.create_options[:server_def][:name]).to be == @instance.config[:chef_node_name] + expect(@instance.create_options[:server_def][:image_ref]).to be == Chef::Config[:knife][:image] + expect(@instance.create_options[:server_def][:security_groups]).to be == Chef::Config[:knife][:openstack_security_groups] + expect(@instance.create_options[:server_def][:flavor_ref]).to be == Chef::Config[:knife][:flavor] + expect(@instance.create_options[:server_def][:nics]).to be == Chef::Config[:knife][:network_ids] + expect(@instance.create_options[:server_def][:metadata]).to be == Chef::Config[:knife][:metadata] + expect(@instance.create_options[:server_create_timeout]).to be == Chef::Config[:knife][:server_create_timeout] end it "doesn't set user data in server_def if user_data not specified" do @@ -97,7 +113,7 @@ allow(@instance.service).to receive(:get_image).and_return(get_mock_resource('image')) allow(@instance.service).to receive(:get_flavor).and_return(get_mock_resource('flavor')) @instance.before_exec_command - @instance.create_options[:server_def].should_not include(:user_data) + expect(@instance.create_options[:server_def]).to_not include(:user_data) end it "sets user data" do @@ -107,7 +123,7 @@ allow(@instance.service).to receive(:get_image).and_return(get_mock_resource('image')) allow(@instance.service).to receive(:get_flavor).and_return(get_mock_resource('flavor')) @instance.before_exec_command - @instance.create_options[:server_def][:user_data].should == user_data + expect(@instance.create_options[:server_def][:user_data]).to be == user_data end context "with multiple network_ids specified" do @@ -115,27 +131,27 @@ @instance.service = double allow(@instance.service).to receive(:get_image).and_return(get_mock_resource('image')) allow(@instance.service).to receive(:get_flavor).and_return(get_mock_resource('flavor')) - @instance.service.should_receive(:create_server_dependencies) + expect(@instance.service).to receive(:create_server_dependencies) Chef::Config[:knife][:network_ids] = "test_network_id1,test_network_id2" - Chef::Config[:knife][:network_ids].stub(:map).and_return(Chef::Config[:knife][:network_ids].split(",")) + allow(Chef::Config[:knife][:network_ids]).to receive(:map).and_return(Chef::Config[:knife][:network_ids].split(",")) end it "creates the server_def with multiple nic_ids." do @instance.before_exec_command - @instance.create_options[:server_def][:nics].should == ["test_network_id1", "test_network_id2"] + expect(@instance.create_options[:server_def][:nics]).to be == ["test_network_id1", "test_network_id2"] end end it "ensures default value for metadata" do options = @instance.options - options[:metadata][:default].should == nil + expect(options[:metadata][:default]).to be == nil end end describe "#after_exec_command" do before(:each) do @instance = Chef::Knife::Cloud::OpenstackServerCreate.new - @instance.stub(:msg_pair) + allow(@instance).to receive(:msg_pair) end after(:all) do @@ -147,9 +163,9 @@ Chef::Config[:knife][:openstack_floating_ip] = "-1" @instance.service = Chef::Knife::Cloud::Service.new @instance.server = double - @instance.server.should_not_receive(:associate_address) - @instance.server.stub(:addresses).and_return({"public"=>[{"version"=>4, "addr"=>"127.0.1.1"}]}) - @instance.should_receive(:bootstrap) + expect(@instance.server).to_not receive(:associate_address) + allow(@instance.server).to receive(:addresses).and_return({"public"=>[{"version"=>4, "addr"=>"127.0.1.1"}]}) + expect(@instance).to receive(:bootstrap) @instance.after_exec_command end @@ -157,31 +173,31 @@ Chef::Config[:knife][:openstack_floating_ip] = nil @instance.service = Chef::Knife::Cloud::Service.new @instance.server = double - @instance.server.stub(:addresses).and_return({"public"=>[{"version"=>4, "addr"=>"127.0.1.1"}]}) - @instance.should_receive(:bootstrap) + allow(@instance.server).to receive(:addresses).and_return({"public"=>[{"version"=>4, "addr"=>"127.0.1.1"}]}) + expect(@instance).to receive(:bootstrap) connection = double - @instance.service.stub(:connection).and_return(double) + allow(@instance.service).to receive(:connection).and_return(double) free_floating = Object.new free_floating.define_singleton_method(:fixed_ip) { return nil } free_floating.define_singleton_method(:ip) { return "127.0.0.1" } - @instance.service.connection.should_receive(:addresses).and_return([free_floating]) - @instance.server.should_receive(:associate_address).with(free_floating.ip) + expect(@instance.service.connection).to receive(:addresses).and_return([free_floating]) + expect(@instance.server).to receive(:associate_address).with(free_floating.ip) @instance.after_exec_command end it "raise error on unavailability of free_floating ip" do Chef::Config[:knife][:openstack_floating_ip] = nil @instance.service = Chef::Knife::Cloud::Service.new - @instance.ui.stub(:fatal) + allow(@instance.ui).to receive(:fatal) @instance.server = double - @instance.server.stub(:addresses).and_return({"public"=>[{"version"=>4, "addr"=>"127.0.1.1"}]}) - @instance.should_not_receive(:bootstrap) + allow(@instance.server).to receive(:addresses).and_return({"public"=>[{"version"=>4, "addr"=>"127.0.1.1"}]}) + expect(@instance).to_not receive(:bootstrap) connection = double - @instance.service.stub(:connection).and_return(double) + allow(@instance.service).to receive(:connection).and_return(double) free_floating = Object.new free_floating.define_singleton_method(:fixed_ip) { return "127.0.0.1" } - @instance.service.connection.should_receive(:addresses).and_return([free_floating]) - @instance.server.should_not_receive(:associate_address) + expect(@instance.service.connection).to receive(:addresses).and_return([free_floating]) + expect(@instance.server).to_not receive(:associate_address) expect { @instance.after_exec_command }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ServerSetupError, "Unable to assign a Floating IP from allocated IPs.") end end @@ -200,55 +216,55 @@ before(:each) { @instance.config[:network] = false } it "set public ip as a bootstrap ip if both public and private ip available" do - @instance.server.stub(:addresses).and_return({"private"=>[{"version"=>4, "addr"=>"127.0.0.1"}], "public"=>[{"version"=>4, "addr"=>"127.0.0.2"}]}) + allow(@instance.server).to receive(:addresses).and_return({"private"=>[{"version"=>4, "addr"=>"127.0.0.1"}], "public"=>[{"version"=>4, "addr"=>"127.0.0.2"}]}) @instance.before_bootstrap - @instance.config[:bootstrap_ip_address].should == "127.0.0.2" + expect(@instance.config[:bootstrap_ip_address]).to be == "127.0.0.2" end it "set private-ip as a bootstrap ip if private ip is available" do - @instance.server.stub(:addresses).and_return({"private"=>[{"version"=>4, "addr"=>"127.0.0.1"}]}) + allow(@instance.server).to receive(:addresses).and_return({"private"=>[{"version"=>4, "addr"=>"127.0.0.1"}]}) @instance.before_bootstrap - @instance.config[:bootstrap_ip_address].should == "127.0.0.1" + expect(@instance.config[:bootstrap_ip_address]).to be == "127.0.0.1" end it "set available ip as a bootstrap ip if no public, private ip available" do - @instance.server.stub(:addresses).and_return({1=>[{"version"=>4, "addr"=>"127.0.0.1"}]}) + allow(@instance.server).to receive(:addresses).and_return({1=>[{"version"=>4, "addr"=>"127.0.0.1"}]}) @instance.before_bootstrap - @instance.config[:bootstrap_ip_address].should == "127.0.0.1" + expect(@instance.config[:bootstrap_ip_address]).to be == "127.0.0.1" end end it "set bootstrap_ip" do - @instance.server.stub(:addresses).and_return({"public"=>[{"version"=>4, "addr"=>"127.0.0.1"}]}) + allow(@instance.server).to receive(:addresses).and_return({"public"=>[{"version"=>4, "addr"=>"127.0.0.1"}]}) @instance.before_bootstrap - @instance.config[:bootstrap_ip_address].should == "127.0.0.1" + expect(@instance.config[:bootstrap_ip_address]).to be == "127.0.0.1" end it "set private-ip as a bootstrap-ip if private-network option set" do - @instance.server.stub(:addresses).and_return({"private"=>[{"version"=>4, "addr"=>"127.0.0.1"}], "public"=>[{"version"=>4, "addr"=>"127.0.0.2"}]}) + allow(@instance.server).to receive(:addresses).and_return({"private"=>[{"version"=>4, "addr"=>"127.0.0.1"}], "public"=>[{"version"=>4, "addr"=>"127.0.0.2"}]}) @instance.config[:private_network] = true @instance.before_bootstrap - @instance.config[:bootstrap_ip_address].should == "127.0.0.1" + expect(@instance.config[:bootstrap_ip_address]).to be == "127.0.0.1" end it "raise error on nil bootstrap_ip" do - @instance.ui.stub(:error) + allow(@instance.ui).to receive(:error) - @instance.server.stub(:addresses).and_return({"public"=>[{"version"=>4, "addr"=>nil}]}) + allow(@instance.server).to receive(:addresses).and_return({"public"=>[{"version"=>4, "addr"=>nil}]}) expect { @instance.before_bootstrap }.to raise_error(Chef::Knife::Cloud::CloudExceptions::BootstrapError, "No IP address available for bootstrapping.") end it "set public ip as default bootstrap network is public" do - @instance.server.stub(:addresses).and_return({"private"=>[{"version"=>4, "addr"=>"127.0.0.1"}], "public"=>[{"version"=>4, "addr"=>"127.0.0.2"}]}) + allow(@instance.server).to receive(:addresses).and_return({"private"=>[{"version"=>4, "addr"=>"127.0.0.1"}], "public"=>[{"version"=>4, "addr"=>"127.0.0.2"}]}) @instance.before_bootstrap - @instance.config[:bootstrap_ip_address].should == "127.0.0.2" + expect(@instance.config[:bootstrap_ip_address]).to be == "127.0.0.2" end it "configures the bootstrap to use alternate network" do - @instance.server.stub(:addresses).and_return({"foo"=>[{"version"=>1, "addr"=>"127.0.0.1"}], "private"=>[{"version"=>4, "addr"=>"127.0.0.2"}], "public"=>[{"version"=>4, "addr"=>"127.0.0.3"}]}) + allow(@instance.server).to receive(:addresses).and_return({"foo"=>[{"version"=>1, "addr"=>"127.0.0.1"}], "private"=>[{"version"=>4, "addr"=>"127.0.0.2"}], "public"=>[{"version"=>4, "addr"=>"127.0.0.3"}]}) @instance.config[:bootstrap_network] = 'foo' @instance.before_bootstrap - @instance.config[:bootstrap_ip_address].should == "127.0.0.1" + expect(@instance.config[:bootstrap_ip_address]).to be == "127.0.0.1" end end end diff --git a/spec/unit/openstack_server_delete_spec.rb b/spec/unit/openstack_server_delete_spec.rb index 5d28239f..4d8cbfbf 100644 --- a/spec/unit/openstack_server_delete_spec.rb +++ b/spec/unit/openstack_server_delete_spec.rb @@ -1,3 +1,23 @@ +# +# Author:: Prabhu Das () +# Author:: Mukta Aphale () +# Author:: Siddheshwar More () +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_server_delete' require 'chef/knife/cloud/openstack_service' @@ -9,7 +29,7 @@ let (:instance) {Chef::Knife::Cloud::OpenstackServerDelete.new} before(:each) do - instance.stub(:exit) + allow(instance).to receive(:exit) end describe "#validate!" do @@ -25,19 +45,19 @@ it "raise error on openstack_username missing" do Chef::Config[:knife].delete(:openstack_username) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Username' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_password missing" do Chef::Config[:knife].delete(:openstack_password) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Password' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_auth_url missing" do Chef::Config[:knife].delete(:openstack_auth_url) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end end diff --git a/spec/unit/openstack_server_list_spec.rb b/spec/unit/openstack_server_list_spec.rb index 494689bb..c76af1cd 100644 --- a/spec/unit/openstack_server_list_spec.rb +++ b/spec/unit/openstack_server_list_spec.rb @@ -1,3 +1,22 @@ +# +# Author:: Mukta Aphale () +# Author:: Siddheshwar More () +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_server_list' require 'chef/knife/cloud/openstack_service' @@ -13,7 +32,7 @@ Chef::Config[:knife][:openstack_username] = "testuser" Chef::Config[:knife][:openstack_password] = "testpassword" Chef::Config[:knife][:openstack_auth_url] = "tsturl" - instance.stub(:exit) + allow(instance).to receive(:exit) end it "validate openstack mandatory options" do @@ -22,22 +41,20 @@ it "raise error on openstack_username missing" do Chef::Config[:knife].delete(:openstack_username) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Username' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_password missing" do Chef::Config[:knife].delete(:openstack_password) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Password' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_auth_url missing" do Chef::Config[:knife].delete(:openstack_auth_url) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end end - - end diff --git a/spec/unit/openstack_server_show_spec.rb b/spec/unit/openstack_server_show_spec.rb index 53969f32..1feda298 100644 --- a/spec/unit/openstack_server_show_spec.rb +++ b/spec/unit/openstack_server_show_spec.rb @@ -1,3 +1,20 @@ +# +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_server_show' require 'chef/knife/cloud/openstack_service' @@ -13,7 +30,7 @@ Chef::Config[:knife][:openstack_username] = "testuser" Chef::Config[:knife][:openstack_password] = "testpassword" Chef::Config[:knife][:openstack_auth_url] = "tsturl" - instance.stub(:exit) + allow(instance).to receive(:exit) end it "validate openstack mandatory options" do @@ -22,19 +39,19 @@ it "raise error on openstack_username missing" do Chef::Config[:knife].delete(:openstack_username) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Username' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_password missing" do Chef::Config[:knife].delete(:openstack_password) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Password' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end it "raise error on openstack_auth_url missing" do Chef::Config[:knife].delete(:openstack_auth_url) - instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") + expect(instance.ui).to receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end end @@ -46,7 +63,7 @@ it "raise error on instance_id missing" do Chef::Config[:knife].delete(:instance_id) - instance.ui.should_receive(:error).with("You must provide a valid Instance Id") + expect(instance.ui).to receive(:error).with("You must provide a valid Instance Id") expect { instance.validate_params! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end end diff --git a/spec/unit/openstack_service_spec.rb b/spec/unit/openstack_service_spec.rb index dc1ad246..6c48dec6 100644 --- a/spec/unit/openstack_service_spec.rb +++ b/spec/unit/openstack_service_spec.rb @@ -1,5 +1,8 @@ # -# Copyright:: Copyright (c) 2011-2013 Chef Software, Inc. +# Author:: Mukta Aphale () +# Author:: Siddheshwar More () +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +16,6 @@ # 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' require 'chef/knife/cloud/openstack_service' @@ -31,55 +33,55 @@ end it "sets the api_endpoint in auth params" do - @instance.instance_variable_get(:@auth_params)[:openstack_auth_url].should == nil + expect(@instance.instance_variable_get(:@auth_params)[:openstack_auth_url]).to be == nil @instance.add_api_endpoint - @instance.instance_variable_get(:@auth_params)[:openstack_auth_url].should == @api_endpoint + expect(@instance.instance_variable_get(:@auth_params)[:openstack_auth_url]).to be == @api_endpoint end it "does not set the endpoint when --api-endpoint option is missing" do Chef::Config[:knife][:api_endpoint] = nil - @instance.instance_variable_get(:@auth_params)[:openstack_auth_url].should == nil + expect(@instance.instance_variable_get(:@auth_params)[:openstack_auth_url]).to be == nil @instance.add_api_endpoint - @instance.instance_variable_get(:@auth_params)[:openstack_auth_url].should_not == @api_endpoint - @instance.instance_variable_get(:@auth_params)[:openstack_auth_url].should == nil + expect(@instance.instance_variable_get(:@auth_params)[:openstack_auth_url]).to_not be == @api_endpoint + expect(@instance.instance_variable_get(:@auth_params)[:openstack_auth_url]).to be == nil end it "doesn't set an OpenStack endpoint type by default" do - Chef::Config[:knife][:openstack_endpoint_type].should == nil + expect(Chef::Config[:knife][:openstack_endpoint_type]).to be == nil end end describe "#get_server" do before(:each) do @instance = Chef::Knife::Cloud::OpenstackService.new - @instance.stub_chain(:connection,:servers,:get) + allow(@instance).to receive_message_chain(:connection,:servers,:get) end context "when instance_id given" do it "return server" do server_id = "123f456-123-453e-9c0c-12345a6789" - @instance.connection.servers.should_receive(:get).and_return(server_id) - @instance.connection.servers.should_not_receive(:all) - @instance.get_server(server_id).should == server_id + expect(@instance.connection.servers).to receive(:get).and_return(server_id) + expect(@instance.connection.servers).to_not receive(:all) + expect(@instance.get_server(server_id)).to be == server_id end end context "when instance_name given" do before(:each) do - @instance.connection.servers.should_receive(:get).and_return(nil) + expect(@instance.connection.servers).to receive(:get).and_return(nil) end let (:server_name) { "testname" } it "return server" do - @instance.connection.servers.should_receive(:all).with({:name=>server_name}).and_return([server_name]) - @instance.get_server(server_name).should == server_name + expect(@instance.connection.servers).to receive(:all).with({:name=>server_name}).and_return([server_name]) + expect(@instance.get_server(server_name)).to be == server_name end it "raise error if multiple server matches found for given instance name" do error_message = "Multiple server matches found for '#{server_name}', use an instance_id to be more specific." - @instance.connection.servers.should_receive(:all).with({:name=>server_name}).and_return([server_name,server_name]) - @instance.stub_chain(:ui,:fatal) + expect(@instance.connection.servers).to receive(:all).with({:name=>server_name}).and_return([server_name,server_name]) + allow(@instance).to receive_message_chain(:ui,:fatal) expect { @instance.get_server(server_name) }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError, error_message) end end From e88a9207b84c645fb4a4158d3aaa9ba3bcc5acb2 Mon Sep 17 00:00:00 2001 From: ameyavarade Date: Thu, 12 Jun 2014 15:11:25 +0530 Subject: [PATCH 2/2] Fixed rspec deprecation warnings for functional tests --- spec/functional/flavor_list_func_spec.rb | 28 ++++++++++--- spec/functional/group_list_func_spec.rb | 29 ++++++++++--- spec/functional/image_list_func_spec.rb | 30 +++++++++++--- spec/functional/network_list_func_spec.rb | 27 +++++++++--- spec/functional/server_create_func_spec.rb | 48 ++++++++++++---------- spec/functional/server_delete_func_spec.rb | 39 ++++++++++-------- spec/functional/server_list_func_spec.rb | 46 +++++++++++++++------ spec/functional/server_show_func_spec.rb | 31 ++++++++++---- 8 files changed, 197 insertions(+), 81 deletions(-) diff --git a/spec/functional/flavor_list_func_spec.rb b/spec/functional/flavor_list_func_spec.rb index 0a20edd0..8ffd3c1b 100644 --- a/spec/functional/flavor_list_func_spec.rb +++ b/spec/functional/flavor_list_func_spec.rb @@ -1,3 +1,21 @@ +# +# Author:: Mukta Aphale () +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_flavor_list' require 'chef/knife/cloud/openstack_service' @@ -11,14 +29,14 @@ resources = [ TestResource.new({:id => "resource-1", :name => "m1.tiny", :vcpus => "1", :ram => 512, :disk => 0}), TestResource.new({:id => "resource-2", :name => "m1-xlarge-bigdisk", :vcpus => "8", :ram => 16384, :disk => 50}) ] - instance.stub(:query_resource).and_return(resources) - instance.stub(:puts) - instance.stub(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) - instance.stub(:validate!) + allow(instance).to receive(:query_resource).and_return(resources) + allow(instance).to receive(:puts) + allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) + allow(instance).to receive(:validate!) end it "lists formatted list of resources" do - instance.ui.should_receive(:list).with(["Name", "ID", "Virtual CPUs", "RAM", "Disk", + expect(instance.ui).to receive(:list).with(["Name", "ID", "Virtual CPUs", "RAM", "Disk", "m1-xlarge-bigdisk", "resource-2", "8", "16384 MB", "50 GB", "m1.tiny", "resource-1", "1", "512 MB", "0 GB"], :uneven_columns_across, 5) instance.run diff --git a/spec/functional/group_list_func_spec.rb b/spec/functional/group_list_func_spec.rb index 9706d48c..7a321946 100644 --- a/spec/functional/group_list_func_spec.rb +++ b/spec/functional/group_list_func_spec.rb @@ -1,3 +1,22 @@ +# +# Author:: Mukta Aphale () +# Author:: Prabhu Das () +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_group_list' require 'chef/knife/cloud/openstack_service' @@ -32,14 +51,14 @@ ] }) ] - instance.stub(:query_resource).and_return(resources) - instance.stub(:puts) - instance.stub(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) - instance.stub(:validate!) + allow(instance).to receive(:query_resource).and_return(resources) + allow(instance).to receive(:puts) + allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) + allow(instance).to receive(:validate!) end it "lists formatted list of resources" do - instance.ui.should_receive(:list).with(["Name", "Protocol", "From", "To", "CIDR", "Description", + expect(instance.ui).to receive(:list).with(["Name", "Protocol", "From", "To", "CIDR", "Description", "Unrestricted", "tcp", "1", "636", "0.0.0.0/0", "All ports open", "WindowsDomain", "tcp", "22", "636", "0.0.0.0/0", "Allows common protocols useful in a Windows domain"], :uneven_columns_across, 6) instance.run diff --git a/spec/functional/image_list_func_spec.rb b/spec/functional/image_list_func_spec.rb index 059a1f14..1e80983e 100644 --- a/spec/functional/image_list_func_spec.rb +++ b/spec/functional/image_list_func_spec.rb @@ -1,3 +1,21 @@ +# +# Author:: Mukta Aphale () +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_image_list' require 'chef/knife/cloud/openstack_service' @@ -10,21 +28,21 @@ resources = [ TestResource.new({:id => "resource-1", :name => "image01", :metadata => {} }), TestResource.new({:id => "resource-2", :name => "initrd", :metadata => {} }) ] - instance.stub(:query_resource).and_return(resources) - instance.stub(:puts) - instance.stub(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) - instance.stub(:validate!) + allow(instance).to receive(:query_resource).and_return(resources) + allow(instance).to receive(:puts) + allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) + allow(instance).to receive(:validate!) end it "displays formatted list of images, filtered by default" do - instance.ui.should_receive(:list).with(["Name", "ID", "Snapshot", + expect(instance.ui).to receive(:list).with(["Name", "ID", "Snapshot", "image01", "resource-1", "no"], :uneven_columns_across, 3) instance.run end it "lists all images when disable_filter = true" do instance.config[:disable_filter] = true - instance.ui.should_receive(:list).with(["Name", "ID", "Snapshot", + expect(instance.ui).to receive(:list).with(["Name", "ID", "Snapshot", "image01", "resource-1", "no", "initrd", "resource-2", "no"], :uneven_columns_across, 3) instance.run diff --git a/spec/functional/network_list_func_spec.rb b/spec/functional/network_list_func_spec.rb index b4bfcc4e..73e9ec47 100644 --- a/spec/functional/network_list_func_spec.rb +++ b/spec/functional/network_list_func_spec.rb @@ -1,3 +1,20 @@ +# +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_network_list' require 'chef/knife/cloud/openstack_service' @@ -11,14 +28,14 @@ resources = [ TestResource.new({:id => "resource-1", :name => "external", :tenant_id => "1", :shared => true}), TestResource.new({:id => "resource-2", :name => "internal", :tenant_id => "2", :shared => false}) ] - instance.stub(:query_resource).and_return(resources) - instance.stub(:puts) - instance.stub(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) - instance.stub(:validate!) + allow(instance).to receive(:query_resource).and_return(resources) + allow(instance).to receive(:puts) + allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) + allow(instance).to receive(:validate!) end it "lists formatted list of network resources" do - instance.ui.should_receive(:list).with(["Name", "ID", "Tenant", "Shared", + expect(instance.ui).to receive(:list).with(["Name", "ID", "Tenant", "Shared", "external", "resource-1", "1", "true", "internal", "resource-2", "2", "false"], :uneven_columns_across, 4) instance.run diff --git a/spec/functional/server_create_func_spec.rb b/spec/functional/server_create_func_spec.rb index 4335c04a..57c75593 100644 --- a/spec/functional/server_create_func_spec.rb +++ b/spec/functional/server_create_func_spec.rb @@ -1,6 +1,10 @@ # +# Author:: Mukta Aphale () +# Author:: Kaustubh Deorukhkar () +# Author:: Siddheshwar More () # Author:: Prabhu Das () -# Copyright:: Copyright (c) 2013 Chef Software, Inc. +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,6 +18,7 @@ # 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 File.expand_path('../../spec_helper', __FILE__) describe Chef::Knife::Cloud::OpenstackServerCreate do @@ -31,8 +36,8 @@ end @openstack_service = Chef::Knife::Cloud::OpenstackService.new - @openstack_service.stub(:msg_pair) - @openstack_service.stub(:print) + allow(@openstack_service).to receive(:msg_pair) + allow(@openstack_service).to receive(:print) image = Object.new allow(image).to receive(:id).and_return('image_id') allow(@openstack_service).to receive(:get_image).and_return(image) @@ -40,8 +45,8 @@ allow(flavor).to receive(:id).and_return('flavor_id') allow(@openstack_service).to receive(:get_flavor).and_return(flavor) - @knife_openstack_create.stub(:create_service_instance).and_return(@openstack_service) - @knife_openstack_create.stub(:puts) + allow(@knife_openstack_create).to receive(:create_service_instance).and_return(@openstack_service) + allow(@knife_openstack_create).to receive(:puts) @new_openstack_server = double() @openstack_server_attribs = { :name => 'Mock Server', @@ -57,16 +62,16 @@ } @openstack_server_attribs.each_pair do |attrib, value| - @new_openstack_server.stub(attrib).and_return(value) + allow(@new_openstack_server).to receive(attrib).and_return(value) end end describe "run" do before(:each) do - @knife_openstack_create.stub(:validate_params!) - Fog::Compute::OpenStack.stub_chain(:new, :servers, :create).and_return(@new_openstack_server) + allow(@knife_openstack_create).to receive(:validate_params!) + allow(Fog::Compute::OpenStack).to receive_message_chain(:new, :servers, :create).and_return(@new_openstack_server) @knife_openstack_create.config[:openstack_floating_ip] = '-1' - @new_openstack_server.stub(:wait_for) + allow(@new_openstack_server).to receive(:wait_for) end context "for Linux" do @@ -76,15 +81,15 @@ @bootstrapper = Chef::Knife::Cloud::Bootstrapper.new(@config) @ssh_bootstrap_protocol = Chef::Knife::Cloud::SshBootstrapProtocol.new(@config) @unix_distribution = Chef::Knife::Cloud::UnixDistribution.new(@config) - @ssh_bootstrap_protocol.stub(:send_bootstrap_command) + allow(@ssh_bootstrap_protocol).to receive(:send_bootstrap_command) end it "Creates an OpenStack instance and bootstraps it" do - Chef::Knife::Cloud::Bootstrapper.should_receive(:new).with(@config).and_return(@bootstrapper) - @bootstrapper.stub(:bootstrap).and_call_original - @bootstrapper.should_receive(:create_bootstrap_protocol).and_return(@ssh_bootstrap_protocol) - @bootstrapper.should_receive(:create_bootstrap_distribution).and_return(@unix_distribution) - @openstack_service.should_receive(:server_summary).exactly(2).times + expect(Chef::Knife::Cloud::Bootstrapper).to receive(:new).with(@config).and_return(@bootstrapper) + allow(@bootstrapper).to receive(:bootstrap).and_call_original + expect(@bootstrapper).to receive(:create_bootstrap_protocol).and_return(@ssh_bootstrap_protocol) + expect(@bootstrapper).to receive(:create_bootstrap_distribution).and_return(@unix_distribution) + expect(@openstack_service).to receive(:server_summary).exactly(2).times @knife_openstack_create.run end end @@ -100,15 +105,14 @@ @windows_distribution = Chef::Knife::Cloud::WindowsDistribution.new(@config) end it "Creates an OpenStack instance for Windows and bootstraps it" do - Chef::Knife::Cloud::Bootstrapper.should_receive(:new).with(@config).and_return(@bootstrapper) - @bootstrapper.stub(:bootstrap).and_call_original - @bootstrapper.should_receive(:create_bootstrap_protocol).and_return(@winrm_bootstrap_protocol) - @bootstrapper.should_receive(:create_bootstrap_distribution).and_return(@windows_distribution) - @winrm_bootstrap_protocol.stub(:send_bootstrap_command) - @openstack_service.should_receive(:server_summary).exactly(2).times + expect(Chef::Knife::Cloud::Bootstrapper).to receive(:new).with(@config).and_return(@bootstrapper) + allow(@bootstrapper).to receive(:bootstrap).and_call_original + expect(@bootstrapper).to receive(:create_bootstrap_protocol).and_return(@winrm_bootstrap_protocol) + expect(@bootstrapper).to receive(:create_bootstrap_distribution).and_return(@windows_distribution) + allow(@winrm_bootstrap_protocol).to receive(:send_bootstrap_command) + expect(@openstack_service).to receive(:server_summary).exactly(2).times @knife_openstack_create.run end end - end end diff --git a/spec/functional/server_delete_func_spec.rb b/spec/functional/server_delete_func_spec.rb index 649af053..bf4bdcd1 100644 --- a/spec/functional/server_delete_func_spec.rb +++ b/spec/functional/server_delete_func_spec.rb @@ -1,6 +1,8 @@ # +# Author:: Siddheshwar More () # Author:: Prabhu Das () -# Copyright:: Copyright (c) 2013 Chef Software, Inc. +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,6 +16,7 @@ # 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 File.expand_path('../../spec_helper', __FILE__) require 'chef/knife/openstack_server_delete' require 'chef/knife/cloud/openstack_service' @@ -34,10 +37,10 @@ end @openstack_service = Chef::Knife::Cloud::OpenstackService.new - @openstack_service.stub(:msg_pair) - @knife_openstack_delete.stub(:create_service_instance).and_return(@openstack_service) - @knife_openstack_delete.ui.stub(:warn) - @knife_openstack_delete.ui.stub(:confirm) + allow(@openstack_service).to receive(:msg_pair) + allow(@knife_openstack_delete).to receive(:create_service_instance).and_return(@openstack_service) + allow(@knife_openstack_delete.ui).to receive(:warn) + allow(@knife_openstack_delete.ui).to receive(:confirm) @openstack_servers = double() @running_openstack_server = double() @openstack_server_attribs = { :name => 'Mock Server', @@ -51,30 +54,30 @@ } @openstack_server_attribs.each_pair do |attrib, value| - @running_openstack_server.stub(attrib).and_return(value) + allow(@running_openstack_server).to receive(attrib).and_return(value) end @knife_openstack_delete.name_args = ['test001'] end describe "run" do it "deletes an OpenStack instance." do - @openstack_servers.should_receive(:get).and_return(@running_openstack_server) - @openstack_connection.should_receive(:servers).and_return(@openstack_servers) - Fog::Compute::OpenStack.should_receive(:new).and_return(@openstack_connection) - @running_openstack_server.should_receive(:destroy) + expect(@openstack_servers).to receive(:get).and_return(@running_openstack_server) + expect(@openstack_connection).to receive(:servers).and_return(@openstack_servers) + expect(Fog::Compute::OpenStack).to receive(:new).and_return(@openstack_connection) + expect(@running_openstack_server).to receive(:destroy) @knife_openstack_delete.run end it "deletes the instance along with the node and client on the chef-server when --purge is given as an option." do @knife_openstack_delete.config[:purge] = true - Chef::Node.should_receive(:load).and_return(@chef_node) - @chef_node.should_receive(:destroy) - Chef::ApiClient.should_receive(:load).and_return(@chef_client) - @chef_client.should_receive(:destroy) - @openstack_servers.should_receive(:get).and_return(@running_openstack_server) - @openstack_connection.should_receive(:servers).and_return(@openstack_servers) - Fog::Compute::OpenStack.should_receive(:new).and_return(@openstack_connection) - @running_openstack_server.should_receive(:destroy) + expect(Chef::Node).to receive(:load).and_return(@chef_node) + expect(@chef_node).to receive(:destroy) + expect(Chef::ApiClient).to receive(:load).and_return(@chef_client) + expect(@chef_client).to receive(:destroy) + expect(@openstack_servers).to receive(:get).and_return(@running_openstack_server) + expect(@openstack_connection).to receive(:servers).and_return(@openstack_servers) + expect(Fog::Compute::OpenStack).to receive(:new).and_return(@openstack_connection) + expect(@running_openstack_server).to receive(:destroy) @knife_openstack_delete.run end end diff --git a/spec/functional/server_list_func_spec.rb b/spec/functional/server_list_func_spec.rb index f4c5c1f9..260ad619 100644 --- a/spec/functional/server_list_func_spec.rb +++ b/spec/functional/server_list_func_spec.rb @@ -1,3 +1,23 @@ +# +# Author:: Mukta Aphale () +# Author:: Siddheshwar More () +# Author:: Prabhu Das () +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_server_list' require 'chef/knife/cloud/openstack_service' @@ -11,14 +31,14 @@ TestResource.new({:id => "resource-2", :name => "windows2008",:availability_zone => "test zone", :addresses => {"public"=>[{"version"=>4, "addr"=>"172.31.6.132"}]}, :flavor => {"id" => "id2"}, :image => {"id" => "image2"}, :key_name => "keypair", :state => "ACTIVE"}), TestResource.new({:id => "resource-3-err", :name => "windows2008", :availability_zone => "test zone", :addresses => {"public"=>[], "private"=>[]}, :flavor => {"id" => "id2"}, :image => {"id" => "image2"}, :key_name => "keypair", :state => "ERROR"}) ] - instance.stub(:query_resource).and_return(@resources) - instance.stub(:puts) - instance.stub(:create_service_instance).and_return(Chef::Knife::Cloud::FogService.new) - instance.stub(:validate!) + allow(instance).to receive(:query_resource).and_return(@resources) + allow(instance).to receive(:puts) + allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::FogService.new) + allow(instance).to receive(:validate!) end it "lists formatted list of resources" do - instance.ui.should_receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", + expect(instance.ui).to receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "ubuntu01", "resource-1", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "windows2008", "resource-2", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", "windows2008", "resource-3-err", nil, nil, "id2", "image2", "keypair", "ERROR", "test zone"], :uneven_columns_across, 9) instance.run @@ -28,12 +48,12 @@ before(:each) do @resources.push(TestResource.new({:id => "server-4", :name => "server-4", :availability_zone => "test zone", :addresses => {"public"=>[{"version"=>4, "addr"=>"172.31.6.132"}], "private"=>[{"version"=>4, "addr"=>"172.31.6.133"}]}, :flavor => {"id" => "1"}, :image => {"id" => "image1"}, :key_name => "keypair", :state => "ACTIVE"})) @node = TestResource.new({:id => "server-4", :name => "server-4", :chef_environment => "_default", :fqdn => "testfqdnnode.us", :run_list => [], :tags => [], :platform => "ubuntu", :platform_family => "debian"}) - Chef::Node.stub(:list).and_return({"server-4" => @node}) + allow(Chef::Node).to receive(:list).and_return({"server-4" => @node}) instance.config[:chef_data] = true end it "lists formatted list of resources on chef data option set" do - instance.ui.should_receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform", + expect(instance.ui).to receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform", "server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "server-4", "_default", "testfqdnnode.us", "[]", "[]", "ubuntu", "ubuntu01", "resource-1", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", "windows2008", "resource-2", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", @@ -43,8 +63,8 @@ it "lists formatted list of resources on chef-data and chef-node-attribute option set" do instance.config[:chef_node_attribute] = "platform_family" - @node.should_receive(:attribute?).with("platform_family").and_return(true) - instance.ui.should_receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform", "platform_family", + expect(@node).to receive(:attribute?).with("platform_family").and_return(true) + expect(instance.ui).to receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform", "platform_family", "server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "server-4", "_default", "testfqdnnode.us", "[]", "[]", "ubuntu", "debian", "ubuntu01", "resource-1", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", "", "windows2008", "resource-2", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", "", @@ -54,16 +74,16 @@ it "raise error on invalid chef-node-attribute set" do instance.config[:chef_node_attribute] = "invalid_attribute" - instance.ui.should_receive(:fatal) - @node.should_receive(:attribute?).with("invalid_attribute").and_return(false) - instance.ui.should_receive(:error).with("The Node does not have a invalid_attribute attribute.") + expect(instance.ui).to receive(:fatal) + expect(@node).to receive(:attribute?).with("invalid_attribute").and_return(false) + expect(instance.ui).to receive(:error).with("The Node does not have a invalid_attribute attribute.") expect { instance.run }.to raise_error end it "not display chef-data on chef-node-attribute set but chef-data option missing" do instance.config[:chef_data] = false instance.config[:chef_node_attribute] = "platform_family" - instance.ui.should_receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", + expect(instance.ui).to receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "ubuntu01", "resource-1", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "windows2008", "resource-2", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", diff --git a/spec/functional/server_show_func_spec.rb b/spec/functional/server_show_func_spec.rb index 2c5ca07f..a3a8c9ce 100644 --- a/spec/functional/server_show_func_spec.rb +++ b/spec/functional/server_show_func_spec.rb @@ -1,3 +1,20 @@ +# +# Author:: Ameya Varade () +# Copyright:: Copyright (c) 2013-2014 Chef Software, Inc. +# 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' require 'chef/knife/openstack_server_show' require 'chef/knife/cloud/openstack_service' @@ -9,17 +26,17 @@ @instance = Chef::Knife::Cloud::OpenstackServerShow.new Chef::Config[:knife][:instance_id] = "instance_id" @openstack_service = Chef::Knife::Cloud::OpenstackService.new - @openstack_service.stub(:msg_pair) - @openstack_service.stub(:print) + allow(@openstack_service).to receive(:msg_pair) + allow(@openstack_service).to receive(:print) allow_message_expectations_on_nil server = Object.new conn = Object.new conn.define_singleton_method(:servers){ } - @openstack_service.stub(:connection).and_return(conn) - @openstack_service.connection.servers.should_receive(:get).and_return(server) - @instance.stub(:create_service_instance).and_return(@openstack_service) - @instance.stub(:validate!) - @openstack_service.should_receive(:server_summary) + allow(@openstack_service).to receive(:connection).and_return(conn) + expect(@openstack_service.connection.servers).to receive(:get).and_return(server) + allow(@instance).to receive(:create_service_instance).and_return(@openstack_service) + allow(@instance).to receive(:validate!) + expect(@openstack_service).to receive(:server_summary) end it "runs server show successfully" do