knife-server 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.cane +1 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +3 -0
  5. data/.travis.yml +12 -8
  6. data/CHANGELOG.md +32 -1
  7. data/Gemfile +9 -4
  8. data/Guardfile +28 -0
  9. data/README.md +28 -5
  10. data/Rakefile +31 -10
  11. data/knife-server.gemspec +18 -8
  12. data/lib/chef/knife/bootstrap/_omnibus.sh +63 -10
  13. data/lib/chef/knife/bootstrap/chef10/rhel.erb +2 -0
  14. data/lib/chef/knife/bootstrap/chef11/omnibus.erb +4 -1
  15. data/lib/chef/knife/bootstrap/chef11/rhel.erb +2 -0
  16. data/lib/chef/knife/server_backup.rb +24 -10
  17. data/lib/chef/knife/server_bootstrap_base.rb +68 -23
  18. data/lib/chef/knife/server_bootstrap_ec2.rb +33 -20
  19. data/lib/chef/knife/server_bootstrap_linode.rb +20 -13
  20. data/lib/chef/knife/server_bootstrap_openstack.rb +128 -0
  21. data/lib/chef/knife/server_bootstrap_standalone.rb +28 -16
  22. data/lib/chef/knife/server_restore.rb +23 -9
  23. data/lib/knife-server.rb +1 -0
  24. data/lib/knife/server/credentials.rb +78 -42
  25. data/lib/knife/server/ec2_security_group.rb +24 -21
  26. data/lib/knife/server/ssh.rb +54 -18
  27. data/lib/knife/server/version.rb +2 -1
  28. data/spec/chef/knife/server_backup_spec.rb +58 -44
  29. data/spec/chef/knife/server_bootstrap_ec2_spec.rb +108 -80
  30. data/spec/chef/knife/server_bootstrap_linode_spec.rb +93 -64
  31. data/spec/chef/knife/server_bootstrap_openstack_spec.rb +305 -0
  32. data/spec/chef/knife/server_bootstrap_standalone_spec.rb +113 -76
  33. data/spec/chef/knife/server_restore_spec.rb +38 -37
  34. data/spec/knife/server/credientials_spec.rb +248 -51
  35. data/spec/knife/server/ec2_security_group_spec.rb +76 -68
  36. data/spec/knife/server/ssh_spec.rb +138 -22
  37. metadata +107 -31
