diff options
author | Daniel DeLeo <dan@opscode.com> | 2010-10-06 17:12:31 -0700 |
---|---|---|
committer | Daniel DeLeo <dan@opscode.com> | 2010-10-06 17:12:31 -0700 |
commit | 30b8eed605006e3962388c912fe7ffeb20ae4694 (patch) | |
tree | 7bcee2919380deeb36f60cb8db0cf89452bb2152 | |
parent | f8c172409d89c43dc334b6a6480b6111b20afdc8 (diff) | |
download | chef-30b8eed605006e3962388c912fe7ffeb20ae4694.tar.gz |
fix and refactor tests for ec2 server create
-rw-r--r-- | chef/lib/chef/knife/bootstrap.rb | 2 | ||||
-rw-r--r-- | chef/lib/chef/knife/ec2_server_create.rb | 29 | ||||
-rw-r--r-- | chef/spec/unit/knife/ec2_server_create_spec.rb | 124 |
3 files changed, 95 insertions, 60 deletions
diff --git a/chef/lib/chef/knife/bootstrap.rb b/chef/lib/chef/knife/bootstrap.rb index b582b4a771..8e36ab5680 100644 --- a/chef/lib/chef/knife/bootstrap.rb +++ b/chef/lib/chef/knife/bootstrap.rb @@ -120,7 +120,7 @@ class Chef exit 1 end - config[:server_name] = @name_args.first + config[:server_name] = Array(@name_args).first $stdout.sync = true diff --git a/chef/lib/chef/knife/ec2_server_create.rb b/chef/lib/chef/knife/ec2_server_create.rb index dbfb26e5f5..3311786817 100644 --- a/chef/lib/chef/knife/ec2_server_create.rb +++ b/chef/lib/chef/knife/ec2_server_create.rb @@ -119,6 +119,7 @@ class Chef tcp_socket = TCPSocket.new(hostname, 22) readable = IO.select([tcp_socket], nil, nil, 5) if readable + Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}") yield true else @@ -179,17 +180,8 @@ class Chef print(".") until tcp_test_ssh(server.dns_name) { sleep @initial_sleep_delay ||= 10; puts("done") } - bootstrap = Chef::Knife::Bootstrap.new - bootstrap.name_args = [server.dns_name] - bootstrap.config[:run_list] = @name_args - bootstrap.config[:ssh_user] = config[:ssh_user] - bootstrap.config[:identity_file] = config[:identity_file] - bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.id - bootstrap.config[:prerelease] = config[:prerelease] - bootstrap.config[:distro] = config[:distro] - bootstrap.config[:use_sudo] = true - bootstrap.config[:template_file] = config[:template_file] - bootstrap.run + + bootstrap_for_node(server).run puts "\n" puts "#{h.color("Instance ID", :cyan)}: #{server.id}" @@ -204,6 +196,21 @@ class Chef puts "#{h.color("Private IP Address", :cyan)}: #{server.private_ip_address}" puts "#{h.color("Run List", :cyan)}: #{@name_args.join(', ')}" end + + def bootstrap_for_node(server) + bootstrap = Chef::Knife::Bootstrap.new + bootstrap.name_args = [server.dns_name] + bootstrap.config[:run_list] = @name_args + bootstrap.config[:ssh_user] = config[:ssh_user] + bootstrap.config[:identity_file] = config[:identity_file] + bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.id + bootstrap.config[:prerelease] = config[:prerelease] + bootstrap.config[:distro] = config[:distro] + bootstrap.config[:use_sudo] = true + bootstrap.config[:template_file] = config[:template_file] + bootstrap + end + end end end diff --git a/chef/spec/unit/knife/ec2_server_create_spec.rb b/chef/spec/unit/knife/ec2_server_create_spec.rb index 756825848f..f599e826e4 100644 --- a/chef/spec/unit/knife/ec2_server_create_spec.rb +++ b/chef/spec/unit/knife/ec2_server_create_spec.rb @@ -20,39 +20,36 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_hel require 'fog' describe Chef::Knife::Ec2ServerCreate do - before(:each) do + before do @knife_ec2_create = Chef::Knife::Ec2ServerCreate.new() - @knife_ec2_create.name_args = ['foo-name'] + @knife_ec2_create.name_args = ['role[base]'] @knife_ec2_create.initial_sleep_delay = 0 + @knife_ec2_create.stub!(:tcp_test_ssh).and_return(true) + + @ec2_connection = mock() + @ec2_servers = mock() + @new_ec2_server = mock() + + @ec2_server_attribs = { :id => 'i-39382318', + :flavor_id => 'm1.small', + :image_id => 'ami-47241231', + :availability_zone => 'us-west-1', + :key_name => 'my_ssh_key', + :groups => ['group1', 'group2'], + :dns_name => 'ec2-75.101.253.10.compute-1.amazonaws.com', + :ip_address => '75.101.253.10', + :private_dns_name => 'ip-10-251-75-20.ec2.internal', + :private_ip_address => '10.251.75.20' } + + @ec2_server_attribs.each_pair do |attrib, value| + @new_ec2_server.stub!(attrib).and_return(value) + end end describe "run" do before do - @ec2_connection = mock() - @ec2_servers = mock() - - @new_ec2_server = mock() - - ec2_server_attribs = { :id => 'i-39382318', - :flavor_id => 'm1.small', - :image_id => 'ami-47241231', - :availability_zone => 'us-west-1', - :key_name => 'my_ssh_key', - :groups => ['group1', 'group2'], - :dns_name => 'ec2-75.101.253.10.compute-1.amazonaws.com', - :ip_address => '75.101.253.10', - :private_dns_name => 'ip-10-251-75-20.ec2.internal', - :private_ip_address => '10.251.75.20' - } - - ec2_server_attribs.each_pair do |attrib, value| - @new_ec2_server.should_receive(attrib).at_least(:once).and_return(value) - end - @new_ec2_server.should_receive(:wait_for).and_return(true) - @ec2_servers.should_receive(:create).and_return(@new_ec2_server) - @ec2_connection.should_receive(:servers).and_return(@ec2_servers) Fog::AWS::EC2.should_receive(:new).and_return(@ec2_connection) @@ -60,44 +57,75 @@ describe Chef::Knife::Ec2ServerCreate do @knife_ec2_create.stub!(:puts) @knife_ec2_create.stub!(:print) - @bootstrap = mock() end - it "should set the bootstrap name_args to an array" do - @bootstrap.should_receive(:name_args=) do |x| - x.should be_a_kind_of(Array) - end + it "creates an EC2 instance and bootstraps it" do + @bootstrap = Chef::Knife::Bootstrap.new + Chef::Knife::Bootstrap.stub!(:new).and_return(@bootstrap) + @bootstrap.should_receive(:run) + @knife_ec2_create.run + end - Chef::Knife::Bootstrap.should_receive(:new).once.and_return(@bootstrap) + end - @bootstrap.should_receive(:config).at_least(:once).and_return({}) - @bootstrap.should_receive(:run) + describe "when configuring the bootstrap process" do + before do + @knife_ec2_create.config[:ssh_user] = "ubuntu" + @knife_ec2_create.config[:identity_file] = "~/.ssh/aws-key.pem" + @knife_ec2_create.config[:chef_node_name] = "blarf" + @knife_ec2_create.config[:template_file] = '~/.chef/templates/my-bootstrap.sh.erb' + @knife_ec2_create.config[:distro] = 'ubuntu-10.04-magic-sparkles' - @knife_ec2_create.run + @bootstrap = @knife_ec2_create.bootstrap_for_node(@new_ec2_server) end - it "should retry to bootstrap if the ssh connection is refused" do - @bootstrap.should_receive(:name_args=).twice + it "should set the bootstrap 'name argument' to the hostname of the EC2 server" do + @bootstrap.name_args.should == ['ec2-75.101.253.10.compute-1.amazonaws.com'] + end - Chef::Knife::Bootstrap.should_receive(:new).twice.and_return(@bootstrap) + it "configures sets the bootstrap's run_list" do + @bootstrap.config[:run_list].should == ['role[base]'] + end - @bootstrap.should_receive(:config).at_least(:once).and_return({}) - @bootstrap.should_receive(:run).once.and_raise(Errno::ECONNREFUSED) - @bootstrap.should_receive(:run).once + it "configures the bootstrap to use the correct ssh_user login" do + @bootstrap.config[:ssh_user].should == 'ubuntu' + end - @knife_ec2_create.run + it "configures the bootstrap to use the correct ssh identity file" do + @bootstrap.config[:identity_file].should == "~/.ssh/aws-key.pem" end - it "should retry to bootstrap if the ssh connection times out" do - @bootstrap.should_receive(:name_args=).twice + it "configures the bootstrap to use the configured node name if provided" do + @bootstrap.config[:chef_node_name].should == 'blarf' + end - Chef::Knife::Bootstrap.should_receive(:new).twice.and_return(@bootstrap) + it "configures the bootstrap to use the EC2 server id if no explicit node name is set" do + @knife_ec2_create.config[:chef_node_name] = nil - @bootstrap.should_receive(:config).at_least(:once).and_return({}) - @bootstrap.should_receive(:run).once.and_raise(Errno::ETIMEDOUT) - @bootstrap.should_receive(:run).once + bootstrap = @knife_ec2_create.bootstrap_for_node(@new_ec2_server) + bootstrap.config[:chef_node_name].should == @new_ec2_server.id + end - @knife_ec2_create.run + it "configures the bootstrap to use prerelease versions of chef if specified" do + @bootstrap.config[:prerelease].should be_false + + @knife_ec2_create.config[:prerelease] = true + + bootstrap = @knife_ec2_create.bootstrap_for_node(@new_ec2_server) + bootstrap.config[:prerelease].should be_true + end + + it "configures the bootstrap to use the desired distro-specific bootstrap script" do + @bootstrap.config[:distro].should == 'ubuntu-10.04-magic-sparkles' + end + + it "configures the bootstrap to use sudo" do + @bootstrap.config[:use_sudo].should be_true + end + + it "configured the bootstrap to use the desired template" do + @bootstrap.config[:template_file].should == '~/.chef/templates/my-bootstrap.sh.erb' end end + end |