knife-ec2 0.11.0.rc.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -74,22 +74,23 @@ class Chef
74
74
  begin
75
75
  @server = connection.servers.get(instance_id)
76
76
 
77
- msg_pair("Instance ID", @server.id)
78
- msg_pair("Flavor", @server.flavor_id)
79
- msg_pair("Image", @server.image_id)
80
- msg_pair("Region", connection.instance_variable_get(:@region))
81
- msg_pair("Availability Zone", @server.availability_zone)
82
- msg_pair("Security Groups", @server.groups.join(", "))
83
- msg_pair("IAM Profile", iam_name_from_profile(@server.iam_instance_profile)) if @server.iam_instance_profile
84
- msg_pair("SSH Key", @server.key_name)
85
- msg_pair("Root Device Type", @server.root_device_type)
86
- msg_pair("Public DNS Name", @server.dns_name)
87
- msg_pair("Public IP Address", @server.public_ip_address)
88
- msg_pair("Private DNS Name", @server.private_dns_name)
89
- msg_pair("Private IP Address", @server.private_ip_address)
90
-
91
- puts "\n"
92
- confirm("Do you really want to delete this server")
77
+ msg_pair('Instance ID', @server.id)
78
+ msg_pair('Instance Name', @server.tags['Name'])
79
+ msg_pair('Flavor', @server.flavor_id)
80
+ msg_pair('Image', @server.image_id)
81
+ msg_pair('Region', connection.instance_variable_get(:@region))
82
+ msg_pair('Availability Zone', @server.availability_zone)
83
+ msg_pair('Security Groups', @server.groups.join(', '))
84
+ msg_pair('IAM Profile', iam_name_from_profile(@server.iam_instance_profile)) if @server.iam_instance_profile
85
+ msg_pair('SSH Key', @server.key_name)
86
+ msg_pair('Root Device Type', @server.root_device_type)
87
+ msg_pair('Public DNS Name', @server.dns_name)
88
+ msg_pair('Public IP Address', @server.public_ip_address)
89
+ msg_pair('Private DNS Name', @server.private_dns_name)
90
+ msg_pair('Private IP Address', @server.private_ip_address)
91
+
92
+ puts '\n'
93
+ confirm('Do you really want to delete this server')
93
94
 
94
95
  @server.destroy
95
96
 
@@ -20,11 +20,21 @@ class Chef
20
20
  end
21
21
 
22
22
  def bucket
23
- URI(@url).host
23
+ uri = URI(@url)
24
+ if uri.scheme == "s3"
25
+ URI(@url).host
26
+ else
27
+ URI(@url).path.split("/")[1]
28
+ end
24
29
  end
25
30
 
26
31
  def path