@@ -0,0 +1,305 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: John Bellone (<jbellone@bloomberg.net>)
4
+ # Copyright:: Copyright (c) 2014 Bloomberg Finance L.P.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require "chef/knife/server_bootstrap_openstack"
21
+ require "chef/knife/ssh"
22
+ require "fakefs/spec_helpers"
23
+ require "net/ssh"
24
+ Chef::Knife::ServerBootstrapOpenstack.load_deps
25
+
26
+ describe Chef::Knife::ServerBootstrapOpenstack do
27
+ include FakeFS::SpecHelpers
28
+
29
+ before do
30
+ Chef::Log.logger = Logger.new(StringIO.new)
31
+ @knife = Chef::Knife::ServerBootstrapOpenstack.new
32
+ @stdout = StringIO.new
33
+ allow(@knife.ui).to receive(:stdout).and_return(@stdout)
34
+ @stderr = StringIO.new
35
+ allow(@knife.ui).to receive(:stderr).and_return(@stderr)
36
+ @knife.config[:chef_node_name] = "yakky"
37
+ @knife.config[:platform] = "omnibus"
38
+ @knife.config[:ssh_user] = "root"
39
+ end
40
+
41
+ let(:connection) { double(Fog::Compute::AWS) }
42
+
43
+ describe "#openstack_bootstrap" do
44
+
45
+ before do
46
+ @knife.config[:chef_node_name] = "shave.yak"
47
+ @knife.config[:ssh_user] = "jdoe"
48
+ @knife.config[:identity_file] = "~/.ssh/mykey_dsa"
49
+ @knife.config[:openstack_password] = "openstack123"
50
+ @knife.config[:distro] = "distro-praha"
51
+ @knife.config[:webui_password] = "daweb"
52
+ @knife.config[:amqp_password] = "queueitup"
53
+
54
+ ENV["_SPEC_WEBUI_PASSWORD"] = ENV["WEBUI_PASSWORD"]
55
+ ENV["_SPEC_AMQP_PASSWORD"] = ENV["AMQP_PASSWORD"]
56
+ end
57
+
58
+ after do
59
+ ENV["WEBUI_PASSWORD"] = ENV.delete("_SPEC_WEBUI_PASSWORD")
60
+ ENV["AMQP_PASSWORD"] = ENV.delete("_SPEC_AMQP_PASSWORD")
61
+ end
62
+
63
+ let(:bootstrap) { @knife.openstack_bootstrap }
64
+
65
+ it "returns a OpenstackServerCreate instance" do
66
+ expect(bootstrap).to be_a(Chef::Knife::OpenstackServerCreate)
67
+ end
68
+
69
+ it "configs the bootstrap's chef_node_name" do
70
+ expect(bootstrap.config[:chef_node_name]).to eq("shave.yak")
71
+ end
72
+
73
+ it "configs the bootstrap's ssh_user" do
74
+ expect(bootstrap.config[:ssh_user]).to eq("jdoe")
75
+ end
76
+
77
+ it "configs the bootstrap's identity_file" do
78
+ expect(bootstrap.config[:identity_file]).to eq("~/.ssh/mykey_dsa")
79
+ end
80
+
81
+ it "configs the bootstrap's openstack_password" do
82
+ expect(bootstrap.config[:openstack_password]).to eq("openstack123")
83
+ end
84
+
85
+ it "configs the bootstrap's distro" do
86
+ expect(bootstrap.config[:distro]).to eq("distro-praha")
87
+ end
88
+
89
+ it "configs the bootstrap's distro to chef11/omnibus by default" do
90
+ @knife.config.delete(:distro)
91
+
92
+ expect(bootstrap.config[:distro]).to eq("chef11/omnibus")
93
+ end
94
+
95
+ it "configs the bootstrap's distro value driven off platform value" do
96
+ @knife.config.delete(:distro)
97
+ @knife.config[:platform] = "freebsd"
98
+
99
+ expect(bootstrap.config[:distro]).to eq("chef11/freebsd")
100
+ end
101
+
102
+ it "configs bootstrap's distro based on bootstrap_version and platform" do
103
+ @knife.config.delete(:distro)
104
+ @knife.config[:platform] = "freebsd"
105
+ @knife.config[:bootstrap_version] = "10"
106
+
107
+ expect(bootstrap.config[:distro]).to eq("chef10/freebsd")
108
+ end
109
+
110
+ it "configs the bootstrap's ENV with the webui password" do
111
+ bootstrap
112
+
113
+ expect(ENV["WEBUI_PASSWORD"]).to eq("daweb")
114
+ end
115
+
116
+ it "configs the bootstrap's ENV with the amqp password" do
117
+ bootstrap
118
+
119
+ expect(ENV["AMQP_PASSWORD"]).to eq("queueitup")
120
+ end
121
+ end
122
+
123
+ describe "#openstack_connection" do
124
+
125
+ before do
126
+ @before_config = Hash.new
127
+ @before_config[:knife] = Chef::Config[:knife]
128
+
129
+ Chef::Config[:knife] = {
130
+ :openstack_username => "jbellone",
131
+ :openstack_password => "key",
132
+ :openstack_auth_url => "http://0.0.0.0",
133
+ :openstack_tenant => "slumlord",
134
+ :openstack_region => "newyork"
135
+ }
136
+ end
137
+
138
+ after do
139
+ Chef::Config[:knife] = @before_config[:knife]
140
+ end
141
+
142
+ it "constructs a connection" do
143
+ expect(Fog::Compute).to receive(:new).with(
144
+ :provider => :openstack,
145
+ :openstack_username => "jbellone",
146
+ :openstack_password => "key",
147
+ :openstack_auth_url => "http://0.0.0.0",
148
+ :openstack_tenant => "slumlord",
149
+ :openstack_region => "newyork"
150
+ )
151
+
152
+ @knife.openstack_connection
153
+ end
154
+ end
155
+
156
+ describe "#server_ip_address" do
157
+
158
+ before do
159
+ @knife.config[:openstack_node_name] = "yak"
160
+ allow(@knife).to receive(:openstack_connection) { connection }
161
+ end
162
+
163
+ context "when server is found" do
164
+
165
+ before do
166
+ allow(connection).to receive(:servers) { [server] }
167
+ end
168
+
169
+ let(:server) do
170
+ double(
171
+ :name => "yak",
172
+ :status => 1,
173
+ :public_ip_address => "10.11.12.13"
174
+ )
175
+ end
176
+
177
+ it "returns the provisioned ip address" do
178
+ expect(@knife.server_ip_address).to eq("10.11.12.13")
179
+ end
180
+
181
+ it "ignores terminated instances" do
182
+ allow(server).to receive(:status) { 0 }
183
+
184
+ expect(@knife.server_ip_address).to be_nil
185
+ end
186
+ end
187
+
188
+ context "when server is not found" do
189
+ before do
190
+ allow(connection).to receive(:servers) { [] }
191
+ end
192
+
193
+ it "returns nil" do
194
+ expect(@knife.server_ip_address).to be_nil
195
+ end
196
+ end
197
+ end
198
+
199
+ describe "#run" do
200
+
201
+ before do
202
+ @before_config = Hash.new
203
+ [:node_name, :client_key].each do |attr|
204
+ @before_config[attr] = Chef::Config[attr]
205
+ end
206
+ Chef::Config[:node_name] = "smithers"
207
+ Chef::Config[:client_key] = "/var/tmp/myclientkey.pem"
208
+
209
+ @knife.config[:validation_key] = "/var/tmp/validation.pem"
210
+ @knife.config[:identity_file] = "~/.ssh/mykey_dsa"
211
+ @knife.config[:ssh_password] = "booboo"
212
+ allow(@knife).to receive(:openstack_connection) { connection }
213
+ allow(@knife).to receive(:server_ip_address) { "11.11.11.13" }
214
+ allow(Chef::Knife::OpenstackServerCreate).to receive(:new) { bootstrap }
215
+ allow(Knife::Server::SSH).to receive(:new) { ssh }
216
+ allow(Knife::Server::Credentials).to receive(:new) { credentials }
217
+ allow(credentials).to receive(:install_validation_key)
218
+ allow(credentials).to receive(:create_root_client)
219
+ end
220
+
221
+ after do
222
+ [:node_name, :client_key].each do |attr|
223
+ Chef::Config[attr] = @before_config[attr]
224
+ end
225
+ end
226
+
227
+ let(:bootstrap) { double(:run => true, :config => Hash.new) }
228
+ let(:ssh) { double }
229
+ let(:credentials) { double.as_null_object }
230
+
231
+ it "exits if Chef::Config[:node_name] is missing" do
232
+ Chef::Config[:node_name] = nil
233
+
234
+ expect { @knife.run }.to raise_error SystemExit
235
+ end
236
+
237
+ it "exits if Chef::Config[:client_key] is missing" do
238
+ Chef::Config[:client_key] = nil
239
+
240
+ expect { @knife.run }.to raise_error SystemExit
241
+ end
242
+
243
+ it "exits if node_name option is missing" do
244
+ @knife.config.delete(:chef_node_name)
245
+
246
+ expect { @knife.run }.to raise_error(SystemExit)
247
+ end
248
+
249
+ it "exits if platform is set to auto" do
250
+ @knife.config[:platform] = "auto"
251
+
252
+ expect { @knife.run }.to raise_error(SystemExit)
253
+ end
254
+
255
+ it "bootstraps a openstack server" do
256
+ expect(bootstrap).to receive(:run)
257
+
258
+ @knife.run
259
+ end
260
+
261
+ it "installs a new validation.pem key from the chef 10 server" do
262
+ @knife.config[:bootstrap_version] = "10"
263
+ expect(Knife::Server::SSH).to receive(:new).with(
264
+ :host => "11.11.11.13",
265
+ :user => "root",
266
+ :port => "22",
267
+ :keys => ["~/.ssh/mykey_dsa"],
268
+ :password => "booboo"
269
+ )
270
+ expect(Knife::Server::Credentials).to receive(:new).
271
+ with(ssh, "/etc/chef/validation.pem", {})
272
+ expect(credentials).to receive(:install_validation_key)
273
+
274
+ @knife.run
275
+ end
276
+
277
+ it "installs a new validation.pem key from the omnibus server" do
278
+ expect(Knife::Server::SSH).to receive(:new).with(
279
+ :host => "11.11.11.13",
280
+ :user => "root",
281
+ :port => "22",
282
+ :keys => ["~/.ssh/mykey_dsa"],
283
+ :password => "booboo"
284
+ )
285
+ expect(Knife::Server::Credentials).to receive(:new).
286
+ with(ssh, "/etc/chef/validation.pem", :omnibus => true)
287
+ expect(credentials).to receive(:install_validation_key)
288
+
289
+ @knife.run
290
+ end
291
+
292
+ it "create a root client key" do
293
+ expect(credentials).to receive(:create_root_client)
294
+
295
+ @knife.run
296
+ end
297
+
298
+ it "installs a client key" do
299
+ expect(credentials).to receive(:install_client_key).
300
+ with("smithers", "/var/tmp/myclientkey.pem")
301
+
302
+ @knife.run
303
+ end
304
+ end
305
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
1
2
  #
