From 475397132c36c32ccd9240cd1d6a4866f4aebaa4 Mon Sep 17 00:00:00 2001 From: Fletcher Nichol Date: Wed, 10 Sep 2014 23:26:21 -0600 Subject: [PATCH] Updates to OpenStack plugin rebase (PR #51). --- lib/chef/knife/server_bootstrap_openstack.rb | 29 ++-- .../knife/server_bootstrap_openstack_spec.rb | 156 ++++++++++-------- 2 files changed, 100 insertions(+), 85 deletions(-) diff --git a/lib/chef/knife/server_bootstrap_openstack.rb b/lib/chef/knife/server_bootstrap_openstack.rb index 7bc3580..6b7f854 100644 --- a/lib/chef/knife/server_bootstrap_openstack.rb +++ b/lib/chef/knife/server_bootstrap_openstack.rb @@ -16,10 +16,11 @@ # limitations under the License. # -require 'chef/knife/server_bootstrap_base' +require "chef/knife/server_bootstrap_base" class Chef class Knife + # Provisions an OpenStack instance and sets up an Open Source Chef Server. class ServerBootstrapOpenstack < Knife banner "knife server bootstrap openstack (options)" @@ -27,17 +28,17 @@ class ServerBootstrapOpenstack < Knife include Knife::ServerBootstrapBase deps do - require 'knife/server/ssh' - require 'knife/server/credentials' + require "knife/server/ssh" + require "knife/server/credentials" begin - require 'chef/knife/openstack_server_create' - require 'fog' + require "chef/knife/openstack_server_create" + require "fog" Chef::Knife::OpenstackServerCreate.load_deps - current_options = self.options + current_options = options self.options = Chef::Knife::OpenstackServerCreate.options.dup - self.options.merge!(current_options) + options.merge!(current_options) rescue LoadError => ex ui.error [ "Knife plugin knife-openstack could not be loaded.", @@ -58,13 +59,15 @@ def run end def openstack_bootstrap - ENV['WEBUI_PASSWORD'] = config_val(:webui_password) - ENV['AMQP_PASSWORD'] = config_val(:amqp_password) - ENV['NO_TEST'] = "1" if config[:no_test] + ENV["WEBUI_PASSWORD"] = config_val(:webui_password) + ENV["AMQP_PASSWORD"] = config_val(:amqp_password) + ENV["NO_TEST"] = "1" if config[:no_test] bootstrap = Chef::Knife::OpenstackServerCreate.new Chef::Knife::OpenstackServerCreate.options.keys.each do |attr| - value = config_val(attr) - bootstrap.config[attr] = value unless ((value.is_a?(Array) and value[1].nil?) or value.nil?) + val = config_val(attr) + next if val.nil? + + bootstrap.config[attr] = val end bootstrap.config[:distro] = bootstrap_distro bootstrap @@ -97,7 +100,7 @@ def validate! exit 1 end if config_val(:platform) == "auto" - ui.error "Auto platform mode cannot be used with knife-openstack plugin" + ui.error "Auto platform cannot be used with knife-openstack plugin" exit 1 end end diff --git a/spec/chef/knife/server_bootstrap_openstack_spec.rb b/spec/chef/knife/server_bootstrap_openstack_spec.rb index 99f180d..7649d6b 100644 --- a/spec/chef/knife/server_bootstrap_openstack_spec.rb +++ b/spec/chef/knife/server_bootstrap_openstack_spec.rb @@ -16,10 +16,10 @@ # limitations under the License. # -require 'chef/knife/server_bootstrap_openstack' -require 'chef/knife/ssh' -require 'fakefs/spec_helpers' -require 'net/ssh' +require "chef/knife/server_bootstrap_openstack" +require "chef/knife/ssh" +require "fakefs/spec_helpers" +require "net/ssh" Chef::Knife::ServerBootstrapOpenstack.load_deps describe Chef::Knife::ServerBootstrapOpenstack do @@ -29,15 +29,15 @@ Chef::Log.logger = Logger.new(StringIO.new) @knife = Chef::Knife::ServerBootstrapOpenstack.new @stdout = StringIO.new - @knife.ui.stub!(:stdout).and_return(@stdout) + allow(@knife.ui).to receive(:stdout).and_return(@stdout) @stderr = StringIO.new - @knife.ui.stub!(:stderr).and_return(@stderr) + allow(@knife.ui).to receive(:stderr).and_return(@stderr) @knife.config[:chef_node_name] = "yakky" @knife.config[:platform] = "omnibus" @knife.config[:ssh_user] = "root" end - let(:connection) { mock(Fog::Compute::AWS) } + let(:connection) { double(Fog::Compute::AWS) } describe "#openstack_bootstrap" do @@ -50,70 +50,72 @@ @knife.config[:webui_password] = "daweb" @knife.config[:amqp_password] = "queueitup" - ENV['_SPEC_WEBUI_PASSWORD'] = ENV['WEBUI_PASSWORD'] - ENV['_SPEC_AMQP_PASSWORD'] = ENV['AMQP_PASSWORD'] + ENV["_SPEC_WEBUI_PASSWORD"] = ENV["WEBUI_PASSWORD"] + ENV["_SPEC_AMQP_PASSWORD"] = ENV["AMQP_PASSWORD"] end after do - ENV['WEBUI_PASSWORD'] = ENV.delete('_SPEC_WEBUI_PASSWORD') - ENV['AMQP_PASSWORD'] = ENV.delete('_SPEC_AMQP_PASSWORD') + ENV["WEBUI_PASSWORD"] = ENV.delete("_SPEC_WEBUI_PASSWORD") + ENV["AMQP_PASSWORD"] = ENV.delete("_SPEC_AMQP_PASSWORD") end let(:bootstrap) { @knife.openstack_bootstrap } it "returns a OpenstackServerCreate instance" do - bootstrap.should be_a(Chef::Knife::OpenstackServerCreate) + expect(bootstrap).to be_a(Chef::Knife::OpenstackServerCreate) end it "configs the bootstrap's chef_node_name" do - bootstrap.config[:chef_node_name].should eq("shave.yak") + expect(bootstrap.config[:chef_node_name]).to eq("shave.yak") end it "configs the bootstrap's ssh_user" do - bootstrap.config[:ssh_user].should eq("jdoe") + expect(bootstrap.config[:ssh_user]).to eq("jdoe") end it "configs the bootstrap's identity_file" do - bootstrap.config[:identity_file].should eq("~/.ssh/mykey_dsa") + expect(bootstrap.config[:identity_file]).to eq("~/.ssh/mykey_dsa") end it "configs the bootstrap's openstack_password" do - bootstrap.config[:openstack_password].should eq("openstack123") + expect(bootstrap.config[:openstack_password]).to eq("openstack123") end it "configs the bootstrap's distro" do - bootstrap.config[:distro].should eq("distro-praha") + expect(bootstrap.config[:distro]).to eq("distro-praha") end it "configs the bootstrap's distro to chef11/omnibus by default" do @knife.config.delete(:distro) - bootstrap.config[:distro].should eq("chef11/omnibus") + expect(bootstrap.config[:distro]).to eq("chef11/omnibus") end it "configs the bootstrap's distro value driven off platform value" do @knife.config.delete(:distro) @knife.config[:platform] = "freebsd" - bootstrap.config[:distro].should eq("chef11/freebsd") + expect(bootstrap.config[:distro]).to eq("chef11/freebsd") end - it "configs the bootstrap's distro based on bootstrap_version and platform" do + it "configs bootstrap's distro based on bootstrap_version and platform" do @knife.config.delete(:distro) @knife.config[:platform] = "freebsd" @knife.config[:bootstrap_version] = "10" - bootstrap.config[:distro].should eq("chef10/freebsd") + expect(bootstrap.config[:distro]).to eq("chef10/freebsd") end it "configs the bootstrap's ENV with the webui password" do bootstrap - ENV['WEBUI_PASSWORD'].should eq("daweb") + + expect(ENV["WEBUI_PASSWORD"]).to eq("daweb") end it "configs the bootstrap's ENV with the amqp password" do bootstrap - ENV['AMQP_PASSWORD'].should eq("queueitup") + + expect(ENV["AMQP_PASSWORD"]).to eq("queueitup") end end @@ -124,11 +126,11 @@ @before_config[:knife] = Chef::Config[:knife] Chef::Config[:knife] = { - :openstack_username => 'jbellone', - :openstack_password => 'key', - :openstack_auth_url => 'http://0.0.0.0', - :openstack_tenant => 'slumlord', - :openstack_region => 'newyork' + :openstack_username => "jbellone", + :openstack_password => "key", + :openstack_auth_url => "http://0.0.0.0", + :openstack_tenant => "slumlord", + :openstack_region => "newyork" } end @@ -137,14 +139,14 @@ end it "constructs a connection" do - Fog::Compute.should_receive(:new).with({ + expect(Fog::Compute).to receive(:new).with( :provider => :openstack, - :openstack_username => 'jbellone', - :openstack_password => 'key', - :openstack_auth_url => 'http://0.0.0.0', - :openstack_tenant => 'slumlord', - :openstack_region => 'newyork' - }) + :openstack_username => "jbellone", + :openstack_password => "key", + :openstack_auth_url => "http://0.0.0.0", + :openstack_tenant => "slumlord", + :openstack_region => "newyork" + ) @knife.openstack_connection end @@ -153,38 +155,42 @@ describe "#server_ip_address" do before do - @knife.config[:openstack_node_name] = 'yak' - @knife.stub(:openstack_connection) { connection } + @knife.config[:openstack_node_name] = "yak" + allow(@knife).to receive(:openstack_connection) { connection } end context "when server is found" do before do - connection.stub(:servers) { [server] } + allow(connection).to receive(:servers) { [server] } end let(:server) do - stub(:name => 'yak', :status => 1, :public_ip_address => '10.11.12.13') + double( + :name => "yak", + :status => 1, + :public_ip_address => "10.11.12.13" + ) end it "returns the provisioned ip address" do - @knife.server_ip_address.should eq('10.11.12.13') + expect(@knife.server_ip_address).to eq("10.11.12.13") end it "ignores terminated instances" do - server.stub(:status) { 0 } + allow(server).to receive(:status) { 0 } - @knife.server_ip_address.should be_nil + expect(@knife.server_ip_address).to be_nil end end context "when server is not found" do before do - connection.stub(:servers) { [] } + allow(connection).to receive(:servers) { [] } end it "returns nil" do - @knife.server_ip_address.should be_nil + expect(@knife.server_ip_address).to be_nil end end end @@ -202,13 +208,13 @@ @knife.config[:validation_key] = "/var/tmp/validation.pem" @knife.config[:identity_file] = "~/.ssh/mykey_dsa" @knife.config[:ssh_password] = "booboo" - @knife.stub(:openstack_connection) { connection } - @knife.stub(:server_ip_address) { "11.11.11.13" } - Chef::Knife::OpenstackServerCreate.stub(:new) { bootstrap } - Knife::Server::SSH.stub(:new) { ssh } - Knife::Server::Credentials.stub(:new) { credentials } - credentials.stub(:install_validation_key) - credentials.stub(:create_root_client) + allow(@knife).to receive(:openstack_connection) { connection } + allow(@knife).to receive(:server_ip_address) { "11.11.11.13" } + allow(Chef::Knife::OpenstackServerCreate).to receive(:new) { bootstrap } + allow(Knife::Server::SSH).to receive(:new) { ssh } + allow(Knife::Server::Credentials).to receive(:new) { credentials } + allow(credentials).to receive(:install_validation_key) + allow(credentials).to receive(:create_root_client) end after do @@ -217,61 +223,67 @@ end end - let(:bootstrap) { stub(:run => true, :config => Hash.new) } - let(:ssh) { stub } - let(:credentials) { stub.as_null_object } + let(:bootstrap) { double(:run => true, :config => Hash.new) } + let(:ssh) { double } + let(:credentials) { double.as_null_object } it "exits if node_name option is missing" do @knife.config.delete(:chef_node_name) - lambda { @knife.run }.should raise_error(SystemExit) + expect { @knife.run }.to raise_error(SystemExit) end it "exits if platform is set to auto" do @knife.config[:platform] = "auto" - lambda { @knife.run }.should raise_error(SystemExit) + expect { @knife.run }.to raise_error(SystemExit) end it "bootstraps a openstack server" do - bootstrap.should_receive(:run) + expect(bootstrap).to receive(:run) @knife.run end it "installs a new validation.pem key from the chef 10 server" do @knife.config[:bootstrap_version] = "10" - Knife::Server::SSH.should_receive(:new).with({ - :host => "11.11.11.13", :user => "root", - :port => "22", :keys => ["~/.ssh/mykey_dsa"], :password => "booboo" - }) - Knife::Server::Credentials.should_receive(:new). + expect(Knife::Server::SSH).to receive(:new).with( + :host => "11.11.11.13", + :user => "root", + :port => "22", + :keys => ["~/.ssh/mykey_dsa"], + :password => "booboo" + ) + expect(Knife::Server::Credentials).to receive(:new). with(ssh, "/etc/chef/validation.pem", {}) - credentials.should_receive(:install_validation_key) + expect(credentials).to receive(:install_validation_key) @knife.run end it "installs a new validation.pem key from the omnibus server" do - Knife::Server::SSH.should_receive(:new).with({ - :host => "11.11.11.13", :user => "root", - :port => "22", :keys => ["~/.ssh/mykey_dsa"], :password => "booboo" - }) - Knife::Server::Credentials.should_receive(:new). - with(ssh, "/etc/chef/validation.pem", {:omnibus => true}) - credentials.should_receive(:install_validation_key) + expect(Knife::Server::SSH).to receive(:new).with( + :host => "11.11.11.13", + :user => "root", + :port => "22", + :keys => ["~/.ssh/mykey_dsa"], + :password => "booboo" + ) + expect(Knife::Server::Credentials).to receive(:new). + with(ssh, "/etc/chef/validation.pem", :omnibus => true) + expect(credentials).to receive(:install_validation_key) @knife.run end it "create a root client key" do - credentials.should_receive(:create_root_client) + expect(credentials).to receive(:create_root_client) @knife.run end it "installs a client key" do - credentials.should_receive(:install_client_key). + expect(credentials).to receive(:install_client_key). with("smithers", "/var/tmp/myclientkey.pem") @knife.run