knife-windows 0.8.2 → 0.8.3.rc.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/.gitignore +4 -4
- data/.travis.yml +6 -6
- data/CHANGELOG.md +47 -43
- data/DOC_CHANGES.md +23 -23
- data/Gemfile +11 -11
- data/LICENSE +201 -201
- data/README.md +161 -161
- data/RELEASE_NOTES.md +39 -62
- data/Rakefile +16 -16
- data/features/knife_help.feature +20 -20
- data/features/support/env.rb +5 -5
- data/knife-windows.gemspec +26 -26
- data/lib/chef/knife/bootstrap/windows-chef-client-msi.erb +222 -222
- data/lib/chef/knife/bootstrap_windows_base.rb +207 -206
- data/lib/chef/knife/bootstrap_windows_ssh.rb +93 -93
- data/lib/chef/knife/bootstrap_windows_winrm.rb +102 -102
- data/lib/chef/knife/core/windows_bootstrap_context.rb +249 -227
- data/lib/chef/knife/windows_helper.rb +34 -34
- data/lib/chef/knife/winrm.rb +315 -315
- data/lib/chef/knife/winrm_base.rb +99 -99
- data/lib/knife-windows/version.rb +6 -6
- data/spec/functional/bootstrap_download_spec.rb +122 -122
- data/spec/spec_helper.rb +61 -61
- data/spec/unit/knife/bootstrap_template_spec.rb +91 -91
- data/spec/unit/knife/bootstrap_windows_winrm_spec.rb +106 -106
- data/spec/unit/knife/core/windows_bootstrap_context_spec.rb +54 -0
- data/spec/unit/knife/winrm_spec.rb +219 -219
- metadata +10 -16
data/spec/spec_helper.rb
CHANGED
@@ -1,61 +1,61 @@
|
|
1
|
-
|
2
|
-
# Author:: Adam Edwards (<adamed@opscode.com>)
|
3
|
-
# Copyright:: Copyright (c) 2012 Opscode, Inc.
|
4
|
-
# License:: Apache License, Version 2.0
|
5
|
-
#
|
6
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
-
# you may not use this file except in compliance with the License.
|
8
|
-
# You may obtain a copy of the License at
|
9
|
-
#
|
10
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
-
#
|
12
|
-
# Unless required by applicable law or agreed to in writing, software
|
13
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
15
|
-
# implied.
|
16
|
-
# See the License for the specific language governing permissions and
|
17
|
-
# limitations under the License.
|
18
|
-
#
|
19
|
-
|
20
|
-
def windows?
|
21
|
-
!!(RUBY_PLATFORM =~ /mswin|mingw|windows/)
|
22
|
-
end
|
23
|
-
|
24
|
-
require_relative '../lib/chef/knife/core/windows_bootstrap_context'
|
25
|
-
require_relative '../lib/chef/knife/bootstrap_windows_winrm'
|
26
|
-
|
27
|
-
if windows?
|
28
|
-
require 'ruby-wmi'
|
29
|
-
end
|
30
|
-
|
31
|
-
def windows2012?
|
32
|
-
is_win2k12 = false
|
33
|
-
|
34
|
-
if windows?
|
35
|
-
this_operating_system = WMI::Win32_OperatingSystem.find(:first)
|
36
|
-
os_version = this_operating_system.send('Version')
|
37
|
-
|
38
|
-
# The operating system version is a string in the following form
|
39
|
-
# that can be split into components based on the '.' delimiter:
|
40
|
-
# MajorVersionNumber.MinorVersionNumber.BuildNumber
|
41
|
-
os_version_components = os_version.split('.')
|
42
|
-
|
43
|
-
if os_version_components.length < 2
|
44
|
-
raise 'WMI returned a Windows version from Win32_OperatingSystem.Version ' +
|
45
|
-
'with an unexpected format. The Windows version could not be determined.'
|
46
|
-
end
|
47
|
-
|
48
|
-
# Windows 6.2 is Windows Server 2012, so test the major and
|
49
|
-
# minor version components
|
50
|
-
is_win2k12 = os_version_components[0] == '6' && os_version_components[1] == '2'
|
51
|
-
end
|
52
|
-
|
53
|
-
is_win2k12
|
54
|
-
end
|
55
|
-
|
56
|
-
|
57
|
-
RSpec.configure do |config|
|
58
|
-
config.filter_run_excluding :windows_only => true unless windows?
|
59
|
-
config.filter_run_excluding :windows_2012_only => true unless windows2012?
|
60
|
-
end
|
61
|
-
|
1
|
+
|
2
|
+
# Author:: Adam Edwards (<adamed@opscode.com>)
|
3
|
+
# Copyright:: Copyright (c) 2012 Opscode, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
15
|
+
# implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
def windows?
|
21
|
+
!!(RUBY_PLATFORM =~ /mswin|mingw|windows/)
|
22
|
+
end
|
23
|
+
|
24
|
+
require_relative '../lib/chef/knife/core/windows_bootstrap_context'
|
25
|
+
require_relative '../lib/chef/knife/bootstrap_windows_winrm'
|
26
|
+
|
27
|
+
if windows?
|
28
|
+
require 'ruby-wmi'
|
29
|
+
end
|
30
|
+
|
31
|
+
def windows2012?
|
32
|
+
is_win2k12 = false
|
33
|
+
|
34
|
+
if windows?
|
35
|
+
this_operating_system = WMI::Win32_OperatingSystem.find(:first)
|
36
|
+
os_version = this_operating_system.send('Version')
|
37
|
+
|
38
|
+
# The operating system version is a string in the following form
|
39
|
+
# that can be split into components based on the '.' delimiter:
|
40
|
+
# MajorVersionNumber.MinorVersionNumber.BuildNumber
|
41
|
+
os_version_components = os_version.split('.')
|
42
|
+
|
43
|
+
if os_version_components.length < 2
|
44
|
+
raise 'WMI returned a Windows version from Win32_OperatingSystem.Version ' +
|
45
|
+
'with an unexpected format. The Windows version could not be determined.'
|
46
|
+
end
|
47
|
+
|
48
|
+
# Windows 6.2 is Windows Server 2012, so test the major and
|
49
|
+
# minor version components
|
50
|
+
is_win2k12 = os_version_components[0] == '6' && os_version_components[1] == '2'
|
51
|
+
end
|
52
|
+
|
53
|
+
is_win2k12
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
RSpec.configure do |config|
|
58
|
+
config.filter_run_excluding :windows_only => true unless windows?
|
59
|
+
config.filter_run_excluding :windows_2012_only => true unless windows2012?
|
60
|
+
end
|
61
|
+
|
@@ -1,91 +1,91 @@
|
|
1
|
-
#
|
2
|
-
# Author:: Chirag Jog (<chirag@clogeny.com>)
|
3
|
-
# Copyright:: Copyright (c) 2013 Chirag Jog
|
4
|
-
# License:: Apache License, Version 2.0
|
5
|
-
#
|
6
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
-
# you may not use this file except in compliance with the License.
|
8
|
-
# You may obtain a copy of the License at
|
9
|
-
#
|
10
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
-
#
|
12
|
-
# Unless required by applicable law or agreed to in writing, software
|
13
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
-
# See the License for the specific language governing permissions and
|
16
|
-
# limitations under the License.
|
17
|
-
|
18
|
-
|
19
|
-
TEMPLATE_FILE = File.expand_path(File.dirname(__FILE__)) + "/../../../lib/chef/knife/bootstrap/windows-chef-client-msi.erb"
|
20
|
-
|
21
|
-
require 'spec_helper'
|
22
|
-
|
23
|
-
describe "While Windows Bootstrapping" do
|
24
|
-
context "the default Windows bootstrapping template" do
|
25
|
-
bootstrap = Chef::Knife::BootstrapWindowsWinrm.new
|
26
|
-
bootstrap.config[:template_file] = TEMPLATE_FILE
|
27
|
-
|
28
|
-
template = bootstrap.load_template
|
29
|
-
template_file_lines = template.split('\n')
|
30
|
-
it "should download Platform specific MSI" do
|
31
|
-
download_url=template_file_lines.find {|l| l.include?("url=")}
|
32
|
-
download_url.include?("%MACHINE_OS%") && download_url.include?("%MACHINE_ARCH%")
|
33
|
-
end
|
34
|
-
it "should download specific version of MSI if supplied" do
|
35
|
-
download_url_ext= template_file_lines.find {|l| l.include?("url +=")}
|
36
|
-
download_url_ext.include?("[:bootstrap_version]")
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
describe Chef::Knife::BootstrapWindowsWinrm do
|
42
|
-
before(:all) do
|
43
|
-
@original_config = Chef::Config.hash_dup
|
44
|
-
@original_knife_config = Chef::Config[:knife].dup
|
45
|
-
end
|
46
|
-
|
47
|
-
after(:all) do
|
48
|
-
Chef::Config.configuration = @original_config
|
49
|
-
Chef::Config[:knife] = @original_knife_config
|
50
|
-
end
|
51
|
-
|
52
|
-
before(:each) do
|
53
|
-
Chef::Log.logger = Logger.new(StringIO.new)
|
54
|
-
@knife = Chef::Knife::BootstrapWindowsWinrm.new
|
55
|
-
# Merge default settings in.
|
56
|
-
@knife.merge_configs
|
57
|
-
@knife.config[:template_file] = TEMPLATE_FILE
|
58
|
-
@stdout = StringIO.new
|
59
|
-
allow(@knife.ui).to receive(:stdout).and_return(@stdout)
|
60
|
-
@stderr = StringIO.new
|
61
|
-
allow(@knife.ui).to receive(:stderr).and_return(@stderr)
|
62
|
-
end
|
63
|
-
|
64
|
-
describe "specifying no_proxy with various entries" do
|
65
|
-
subject(:knife) { described_class.new }
|
66
|
-
let(:options){ ["--bootstrap-proxy", "", "--bootstrap-no-proxy", setting] }
|
67
|
-
let(:template_file) { TEMPLATE_FILE }
|
68
|
-
let(:rendered_template) do
|
69
|
-
knife.instance_variable_set("@template_file", template_file)
|
70
|
-
knife.parse_options(options)
|
71
|
-
# Avoid referencing a validation keyfile we won't find during #render_template
|
72
|
-
template_string = knife.read_template.gsub(/^.*[Vv]alidation_key.*$/, '')
|
73
|
-
knife.render_template(template_string)
|
74
|
-
end
|
75
|
-
|
76
|
-
context "via --bootstrap-no-proxy" do
|
77
|
-
let(:setting) { "api.opscode.com" }
|
78
|
-
|
79
|
-
it "renders the client.rb with a single FQDN no_proxy entry" do
|
80
|
-
expect(rendered_template).to match(%r{.*no_proxy\s*\"api.opscode.com\".*})
|
81
|
-
end
|
82
|
-
end
|
83
|
-
context "via --bootstrap-no-proxy multiple" do
|
84
|
-
let(:setting) { "api.opscode.com,172.16.10.*" }
|
85
|
-
|
86
|
-
it "renders the client.rb with comma-separated FQDN and wildcard IP address no_proxy entries" do
|
87
|
-
expect(rendered_template).to match(%r{.*no_proxy\s*"api.opscode.com,172.16.10.\*".*})
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
1
|
+
#
|
2
|
+
# Author:: Chirag Jog (<chirag@clogeny.com>)
|
3
|
+
# Copyright:: Copyright (c) 2013 Chirag Jog
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
|
18
|
+
|
19
|
+
TEMPLATE_FILE = File.expand_path(File.dirname(__FILE__)) + "/../../../lib/chef/knife/bootstrap/windows-chef-client-msi.erb"
|
20
|
+
|
21
|
+
require 'spec_helper'
|
22
|
+
|
23
|
+
describe "While Windows Bootstrapping" do
|
24
|
+
context "the default Windows bootstrapping template" do
|
25
|
+
bootstrap = Chef::Knife::BootstrapWindowsWinrm.new
|
26
|
+
bootstrap.config[:template_file] = TEMPLATE_FILE
|
27
|
+
|
28
|
+
template = bootstrap.load_template
|
29
|
+
template_file_lines = template.split('\n')
|
30
|
+
it "should download Platform specific MSI" do
|
31
|
+
download_url=template_file_lines.find {|l| l.include?("url=")}
|
32
|
+
download_url.include?("%MACHINE_OS%") && download_url.include?("%MACHINE_ARCH%")
|
33
|
+
end
|
34
|
+
it "should download specific version of MSI if supplied" do
|
35
|
+
download_url_ext= template_file_lines.find {|l| l.include?("url +=")}
|
36
|
+
download_url_ext.include?("[:bootstrap_version]")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe Chef::Knife::BootstrapWindowsWinrm do
|
42
|
+
before(:all) do
|
43
|
+
@original_config = Chef::Config.hash_dup
|
44
|
+
@original_knife_config = Chef::Config[:knife].dup
|
45
|
+
end
|
46
|
+
|
47
|
+
after(:all) do
|
48
|
+
Chef::Config.configuration = @original_config
|
49
|
+
Chef::Config[:knife] = @original_knife_config
|
50
|
+
end
|
51
|
+
|
52
|
+
before(:each) do
|
53
|
+
Chef::Log.logger = Logger.new(StringIO.new)
|
54
|
+
@knife = Chef::Knife::BootstrapWindowsWinrm.new
|
55
|
+
# Merge default settings in.
|
56
|
+
@knife.merge_configs
|
57
|
+
@knife.config[:template_file] = TEMPLATE_FILE
|
58
|
+
@stdout = StringIO.new
|
59
|
+
allow(@knife.ui).to receive(:stdout).and_return(@stdout)
|
60
|
+
@stderr = StringIO.new
|
61
|
+
allow(@knife.ui).to receive(:stderr).and_return(@stderr)
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "specifying no_proxy with various entries" do
|
65
|
+
subject(:knife) { described_class.new }
|
66
|
+
let(:options){ ["--bootstrap-proxy", "", "--bootstrap-no-proxy", setting] }
|
67
|
+
let(:template_file) { TEMPLATE_FILE }
|
68
|
+
let(:rendered_template) do
|
69
|
+
knife.instance_variable_set("@template_file", template_file)
|
70
|
+
knife.parse_options(options)
|
71
|
+
# Avoid referencing a validation keyfile we won't find during #render_template
|
72
|
+
template_string = knife.read_template.gsub(/^.*[Vv]alidation_key.*$/, '')
|
73
|
+
knife.render_template(template_string)
|
74
|
+
end
|
75
|
+
|
76
|
+
context "via --bootstrap-no-proxy" do
|
77
|
+
let(:setting) { "api.opscode.com" }
|
78
|
+
|
79
|
+
it "renders the client.rb with a single FQDN no_proxy entry" do
|
80
|
+
expect(rendered_template).to match(%r{.*no_proxy\s*\"api.opscode.com\".*})
|
81
|
+
end
|
82
|
+
end
|
83
|
+
context "via --bootstrap-no-proxy multiple" do
|
84
|
+
let(:setting) { "api.opscode.com,172.16.10.*" }
|
85
|
+
|
86
|
+
it "renders the client.rb with comma-separated FQDN and wildcard IP address no_proxy entries" do
|
87
|
+
expect(rendered_template).to match(%r{.*no_proxy\s*"api.opscode.com,172.16.10.\*".*})
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -1,106 +1,106 @@
|
|
1
|
-
#
|
2
|
-
# Author:: Adam Edwards(<adamed@getchef.com>)
|
3
|
-
# Copyright:: Copyright (c) 2014 Chef Software, Inc.
|
4
|
-
# License:: Apache License, Version 2.0
|
5
|
-
#
|
6
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
-
# you may not use this file except in compliance with the License.
|
8
|
-
# You may obtain a copy of the License at
|
9
|
-
#
|
10
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
-
#
|
12
|
-
# Unless required by applicable law or agreed to in writing, software
|
13
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
-
# See the License for the specific language governing permissions and
|
16
|
-
# limitations under the License.
|
17
|
-
#
|
18
|
-
|
19
|
-
require 'spec_helper'
|
20
|
-
|
21
|
-
|
22
|
-
describe Chef::Knife::BootstrapWindowsWinrm do
|
23
|
-
before(:all) do
|
24
|
-
Chef::Config.reset
|
25
|
-
end
|
26
|
-
|
27
|
-
before do
|
28
|
-
# Kernel.stub(:sleep).and_return 10
|
29
|
-
allow(bootstrap).to receive(:sleep).and_return(10)
|
30
|
-
end
|
31
|
-
|
32
|
-
after do
|
33
|
-
# Kernel.unstub(:sleep)
|
34
|
-
allow(bootstrap).to receive(:sleep).and_return(10)
|
35
|
-
end
|
36
|
-
|
37
|
-
let (:bootstrap) { Chef::Knife::BootstrapWindowsWinrm.new(['winrm', '-d', 'windows-chef-client-msi', '-x', 'Administrator', 'localhost']) }
|
38
|
-
|
39
|
-
let(:initial_fail_count) { 4 }
|
40
|
-
it 'should retry if a 401 is received from WinRM' do
|
41
|
-
call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '401'}}
|
42
|
-
call_result_sequence.push(0)
|
43
|
-
allow(bootstrap).to receive(:run_command).and_return(*call_result_sequence)
|
44
|
-
|
45
|
-
expect(bootstrap).to receive(:run_command).exactly(call_result_sequence.length).times
|
46
|
-
bootstrap.send(:wait_for_remote_response, 2)
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'should retry if something other than a 401 is received from WinRM' do
|
50
|
-
call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '500'}}
|
51
|
-
call_result_sequence.push(0)
|
52
|
-
allow(bootstrap).to receive(:run_command).and_return(*call_result_sequence)
|
53
|
-
|
54
|
-
expect(bootstrap).to receive(:run_command).exactly(call_result_sequence.length).times
|
55
|
-
bootstrap.send(:wait_for_remote_response, 2)
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'should have a wait timeout of 2 minutes by default' do
|
59
|
-
allow(bootstrap).to receive(:run_command).and_raise(WinRM::WinRMHTTPTransportError)
|
60
|
-
allow(bootstrap).to receive(:create_bootstrap_bat_command).and_raise(SystemExit)
|
61
|
-
expect(bootstrap).to receive(:wait_for_remote_response).with(2)
|
62
|
-
allow(bootstrap).to receive(:validate_name_args!).and_return(nil)
|
63
|
-
bootstrap.config[:auth_timeout] = bootstrap.options[:auth_timeout][:default]
|
64
|
-
expect { bootstrap.bootstrap }.to raise_error(SystemExit)
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'should keep retrying at 10s intervals if the timeout in minutes has not elapsed' do
|
68
|
-
call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '500'}}
|
69
|
-
call_result_sequence.push(0)
|
70
|
-
allow(bootstrap).to receive(:run_command).and_return(*call_result_sequence)
|
71
|
-
|
72
|
-
expect(bootstrap).to receive(:run_command).exactly(call_result_sequence.length).times
|
73
|
-
bootstrap.send(:wait_for_remote_response, 2)
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should exit bootstrap with non-zero status if the bootstrap fails" do
|
77
|
-
command_status = 1
|
78
|
-
|
79
|
-
#Stub out calls to create the session and just get the exit codes back
|
80
|
-
winrm_mock = Chef::Knife::Winrm.new
|
81
|
-
allow(Chef::Knife::Winrm).to receive(:new).and_return(winrm_mock)
|
82
|
-
allow(winrm_mock).to receive(:configure_session)
|
83
|
-
allow(winrm_mock).to receive(:winrm_command)
|
84
|
-
session_mock = EventMachine::WinRM::Session.new
|
85
|
-
allow(EventMachine::WinRM::Session).to receive(:new).and_return(session_mock)
|
86
|
-
allow(session_mock).to receive(:exit_codes).and_return({"thishost" => command_status})
|
87
|
-
|
88
|
-
#Skip over templating stuff and checking with the remote end
|
89
|
-
allow(bootstrap).to receive(:create_bootstrap_bat_command)
|
90
|
-
allow(bootstrap).to receive(:wait_for_remote_response)
|
91
|
-
|
92
|
-
expect { bootstrap.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| expect(e.status).to eq(command_status) }
|
93
|
-
end
|
94
|
-
|
95
|
-
|
96
|
-
it 'should stop retrying if more than 2 minutes has elapsed' do
|
97
|
-
times = [ Time.new(2014, 4, 1, 22, 25), Time.new(2014, 4, 1, 22, 51), Time.new(2014, 4, 1, 22, 28) ]
|
98
|
-
allow(Time).to receive(:now).and_return(*times)
|
99
|
-
run_command_result = lambda {raise WinRM::WinRMHTTPTransportError, '401'}
|
100
|
-
allow(bootstrap).to receive(:validate_name_args!).and_return(nil)
|
101
|
-
allow(bootstrap).to receive(:run_command).and_return(run_command_result)
|
102
|
-
expect(bootstrap).to receive(:run_command).exactly(1).times
|
103
|
-
bootstrap.config[:auth_timeout] = bootstrap.options[:auth_timeout][:default]
|
104
|
-
expect { bootstrap.bootstrap }.to raise_error RuntimeError
|
105
|
-
end
|
106
|
-
end
|
1
|
+
#
|
2
|
+
# Author:: Adam Edwards(<adamed@getchef.com>)
|
3
|
+
# Copyright:: Copyright (c) 2014 Chef Software, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'spec_helper'
|
20
|
+
|
21
|
+
|
22
|
+
describe Chef::Knife::BootstrapWindowsWinrm do
|
23
|
+
before(:all) do
|
24
|
+
Chef::Config.reset
|
25
|
+
end
|
26
|
+
|
27
|
+
before do
|
28
|
+
# Kernel.stub(:sleep).and_return 10
|
29
|
+
allow(bootstrap).to receive(:sleep).and_return(10)
|
30
|
+
end
|
31
|
+
|
32
|
+
after do
|
33
|
+
# Kernel.unstub(:sleep)
|
34
|
+
allow(bootstrap).to receive(:sleep).and_return(10)
|
35
|
+
end
|
36
|
+
|
37
|
+
let (:bootstrap) { Chef::Knife::BootstrapWindowsWinrm.new(['winrm', '-d', 'windows-chef-client-msi', '-x', 'Administrator', 'localhost']) }
|
38
|
+
|
39
|
+
let(:initial_fail_count) { 4 }
|
40
|
+
it 'should retry if a 401 is received from WinRM' do
|
41
|
+
call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '401'}}
|
42
|
+
call_result_sequence.push(0)
|
43
|
+
allow(bootstrap).to receive(:run_command).and_return(*call_result_sequence)
|
44
|
+
|
45
|
+
expect(bootstrap).to receive(:run_command).exactly(call_result_sequence.length).times
|
46
|
+
bootstrap.send(:wait_for_remote_response, 2)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should retry if something other than a 401 is received from WinRM' do
|
50
|
+
call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '500'}}
|
51
|
+
call_result_sequence.push(0)
|
52
|
+
allow(bootstrap).to receive(:run_command).and_return(*call_result_sequence)
|
53
|
+
|
54
|
+
expect(bootstrap).to receive(:run_command).exactly(call_result_sequence.length).times
|
55
|
+
bootstrap.send(:wait_for_remote_response, 2)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should have a wait timeout of 2 minutes by default' do
|
59
|
+
allow(bootstrap).to receive(:run_command).and_raise(WinRM::WinRMHTTPTransportError)
|
60
|
+
allow(bootstrap).to receive(:create_bootstrap_bat_command).and_raise(SystemExit)
|
61
|
+
expect(bootstrap).to receive(:wait_for_remote_response).with(2)
|
62
|
+
allow(bootstrap).to receive(:validate_name_args!).and_return(nil)
|
63
|
+
bootstrap.config[:auth_timeout] = bootstrap.options[:auth_timeout][:default]
|
64
|
+
expect { bootstrap.bootstrap }.to raise_error(SystemExit)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should keep retrying at 10s intervals if the timeout in minutes has not elapsed' do
|
68
|
+
call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '500'}}
|
69
|
+
call_result_sequence.push(0)
|
70
|
+
allow(bootstrap).to receive(:run_command).and_return(*call_result_sequence)
|
71
|
+
|
72
|
+
expect(bootstrap).to receive(:run_command).exactly(call_result_sequence.length).times
|
73
|
+
bootstrap.send(:wait_for_remote_response, 2)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should exit bootstrap with non-zero status if the bootstrap fails" do
|
77
|
+
command_status = 1
|
78
|
+
|
79
|
+
#Stub out calls to create the session and just get the exit codes back
|
80
|
+
winrm_mock = Chef::Knife::Winrm.new
|
81
|
+
allow(Chef::Knife::Winrm).to receive(:new).and_return(winrm_mock)
|
82
|
+
allow(winrm_mock).to receive(:configure_session)
|
83
|
+
allow(winrm_mock).to receive(:winrm_command)
|
84
|
+
session_mock = EventMachine::WinRM::Session.new
|
85
|
+
allow(EventMachine::WinRM::Session).to receive(:new).and_return(session_mock)
|
86
|
+
allow(session_mock).to receive(:exit_codes).and_return({"thishost" => command_status})
|
87
|
+
|
88
|
+
#Skip over templating stuff and checking with the remote end
|
89
|
+
allow(bootstrap).to receive(:create_bootstrap_bat_command)
|
90
|
+
allow(bootstrap).to receive(:wait_for_remote_response)
|
91
|
+
|
92
|
+
expect { bootstrap.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| expect(e.status).to eq(command_status) }
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
it 'should stop retrying if more than 2 minutes has elapsed' do
|
97
|
+
times = [ Time.new(2014, 4, 1, 22, 25), Time.new(2014, 4, 1, 22, 51), Time.new(2014, 4, 1, 22, 28) ]
|
98
|
+
allow(Time).to receive(:now).and_return(*times)
|
99
|
+
run_command_result = lambda {raise WinRM::WinRMHTTPTransportError, '401'}
|
100
|
+
allow(bootstrap).to receive(:validate_name_args!).and_return(nil)
|
101
|
+
allow(bootstrap).to receive(:run_command).and_return(run_command_result)
|
102
|
+
expect(bootstrap).to receive(:run_command).exactly(1).times
|
103
|
+
bootstrap.config[:auth_timeout] = bootstrap.options[:auth_timeout][:default]
|
104
|
+
expect { bootstrap.bootstrap }.to raise_error RuntimeError
|
105
|
+
end
|
106
|
+
end
|