2
3
  # Author:: Fletcher Nichol (<fnichol@nichol.ca>)
3
4
  # Copyright:: Copyright (c) 2012 Fletcher Nichol
@@ -16,10 +17,10 @@
16
17
  # limitations under the License.
17
18
  #
18
19
 
19
- require 'chef/knife/server_bootstrap_standalone'
20
- require 'chef/knife/ssh'
21
- require 'fakefs/spec_helpers'
22
- require 'net/ssh'
20
+ require "chef/knife/server_bootstrap_standalone"
21
+ require "chef/knife/ssh"
22
+ require "fakefs/spec_helpers"
23
+ require "net/ssh"
23
24
  Chef::Knife::ServerBootstrapStandalone.load_deps
24
25
 
25
26
  describe Chef::Knife::ServerBootstrapStandalone do
@@ -29,9 +30,9 @@ describe Chef::Knife::ServerBootstrapStandalone do
29
30
  Chef::Log.logger = Logger.new(StringIO.new)
30
31
  @knife = Chef::Knife::ServerBootstrapStandalone.new
31
32
  @stdout = StringIO.new
32
- @knife.ui.stub!(:stdout).and_return(@stdout)
33
+ allow(@knife.ui).to receive(:stdout).and_return(@stdout)
33
34
  @stderr = StringIO.new
