Skip to content

Commit

Permalink
Improve speed of readiness detection for Windows instances (#582)
Browse files Browse the repository at this point in the history
* Move the fetching of the Windows admin password into the `wait_until_ready` function to speed up readiness detection of Windows instances

* Fix syntax issues

* Forgot to remove the block of code that was moved
Remove duplicate `windows_os?` check

* Adds/fixes tests
  • Loading branch information
jakauppila authored May 9, 2022
1 parent 4feff98 commit 812076f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
25 changes: 12 additions & 13 deletions lib/kitchen/driver/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,6 @@ def create(state)
wait_until_ready(server, state)
end

if windows_os? &&
instance.transport[:username] =~ /administrator/i &&
instance.transport[:password].nil?
# If we're logging into the administrator user and a password isn't
# supplied, try to fetch it from the AWS instance
fetch_windows_admin_password(server, state)
end

info("EC2 instance <#{state[:server_id]}> ready (hostname: #{state[:hostname]}).")
instance.transport.connection(state).wait_until_ready
attach_network_interface(state) unless config[:elastic_network_interface_id].nil?
Expand Down Expand Up @@ -536,12 +528,19 @@ def wait_until_ready(server, state)
state[:hostname] = hostname(aws_instance, nil)
end
if ready && windows_os?
output = server.console_output.output
unless output.nil?
output = Base64.decode64(output)
debug "Console output: --- \n#{output}"
if instance.transport[:username] =~ /administrator/i &&
instance.transport[:password].nil?
# If we're logging into the administrator user and a password isn't
# supplied, try to fetch it from the AWS instance
fetch_windows_admin_password(server, state)
else
output = server.console_output.output
unless output.nil?
output = Base64.decode64(output)
debug "Console output: --- \n#{output}"
end
ready = !!(output =~ /Windows is Ready to use/)
end
ready = !!(output =~ /Windows is Ready to use/)
end
ready
end
Expand Down
44 changes: 40 additions & 4 deletions spec/kitchen/driver/ec2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,46 @@
expect(driver.wait_until_ready(server, state)).to eq(true)
end
end

context "when windows instance" do
let(:hostname) { "windows" }

before do
expect(aws_instance).to receive(:exists?).and_return(true)
expect(aws_instance).to receive_message_chain("state.name").and_return("running")
expect(driver).to receive(:windows_os?).and_return(true)
end

context "does define a username/password" do
before do
expect(transport).to receive(:[]).with(:username).and_return("foo")
end

context "console output is ready" do
it "returns true" do
# base64 encoded `Windows is Ready to use`
expect(server).to receive_message_chain("console_output.output").and_return("V2luZG93cyBpcyBSZWFkeSB0byB1c2U=")
expect(driver.wait_until_ready(server, state)).to eq(true)
end
end

context "console output is not ready" do
it "returns false" do
expect(server).to receive_message_chain("console_output.output").and_return("")
expect(driver.wait_until_ready(server, state)).to eq(false)
end
end
end

context "does not define a username/password" do
it "returns true" do
expect(transport).to receive(:[]).with(:username).and_return("administrator")
expect(transport).to receive(:[]).with(:password).and_return(nil)
expect(driver).to receive(:fetch_windows_admin_password).with(server, state)
expect(driver.wait_until_ready(server, state)).to eq(true)
end
end
end
end

describe "#fetch_windows_admin_password" do
Expand Down Expand Up @@ -482,11 +522,7 @@

context "instance is a windows machine" do
before do
expect(driver).to receive(:windows_os?).and_return(true)
expect(transport).to receive(:[]).with(:username).and_return("administrator")
expect(transport).to receive(:[]).with(:password).and_return(nil)
expect(driver).to receive(:submit_server).and_return(server)
expect(driver).to receive(:fetch_windows_admin_password).with(server, state)
end

include_examples "common create"
Expand Down

0 comments on commit 812076f

Please sign in to comment.