27
- URI(@url).path.sub(/^\//, '')
32
+ uri = URI(@url)
33
+ if uri.scheme == "s3"
34
+ URI(@url).path.sub(/^\//, '')
35
+ else
36
+ URI(@url).path.split(bucket).last.sub(/^\//, '')
37
+ end
28
38
  end
29
39
 
30
40
  def fog
@@ -1,6 +1,6 @@
1
1
  module Knife
2
2
  module Ec2
3
- VERSION = "0.11.0.rc.0"
3
+ VERSION = "0.11.0"
4
4
  MAJOR, MINOR, TINY = VERSION.split('.')
5
5
  end
6
6
  end
@@ -8,6 +8,7 @@ require 'chef/knife/ec2_server_list'
8
8
  # Clear config between each example
9
9
  # to avoid dependencies between examples
10
10
  RSpec.configure do |c|
11
+ c.raise_errors_for_deprecations!
11
12
  c.filter_run_excluding :exclude => true
12
13
  c.before(:each) do
13
14
  Chef::Config.reset
@@ -29,11 +29,11 @@ describe Chef::Knife::Ec2ServerCreate do
29
29
  before(:each) do
30
30
  @knife_ec2_create = Chef::Knife::Ec2ServerCreate.new
31
31
  @knife_ec2_create.initial_sleep_delay = 0
32
- @knife_ec2_create.stub(:tcp_test_ssh).and_return(true)
32
+ allow(@knife_ec2_create).to receive(:tcp_test_ssh).and_return(true)
33
33
 
34
34
  {
35
35
  :image => 'image',
36
- :aws_ssh_key_id => 'aws_ssh_key_id',
36
+ :ssh_key_name => 'ssh_key_name',
37
37
  :aws_access_key_id => 'aws_access_key_id',
38
38
  :aws_secret_access_key => 'aws_secret_access_key'
39
39
  }.each do |key, value|
@@ -41,9 +41,9 @@ describe Chef::Knife::Ec2ServerCreate do
41
41
  end
42
42
 
43
43
  @ec2_connection = double(Fog::Compute::AWS)
44
- @ec2_connection.stub_chain(:tags).and_return double('create', :create => true)
45
- @ec2_connection.stub_chain(:images, :get).and_return double('ami', :root_device_type => 'not_ebs', :platform => 'linux')
46
- @ec2_connection.stub_chain(:addresses).and_return [double('addesses', {
44
+ allow(@ec2_connection).to receive(:tags).and_return double('create', :create => true)
45
+ allow(@ec2_connection).to receive_message_chain(:images, :get).and_return double('ami', :root_device_type => 'not_ebs', :platform => 'linux')
46
+ allow(@ec2_connection).to receive(:addresses).and_return [double('addesses', {
47
47
  :domain => 'standard',
48
48
  :public_ip => '111.111.111.111',
49
49
  :server_id => nil,
@@ -52,6 +52,8 @@ describe Chef::Knife::Ec2ServerCreate do
52
52
 
53
53
  @ec2_servers = double()
54
54
  @new_ec2_server = double()
55
+ @spot_requests = double
56
+ @new_spot_request = double
55
57
 
56
58
  @ec2_server_attribs = { :id => 'i-39382318',
57
59
  :flavor_id => 'm1.small',
@@ -67,47 +69,128 @@ describe Chef::Knife::Ec2ServerCreate do
67
69
  :private_ip_address => '10.251.75.20',
68
70
  :root_device_type => 'not_ebs' }
69
71
 
72
+ @spot_request_attribs = { :id => 'test_spot_request_id',
73
+ :price => 0.001,
74
+ :request_type => 'persistent',
75
+ :created_at => '2015-07-14 09:53:11 UTC',
76
+ :instance_count => nil,
77
+ :instance_id => 'test_spot_instance_id',
78
+ :state => 'open',
79
+ :key_name => 'ssh_key_name',
80
+ :availability_zone => nil,
81
+ :flavor_id => 'm1.small',
82
+ :image_id => 'image' }
83
+
84
+
70
85
  @ec2_server_attribs.each_pair do |attrib, value|
71
- @new_ec2_server.stub(attrib).and_return(value)
86
+ allow(@new_ec2_server).to receive(attrib).and_return(value)
87
+ end
88
+
89
+ @spot_request_attribs.each_pair do |attrib, value|
90
+ allow(@new_spot_request).to receive(attrib).and_return(value)
72
91
  end
73
92
 
74
93
  @s3_connection = double(Fog::Storage::AWS)
75
94
 
76
95
  @bootstrap = Chef::Knife::Bootstrap.new
77
- Chef::Knife::Bootstrap.stub(:new).and_return(@bootstrap)
96
+ allow(Chef::Knife::Bootstrap).to receive(:new).and_return(@bootstrap)
78
97
 
79
98
  @validation_key_url = 's3://bucket/foo/bar'
80
99
  @validation_key_file = '/tmp/a_good_temp_file'
81
100
  @validation_key_body = "TEST VALIDATION KEY\n"
82
101
  end
83
102
 
103
+ describe "Spot Instance creation" do
104
+ before do
105
+ allow(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
106
+ @knife_ec2_create.config[:spot_price] = 0.001
107
+ @knife_ec2_create.config[:spot_request_type] = 'persistent'
108
+ allow(@knife_ec2_create).to receive(:puts)
109
+ allow(@knife_ec2_create).to receive(:msg_pair)
110
+ allow(@knife_ec2_create.ui).to receive(:color).and_return('')
111
+ allow(@knife_ec2_create).to receive(:confirm)
112
+ @spot_instance_server_def = {
113
+ :image_id => "image",
114
+ :groups => nil,
115
+ :security_group_ids => nil,
116
+ :flavor_id => nil,
117
+ :key_name => "ssh_key_name",
118
+ :availability_zone => nil,
119
+ :price => 0.001,
120
+ :request_type => 'persistent',
121
+ :placement_group => nil,
122
+ :iam_instance_profile_name => nil,
123
+ :ebs_optimized => "false"
124
+ }
125
+ allow(@bootstrap).to receive(:run)
126
+ end
127
+
128
+ it "creates a new spot instance request with request type as persistent" do
129
+ expect(@ec2_connection).to receive(
130
+ :spot_requests).and_return(@spot_requests)
131
+ expect(@spot_requests).to receive(
132
+ :create).with(@spot_instance_server_def).and_return(@new_spot_request)
133
+ @knife_ec2_create.config[:yes] = true
134
+ allow(@new_spot_request).to receive(:wait_for).and_return(true)
135
+ allow(@ec2_connection).to receive(:servers).and_return(@ec2_servers)
136
+ allow(@ec2_servers).to receive(
137
+ :get).with(@new_spot_request.instance_id).and_return(@new_ec2_server)
138
+ allow(@new_ec2_server).to receive(:wait_for).and_return(true)
139
+ @knife_ec2_create.run
140
+ expect(@new_spot_request.request_type).to eq('persistent')
141
+ end
142
+
143
+ it "successfully creates a new spot instance" do
144
+ allow(@ec2_connection).to receive(
145
+ :spot_requests).and_return(@spot_requests)
146
+ allow(@spot_requests).to receive(
147
+ :create).with(@spot_instance_server_def).and_return(@new_spot_request)
148
+ @knife_ec2_create.config[:yes] = true
149
+ expect(@new_spot_request).to receive(:wait_for).and_return(true)
150
+ expect(@ec2_connection).to receive(:servers).and_return(@ec2_servers)
151
+ expect(@ec2_servers).to receive(
152
+ :get).with(@new_spot_request.instance_id).and_return(@new_ec2_server)
153
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
154
+ @knife_ec2_create.run
155
+ end
156
+
157
+ it "does not create the spot instance request and creates a regular instance" do
158
+ @knife_ec2_create.config.delete(:spot_price)
159
+ expect(@ec2_connection).to receive(:servers).and_return(@ec2_servers)
160
+ expect(@ec2_servers).to receive(
161
+ :create).and_return(@new_ec2_server)
162
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
163
+ @knife_ec2_create.run
164
+ end
165
+ end
166
+
84
167
  describe "run" do
85
168
  before do
86
- @ec2_servers.should_receive(:create).and_return(@new_ec2_server)
87
- @ec2_connection.should_receive(:servers).and_return(@ec2_servers)
88
- @ec2_connection.should_receive(:addresses)
169
+ expect(@ec2_servers).to receive(:create).and_return(@new_ec2_server)
170
+ expect(@ec2_connection).to receive(:servers).and_return(@ec2_servers)
171
+ expect(@ec2_connection).to receive(:addresses)
89
172
 
90
173
  @eip = "111.111.111.111"
91
- Fog::Compute::AWS.should_receive(:new).and_return(@ec2_connection)
174
+ expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
92
175
 
93
- @knife_ec2_create.stub(:puts)
94
- @knife_ec2_create.stub(:print)
176
+ allow(@knife_ec2_create).to receive(:puts)
177
+ allow(@knife_ec2_create).to receive(:print)
95
178
  @knife_ec2_create.config[:image] = '12345'
96
- @bootstrap.should_receive(:run)
179
+ expect(@bootstrap).to receive(:run)
97
180
  end
98
181
 
99
182
  it "defaults to a distro of 'chef-full' for a linux instance" do
100
- @new_ec2_server.should_receive(:wait_for).and_return(true)
183
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
101
184
  @knife_ec2_create.config[:distro] = @knife_ec2_create.options[:distro][:default]
185
+ expect(@knife_ec2_create).to receive(:default_bootstrap_template).and_return('chef-full')
102
186
  @knife_ec2_create.run
103
- @bootstrap.config[:distro].should == 'chef-full'
104
187
  end
105
188
 
106
189
  it "creates an EC2 instance and bootstraps it" do
107
- @new_ec2_server.should_receive(:wait_for).and_return(true)
108
- @knife_ec2_create.should_receive(:ssh_override_winrm)
190
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
191
+ expect(@knife_ec2_create).to receive(:ssh_override_winrm)
109
192
  @knife_ec2_create.run
110
- @knife_ec2_create.server.should_not == nil
193
+ expect(@knife_ec2_create.server).to_not be_nil
111
194
  end
112
195
 
113
196
  it "set ssh_user value by using -x option for ssh bootstrap protocol or linux image" do
@@ -116,10 +199,10 @@ describe Chef::Knife::Ec2ServerCreate do
116
199
  @knife_ec2_create.config[:winrm_user] = "ubuntu"
117
200
  @knife_ec2_create.config[:ssh_user] = "root"
118
201
 
119
- @new_ec2_server.should_receive(:wait_for).and_return(true)
202
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
120
203
  @knife_ec2_create.run
121
- @knife_ec2_create.config[:ssh_user].should == "ubuntu"
122
- @knife_ec2_create.server.should_not == nil
204
+ expect(@knife_ec2_create.config[:ssh_user]).to eq("ubuntu")
205
+ expect(@knife_ec2_create.server).to_not be_nil
123
206
  end
124
207
 
125
208
  it "set ssh_password value by using -P option for ssh bootstrap protocol or linux image" do
@@ -128,10 +211,10 @@ describe Chef::Knife::Ec2ServerCreate do
128
211
  @knife_ec2_create.config[:winrm_password] = "winrm_password"
129
212
  @knife_ec2_create.config[:ssh_password] = nil
130
213
 
131
- @new_ec2_server.should_receive(:wait_for).and_return(true)
214
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
132
215
  @knife_ec2_create.run
133
- @knife_ec2_create.config[:ssh_password].should == "winrm_password"
134
- @knife_ec2_create.server.should_not == nil
216
+ expect(@knife_ec2_create.config[:ssh_password]).to eq("winrm_password")
217
+ expect(@knife_ec2_create.server).to_not be_nil
135
218
  end
136
219
 
137
220
  it "set ssh_port value by using -p option for ssh bootstrap protocol or linux image" do
@@ -140,10 +223,10 @@ describe Chef::Knife::Ec2ServerCreate do
140
223
  @knife_ec2_create.config[:winrm_port] = "1234"
141
224
  @knife_ec2_create.config[:ssh_port] = "22"
142
225
 
143
- @new_ec2_server.should_receive(:wait_for).and_return(true)
226
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
144
227
  @knife_ec2_create.run
145
- @knife_ec2_create.config[:ssh_port].should == "1234"
146
- @knife_ec2_create.server.should_not == nil
228
+ expect(@knife_ec2_create.config[:ssh_port]).to eq("1234")
229
+ expect(@knife_ec2_create.server).to_not be_nil
147
230
  end
148
231
 
149
232
  it "set identity_file value by using -i option for ssh bootstrap protocol or linux image" do
@@ -152,97 +235,98 @@ describe Chef::Knife::Ec2ServerCreate do
152
235
  @knife_ec2_create.config[:kerberos_keytab_file] = "kerberos_keytab_file"
153
236
  @knife_ec2_create.config[:identity_file] = nil
154
237
 
155
- @new_ec2_server.should_receive(:wait_for).and_return(true)
238
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
156
239
  @knife_ec2_create.run
157
- @knife_ec2_create.config[:identity_file].should == "kerberos_keytab_file"
158
- @knife_ec2_create.server.should_not == nil
240
+ expect(@knife_ec2_create.config[:identity_file]).to eq("kerberos_keytab_file")
241
+ expect(@knife_ec2_create.server).to_not be_nil
159
242
  end
160
243
 
161
244
  it "should never invoke windows bootstrap for linux instance" do
162
- @new_ec2_server.should_receive(:wait_for).and_return(true)
163
- @knife_ec2_create.should_not_receive(:bootstrap_for_windows_node)
245
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
246
+ expect(@knife_ec2_create).not_to receive(:bootstrap_for_windows_node)
164
247
  @knife_ec2_create.run
165
248
  end
166
249
 
167
250
  it "creates an EC2 instance, assigns existing EIP and bootstraps it" do
168
251
  @knife_ec2_create.config[:associate_eip] = @eip
169
252
 
170
- @new_ec2_server.stub(:public_ip_address).and_return(@eip)
171
- @ec2_connection.should_receive(:associate_address).with(@ec2_server_attribs[:id], @eip, nil, '')
172
- @new_ec2_server.should_receive(:wait_for).at_least(:twice).and_return(true)
253
+ allow(@new_ec2_server).to receive(:public_ip_address).and_return(@eip)
254
+ expect(@ec2_connection).to receive(:associate_address).with(@ec2_server_attribs[:id], @eip, nil, '')
255
+ expect(@new_ec2_server).to receive(:wait_for).at_least(:twice).and_return(true)
173
256
 
174
257
  @knife_ec2_create.run
175
- @knife_ec2_create.server.should_not == nil
258
+ expect(@knife_ec2_create.server).to_not be_nil
176
259
  end
177
260
 
178
261
  it "retries if it receives Fog::Compute::AWS::NotFound" do
179
- @new_ec2_server.should_receive(:wait_for).and_return(true)
180
- @knife_ec2_create.should_receive(:create_tags).and_raise(Fog::Compute::AWS::NotFound)
181
- @knife_ec2_create.should_receive(:create_tags).and_return(true)
182
- @knife_ec2_create.should_receive(:sleep).and_return(true)
183
- @knife_ec2_create.ui.should_receive(:warn).with(/retrying/)
262
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
263
+ expect(@knife_ec2_create).to receive(:create_tags).and_raise(Fog::Compute::AWS::NotFound)
264
+ expect(@knife_ec2_create).to receive(:create_tags).and_return(true)
265
+ expect(@knife_ec2_create).to receive(:sleep).and_return(true)
266
+ expect(@knife_ec2_create.ui).to receive(:warn).with(/retrying/)
184
267
  @knife_ec2_create.run
185
268
  end
186
269
 
187
270
  it 'actually writes to the validation key tempfile' do
188
- @new_ec2_server.should_receive(:wait_for).and_return(true)
271
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
189
272
  Chef::Config[:knife][:validation_key_url] =
190
273
  @validation_key_url
191
274
  @knife_ec2_create.config[:validation_key_url] =
192
275
  @validation_key_url
193
276
 
194
- @knife_ec2_create.stub_chain(:validation_key_tmpfile, :path).and_return(@validation_key_file)
195
- Chef::Knife::S3Source.stub(:fetch).with(@validation_key_url).and_return(@validation_key_body)
196
- File.should_receive(:open).with(@validation_key_file, 'w')
277
+ allow(@knife_ec2_create).to receive_message_chain(:validation_key_tmpfile, :path).and_return(@validation_key_file)
278
+ allow(Chef::Knife::S3Source).to receive(:fetch).with(@validation_key_url).and_return(@validation_key_body)
279
+ expect(File).to receive(:open).with(@validation_key_file, 'w')
197
280
  @knife_ec2_create.run
198
281
  end
199
282
  end
200
283
 
201
284
  describe "run for EC2 Windows instance" do
202
285
  before do
203
- @ec2_servers.should_receive(:create).and_return(@new_ec2_server)
204
- @ec2_connection.should_receive(:servers).and_return(@ec2_servers)
205
- @ec2_connection.should_receive(:addresses)
286
+ expect(@ec2_servers).to receive(:create).and_return(@new_ec2_server)
287
+ expect(@ec2_connection).to receive(:servers).and_return(@ec2_servers)
288
+ expect(@ec2_connection).to receive(:addresses)
206
289
 
207
- Fog::Compute::AWS.should_receive(:new).and_return(@ec2_connection)
290
+ expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
208
291
 
209
- @knife_ec2_create.stub(:puts)
210
- @knife_ec2_create.stub(:print)
292
+ allow(@knife_ec2_create).to receive(:puts)
293
+ allow(@knife_ec2_create).to receive(:print)
211
294
  @knife_ec2_create.config[:identity_file] = "~/.ssh/aws-key.pem"
212
295
  @knife_ec2_create.config[:image] = '12345'
213
- @knife_ec2_create.stub(:is_image_windows?).and_return(true)
214
- @knife_ec2_create.stub(:tcp_test_winrm).and_return(true)
296
+ allow(@knife_ec2_create).to receive(:is_image_windows?).and_return(true)
297
+ allow(@knife_ec2_create).to receive(:tcp_test_winrm).and_return(true)
215
298
  end
216
299
 
217
300
  it "bootstraps via the WinRM protocol" do
218
301
  @knife_ec2_create.config[:winrm_password] = 'winrm-password'
219
302
  @knife_ec2_create.config[:bootstrap_protocol] = 'winrm'
220
303
  @bootstrap_winrm = Chef::Knife::BootstrapWindowsWinrm.new
221
- Chef::Knife::BootstrapWindowsWinrm.stub(:new).and_return(@bootstrap_winrm)
222
- @bootstrap_winrm.should_receive(:run)
223
- @knife_ec2_create.should_not_receive(:ssh_override_winrm)
224
- @new_ec2_server.should_receive(:wait_for).and_return(true)
304
+ allow(Chef::Knife::BootstrapWindowsWinrm).to receive(:new).and_return(@bootstrap_winrm)
305
+ expect(@bootstrap_winrm).to receive(:run)
306
+ expect(@knife_ec2_create).not_to receive(:ssh_override_winrm)
307
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
225
308
  @knife_ec2_create.run
226
309
  end
227
310
 
228
311
  it "set default distro to windows-chef-client-msi for windows" do
229
312
  @knife_ec2_create.config[:winrm_password] = 'winrm-password'
230
- @knife_ec2_create.config[:bootstrap_protocol] = 'winrm'
313
+ @knife_ec2_create.config[:bootstrap_protocol] = 'winrm'
231
314
  @bootstrap_winrm = Chef::Knife::BootstrapWindowsWinrm.new
232
- Chef::Knife::BootstrapWindowsWinrm.stub(:new).and_return(@bootstrap_winrm)
233
- @bootstrap_winrm.should_receive(:run)
234
- @new_ec2_server.should_receive(:wait_for).and_return(true)
315
+ allow(Chef::Knife::BootstrapWindowsWinrm).to receive(:new).and_return(@bootstrap_winrm)
316
+ expect(@bootstrap_winrm).to receive(:run)
317
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
318
+ allow(@knife_ec2_create).to receive(:is_image_windows?).and_return(true)
319
+ expect(@knife_ec2_create).to receive(:default_bootstrap_template).and_return("windows-chef-client-msi")
235
320
  @knife_ec2_create.run
236
- @knife_ec2_create.config[:distro].should == "windows-chef-client-msi"
237
321
  end
238
322
 
239
323
  it "bootstraps via the SSH protocol" do
240
324
  @knife_ec2_create.config[:bootstrap_protocol] = 'ssh'
241
325
  bootstrap_win_ssh = Chef::Knife::BootstrapWindowsSsh.new
242
- Chef::Knife::BootstrapWindowsSsh.stub(:new).and_return(bootstrap_win_ssh)
243
- bootstrap_win_ssh.should_receive(:run)
244
- @knife_ec2_create.should_receive(:ssh_override_winrm)
245
- @new_ec2_server.should_receive(:wait_for).and_return(true)
326
+ allow(Chef::Knife::BootstrapWindowsSsh).to receive(:new).and_return(bootstrap_win_ssh)
327
+ expect(bootstrap_win_ssh).to receive(:run)
328
+ expect(@knife_ec2_create).to receive(:ssh_override_winrm)
329
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
246
330
  @knife_ec2_create.run
247
331
  end
248
332
 
@@ -250,51 +334,51 @@ describe Chef::Knife::Ec2ServerCreate do
250
334
  @knife_ec2_create.config[:bootstrap_protocol] = 'ssh'
251
335
  @knife_ec2_create.config[:ssh_port] = 422
252
336
 
253
- @knife_ec2_create.should_receive(:tcp_test_ssh).with('ec2-75.101.253.10.compute-1.amazonaws.com', 422).and_return(true)
337
+ expect(@knife_ec2_create).to receive(:tcp_test_ssh).with('ec2-75.101.253.10.compute-1.amazonaws.com', 422).and_return(true)
254
338
 
255
339
  bootstrap_win_ssh = Chef::Knife::BootstrapWindowsSsh.new
256
- Chef::Knife::BootstrapWindowsSsh.stub(:new).and_return(bootstrap_win_ssh)
257
- bootstrap_win_ssh.should_receive(:run)
258
- @new_ec2_server.should_receive(:wait_for).and_return(true)
340
+ allow(Chef::Knife::BootstrapWindowsSsh).to receive(:new).and_return(bootstrap_win_ssh)
341
+ expect(bootstrap_win_ssh).to receive(:run)
342
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
259
343
  @knife_ec2_create.run
260
344
  end
261
345
 
262
346
  it "should never invoke linux bootstrap" do
263
347
  @knife_ec2_create.config[:bootstrap_protocol] = 'winrm'
264
- @knife_ec2_create.stub(:windows_password).and_return("")
265
- @knife_ec2_create.should_not_receive(:bootstrap_for_linux_node)
266
- @new_ec2_server.should_receive(:wait_for).and_return(true)
267
- @knife_ec2_create.stub(:bootstrap_for_windows_node).and_return double("bootstrap", :run => true)
348
+ allow(@knife_ec2_create).to receive(:windows_password).and_return("")
349
+ expect(@knife_ec2_create).not_to receive(:bootstrap_for_linux_node)
350
+ expect(@new_ec2_server).to receive(:wait_for).and_return(true)
351
+ allow(@knife_ec2_create).to receive(:bootstrap_for_windows_node).and_return double("bootstrap", :run => true)
268
352
  @knife_ec2_create.run
269
353
  end
270
354
 
271
355
  it "waits for EC2 to generate password if not supplied" do
272
356
  @knife_ec2_create.config[:bootstrap_protocol] = 'winrm'
273
357
  @knife_ec2_create.config[:winrm_password] = nil
274
- @knife_ec2_create.should_receive(:windows_password).and_return("")
275
- @new_ec2_server.stub(:wait_for).and_return(true)
276
- @knife_ec2_create.stub(:check_windows_password_available).and_return(true)
358
+ expect(@knife_ec2_create).to receive(:windows_password).and_return("")
359
+ allow(@new_ec2_server).to receive(:wait_for).and_return(true)
360
+ allow(@knife_ec2_create).to receive(:check_windows_password_available).and_return(true)
277
361
  bootstrap_winrm = Chef::Knife::BootstrapWindowsWinrm.new
278
- Chef::Knife::BootstrapWindowsWinrm.stub(:new).and_return(bootstrap_winrm)
279
- bootstrap_winrm.should_receive(:run)
362
+ allow(Chef::Knife::BootstrapWindowsWinrm).to receive(:new).and_return(bootstrap_winrm)
363
+ expect(bootstrap_winrm).to receive(:run)
280
364
  @knife_ec2_create.run
281
365
  end
282
366
  end
283
367
 
284
368
  describe "when setting tags" do
285
369
  before do
286
- Fog::Compute::AWS.should_receive(:new).and_return(@ec2_connection)
287
- @knife_ec2_create.stub(:bootstrap_for_linux_node).and_return double("bootstrap", :run => true)
288
- @ec2_connection.stub(:servers).and_return(@ec2_servers)
289
- @ec2_connection.should_receive(:addresses)
290
- @new_ec2_server.stub(:wait_for).and_return(true)
291
- @ec2_servers.stub(:create).and_return(@new_ec2_server)
292
- @knife_ec2_create.stub(:puts)
293
- @knife_ec2_create.stub(:print)
370
+ expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
371
+ allow(@knife_ec2_create).to receive(:bootstrap_for_linux_node).and_return double("bootstrap", :run => true)
372
+ allow(@ec2_connection).to receive(:servers).and_return(@ec2_servers)
373
+ expect(@ec2_connection).to receive(:addresses)
374
+ allow(@new_ec2_server).to receive(:wait_for).and_return(true)
375
+ allow(@ec2_servers).to receive(:create).and_return(@new_ec2_server)
376
+ allow(@knife_ec2_create).to receive(:puts)
377
+ allow(@knife_ec2_create).to receive(:print)
294
378
  end
295
379
 
296
380
  it "sets the Name tag to the instance id by default" do
297
- @ec2_connection.tags.should_receive(:create).with(:key => "Name",
381
+ expect(@ec2_connection.tags).to receive(:create).with(:key => "Name",
298
382
  :value => @new_ec2_server.id,
299
383
  :resource_id => @new_ec2_server.id)
300
384
  @knife_ec2_create.run
@@ -302,7 +386,7 @@ describe Chef::Knife::Ec2ServerCreate do
302
386
 
303
387
  it "sets the Name tag to the chef_node_name when given" do
304
388
  @knife_ec2_create.config[:chef_node_name] = "wombat"
305
- @ec2_connection.tags.should_receive(:create).with(:key => "Name",
389
+ expect(@ec2_connection.tags).to receive(:create).with(:key => "Name",
306
390
  :value => "wombat",
307
391
  :resource_id => @new_ec2_server.id)
308
392
  @knife_ec2_create.run
@@ -310,7 +394,7 @@ describe Chef::Knife::Ec2ServerCreate do
310
394
 
311
395
  it "sets the Name tag to the specified name when given --tags Name=NAME" do
312
396
  @knife_ec2_create.config[:tags] = ["Name=bobcat"]
313
- @ec2_connection.tags.should_receive(:create).with(:key => "Name",
397
+ expect(@ec2_connection.tags).to receive(:create).with(:key => "Name",
314
398
  :value => "bobcat",
315
399
  :resource_id => @new_ec2_server.id)
316
400
  @knife_ec2_create.run
@@ -318,7 +402,7 @@ describe Chef::Knife::Ec2ServerCreate do
318
402
 
319
403
  it "sets arbitrary tags" do
320
404
  @knife_ec2_create.config[:tags] = ["foo=bar"]
321
- @ec2_connection.tags.should_receive(:create).with(:key => "foo",
405
+ expect(@ec2_connection.tags).to receive(:create).with(:key => "foo",
322
406
  :value => "bar",
323
407
  :resource_id => @new_ec2_server.id)
324
408
  @knife_ec2_create.run
@@ -365,7 +449,7 @@ describe Chef::Knife::Ec2ServerCreate do
365
449
  Chef::Config[:knife][:s3_secret] =
366
450
  's3://test.bucket/folder/encrypted_data_bag_secret'
367
451
  @secret_content = "TEST DATA BAG SECRET\n"
368
- @knife_ec2_create.stub(:s3_secret).and_return(@secret_content)
452
+ allow(@knife_ec2_create).to receive(:s3_secret).and_return(@secret_content)
369
453
  end
370
454
 
371
455
  it 'sets the secret to the expected test string' do
@@ -374,6 +458,59 @@ describe Chef::Knife::Ec2ServerCreate do
374
458
  end
375
459
  end
376
460
 
461
+ context "when deprecated aws_ssh_key_id option is used in knife config and no ssh-key is supplied on the CLI" do
462
+ before do
463
+ Chef::Config[:knife][:aws_ssh_key_id] = "mykey"
464
+ Chef::Config[:knife].delete(:ssh_key_name)
465
+ @aws_key = Chef::Config[:knife][:aws_ssh_key_id]
466
+ allow(@knife_ec2_create).to receive(:ami).and_return(false)
467
+ end
468
+
469
+ it "gives warning message and creates the attribute with the required name" do
470
+ expect(@knife_ec2_create.ui).to receive(:warn).with("Use of aws_ssh_key_id option in knife.rb config is deprecated, use ssh_key_name option instead.")
471
+ @knife_ec2_create.validate!
472
+ expect(Chef::Config[:knife][:ssh_key_name]).to eq(@aws_key)
473
+ end
474
+ end
475
+
476
+ context "when deprecated aws_ssh_key_id option is used in knife config but ssh-key is also supplied on the CLI" do
477
+ before do
478
+ Chef::Config[:knife][:aws_ssh_key_id] = "mykey"
479
+ @aws_key = Chef::Config[:knife][:aws_ssh_key_id]
480
+ allow(@knife_ec2_create).to receive(:ami).and_return(false)
481
+ end
482
+
483
+ it "gives warning message and gives preference to CLI value over knife config's value" do
484
+ expect(@knife_ec2_create.ui).to receive(:warn).with("Use of aws_ssh_key_id option in knife.rb config is deprecated, use ssh_key_name option instead.")
485
+ @knife_ec2_create.validate!
486
+ expect(Chef::Config[:knife][:ssh_key_name]).to_not eq(@aws_key)
487
+ end
488
+ end
489
+
490
+ context "when ssh_key_name option is used in knife config instead of deprecated aws_ssh_key_id option" do
491
+ before do
492
+ Chef::Config[:knife][:ssh_key_name] = "mykey"
493
+ allow(@knife_ec2_create).to receive(:ami).and_return(false)
494
+ end
495
+
496
+ it "does nothing" do
497
+ @knife_ec2_create.validate!
498
+ end
499
+ end
500
+
501
+ context "when ssh_key_name option is used in knife config also it is passed on the CLI" do
502
+ before do
503
+ allow(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
504
+ Chef::Config[:knife][:ssh_key_name] = "mykey"
505
+ @knife_ec2_create.config[:ssh_key_name] = "ssh_key_name"
506
+ end
507
+
508
+ it "ssh-key passed over CLI gets preference over knife config value" do
509
+ server_def = @knife_ec2_create.create_server_def
510
+ expect(server_def[:key_name]).to eq(@knife_ec2_create.config[:ssh_key_name])
511
+ end
512
+ end
513
+
377
514
  describe "when configuring the bootstrap process" do
378
515
  before do
379
516
  @knife_ec2_create.config[:ssh_user] = "ubuntu"
@@ -395,72 +532,72 @@ describe Chef::Knife::Ec2ServerCreate do
395
532
  end
396
533
 
397
534
  it "should set the bootstrap 'name argument' to the hostname of the EC2 server" do
398
- @bootstrap.name_args.should == ['ec2-75.101.253.10.compute-1.amazonaws.com']
535
+ expect(@bootstrap.name_args).to eq(['ec2-75.101.253.10.compute-1.amazonaws.com'])
399
536
  end
400
537
 
401
538
  it "should set the bootstrap 'first_boot_attributes' correctly" do
402
- @bootstrap.config[:first_boot_attributes].should == "{'my_attributes':{'foo':'bar'}"
539
+ expect(@bootstrap.config[:first_boot_attributes]).to eq("{'my_attributes':{'foo':'bar'}")
403
540
  end
404
541
 
405
542
  it "configures sets the bootstrap's run_list" do
406
- @bootstrap.config[:run_list].should == ['role[base]']
543
+ expect(@bootstrap.config[:run_list]).to eq(['role[base]'])
407
544
  end
408
545
 
409
546
  it "configures the bootstrap to use the correct ssh_user login" do
410
- @bootstrap.config[:ssh_user].should == 'ubuntu'
547
+ expect(@bootstrap.config[:ssh_user]).to eq('ubuntu')
411
548
  end
412
549
 
413
550
  it "configures the bootstrap to use the correct ssh_gateway host" do
414
- @bootstrap.config[:ssh_gateway].should == 'bastion.host.com'
551
+ expect(@bootstrap.config[:ssh_gateway]).to eq('bastion.host.com')
415
552
  end
416
553
 
417
554
  it "configures the bootstrap to use the correct ssh identity file" do
418
- @bootstrap.config[:identity_file].should == "~/.ssh/aws-key.pem"
555
+ expect(@bootstrap.config[:identity_file]).to eq("~/.ssh/aws-key.pem")
419
556
  end
420
557
 
421
558
  it "configures the bootstrap to use the correct ssh_port number" do
422
- @bootstrap.config[:ssh_port].should == 22
559
+ expect(@bootstrap.config[:ssh_port]).to eq(22)
423
560
  end
424
561
 
425
562
  it "configures the bootstrap to use the configured node name if provided" do
426
- @bootstrap.config[:chef_node_name].should == 'blarf'
563
+ expect(@bootstrap.config[:chef_node_name]).to eq('blarf')
427
564
  end
428
565
 
429
566
  it "configures the bootstrap to use the EC2 server id if no explicit node name is set" do
430
567
  @knife_ec2_create.config[:chef_node_name] = nil
431
568
 
432
569
  bootstrap = @knife_ec2_create.bootstrap_for_linux_node(@new_ec2_server, @new_ec2_server.dns_name)
433
- bootstrap.config[:chef_node_name].should == @new_ec2_server.id
570
+ expect(bootstrap.config[:chef_node_name]).to eq(@new_ec2_server.id)
434
571
  end
435
572
 
436
573
  it "configures the bootstrap to use prerelease versions of chef if specified" do
437
- @bootstrap.config[:prerelease].should be_falsey
574
+ expect(@bootstrap.config[:prerelease]).to be_falsey
438
575
 
439
576
  @knife_ec2_create.config[:prerelease] = true
440
577
 
441
578
  bootstrap = @knife_ec2_create.bootstrap_for_linux_node(@new_ec2_server, @new_ec2_server.dns_name)
442
- bootstrap.config[:prerelease].should == true
579
+ expect(bootstrap.config[:prerelease]).to eq(true)
443
580
  end
444
581
 
445
582
  it "configures the bootstrap to use the desired distro-specific bootstrap script" do
446
- @bootstrap.config[:distro].should == 'ubuntu-10.04-magic-sparkles'
583
+ expect(@bootstrap.config[:distro]).to eq('ubuntu-10.04-magic-sparkles')
447
584
  end
448
585
 
449
586
  it "configures the bootstrap to use sudo" do
450
- @bootstrap.config[:use_sudo].should == true
587
+ expect(@bootstrap.config[:use_sudo]).to eq(true)
451
588
  end
452
589
 
453
590
  it "configured the bootstrap to use the desired template" do
454
- @bootstrap.config[:template_file].should == '~/.chef/templates/my-bootstrap.sh.erb'
591
+ expect(@bootstrap.config[:template_file]).to eq('~/.chef/templates/my-bootstrap.sh.erb')
455
592
  end
456
593
 
457
594
  it "configured the bootstrap to set an ec2 hint (via Chef::Config)" do
458
- Chef::Config[:knife][:hints]["ec2"].should_not be_nil
595
+ expect(Chef::Config[:knife][:hints]["ec2"]).not_to be_nil
459
596
  end
460
597
  end
461
598
  describe "when configuring the winrm bootstrap process for windows" do
462
599
  before do
463
- @knife_ec2_create.stub(:fetch_server_fqdn).and_return("SERVERNAME")
600
+ allow(@knife_ec2_create).to receive(:fetch_server_fqdn).and_return("SERVERNAME")
464
601
  @knife_ec2_create.config[:winrm_user] = "Administrator"
465
602
  @knife_ec2_create.config[:winrm_password] = "password"
466
603
  @knife_ec2_create.config[:winrm_port] = 12345
@@ -482,44 +619,44 @@ describe Chef::Knife::Ec2ServerCreate do
482
619
  end
483
620
 
484
621
  it "should set the winrm username correctly" do
485
- @bootstrap.config[:winrm_user].should == @knife_ec2_create.config[:winrm_user]
622
+ expect(@bootstrap.config[:winrm_user]).to eq(@knife_ec2_create.config[:winrm_user])
486
623
  end
487
624
  it "should set the winrm password correctly" do
488
- @bootstrap.config[:winrm_password].should == @knife_ec2_create.config[:winrm_password]
625
+ expect(@bootstrap.config[:winrm_password]).to eq(@knife_ec2_create.config[:winrm_password])
489
626
  end
490
627
 
491
628
  it "should set the winrm port correctly" do
492
- @bootstrap.config[:winrm_port].should == @knife_ec2_create.config[:winrm_port]
629
+ expect(@bootstrap.config[:winrm_port]).to eq(@knife_ec2_create.config[:winrm_port])
493
630
  end
494
631
 
495
632
  it "should set the winrm transport layer correctly" do
496
- @bootstrap.config[:winrm_transport].should == @knife_ec2_create.config[:winrm_transport]
633
+ expect(@bootstrap.config[:winrm_transport]).to eq(@knife_ec2_create.config[:winrm_transport])
497
634
  end
498
635
 
499
636
  it "should set the kerberos realm correctly" do
500
- @bootstrap.config[:kerberos_realm].should == @knife_ec2_create.config[:kerberos_realm]
637
+ expect(@bootstrap.config[:kerberos_realm]).to eq(@knife_ec2_create.config[:kerberos_realm])
501
638
  end
502
639
 
503
640
  it "should set the kerberos service correctly" do
504
- @bootstrap.config[:kerberos_service].should == @knife_ec2_create.config[:kerberos_service]
641
+ expect(@bootstrap.config[:kerberos_service]).to eq(@knife_ec2_create.config[:kerberos_service])
505
642
  end
506
643
 
507
644
  it "should set the bootstrap 'name argument' to the Windows/AD hostname of the EC2 server" do
508
- @bootstrap.name_args.should == ["SERVERNAME"]
645
+ expect(@bootstrap.name_args).to eq(["SERVERNAME"])
509
646
  end
510
647
 
511
648
  it "should set the bootstrap 'name argument' to the hostname of the EC2 server when AD/Kerberos is not used" do
512
649
  @knife_ec2_create.config[:kerberos_realm] = nil
513
650
  @bootstrap = @knife_ec2_create.bootstrap_for_windows_node(@new_ec2_server, @new_ec2_server.dns_name)
514
- @bootstrap.name_args.should == ['ec2-75.101.253.10.compute-1.amazonaws.com']
651
+ expect(@bootstrap.name_args).to eq(['ec2-75.101.253.10.compute-1.amazonaws.com'])
515
652
  end
516
653
 
517
654
  it "should set the bootstrap 'first_boot_attributes' correctly" do
518
- @bootstrap.config[:first_boot_attributes].should == "{'my_attributes':{'foo':'bar'}"
655
+ expect(@bootstrap.config[:first_boot_attributes]).to eq("{'my_attributes':{'foo':'bar'}")
519
656
  end
520
657
 
521
658
  it "configures sets the bootstrap's run_list" do
522
- @bootstrap.config[:run_list].should == ['role[base]']
659
+ expect(@bootstrap.config[:run_list]).to eq(['role[base]'])
523
660
  end
524
661
 
525
662
  it "configures auth_timeout for bootstrap to default to 25 minutes" do
@@ -535,12 +672,12 @@ describe Chef::Knife::Ec2ServerCreate do
535
672
 
536
673
  describe "when validating the command-line parameters" do
537
674
  before do
538
- Fog::Compute::AWS.stub(:new).and_return(@ec2_connection)
539
- @knife_ec2_create.ui.stub(:error)
540
- @knife_ec2_create.ui.stub(:msg)
675
+ allow(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
676
+ allow(@knife_ec2_create.ui).to receive(:error)
677
+ allow(@knife_ec2_create.ui).to receive(:msg)
541
678
  end
542
679
 
543
- describe "when reading aws_credential_file" do
680
+ describe "when reading aws_credential_file" do
544
681
  before do
545
682
  Chef::Config[:knife].delete(:aws_access_key_id)
546
683
  Chef::Config[:knife].delete(:aws_secret_access_key)
@@ -551,49 +688,49 @@ describe Chef::Knife::Ec2ServerCreate do
551
688
  end
552
689
 
553
690
  it "reads UNIX Line endings" do
554
- File.stub(:read).
691
+ allow(File).to receive(:read).
555
692
  and_return("AWSAccessKeyId=#{@access_key_id}\nAWSSecretKey=#{@secret_key}")
556
693
  @knife_ec2_create.validate!
557
- Chef::Config[:knife][:aws_access_key_id].should == @access_key_id
558
- Chef::Config[:knife][:aws_secret_access_key].should == @secret_key
694
+ expect(Chef::Config[:knife][:aws_access_key_id]).to eq(@access_key_id)
695
+ expect(Chef::Config[:knife][:aws_secret_access_key]).to eq(@secret_key)
559
696
  end
560
697
 
561
698
  it "reads DOS Line endings" do
562
- File.stub(:read).
699
+ allow(File).to receive(:read).
563
700
  and_return("AWSAccessKeyId=#{@access_key_id}\r\nAWSSecretKey=#{@secret_key}")
564
701
  @knife_ec2_create.validate!
565
- Chef::Config[:knife][:aws_access_key_id].should == @access_key_id
566
- Chef::Config[:knife][:aws_secret_access_key].should == @secret_key
702
+ expect(Chef::Config[:knife][:aws_access_key_id]).to eq(@access_key_id)
703
+ expect(Chef::Config[:knife][:aws_secret_access_key]).to eq(@secret_key)
567
704
  end
568
705
  it "reads UNIX Line endings for new format" do
569
- File.stub(:read).
706
+ allow(File).to receive(:read).
570
707
  and_return("aws_access_key_id=#{@access_key_id}\naws_secret_access_key=#{@secret_key}")
571
708
  @knife_ec2_create.validate!
572
- Chef::Config[:knife][:aws_access_key_id].should == @access_key_id
573
- Chef::Config[:knife][:aws_secret_access_key].should == @secret_key
709
+ expect(Chef::Config[:knife][:aws_access_key_id]).to eq(@access_key_id)
710
+ expect(Chef::Config[:knife][:aws_secret_access_key]).to eq(@secret_key)
574
711
  end
575
712
 
576
713
  it "reads DOS Line endings for new format" do
577
- File.stub(:read).
714
+ allow(File).to receive(:read).
578
715
  and_return("aws_access_key_id=#{@access_key_id}\r\naws_secret_access_key=#{@secret_key}")
579
716
  @knife_ec2_create.validate!
580
- Chef::Config[:knife][:aws_access_key_id].should == @access_key_id
581
- Chef::Config[:knife][:aws_secret_access_key].should == @secret_key
717
+ expect(Chef::Config[:knife][:aws_access_key_id]).to eq(@access_key_id)
718
+ expect(Chef::Config[:knife][:aws_secret_access_key]).to eq(@secret_key)
582
719
  end
583
720
  end
584
721
 
585
722
  it 'understands that file:// validation key URIs are just paths' do
586
723
  Chef::Config[:knife][:validation_key_url] = 'file:///foo/bar'
587
- @knife_ec2_create.validation_key_path.should eq('/foo/bar')
724
+ expect(@knife_ec2_create.validation_key_path).to eq('/foo/bar')
588
725
  end
589
726
 
590
727
  it 'returns a path to a tmp file when presented with a URI for the ' \
591
728
  'validation key' do
592
729
  Chef::Config[:knife][:validation_key_url] = @validation_key_url
593
730
 
594
- @knife_ec2_create.stub_chain(:validation_key_tmpfile, :path).and_return(@validation_key_file)
731
+ allow(@knife_ec2_create).to receive_message_chain(:validation_key_tmpfile, :path).and_return(@validation_key_file)
595
732
 
596
- @knife_ec2_create.validation_key_path.should eq(@validation_key_file)
733
+ expect(@knife_ec2_create.validation_key_path).to eq(@validation_key_file)
597
734
  end
598
735
 
599
736
  it "disallows security group names when using a VPC" do
@@ -601,55 +738,101 @@ describe Chef::Knife::Ec2ServerCreate do
601
738
  @knife_ec2_create.config[:security_group_ids] = 'sg-aabbccdd'
602
739
  @knife_ec2_create.config[:security_groups] = 'groupname'
603
740
 
604
- lambda { @knife_ec2_create.validate! }.should raise_error SystemExit
741
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
605
742
  end
606
743
 
607
744
  it "disallows private ips when not using a VPC" do
608
745
  @knife_ec2_create.config[:private_ip_address] = '10.0.0.10'
609
746
 
610
- lambda { @knife_ec2_create.validate! }.should raise_error SystemExit
747
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
611
748
  end
612
749
 
613
750
  it "disallows specifying credentials file and aws keys" do
614
751
  Chef::Config[:knife][:aws_credential_file] = '/apple/pear'
615
- File.stub(:read).and_return("AWSAccessKeyId=b\nAWSSecretKey=a")
752
+ allow(File).to receive(:read).and_return("AWSAccessKeyId=b\nAWSSecretKey=a")
616
753
 
617
- lambda { @knife_ec2_create.validate! }.should raise_error SystemExit
754
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
618
755
  end
619
756
 
620
757
  it "disallows associate public ip option when not using a VPC" do
621
758
  @knife_ec2_create.config[:associate_public_ip] = true
622
759
  @knife_ec2_create.config[:subnet_id] = nil
623
760
 
624
- lambda { @knife_ec2_create.validate! }.should raise_error SystemExit
761
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
625
762
  end
626
763
 
627
764
  it "disallows ebs provisioned iops option when not using ebs volume type" do
628
765
  @knife_ec2_create.config[:ebs_provisioned_iops] = "123"
629
766
  @knife_ec2_create.config[:ebs_volume_type] = nil
630
767
 
631
- lambda { @knife_ec2_create.validate! }.should raise_error SystemExit
768
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
632
769
  end
633
770
 
634
771
  it "disallows ebs provisioned iops option when not using ebs volume type 'io1'" do
635
772
  @knife_ec2_create.config[:ebs_provisioned_iops] = "123"
636
773
  @knife_ec2_create.config[:ebs_volume_type] = "standard"
637
774
 
638
- lambda { @knife_ec2_create.validate! }.should raise_error SystemExit
775
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
639
776
  end
640
777
 
641
778
  it "disallows ebs volume type if its other than 'io1' or 'gp2' or 'standard'" do
642
779
  @knife_ec2_create.config[:ebs_provisioned_iops] = "123"
643
780
  @knife_ec2_create.config[:ebs_volume_type] = 'invalid'
644
781
 
645
- lambda { @knife_ec2_create.validate! }.should raise_error SystemExit
782
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
646
783
  end
647
784
 
648
785
  it "disallows 'io1' ebs volume type when not using ebs provisioned iops" do
649
786
  @knife_ec2_create.config[:ebs_provisioned_iops] = nil
650
787
  @knife_ec2_create.config[:ebs_volume_type] = 'io1'
651
788
 
652
- lambda { @knife_ec2_create.validate! }.should raise_error SystemExit
789
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
790
+ end
791
+
792
+ context "when ebs_encrypted option specified" do
793
+ it "not raise any validation error if valid ebs_size specified" do
794
+ @knife_ec2_create.config[:ebs_size] = "8"
795
+ @knife_ec2_create.config[:flavor] = "m3.medium"
796
+ @knife_ec2_create.config[:ebs_encrypted] = true
797
+ expect(@knife_ec2_create.ui).to_not receive(:error).with(" --ebs-encrypted option requires valid --ebs-size to be specified.")
798
+ @knife_ec2_create.validate!
799
+ end
800
+
801
+ it "raise error on missing ebs_size" do
802
+ @knife_ec2_create.config[:ebs_size] = nil
803
+ @knife_ec2_create.config[:flavor] = "m3.medium"
804
+ @knife_ec2_create.config[:ebs_encrypted] = true
805
+ expect(@knife_ec2_create.ui).to receive(:error).with(" --ebs-encrypted option requires valid --ebs-size to be specified.")
806
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
807
+ end
808
+
809
+ it "raise error if invalid ebs_size specified for 'standard' VolumeType" do
810
+ @knife_ec2_create.config[:ebs_size] = "1055"
811
+ @knife_ec2_create.config[:ebs_volume_type] = 'standard'
812
+ @knife_ec2_create.config[:flavor] = "m3.medium"
813
+ @knife_ec2_create.config[:ebs_encrypted] = true
814
+ expect(@knife_ec2_create.ui).to receive(:error).with(" --ebs-size should be in between 1-1024 for 'standard' ebs volume type.")
815
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
816
+ end
817
+
818
+ it "raise error on invalid ebs_size specified for 'gp2' VolumeType" do
819
+ @knife_ec2_create.config[:ebs_size] = "16500"
820
+ @knife_ec2_create.config[:ebs_volume_type] = 'gp2'
821
+ @knife_ec2_create.config[:flavor] = "m3.medium"
822
+ @knife_ec2_create.config[:ebs_encrypted] = true
823
+ expect(@knife_ec2_create.ui).to receive(:error).with(" --ebs-size should be in between 1-16384 for 'gp2' ebs volume type.")
824
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
825
+ end
826
+
827
+ it "raise error on invalid ebs_size specified for 'io1' VolumeType" do
828
+ @knife_ec2_create.config[:ebs_size] = "3"
829
+ @knife_ec2_create.config[:ebs_provisioned_iops] = "200"
830
+ @knife_ec2_create.config[:ebs_volume_type] = 'io1'
831
+ @knife_ec2_create.config[:flavor] = "m3.medium"
832
+ @knife_ec2_create.config[:ebs_encrypted] = true
833
+ expect(@knife_ec2_create.ui).to receive(:error).with(" --ebs-size should be in between 4-16384 for 'io1' ebs volume type.")
834
+ expect { @knife_ec2_create.validate! }.to raise_error SystemExit
835
+ end
653
836
  end
654
837
  end
655
838
 
@@ -662,7 +845,15 @@ describe Chef::Knife::Ec2ServerCreate do
662
845
 
663
846
  it "creates a connection without access keys" do
664
847
  @knife_ec2_create.config[:use_iam_profile] = true
665
- Fog::Compute::AWS.should_receive(:new).with(hash_including(:use_iam_profile => true)).and_return(@ec2_connection)
848
+ expect(Fog::Compute::AWS).to receive(:new).with(hash_including(:use_iam_profile => true)).and_return(@ec2_connection)
849
+ @knife_ec2_create.connection
850
+ end
851
+ end
852
+
853
+ describe "when aws_session_token is present" do
854
+ it "creates a connection using the session token" do
855
+ @knife_ec2_create.config[:aws_session_token] = 'session-token'
856
+ expect(Fog::Compute::AWS).to receive(:new).with(hash_including(:aws_session_token => 'session-token')).and_return(@ec2_connection)
666
857
  @knife_ec2_create.connection
667
858
  end
668
859
  end
@@ -670,28 +861,28 @@ describe Chef::Knife::Ec2ServerCreate do
670
861
 
671
862
  describe "when creating the server definition" do
672
863
  before do
673
- Fog::Compute::AWS.stub(:new).and_return(@ec2_connection)
864
+ allow(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
674
865
  end
675
866
 
676
867
  it "sets the specified placement_group" do
677
868
  @knife_ec2_create.config[:placement_group] = ['some_placement_group']
678
869
  server_def = @knife_ec2_create.create_server_def
679
870
 
680
- server_def[:placement_group].should == ['some_placement_group']
871
+ expect(server_def[:placement_group]).to eq(['some_placement_group'])
681
872
  end
682
873
 
683
874
  it "sets the specified security group names" do
684
875
  @knife_ec2_create.config[:security_groups] = ['groupname']
685
876
  server_def = @knife_ec2_create.create_server_def
686
877
 
687
- server_def[:groups].should == ['groupname']
878
+ expect(server_def[:groups]).to eq(['groupname'])
688
879
  end
689
880
 
690
881
  it "sets the specified security group ids" do
691
882
  @knife_ec2_create.config[:security_group_ids] = ['sg-aabbccdd']
692
883
  server_def = @knife_ec2_create.create_server_def
693
884
 
694
- server_def[:security_group_ids].should == ['sg-aabbccdd']
885
+ expect(server_def[:security_group_ids]).to eq(['sg-aabbccdd'])
695
886
  end
696
887
 
697
888
  it "sets the image id from CLI arguments over knife config" do
@@ -699,7 +890,7 @@ describe Chef::Knife::Ec2ServerCreate do
699
890
  Chef::Config[:knife][:image] = "ami-zzz"
700
891
  server_def = @knife_ec2_create.create_server_def
701
892
 
702
- server_def[:image_id].should == "ami-aaa"
893
+ expect(server_def[:image_id]).to eq("ami-aaa")
703
894
  end
704
895
 
705
896
  it "sets the flavor id from CLI arguments over knife config" do
@@ -707,7 +898,7 @@ describe Chef::Knife::Ec2ServerCreate do
707
898
  Chef::Config[:knife][:flavor] = "bitty"
708
899
  server_def = @knife_ec2_create.create_server_def
709
900
 
710
- server_def[:flavor_id].should == "massive"
901
+ expect(server_def[:flavor_id]).to eq("massive")
711
902
  end
712
903
 
713
904
  it "sets the availability zone from CLI arguments over knife config" do
@@ -715,17 +906,17 @@ describe Chef::Knife::Ec2ServerCreate do
715
906
  Chef::Config[:knife][:availability_zone] = "dat-one"
716
907
  server_def = @knife_ec2_create.create_server_def
717
908
 
718
- server_def[:availability_zone].should == "dis-one"
909
+ expect(server_def[:availability_zone]).to eq("dis-one")
719
910
  end
720
911
 
721
912
  it "adds the specified ephemeral device mappings" do
722
913
  @knife_ec2_create.config[:ephemeral] = [ "/dev/sdb", "/dev/sdc", "/dev/sdd", "/dev/sde" ]
723
914
  server_def = @knife_ec2_create.create_server_def
724
915
 
725
- server_def[:block_device_mapping].should == [{ "VirtualName" => "ephemeral0", "DeviceName" => "/dev/sdb" },
916
+ expect(server_def[:block_device_mapping]).to eq([{ "VirtualName" => "ephemeral0", "DeviceName" => "/dev/sdb" },
726
917
  { "VirtualName" => "ephemeral1", "DeviceName" => "/dev/sdc" },
727
918
  { "VirtualName" => "ephemeral2", "DeviceName" => "/dev/sdd" },
728
- { "VirtualName" => "ephemeral3", "DeviceName" => "/dev/sde" }]
919
+ { "VirtualName" => "ephemeral3", "DeviceName" => "/dev/sde" }])
729
920
  end
730
921
 
731
922
  it "sets the specified private ip address" do
@@ -733,94 +924,105 @@ describe Chef::Knife::Ec2ServerCreate do
733
924
  @knife_ec2_create.config[:private_ip_address] = '10.0.0.10'
734
925
  server_def = @knife_ec2_create.create_server_def
735
926
 
736
- server_def[:subnet_id].should == 'subnet-1a2b3c4d'
737
- server_def[:private_ip_address].should == '10.0.0.10'
927
+ expect(server_def[:subnet_id]).to eq('subnet-1a2b3c4d')
928
+ expect(server_def[:private_ip_address]).to eq('10.0.0.10')
738
929
  end
739
930
 
740
931
  it "sets the IAM server role when one is specified" do
741
932
  @knife_ec2_create.config[:iam_instance_profile] = ['iam-role']
742
933
  server_def = @knife_ec2_create.create_server_def
743
934
 
744
- server_def[:iam_instance_profile_name].should == ['iam-role']
935
+ expect(server_def[:iam_instance_profile_name]).to eq(['iam-role'])
745
936
  end
746
937
 
747
938
  it "doesn't set an IAM server role by default" do
748
939
  server_def = @knife_ec2_create.create_server_def
749
940
 
750
- server_def[:iam_instance_profile_name].should == nil
941
+ expect(server_def[:iam_instance_profile_name]).to eq(nil)
751
942
  end
752
943
 
753
944
  it "doesn't use IAM profile by default" do
754
945
  server_def = @knife_ec2_create.create_server_def
755
946
 
756
- server_def[:use_iam_profile].should == nil
947
+ expect(server_def[:use_iam_profile]).to eq(nil)
757
948
  end
758
-
949
+
759
950
  it 'Set Tenancy Dedicated when both VPC mode and Flag is True' do
760
951
  @knife_ec2_create.config[:dedicated_instance] = true
761
- @knife_ec2_create.stub(:vpc_mode? => true)
762
-
952
+ allow(@knife_ec2_create).to receive_messages(:vpc_mode? => true)
763
953
  server_def = @knife_ec2_create.create_server_def
764
- server_def[:tenancy].should == "dedicated"
954
+ expect(server_def[:tenancy]).to eq("dedicated")
765
955
  end
766
-
956
+
767
957
  it 'Tenancy should be default with no vpc mode even is specified' do
768
958
  @knife_ec2_create.config[:dedicated_instance] = true
769
-
770
959
  server_def = @knife_ec2_create.create_server_def
771
- server_def[:tenancy].should == nil
960
+ expect(server_def[:tenancy]).to eq(nil)
772
961
  end
773
-
962
+
774
963
  it 'Tenancy should be default with vpc but not requested' do
775
- @knife_ec2_create.stub(:vpc_mode? => true)
776
-
964
+ allow(@knife_ec2_create).to receive_messages(:vpc_mode? => true)
777
965
  server_def = @knife_ec2_create.create_server_def
778
- server_def[:tenancy].should == nil
966
+ expect(server_def[:tenancy]).to eq(nil)
779
967
  end
780
-
968
+
781
969
  it "sets associate_public_ip to true if specified and in vpc_mode" do
782
970
  @knife_ec2_create.config[:subnet_id] = 'subnet-1a2b3c4d'
783
971
  @knife_ec2_create.config[:associate_public_ip] = true
784
972
  server_def = @knife_ec2_create.create_server_def
785
973
 
786
- server_def[:subnet_id].should == 'subnet-1a2b3c4d'
787
- server_def[:associate_public_ip].should == true
974
+ expect(server_def[:subnet_id]).to eq('subnet-1a2b3c4d')
975
+ expect(server_def[:associate_public_ip]).to eq(true)
788
976
  end
789
977
 
790
978
  it "sets the spot price" do
791
979
  @knife_ec2_create.config[:spot_price] = '1.99'
792
980
  server_def = @knife_ec2_create.create_server_def
793
981
 
794
- server_def[:price].should == '1.99'
982
+ expect(server_def[:price]).to eq('1.99')
983
+ end
984
+
985
+ it "sets the spot instance request type as persistent" do
986
+ @knife_ec2_create.config[:spot_request_type] = 'persistent'
987
+ server_def = @knife_ec2_create.create_server_def
988
+
989
+ expect(server_def[:request_type]).to eq('persistent')
990
+ end
991
+
992
+ it "sets the spot instance request type as one-time" do
993
+ @knife_ec2_create.config[:spot_request_type] = 'one-time'
994
+ server_def = @knife_ec2_create.create_server_def
995
+
996
+ expect(server_def[:request_type]).to eq('one-time')
795
997
  end
796
998
 
797
999
  context "when using ebs volume type and ebs provisioned iops rate options" do
798
1000
  before do
799
- @knife_ec2_create.stub_chain(:ami, :root_device_type).and_return("ebs")
800
- @knife_ec2_create.stub_chain(:ami, :block_device_mapping).and_return([{"iops" => 123}])
801
- @knife_ec2_create.stub(:msg)
802
- @knife_ec2_create.stub(:puts)
1001
+ allow(@knife_ec2_create).to receive_message_chain(:ami, :root_device_type).and_return("ebs")
1002
+ allow(@knife_ec2_create).to receive_message_chain(:ami, :block_device_mapping).and_return([{"iops" => 123}])
1003
+ allow(@knife_ec2_create).to receive(:msg)
1004
+ allow(@knife_ec2_create).to receive(:puts)
803
1005
  end
804
1006
 
805
1007
  it "sets the specified 'standard' ebs volume type" do
806
1008
  @knife_ec2_create.config[:ebs_volume_type] = 'standard'
807
1009
  server_def = @knife_ec2_create.create_server_def
808
1010
 
809
- server_def[:block_device_mapping].first['Ebs.VolumeType'].should == 'standard'
1011
+ expect(server_def[:block_device_mapping].first['Ebs.VolumeType']).to eq('standard')
810
1012
  end
811
1013
 
812
1014
  it "sets the specified 'io1' ebs volume type" do
813
1015
  @knife_ec2_create.config[:ebs_volume_type] = 'io1'
814
1016
  server_def = @knife_ec2_create.create_server_def
815
1017
 
816
- server_def[:block_device_mapping].first['Ebs.VolumeType'].should == 'io1'
1018
+ expect(server_def[:block_device_mapping].first['Ebs.VolumeType']).to eq('io1')
817
1019
  end
818
1020
 
819
1021
  it "sets the specified 'gp2' ebs volume type" do
820
1022
  @knife_ec2_create.config[:ebs_volume_type] = 'gp2'
821
1023
  server_def = @knife_ec2_create.create_server_def
822
1024
 
823
- server_def[:block_device_mapping].first['Ebs.VolumeType'].should == 'gp2'
1025
+ expect(server_def[:block_device_mapping].first['Ebs.VolumeType']).to eq('gp2')
824
1026
  end
825
1027
 
826
1028
  it "sets the specified ebs provisioned iops rate" do
@@ -828,20 +1030,20 @@ describe Chef::Knife::Ec2ServerCreate do
828
1030
  @knife_ec2_create.config[:ebs_volume_type] = 'io1'
829
1031
  server_def = @knife_ec2_create.create_server_def
830
1032
 
831
- server_def[:block_device_mapping].first['Ebs.Iops'].should == '1234'
1033
+ expect(server_def[:block_device_mapping].first['Ebs.Iops']).to eq('1234')
832
1034
  end
833
1035
 
834
1036
  it "disallows non integer ebs provisioned iops rate" do
835
1037
  @knife_ec2_create.config[:ebs_provisioned_iops] = "123abcd"
836
1038
 
837
- lambda { @knife_ec2_create.create_server_def }.should raise_error SystemExit
1039
+ expect { @knife_ec2_create.create_server_def }.to raise_error SystemExit
838
1040
  end
839
1041
 
840
1042
  it "sets the iops rate from ami" do
841
1043
  @knife_ec2_create.config[:ebs_volume_type] = 'io1'
842
1044
  server_def = @knife_ec2_create.create_server_def
843
1045
 
844
- server_def[:block_device_mapping].first['Ebs.Iops'].should == '123'
1046
+ expect(server_def[:block_device_mapping].first['Ebs.Iops']).to eq('123')
845
1047
  end
846
1048
  end
847
1049
  end
@@ -851,15 +1053,15 @@ describe Chef::Knife::Ec2ServerCreate do
851
1053
  let(:hostname) { 'test.host.com' }
852
1054
 
853
1055
  it "should wait for tunnelled ssh if a ssh gateway is provided" do
854
- @knife_ec2_create.stub(:get_ssh_gateway_for).and_return(gateway)
855
- @knife_ec2_create.should_receive(:wait_for_tunnelled_sshd).with(gateway, hostname)
1056
+ allow(@knife_ec2_create).to receive(:get_ssh_gateway_for).and_return(gateway)
1057
+ expect(@knife_ec2_create).to receive(:wait_for_tunnelled_sshd).with(gateway, hostname)
856
1058
  @knife_ec2_create.wait_for_sshd(hostname)
857
1059
  end
858
1060
 
859
1061
  it "should wait for direct ssh if a ssh gateway is not provided" do
860
- @knife_ec2_create.stub(:get_ssh_gateway_for).and_return(nil)
1062
+ allow(@knife_ec2_create).to receive(:get_ssh_gateway_for).and_return(nil)
861
1063
  @knife_ec2_create.config[:ssh_port] = 22
862
- @knife_ec2_create.should_receive(:wait_for_direct_sshd).with(hostname, 22)
1064
+ expect(@knife_ec2_create).to receive(:wait_for_direct_sshd).with(hostname, 22)
863
1065
  @knife_ec2_create.wait_for_sshd(hostname)
864
1066
  end
865
1067
  end
@@ -869,70 +1071,70 @@ describe Chef::Knife::Ec2ServerCreate do
869
1071
  let(:hostname) { 'test.host.com' }
870
1072
 
871
1073
  it "should give precedence to the ssh gateway specified in the knife configuration" do
872
- Net::SSH::Config.stub(:for).and_return(:proxy => Net::SSH::Proxy::Command.new("ssh some.other.gateway.com nc %h %p"))
1074
+ allow(Net::SSH::Config).to receive(:for).and_return(:proxy => Net::SSH::Proxy::Command.new("ssh some.other.gateway.com nc %h %p"))
873
1075
  @knife_ec2_create.config[:ssh_gateway] = gateway
874
- @knife_ec2_create.get_ssh_gateway_for(hostname).should == gateway
1076
+ expect(@knife_ec2_create.get_ssh_gateway_for(hostname)).to eq(gateway)
875
1077
  end
876
1078
 
877
1079
  it "should return the ssh gateway specified in the ssh configuration even if the config option is not set" do
878
1080
  # This should already be false, but test this explicitly for regression
879
1081
  @knife_ec2_create.config[:ssh_gateway] = false
880
- Net::SSH::Config.stub(:for).and_return(:proxy => Net::SSH::Proxy::Command.new("ssh #{gateway} nc %h %p"))
881
- @knife_ec2_create.get_ssh_gateway_for(hostname).should == gateway
1082
+ allow(Net::SSH::Config).to receive(:for).and_return(:proxy => Net::SSH::Proxy::Command.new("ssh #{gateway} nc %h %p"))
1083
+ expect(@knife_ec2_create.get_ssh_gateway_for(hostname)).to eq(gateway)
882
1084
  end
883
1085
 
884
1086
  it "should return nil if the ssh gateway cannot be parsed from the ssh proxy command" do
885
- Net::SSH::Config.stub(:for).and_return(:proxy => Net::SSH::Proxy::Command.new("cannot parse host"))
886
- @knife_ec2_create.get_ssh_gateway_for(hostname).should be_nil
1087
+ allow(Net::SSH::Config).to receive(:for).and_return(:proxy => Net::SSH::Proxy::Command.new("cannot parse host"))
1088
+ expect(@knife_ec2_create.get_ssh_gateway_for(hostname)).to be_nil
887
1089
  end
888
1090
 
889
1091
  it "should return nil if the ssh proxy is not a proxy command" do
890
- Net::SSH::Config.stub(:for).and_return(:proxy => Net::SSH::Proxy::HTTP.new("httphost.com"))
891
- @knife_ec2_create.get_ssh_gateway_for(hostname).should be_nil
1092
+ allow(Net::SSH::Config).to receive(:for).and_return(:proxy => Net::SSH::Proxy::HTTP.new("httphost.com"))
1093
+ expect(@knife_ec2_create.get_ssh_gateway_for(hostname)).to be_nil
892
1094
  end
893
1095
 
894
1096
  it "returns nil if the ssh config has no proxy" do
895
- Net::SSH::Config.stub(:for).and_return(:user => "darius")
896
- @knife_ec2_create.get_ssh_gateway_for(hostname).should be_nil
1097
+ allow(Net::SSH::Config).to receive(:for).and_return(:user => "darius")
1098
+ expect(@knife_ec2_create.get_ssh_gateway_for(hostname)).to be_nil
897
1099
  end
898
1100
 
899
1101
  end
900
1102
 
901
1103
  describe "ssh_connect_host" do
902
1104
  before(:each) do
903
- @new_ec2_server.stub(
1105
+ allow(@new_ec2_server).to receive_messages(
904
1106
  :dns_name => 'public_name',
905
1107
  :private_ip_address => 'private_ip',
906
1108
  :custom => 'custom',
907
1109
  :public_ip_address => '111.111.111.111'
908
1110
  )
909
- @knife_ec2_create.stub(:server => @new_ec2_server)
1111
+ allow(@knife_ec2_create).to receive_messages(:server => @new_ec2_server)
910
1112
  end
911
1113
 
912
1114
  describe "by default" do
913
1115
  it 'should use public dns name' do
914
- @knife_ec2_create.ssh_connect_host.should == 'public_name'
1116
+ expect(@knife_ec2_create.ssh_connect_host).to eq('public_name')
915
1117
  end
916
1118
  end
917
1119
 
918
1120
  describe "when dns name not exist" do
919
1121
  it 'should use public_ip_address ' do
920
- @new_ec2_server.stub(:dns_name).and_return(nil)
921
- @knife_ec2_create.ssh_connect_host.should == '111.111.111.111'
1122
+ allow(@new_ec2_server).to receive(:dns_name).and_return(nil)
1123
+ expect(@knife_ec2_create.ssh_connect_host).to eq('111.111.111.111')
922
1124
  end
923
1125
  end
924
1126
 
925
1127
  describe "with vpc_mode?" do
926
1128
  it 'should use private ip' do
927
- @knife_ec2_create.stub(:vpc_mode? => true)
928
- @knife_ec2_create.ssh_connect_host.should == 'private_ip'
1129
+ allow(@knife_ec2_create).to receive_messages(:vpc_mode? => true)
1130
+ expect(@knife_ec2_create.ssh_connect_host).to eq('private_ip')
929
1131
  end
930
1132
  end
931
1133
 
932
1134
  describe "with custom server attribute" do
933
1135
  it 'should use custom server attribute' do
934
1136
  @knife_ec2_create.config[:server_connect_attribute] = 'custom'
935
- @knife_ec2_create.ssh_connect_host.should == 'custom'
1137
+ expect(@knife_ec2_create.ssh_connect_host).to eq('custom')
936
1138
  end
937
1139
  end
938
1140
  end
@@ -944,14 +1146,14 @@ describe Chef::Knife::Ec2ServerCreate do
944
1146
  let(:local_port) { 23 }
945
1147
 
946
1148
  before(:each) do
947
- @knife_ec2_create.stub(:configure_ssh_gateway).and_return(gateway)
1149
+ allow(@knife_ec2_create).to receive(:configure_ssh_gateway).and_return(gateway)
948
1150
  end
949
1151
 
950
1152
  it "should test ssh through a gateway" do
951
1153
  @knife_ec2_create.config[:ssh_port] = 22
952
- gateway.should_receive(:open).with(hostname, 22).and_yield(local_port)
953
- @knife_ec2_create.should_receive(:tcp_test_ssh).with('localhost', local_port).and_return(true)
954
- @knife_ec2_create.tunnel_test_ssh(gateway_host, hostname).should == true
1154
+ expect(gateway).to receive(:open).with(hostname, 22).and_yield(local_port)
1155
+ expect(@knife_ec2_create).to receive(:tcp_test_ssh).with('localhost', local_port).and_return(true)
1156
+ expect(@knife_ec2_create.tunnel_test_ssh(gateway_host, hostname)).to eq(true)
955
1157
  end
956
1158
  end
957
1159
 
@@ -960,46 +1162,46 @@ describe Chef::Knife::Ec2ServerCreate do
960
1162
  let(:gateway_user) { 'gateway_user' }
961
1163
 
962
1164
  it "configures a ssh gateway with no user and the default port when the SSH Config is empty" do
963
- Net::SSH::Config.stub(:for).and_return({})
964
- Net::SSH::Gateway.should_receive(:new).with(gateway_host, nil, :port => 22)
1165
+ allow(Net::SSH::Config).to receive(:for).and_return({})
1166
+ expect(Net::SSH::Gateway).to receive(:new).with(gateway_host, nil, :port => 22)
965
1167
  @knife_ec2_create.configure_ssh_gateway(gateway_host)
966
1168
  end
967
1169
 
968
1170
  it "configures a ssh gateway with the user specified in the SSH Config" do
969
- Net::SSH::Config.stub(:for).and_return({ :user => gateway_user })
970
- Net::SSH::Gateway.should_receive(:new).with(gateway_host, gateway_user, :port => 22)
1171
+ allow(Net::SSH::Config).to receive(:for).and_return({ :user => gateway_user })
1172
+ expect(Net::SSH::Gateway).to receive(:new).with(gateway_host, gateway_user, :port => 22)
971
1173
  @knife_ec2_create.configure_ssh_gateway(gateway_host)
972
1174
  end
973
1175
 
974
1176
  it "configures a ssh gateway with the user specified in the ssh gateway string" do
975
- Net::SSH::Config.stub(:for).and_return({ :user => gateway_user })
976
- Net::SSH::Gateway.should_receive(:new).with(gateway_host, 'override_user', :port => 22)
1177
+ allow(Net::SSH::Config).to receive(:for).and_return({ :user => gateway_user })
1178
+ expect(Net::SSH::Gateway).to receive(:new).with(gateway_host, 'override_user', :port => 22)
977
1179
  @knife_ec2_create.configure_ssh_gateway("override_user@#{gateway_host}")
978
1180
  end
979
1181
 
980
1182
  it "configures a ssh gateway with the port specified in the ssh gateway string" do
981
- Net::SSH::Config.stub(:for).and_return({})
982
- Net::SSH::Gateway.should_receive(:new).with(gateway_host, nil, :port => '24')
1183
+ allow(Net::SSH::Config).to receive(:for).and_return({})
1184
+ expect(Net::SSH::Gateway).to receive(:new).with(gateway_host, nil, :port => '24')
983
1185
  @knife_ec2_create.configure_ssh_gateway("#{gateway_host}:24")
984
1186
  end
985
1187
 
986
1188
  it "configures a ssh gateway with the keys specified in the SSH Config" do
987
- Net::SSH::Config.stub(:for).and_return({ :keys => ['configuredkey'] })
988
- Net::SSH::Gateway.should_receive(:new).with(gateway_host, nil, :port => 22, :keys => ['configuredkey'])
1189
+ allow(Net::SSH::Config).to receive(:for).and_return({ :keys => ['configuredkey'] })
1190
+ expect(Net::SSH::Gateway).to receive(:new).with(gateway_host, nil, :port => 22, :keys => ['configuredkey'])
989
1191
  @knife_ec2_create.configure_ssh_gateway(gateway_host)
990
1192
  end
991
1193
 
992
1194
  it "configures the ssh gateway with the key specified on the knife config / command line" do
993
1195
  @knife_ec2_create.config[:ssh_gateway_identity] = "/home/fireman/.ssh/gateway.pem"
994
1196
  #Net::SSH::Config.stub(:for).and_return({ :keys => ['configuredkey'] })
995
- Net::SSH::Gateway.should_receive(:new).with(gateway_host, nil, :port => 22, :keys => ['/home/fireman/.ssh/gateway.pem'])
1197
+ expect(Net::SSH::Gateway).to receive(:new).with(gateway_host, nil, :port => 22, :keys => ['/home/fireman/.ssh/gateway.pem'])
996
1198
  @knife_ec2_create.configure_ssh_gateway(gateway_host)
997
1199
  end
998
1200
 
999
1201
  it "prefers the knife config over the ssh config for the gateway keys" do
1000
1202
  @knife_ec2_create.config[:ssh_gateway_identity] = "/home/fireman/.ssh/gateway.pem"
1001
- Net::SSH::Config.stub(:for).and_return({ :keys => ['not_this_key_dude'] })
1002
- Net::SSH::Gateway.should_receive(:new).with(gateway_host, nil, :port => 22, :keys => ['/home/fireman/.ssh/gateway.pem'])
1203
+ allow(Net::SSH::Config).to receive(:for).and_return({ :keys => ['not_this_key_dude'] })
1204
+ expect(Net::SSH::Gateway).to receive(:new).with(gateway_host, nil, :port => 22, :keys => ['/home/fireman/.ssh/gateway.pem'])
1003
1205
  @knife_ec2_create.configure_ssh_gateway(gateway_host)
1004
1206
  end
1005
1207
  end
@@ -1008,24 +1210,24 @@ describe Chef::Knife::Ec2ServerCreate do
1008
1210
  # Normally we would only get the header after we send a client header, e.g. 'SSH-2.0-client'
1009
1211
  it "should return true if we get an ssh header" do
1010
1212
  @knife_ec2_create = Chef::Knife::Ec2ServerCreate.new
1011
- TCPSocket.stub(:new).and_return(StringIO.new("SSH-2.0-OpenSSH_6.1p1 Debian-4"))
1012
- IO.stub(:select).and_return(true)
1013
- @knife_ec2_create.should_receive(:tcp_test_ssh).and_yield.and_return(true)
1213
+ allow(TCPSocket).to receive(:new).and_return(StringIO.new("SSH-2.0-OpenSSH_6.1p1 Debian-4"))
1214
+ allow(IO).to receive(:select).and_return(true)
1215
+ expect(@knife_ec2_create).to receive(:tcp_test_ssh).and_yield.and_return(true)
1014
1216
  @knife_ec2_create.tcp_test_ssh("blackhole.ninja", 22) {nil}
1015
1217
  end
1016
1218
 
1017
1219
  it "should return false if we do not get an ssh header" do
1018
1220
  @knife_ec2_create = Chef::Knife::Ec2ServerCreate.new
1019
- TCPSocket.stub(:new).and_return(StringIO.new(""))
1020
- IO.stub(:select).and_return(true)
1021
- @knife_ec2_create.tcp_test_ssh("blackhole.ninja", 22).should be_falsey
1221
+ allow(TCPSocket).to receive(:new).and_return(StringIO.new(""))
1222
+ allow(IO).to receive(:select).and_return(true)
1223
+ expect(@knife_ec2_create.tcp_test_ssh("blackhole.ninja", 22)).to be_falsey
1022
1224
  end
1023
1225
 
1024
1226
  it "should return false if the socket isn't ready" do
1025
1227
  @knife_ec2_create = Chef::Knife::Ec2ServerCreate.new
1026
- TCPSocket.stub(:new)
1027
- IO.stub(:select).and_return(false)
1028
- @knife_ec2_create.tcp_test_ssh("blackhole.ninja", 22).should be_falsey
1228
+ allow(TCPSocket).to receive(:new)
1229
+ allow(IO).to receive(:select).and_return(false)
1230
+ expect(@knife_ec2_create.tcp_test_ssh("blackhole.ninja", 22)).to be_falsey
1029
1231
  end
1030
1232
  end
1031
1233
  end