34
- @knife.ui.stub!(:stderr).and_return(@stderr)
35
+ allow(@knife.ui).to receive(:stderr).and_return(@stderr)
35
36
  @knife.config[:chef_node_name] = "yakky"
36
37
  @knife.config[:ssh_user] = "root"
37
38
  end
@@ -40,24 +41,26 @@ describe Chef::Knife::ServerBootstrapStandalone do
40
41
 
41
42
  before do
42
43
  @knife.config[:bootstrap_version] = "10"
43
- @knife.stub(:determine_platform) { @knife.send(:distro_auto_map, "debian", "6") }
44
+ allow(@knife).to receive(:determine_platform) do
45
+ @knife.send(:distro_auto_map, "debian", "6")
46
+ end
44
47
  @knife.config[:platform] = "auto"
45
48
  end
46
49
 
47
50
  it "should auto-select from determine_platform by default" do
48
51
  @knife.config.delete(:distro)
49
- @knife.send(:bootstrap_distro).should eq("chef10/debian")
50
- @knife.stub(:determine_platform) { "chef10/rhel" }
51
- @knife.send(:bootstrap_distro).should eq("chef10/rhel")
52
+ expect(@knife.send(:bootstrap_distro)).to eq("chef10/debian")
53
+ allow(@knife).to receive(:determine_platform) { "chef10/rhel" }
54
+ expect(@knife.send(:bootstrap_distro)).to eq("chef10/rhel")
52
55
  end
53
56
 
