knife-ec2 0.12.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,141 +1,141 @@
1
- # License:: Apache License, Version 2.0
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- #
15
-
16
- require File.expand_path('../../spec_helper', __FILE__)
17
- require 'fog'
18
-
19
-
20
- describe Chef::Knife::Ec2ServerDelete do
21
- before do
22
- end
23
-
24
- describe "run" do
25
- before(:each) do
26
- {
27
- :image => 'image',
28
- :ssh_key_name => 'ssh_key_name',
29
- :aws_access_key_id => 'aws_access_key_id',
30
- :aws_secret_access_key => 'aws_secret_access_key'
31
- }.each do |key, value|
32
- Chef::Config[:knife][key] = value
33
- end
34
-
35
- @ec2_server_attribs = { :id => 'i-39382318',
36
- :flavor_id => 'm1.small',
37
- :image_id => 'ami-47241231',
38
- :availability_zone => 'us-west-1',
39
- :key_name => 'my_ssh_key',
40
- :groups => ['group1', 'group2'],
41
- :security_group_ids => ['sg-00aa11bb'],
42
- :dns_name => 'ec2-75.101.253.10.compute-1.amazonaws.com',
43
- :iam_instance_profile => {},
44
- :public_ip_address => '75.101.253.10',
45
- :private_dns_name => 'ip-10-251-75-20.ec2.internal',
46
- :private_ip_address => '10.251.75.20',
47
- :root_device_type => 'not_ebs',
48
- :tags => {'Name' => 'foo'}
49
- }
50
- @knife_ec2_delete = Chef::Knife::Ec2ServerDelete.new
51
- @ec2_servers = double()
52
- allow(@knife_ec2_delete.ui).to receive(:confirm)
53
- allow(@knife_ec2_delete).to receive(:msg_pair)
54
- @ec2_server = double(@ec2_server_attribs)
55
- @ec2_connection = double(Fog::Compute::AWS)
56
- allow(@ec2_connection).to receive(:servers).and_return(@ec2_servers)
57
- allow(@knife_ec2_delete.ui).to receive(:warn)
58
- end
59
-
60
- it "should invoke validate!" do
61
- knife_ec2_delete = Chef::Knife::Ec2ServerDelete.new
62
- expect(knife_ec2_delete).to receive(:validate!)
63
- knife_ec2_delete.run
64
- end
65
-
66
- it "should use invoke fog api to delete instance if instance id is passed" do
67
- expect(@ec2_servers).to receive(:get).with('foo').and_return(@ec2_server)
68
- expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
69
- @knife_ec2_delete.name_args = ['foo']
70
- expect(@knife_ec2_delete).to receive(:validate!)
71
- expect(@ec2_server).to receive(:destroy)
72
- @knife_ec2_delete.run
73
- end
74
-
75
- it "should use node_name to figure out instance id if not specified explicitly" do
76
- expect(@ec2_servers).to receive(:get).with('foo').and_return(@ec2_server)
77
- expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
78
- expect(@knife_ec2_delete).to receive(:validate!)
79
- expect(@ec2_server).to receive(:destroy)
80
- @knife_ec2_delete.config[:purge] = false
81
- @knife_ec2_delete.config[:chef_node_name] = 'baz'
82
- double_node = double(Chef::Node)
83
- expect(double_node).to receive(:attribute?).with('ec2').and_return(true)
84
- expect(double_node).to receive(:[]).with('ec2').and_return('instance_id'=>'foo')
85
- double_search = double(Chef::Search::Query)
86
- expect(double_search).to receive(:search).with(:node,"name:baz").and_return([[double_node],nil,nil])
87
- expect(Chef::Search::Query).to receive(:new).and_return(double_search)
88
- @knife_ec2_delete.name_args = []
89
- @knife_ec2_delete.run
90
- end
91
-
92
- describe "when --purge is passed" do
93
- it "should use the node name if its set" do
94
- expect(@ec2_servers).to receive(:get).with('foo').and_return(@ec2_server)
95
- expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
96
- @knife_ec2_delete.name_args = ['foo']
97
- expect(@knife_ec2_delete).to receive(:validate!)
98
- expect(@ec2_server).to receive(:destroy)
99
- @knife_ec2_delete.config[:purge] = true
100
- @knife_ec2_delete.config[:chef_node_name] = 'baz'
101
- expect(Chef::Node).to receive(:load).with('baz').and_return(double(:destroy=>true))
102
- expect(Chef::ApiClient).to receive(:load).with('baz').and_return(double(:destroy=>true))
103
- @knife_ec2_delete.run
104
- end
105
-
106
- it "should search for the node name using the instance id when node name is not specified" do
107
- expect(@ec2_servers).to receive(:get).with('i-foo').and_return(@ec2_server)
108
- expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
109
- @knife_ec2_delete.name_args = ['i-foo']
110
- expect(@knife_ec2_delete).to receive(:validate!)
111
- expect(@ec2_server).to receive(:destroy)
112
- @knife_ec2_delete.config[:purge] = true
113
- @knife_ec2_delete.config[:chef_node_name] = nil
114
- double_search = double(Chef::Search::Query)
115
- double_node = double(Chef::Node)
116
- expect(double_node).to receive(:name).and_return("baz")
117
- expect(Chef::Node).to receive(:load).with('baz').and_return(double(:destroy=>true))
118
- expect(Chef::ApiClient).to receive(:load).with('baz').and_return(double(:destroy=>true))
119
- expect(double_search).to receive(:search).with(:node,"ec2_instance_id:i-foo").and_return([[double_node],nil,nil])
120
- expect(Chef::Search::Query).to receive(:new).and_return(double_search)
121
- @knife_ec2_delete.run
122
- end
123
-
124
- it "should use the instance id if search does not return anything" do
125
- expect(@ec2_servers).to receive(:get).with('i-foo').and_return(@ec2_server)
126
- expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
127
- @knife_ec2_delete.name_args = ['i-foo']
128
- expect(@knife_ec2_delete).to receive(:validate!)
129
- expect(@ec2_server).to receive(:destroy)
130
- @knife_ec2_delete.config[:purge] = true
131
- @knife_ec2_delete.config[:chef_node_name] = nil
132
- expect(Chef::Node).to receive(:load).with('i-foo').and_return(double(:destroy=>true))
133
- expect(Chef::ApiClient).to receive(:load).with('i-foo').and_return(double(:destroy=>true))
134
- double_search = double(Chef::Search::Query)
135
- expect(double_search).to receive(:search).with(:node,"ec2_instance_id:i-foo").and_return([[],nil,nil])
136
- expect(Chef::Search::Query).to receive(:new).and_return(double_search)
137
- @knife_ec2_delete.run
138
- end
139
- end
140
- end
141
- end
1
+ # License:: Apache License, Version 2.0
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+
16
+ require File.expand_path('../../spec_helper', __FILE__)
17
+ require 'fog/aws'
18
+
19
+
20
+ describe Chef::Knife::Ec2ServerDelete do
21
+ before do
22
+ end
23
+
24
+ describe "run" do
25
+ before(:each) do
26
+ {
27
+ :image => 'image',
28
+ :ssh_key_name => 'ssh_key_name',
29
+ :aws_access_key_id => 'aws_access_key_id',
30
+ :aws_secret_access_key => 'aws_secret_access_key'
31
+ }.each do |key, value|
32
+ Chef::Config[:knife][key] = value
33
+ end
34
+
35
+ @ec2_server_attribs = { :id => 'i-39382318',
36
+ :flavor_id => 'm1.small',
37
+ :image_id => 'ami-47241231',
38
+ :availability_zone => 'us-west-1',
39
+ :key_name => 'my_ssh_key',
40
+ :groups => ['group1', 'group2'],
41
+ :security_group_ids => ['sg-00aa11bb'],
42
+ :dns_name => 'ec2-75.101.253.10.compute-1.amazonaws.com',
43
+ :iam_instance_profile => {},
44
+ :public_ip_address => '75.101.253.10',
45
+ :private_dns_name => 'ip-10-251-75-20.ec2.internal',
46
+ :private_ip_address => '10.251.75.20',
47
+ :root_device_type => 'not_ebs',
48
+ :tags => {'Name' => 'foo'}
49
+ }
50
+ @knife_ec2_delete = Chef::Knife::Ec2ServerDelete.new
51
+ @ec2_servers = double()
52
+ allow(@knife_ec2_delete.ui).to receive(:confirm)
53
+ allow(@knife_ec2_delete).to receive(:msg_pair)
54
+ @ec2_server = double(@ec2_server_attribs)
55
+ @ec2_connection = double(Fog::Compute::AWS)
56
+ allow(@ec2_connection).to receive(:servers).and_return(@ec2_servers)
57
+ allow(@knife_ec2_delete.ui).to receive(:warn)
58
+ end
59
+
60
+ it "should invoke validate!" do
61
+ knife_ec2_delete = Chef::Knife::Ec2ServerDelete.new
62
+ expect(knife_ec2_delete).to receive(:validate!)
63
+ knife_ec2_delete.run
64
+ end
65
+
66
+ it "should use invoke fog api to delete instance if instance id is passed" do
67
+ expect(@ec2_servers).to receive(:get).with('foo').and_return(@ec2_server)
68
+ expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
69
+ @knife_ec2_delete.name_args = ['foo']
70
+ expect(@knife_ec2_delete).to receive(:validate!)
71
+ expect(@ec2_server).to receive(:destroy)
72
+ @knife_ec2_delete.run
73
+ end
74
+
75
+ it "should use node_name to figure out instance id if not specified explicitly" do
76
+ expect(@ec2_servers).to receive(:get).with('foo').and_return(@ec2_server)
77
+ expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
78
+ expect(@knife_ec2_delete).to receive(:validate!)
79
+ expect(@ec2_server).to receive(:destroy)
80
+ @knife_ec2_delete.config[:purge] = false
81
+ @knife_ec2_delete.config[:chef_node_name] = 'baz'
82
+ double_node = double(Chef::Node)
83
+ expect(double_node).to receive(:attribute?).with('ec2').and_return(true)
84
+ expect(double_node).to receive(:[]).with('ec2').and_return('instance_id'=>'foo')
85
+ double_search = double(Chef::Search::Query)
86
+ expect(double_search).to receive(:search).with(:node,"name:baz").and_return([[double_node],nil,nil])
87
+ expect(Chef::Search::Query).to receive(:new).and_return(double_search)
88
+ @knife_ec2_delete.name_args = []
89
+ @knife_ec2_delete.run
90
+ end
91
+
92
+ describe "when --purge is passed" do
93
+ it "should use the node name if its set" do
94
+ expect(@ec2_servers).to receive(:get).with('foo').and_return(@ec2_server)
95
+ expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
96
+ @knife_ec2_delete.name_args = ['foo']
97
+ expect(@knife_ec2_delete).to receive(:validate!)
98
+ expect(@ec2_server).to receive(:destroy)
99
+ @knife_ec2_delete.config[:purge] = true
100
+ @knife_ec2_delete.config[:chef_node_name] = 'baz'
101
+ expect(Chef::Node).to receive(:load).with('baz').and_return(double(:destroy=>true))
102
+ expect(Chef::ApiClient).to receive(:load).with('baz').and_return(double(:destroy=>true))
103
+ @knife_ec2_delete.run
104
+ end
105
+
106
+ it "should search for the node name using the instance id when node name is not specified" do
107
+ expect(@ec2_servers).to receive(:get).with('i-foo').and_return(@ec2_server)
108
+ expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
109
+ @knife_ec2_delete.name_args = ['i-foo']
110
+ expect(@knife_ec2_delete).to receive(:validate!)
111
+ expect(@ec2_server).to receive(:destroy)
112
+ @knife_ec2_delete.config[:purge] = true
113
+ @knife_ec2_delete.config[:chef_node_name] = nil
114
+ double_search = double(Chef::Search::Query)
115
+ double_node = double(Chef::Node)
116
+ expect(double_node).to receive(:name).and_return("baz")
117
+ expect(Chef::Node).to receive(:load).with('baz').and_return(double(:destroy=>true))
118
+ expect(Chef::ApiClient).to receive(:load).with('baz').and_return(double(:destroy=>true))
119
+ expect(double_search).to receive(:search).with(:node,"ec2_instance_id:i-foo").and_return([[double_node],nil,nil])
120
+ expect(Chef::Search::Query).to receive(:new).and_return(double_search)
121
+ @knife_ec2_delete.run
122
+ end
123
+
124
+ it "should use the instance id if search does not return anything" do
125
+ expect(@ec2_servers).to receive(:get).with('i-foo').and_return(@ec2_server)
126
+ expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
127
+ @knife_ec2_delete.name_args = ['i-foo']
128
+ expect(@knife_ec2_delete).to receive(:validate!)
129
+ expect(@ec2_server).to receive(:destroy)
130
+ @knife_ec2_delete.config[:purge] = true
131
+ @knife_ec2_delete.config[:chef_node_name] = nil
132
+ expect(Chef::Node).to receive(:load).with('i-foo').and_return(double(:destroy=>true))
133
+ expect(Chef::ApiClient).to receive(:load).with('i-foo').and_return(double(:destroy=>true))
134
+ double_search = double(Chef::Search::Query)
135
+ expect(double_search).to receive(:search).with(:node,"ec2_instance_id:i-foo").and_return([[],nil,nil])
136
+ expect(Chef::Search::Query).to receive(:new).and_return(double_search)
137
+ @knife_ec2_delete.run
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,131 @@
1
+ # License:: Apache License, Version 2.0
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+
16
+ require File.expand_path('../../spec_helper', __FILE__)
17
+ require 'fog/aws'
18
+
19
+ describe Chef::Knife::Ec2ServerList do
20
+
21
+ describe '#run' do
22
+ let(:knife_ec2_list) { Chef::Knife::Ec2ServerList.new }
23
+ let(:ec2_connection) { double(Fog::Compute::AWS) }
24
+ before do
25
+ allow(knife_ec2_list).to receive(:connection).and_return(ec2_connection)
26
+ end
27
+
28
+ it 'invokes validate!' do
29
+ ec2_servers = double()
30
+ allow(ec2_connection).to receive(:servers).and_return(ec2_servers)
31
+ allow(knife_ec2_list.ui).to receive(:warn)
32
+ expect(knife_ec2_list).to receive(:validate!)
33
+ knife_ec2_list.run
34
+ end
35
+
36
+ context 'when region is not specified' do
37
+ it 'shows warning that default region will be will be used' do
38
+ knife_ec2_list.config.delete(:region)
39
+ Chef::Config[:knife].delete(:region)
40
+ ec2_servers = double()
41
+ allow(ec2_connection).to receive(:servers).and_return(ec2_servers)
42
+ allow(knife_ec2_list).to receive(:validate!)
43
+ expect(knife_ec2_list.ui).to receive(:warn).with("No region was specified in knife.rb or as an argument. The default region, us-east-1, will be used:")
44
+ knife_ec2_list.run
45
+ end
46
+ end
47
+
48
+ context '--format option' do
49
+ context 'when format=summary' do
50
+ before do
51
+ knife_ec2_list.config[:format] = 'summary'
52
+ allow(knife_ec2_list.ui).to receive(:warn)
53
+ end
54
+
55
+ it 'shows the output without Tags and Availability Zone in summary format' do
56
+ output_column = ["Instance ID", "Public IP", "Private IP", "Flavor",
57
+ "Image", "SSH Key", "Security Groups", "IAM Profile", "State"]
58
+ output_column_count = output_column.length
59
+ allow(ec2_connection).to receive(:servers).and_return([])
60
+ allow(knife_ec2_list).to receive(:validate!)
61
+ expect(knife_ec2_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count)
62
+ knife_ec2_list.run
63
+ end
64
+ end
65
+
66
+ context 'when format=json' do
67
+ before do
68
+ knife_ec2_list.config[:format] = 'json'
69
+ allow(knife_ec2_list.ui).to receive(:warn)
70
+ end
71
+
72
+ it 'shows the output without Tags and Availability Zone in summary format' do
73
+ allow(ec2_connection).to receive(:servers).and_return([])
74
+ allow(knife_ec2_list).to receive(:validate!)
75
+ allow(knife_ec2_list).to receive(:format_for_display)
76
+ expect(knife_ec2_list).to receive(:output)
77
+ knife_ec2_list.run
78
+ end
79
+ end
80
+ end
81
+
82
+ context 'when --tags option is passed' do
83
+ before do
84
+ knife_ec2_list.config[:format] = 'summary'
85
+ allow(knife_ec2_list.ui).to receive(:warn)
86
+ allow(ec2_connection).to receive(:servers).and_return([])
87
+ allow(knife_ec2_list).to receive(:validate!)
88
+ end
89
+
90
+ context 'when single tag is passed' do
91
+ it 'shows single tag field in the output' do
92
+ knife_ec2_list.config[:tags] = 'tag1'
93
+ output_column = ["Instance ID", "Public IP", "Private IP", "Flavor",
94
+ "Image", "SSH Key", "Security Groups", "Tag:tag1", "IAM Profile", "State"]
95
+ output_column_count = output_column.length
96
+ expect(knife_ec2_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count)
97
+ knife_ec2_list.run
98
+ end
99
+ end
100
+
101
+ context 'when multiple tags are passed' do
102
+ it 'shows multiple tags fields in the output' do
103
+ knife_ec2_list.config[:tags] = 'tag1,tag2'
104
+ output_column = ["Instance ID", "Public IP", "Private IP", "Flavor",
105
+ "Image", "SSH Key", "Security Groups", "Tag:tag1", "Tag:tag2", "IAM Profile", "State"]
106
+ output_column_count = output_column.length
107
+ expect(knife_ec2_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count)
108
+ knife_ec2_list.run
109
+ end
110
+ end
111
+ end
112
+
113
+ context 'when --availability-zone is passed' do
114
+ before do
115
+ knife_ec2_list.config[:format] = 'summary'
116
+ allow(knife_ec2_list.ui).to receive(:warn)
117
+ allow(ec2_connection).to receive(:servers).and_return([])
118
+ allow(knife_ec2_list).to receive(:validate!)
119
+ end
120
+
121
+ it 'shows the availability zones in the output' do
122
+ knife_ec2_list.config[:az] = true
123
+ output_column = ["Instance ID", "Public IP", "Private IP", "Flavor", "AZ",
124
+ "Image", "SSH Key", "Security Groups", "IAM Profile", "State"]
125
+ output_column_count = output_column.length
126
+ expect(knife_ec2_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count)
127
+ knife_ec2_list.run
128
+ end
129
+ end
130
+ end
131
+ end
@@ -1,24 +1,24 @@
1
- require File.expand_path('../../spec_helper', __FILE__)
2
-
3
- #This spec can only be run separately from the rest due to inclusion of fog library in other specs.
4
- #rspec spec/unit/s3_source_deps_spec.rb
5
-
6
- describe 'Check Dependencies', :exclude => Object.constants.include?(:Fog) do
7
- before(:each) do
8
- end
9
- it 'should not load fog by default' do
10
- begin
11
- Fog::Storage::AWS.new()
12
- rescue Exception => e
13
- expect(e).to be_a_kind_of(NameError)
14
- end
15
- end
16
-
17
- it 'lazy loads fog' do
18
- begin
19
- Chef::Knife::S3Source.fetch('test')
20
- rescue Exception => e
21
- expect(e).to be_a_kind_of(ArgumentError)
22
- end
23
- end
24
- end
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ #This spec can only be run separately from the rest due to inclusion of fog library in other specs.
4
+ #rspec spec/unit/s3_source_deps_spec.rb
5
+
6
+ describe 'Check Dependencies', :exclude => Object.constants.include?(:Fog) do
7
+ before(:each) do
8
+ end
9
+ it 'should not load fog by default' do
10
+ begin
11
+ Fog::Storage::AWS.new()
12
+ rescue Exception => e
13
+ expect(e).to be_a_kind_of(NameError)
14
+ end
15
+ end
16
+
17
+ it 'lazy loads fog' do
18
+ begin
19
+ Chef::Knife::S3Source.fetch('test')
20
+ rescue Exception => e
21
+ expect(e).to be_a_kind_of(ArgumentError)
22
+ end
23
+ end
24
+ end