54
- it "should construct the distro path based on the chef server version and platform" do
55
- @knife.send(:construct_distro, "rhel").should eq("chef10/rhel")
57
+ it "constructs the distro path based on chef server version and platform" do
58
+ expect(@knife.send(:construct_distro, "rhel")).to eq("chef10/rhel")
56
59
  @knife.config[:bootstrap_version] = "11"
57
- @knife.send(:construct_distro, "rhel").should eq("chef11/rhel")
60
+ expect(@knife.send(:construct_distro, "rhel")).to eq("chef11/rhel")
58
61
  end
59
62
 
60
- it "should map the distro template based on a tuple of (platform, platform_version)" do
63
+ it "maps the distro template based on a platform/platform_version tuple" do
61
64
  {
62
65
  "el" => "rhel",
63
66
  "redhat" => "rhel",
@@ -69,9 +72,9 @@ describe Chef::Knife::ServerBootstrapStandalone do
69
72
  "suse" => "suse"
70
73
  }.each do |key, value|
71
74
  @knife.config[:bootstrap_version] = "10"
72
- @knife.send(:distro_auto_map, key, 0).should eq("chef10/#{value}")
75
+ expect(@knife.send(:distro_auto_map, key, 0)).to eq("chef10/#{value}")
73
76
  @knife.config[:bootstrap_version] = "11"
74
- @knife.send(:distro_auto_map, key, 0).should eq("chef11/#{value}")
77
+ expect(@knife.send(:distro_auto_map, key, 0)).to eq("chef11/#{value}")
75
78
  end
76
79
  end
77
80
  end
@@ -85,114 +88,121 @@ describe Chef::Knife::ServerBootstrapStandalone do
85
88
  @knife.config[:ssh_password] = "nevereverguess"
86
89
  @knife.config[:ssh_port] = "2222"
87
90
  @knife.config[:identity_file] = "~/.ssh/mykey_dsa"
88
- @knife.config[:security_groups] = %w{x y z}
89
- @knife.config[:tags] = %w{tag1=val1 tag2=val2}
91
+ @knife.config[:security_groups] = %w[x y z]
92
+ @knife.config[:tags] = %w[tag1=val1 tag2=val2]
90
93
  @knife.config[:distro] = "distro-praha"
91
94
  @knife.config[:ebs_size] = "42"
92
95
  @knife.config[:webui_password] = "daweb"
93
96
  @knife.config[:amqp_password] = "queueitup"
94
97
 
95
- ENV['_SPEC_WEBUI_PASSWORD'] = ENV['WEBUI_PASSWORD']
96
- ENV['_SPEC_AMQP_PASSWORD'] = ENV['AMQP_PASSWORD']
98
+ ENV["_SPEC_WEBUI_PASSWORD"] = ENV["WEBUI_PASSWORD"]
99
+ ENV["_SPEC_AMQP_PASSWORD"] = ENV["AMQP_PASSWORD"]
97
100
  end
98
101
 
99
102
  after do
100
- ENV['WEBUI_PASSWORD'] = ENV.delete('_SPEC_WEBUI_PASSWORD')
101
- ENV['AMQP_PASSWORD'] = ENV.delete('_SPEC_AMQP_PASSWORD')
103
+ ENV["WEBUI_PASSWORD"] = ENV.delete("_SPEC_WEBUI_PASSWORD")
104
+ ENV["AMQP_PASSWORD"] = ENV.delete("_SPEC_AMQP_PASSWORD")
102
105
  end
103
106
 
104
107
  let(:bootstrap) { @knife.standalone_bootstrap }
105
108
 
106
109
  it "returns a Bootstrap instance" do
107
- bootstrap.should be_a(Chef::Knife::Bootstrap)
110
+ expect(bootstrap).to be_a(Chef::Knife::Bootstrap)
108
111
  end
109
112
 
110
113
  it "copies our UI object to the bootstrap object" do
111
- bootstrap.ui.object_id.should eq(@knife.ui.object_id)
114
+ expect(bootstrap.ui.object_id).to eq(@knife.ui.object_id)
112
115
  end
113
116
 
114
117
  it "sets NO_TEST in the environment when the option is provided" do
115
118
  @knife.config[:no_test] = true
116
- bootstrap.should_not be_nil
117
- ENV["NO_TEST"].should eq("1")
119
+
120
+ expect(bootstrap).to_not be_nil
121
+ expect(ENV["NO_TEST"]).to eq("1")
122
+
118
123
  ENV.delete("NO_TEST")
119
124
  end
120
125
 
121
126
  it "configs the bootstrap's chef_node_name" do
122
- bootstrap.config[:chef_node_name].should eq("shave.yak")
127
+ expect(bootstrap.config[:chef_node_name]).to eq("shave.yak")
123
128
  end
124
129
 
125
130
  it "configs the bootstrap's ssh_user" do
126
- bootstrap.config[:ssh_user].should eq("jdoe")
131
+ expect(bootstrap.config[:ssh_user]).to eq("jdoe")
127
132
  end
128
133
 
129
134
  it "configs the bootstrap's ssh_password" do
130
- bootstrap.config[:ssh_password].should eq("nevereverguess")
135
+ expect(bootstrap.config[:ssh_password]).to eq("nevereverguess")
131
136
  end
132
137
 
133
138
  it "does not config the bootstrap's ssh_password if not given" do
134
139
  @knife.config.delete(:ssh_password)
135
140
 
136
- bootstrap.config[:ssh_password].should be_nil
141
+ expect(bootstrap.config[:ssh_password]).to be_nil
137
142
  end
138
143
 
139
144
  it "configs the bootstrap's ssh_port" do
140
- bootstrap.config[:ssh_port].should eq("2222")
145
+ expect(bootstrap.config[:ssh_port]).to eq("2222")
141
146
  end
142
147
 
143
148
  it "configs the bootstrap's identity_file" do
144
- bootstrap.config[:identity_file].should eq("~/.ssh/mykey_dsa")
149
+ expect(bootstrap.config[:identity_file]).to eq("~/.ssh/mykey_dsa")
145
150
  end
146
151
 
147
152
  it "configs the bootstrap's distro" do
148
- bootstrap.config[:distro].should eq("distro-praha")
153
+ expect(bootstrap.config[:distro]).to eq("distro-praha")
149
154
  end
150
155
 
151
156
  it "configs the bootstrap's distro to chef11/omnibus by default" do
152
157
  @knife.config.delete(:distro)
153
158
 
154
- bootstrap.config[:distro].should eq("chef11/omnibus")
159
+ expect(bootstrap.config[:distro]).to eq("chef11/omnibus")
155
160
  end
156
161
 
157
162
  it "configs the bootstrap's distro value driven off platform value" do
158
163
  @knife.config.delete(:distro)
159
164
  @knife.config[:platform] = "freebsd"
160
165
 
161
- bootstrap.config[:distro].should eq("chef11/freebsd")
166
+ expect(bootstrap.config[:distro]).to eq("chef11/freebsd")
162
167
  end
163
168
 
164
169
  it "configs the bootstrap's ENV with the webui password" do
165
170
  bootstrap
166
- ENV['WEBUI_PASSWORD'].should eq("daweb")
171
+
172
+ expect(ENV["WEBUI_PASSWORD"]).to eq("daweb")
167
173
  end
168
174
 
169
175
  it "configs the bootstrap's ENV with the amqp password" do
170
176
  bootstrap
171
- ENV['AMQP_PASSWORD'].should eq("queueitup")
177
+
178
+ expect(ENV["AMQP_PASSWORD"]).to eq("queueitup")
172
179
  end
173
180
 
174
181
  it "configs the bootstrap's name_args with the host" do
175
- bootstrap.name_args.should eq([ "172.0.10.21" ])
182
+ expect(bootstrap.name_args).to eq(%w[172.0.10.21])
176
183
  end
177
184
 
178
185
  it "configs the bootstrap's use_sudo to true if ssh-user is not root" do
179
- bootstrap.config[:use_sudo].should be_true
186
+ expect(bootstrap.config[:use_sudo]).to be_truthy
180
187
  end
181
188
 
182
189
  it "configs the bootstrap's use_sudo to false if ssh-user is root" do
183
190
  @knife.config[:ssh_user] = "root"
184
191
 
185
- bootstrap.config[:use_sudo].should_not be_true
192
+ expect(bootstrap.config[:use_sudo]).to_not be_truthy
193
+ end
194
+
195
+ it "skips config values with nil defaults" do
196
+ expect(bootstrap.config[:bootstrap_version]).to be_nil
186
197
  end
187
198
 
188
199
  describe "#bootstrap_auto?" do
189
- it "should always be true if it was set via --platform, even if the distro changes" do
200
+ it "should be true if set via --platform, even if the distro changes" do
190
201
  @knife.config[:platform] = "auto"
191
- bootstrap.config[:distro].should_not eq("auto")
192
- @knife.send(:bootstrap_auto?).should be_true
202
+ expect(bootstrap.config[:distro]).to_not eq("auto")
203
+ expect(@knife.send(:bootstrap_auto?)).to be_truthy
193
204
  end
194
205
  end
195
-
196
206
  end
197
207
 
198
208
  describe "#run" do
@@ -206,11 +216,11 @@ describe Chef::Knife::ServerBootstrapStandalone do
206
216
 
207
217
  @knife.config[:host] = "192.168.0.1"
208
218
  @knife.config[:ssh_port] = "2345"
209
- Chef::Knife::Bootstrap.stub(:new) { bootstrap }
210
- Knife::Server::SSH.stub(:new) { ssh }
211
- Knife::Server::Credentials.stub(:new) { credentials }
212
- credentials.stub(:install_validation_key)
213
- credentials.stub(:create_root_client)
219
+ allow(Chef::Knife::Bootstrap).to receive(:new) { bootstrap }
220
+ allow(Knife::Server::SSH).to receive(:new) { ssh }
221
+ allow(Knife::Server::Credentials).to receive(:new) { credentials }
222
+ allow(credentials).to receive(:install_validation_key)
223
+ allow(credentials).to receive(:create_root_client)
214
224
  end
215
225
 
216
226
  after do
@@ -220,11 +230,28 @@ describe Chef::Knife::ServerBootstrapStandalone do
220
230
  end
221
231
 
222
232
  let(:bootstrap) do
223
- stub(:run => true, :config => Hash.new, :ui= => true, :name_args= => true)
233
+ double(
234
+ :run => true,
235
+ :config => Hash.new,
236
+ :ui= => true,
237
+ :name_args= => true
238
+ )
224
239
  end
225
240
 
226
- let(:ssh) { stub(:exec! => true) }
227
- let(:credentials) { stub.as_null_object }
241
+ let(:ssh) { double(:exec! => true) }
242
+ let(:credentials) { double.as_null_object }
243
+
244
+ it "exits if Chef::Config[:node_name] is missing" do
245
+ Chef::Config[:node_name] = nil
246
+
247
+ expect { @knife.run }.to raise_error SystemExit
248
+ end
249
+
250
+ it "exits if Chef::Config[:client_key] is missing" do
251
+ Chef::Config[:client_key] = nil
252
+
253
+ expect { @knife.run }.to raise_error SystemExit
254
+ end
228
255
 
229
256
  it "exits if node_name option is missing" do
230
257
  @knife.config.delete(:chef_node_name)
@@ -239,18 +266,19 @@ describe Chef::Knife::ServerBootstrapStandalone do
239
266
  end
240
267
 
241
268
  it "bootstraps a standalone server" do
242
- bootstrap.should_receive(:run)
269
+ expect(bootstrap).to receive(:run)
270
+
243
271
  @knife.run
244
272
  end
245
273
 
246
274
  it "create a root client key" do
247
- credentials.should_receive(:create_root_client)
275
+ expect(credentials).to receive(:create_root_client)
248
276
 
249
277
  @knife.run
250
278
  end
251
279
 
252
280
  it "installs a client key" do
253
- credentials.should_receive(:install_client_key).
281
+ expect(credentials).to receive(:install_client_key).
254
282
  with("smithers", "/var/tmp/myclientkey.pem")
255
283
 
256
284
  @knife.run
@@ -259,27 +287,30 @@ describe Chef::Knife::ServerBootstrapStandalone do
259
287
  it "installs a new validation.pem key from the chef 10 server" do
260
288
  @knife.config[:bootstrap_version] = "10"
261
289
  @knife.config[:distro] = "yabba-debian"
262
- Knife::Server::Credentials.should_receive(:new).
290
+ expect(Knife::Server::Credentials).to receive(:new).
263
291
  with(ssh, "/etc/chef/validation.pem", {})
264
- credentials.should_receive(:install_validation_key)
292
+ expect(credentials).to receive(:install_validation_key)
265
293
 
266
294
  @knife.run
267
295
  end
268
296
 
269
297
  it "installs a new validation.pem key from the omnibus server" do
270
- Knife::Server::Credentials.should_receive(:new).
271
- with(ssh, "/etc/chef/validation.pem", {:omnibus => true})
272
- credentials.should_receive(:install_validation_key)
298
+ expect(Knife::Server::Credentials).to receive(:new).
299
+ with(ssh, "/etc/chef/validation.pem", :omnibus => true)
300
+ expect(credentials).to receive(:install_validation_key)
273
301
 
274
302
  @knife.run
275
303
  end
276
304
 
277
305
  context "when an ssh password is missing" do
278
306
  it "creates an SSH connection without a password" do
279
- Knife::Server::SSH.should_receive(:new).with({
280
- :host => "192.168.0.1", :port => "2345",
281
- :user => "root", :password => nil, :keys => []
282
- })
307
+ expect(Knife::Server::SSH).to receive(:new).with(
308
+ :host => "192.168.0.1",
309
+ :port => "2345",
310
+ :user => "root",
311
+ :password => nil,
312
+ :keys => []
313
+ )
283
314
 
284
315
  @knife.run
285
316
  end
@@ -291,10 +322,13 @@ describe Chef::Knife::ServerBootstrapStandalone do
291
322
  end
292
323
 
293
324
  it "creates an SSH connection with a password" do
294
- Knife::Server::SSH.should_receive(:new).with({
295
- :host => "192.168.0.1", :port => "2345",
296
- :user => "root", :password => "snoopy", :keys => []
297
- })
325
+ expect(Knife::Server::SSH).to receive(:new).with(
326
+ :host => "192.168.0.1",
327
+ :port => "2345",
328
+ :user => "root",
329
+ :password => "snoopy",
330
+ :keys => []
331
+ )
298
332
 
299
333
  @knife.run
300
334
  end
@@ -306,10 +340,13 @@ describe Chef::Knife::ServerBootstrapStandalone do
306
340
  end
307
341
 
308
342
  it "creates an SSH connection with an identity file" do
309
- Knife::Server::SSH.should_receive(:new).with({
310
- :host => "192.168.0.1", :port => "2345",
311
- :user => "root", :password => nil, :keys => ["poop.pem"]
312
- })
343
+ expect(Knife::Server::SSH).to receive(:new).with(
344
+ :host => "192.168.0.1",
345
+ :port => "2345",
346
+ :user => "root",
347
+ :password => nil,
348
+ :keys => ["poop.pem"]
349
+ )
313
350
 
314
351
  @knife.run
315
352
  end
@@ -317,13 +354,13 @@ describe Chef::Knife::ServerBootstrapStandalone do
317
354
 
318
355
  context "when key-based ssh authentication fails" do
319
356
  before do
320
- ssh.stub(:exec!).
357
+ allow(ssh).to receive(:exec!).
321
358
  with("hostname -f") { raise ::Net::SSH::AuthenticationFailed }
322
- @knife.ui.stub(:ask) { "hellacool" }
359
+ allow(@knife.ui).to receive(:ask) { "hellacool" }
323
360
  end
324
361
 
325
362
  it "sends a authentication failure message" do
326
- @knife.ui.should_receive(:warn).with(/Failed to authenticate/i)
363
+ expect(@knife.ui).to receive(:warn).with(/Failed to authenticate/i)
327
364
 
328
365
  @knife.run
329
366
  end
@@ -331,7 +368,7 @@ describe Chef::Knife::ServerBootstrapStandalone do
331
368
  it "sets the :ssh_password config from user input" do
332
369
  @knife.run
333
370
 
334
- @knife.config[:ssh_password].should eq("hellacool")
371
+ expect(@knife.config[:ssh_password]).to eq("hellacool")
335
372
  end
336
373
  end
337
374
  end