knife-ec2 0.19.15 → 0.19.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,140 +0,0 @@
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::Ec2ServerDelete do
20
- before do
21
- end
22
-
23
- describe "run" do
24
- before(:each) do
25
- {
26
- image: "image",
27
- ssh_key_name: "ssh_key_name",
28
- aws_access_key_id: "aws_access_key_id",
29
- aws_secret_access_key: "aws_secret_access_key",
30
- }.each do |key, value|
31
- Chef::Config[:knife][key] = value
32
- end
33
-
34
- @ec2_server_attribs = { id: "i-39382318",
35
- flavor_id: "m1.small",
36
- image_id: "ami-47241231",
37
- availability_zone: "us-west-1",
38
- key_name: "my_ssh_key",
39
- groups: %w{group1 group2},
40
- security_group_ids: ["sg-00aa11bb"],
41
- dns_name: "ec2-75.101.253.10.compute-1.amazonaws.com",
42
- iam_instance_profile: {},
43
- public_ip_address: "75.101.253.10",
44
- private_dns_name: "ip-10-251-75-20.ec2.internal",
45
- private_ip_address: "10.251.75.20",
46
- root_device_type: "not_ebs",
47
- tags: { "Name" => "foo" },
48
- }
49
- @knife_ec2_delete = Chef::Knife::Ec2ServerDelete.new
50
- @ec2_servers = double()
51
- allow(@knife_ec2_delete.ui).to receive(:confirm)
52
- allow(@knife_ec2_delete).to receive(:msg_pair)
53
- @ec2_server = double(@ec2_server_attribs)
54
- @ec2_connection = double(Fog::Compute::AWS)
55
- allow(@ec2_connection).to receive(:servers).and_return(@ec2_servers)
56
- allow(@knife_ec2_delete.ui).to receive(:warn)
57
- end
58
-
59
- it "should invoke validate!" do
60
- knife_ec2_delete = Chef::Knife::Ec2ServerDelete.new
61
- expect(knife_ec2_delete).to receive(:validate!)
62
- knife_ec2_delete.run
63
- end
64
-
65
- it "should use invoke fog api to delete instance if instance id is passed" do
66
- expect(@ec2_servers).to receive(:get).with("foo").and_return(@ec2_server)
67
- expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
68
- @knife_ec2_delete.name_args = ["foo"]
69
- expect(@knife_ec2_delete).to receive(:validate!)
70
- expect(@ec2_server).to receive(:destroy)
71
- @knife_ec2_delete.run
72
- end
73
-
74
- it "should use node_name to figure out instance id if not specified explicitly" do
75
- expect(@ec2_servers).to receive(:get).with("foo").and_return(@ec2_server)
76
- expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
77
- expect(@knife_ec2_delete).to receive(:validate!)
78
- expect(@ec2_server).to receive(:destroy)
79
- @knife_ec2_delete.config[:purge] = false
80
- @knife_ec2_delete.config[:chef_node_name] = "baz"
81
- double_node = double(Chef::Node)
82
- expect(double_node).to receive(:attribute?).with("ec2").and_return(true)
83
- expect(double_node).to receive(:[]).with("ec2").and_return("instance_id" => "foo")
84
- double_search = double(Chef::Search::Query)
85
- expect(double_search).to receive(:search).with(:node, "name:baz").and_return([[double_node], nil, nil])
86
- expect(Chef::Search::Query).to receive(:new).and_return(double_search)
87
- @knife_ec2_delete.name_args = []
88
- @knife_ec2_delete.run
89
- end
90
-
91
- describe "when --purge is passed" do
92
- it "should use the node name if its set" do
93
- expect(@ec2_servers).to receive(:get).with("foo").and_return(@ec2_server)
94
- expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
95
- @knife_ec2_delete.name_args = ["foo"]
96
- expect(@knife_ec2_delete).to receive(:validate!)
97
- expect(@ec2_server).to receive(:destroy)
98
- @knife_ec2_delete.config[:purge] = true
99
- @knife_ec2_delete.config[:chef_node_name] = "baz"
100
- expect(Chef::Node).to receive(:load).with("baz").and_return(double(destroy: true))
101
- expect(Chef::ApiClient).to receive(:load).with("baz").and_return(double(destroy: true))
102
- @knife_ec2_delete.run
103
- end
104
-
105
- it "should search for the node name using the instance id when node name is not specified" do
106
- expect(@ec2_servers).to receive(:get).with("i-foo").and_return(@ec2_server)
107
- expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
108
- @knife_ec2_delete.name_args = ["i-foo"]
109
- expect(@knife_ec2_delete).to receive(:validate!)
110
- expect(@ec2_server).to receive(:destroy)
111
- @knife_ec2_delete.config[:purge] = true
112
- @knife_ec2_delete.config[:chef_node_name] = nil
113
- double_search = double(Chef::Search::Query)
114
- double_node = double(Chef::Node)
115
- expect(double_node).to receive(:name).and_return("baz")
116
- expect(Chef::Node).to receive(:load).with("baz").and_return(double(destroy: true))
117
- expect(Chef::ApiClient).to receive(:load).with("baz").and_return(double(destroy: true))
118
- expect(double_search).to receive(:search).with(:node, "ec2_instance_id:i-foo").and_return([[double_node], nil, nil])
119
- expect(Chef::Search::Query).to receive(:new).and_return(double_search)
120
- @knife_ec2_delete.run
121
- end
122
-
123
- it "should use the instance id if search does not return anything" do
124
- expect(@ec2_servers).to receive(:get).with("i-foo").and_return(@ec2_server)
125
- expect(Fog::Compute::AWS).to receive(:new).and_return(@ec2_connection)
126
- @knife_ec2_delete.name_args = ["i-foo"]
127
- expect(@knife_ec2_delete).to receive(:validate!)
128
- expect(@ec2_server).to receive(:destroy)
129
- @knife_ec2_delete.config[:purge] = true
130
- @knife_ec2_delete.config[:chef_node_name] = nil
131
- expect(Chef::Node).to receive(:load).with("i-foo").and_return(double(destroy: true))
132
- expect(Chef::ApiClient).to receive(:load).with("i-foo").and_return(double(destroy: true))
133
- double_search = double(Chef::Search::Query)
134
- expect(double_search).to receive(:search).with(:node, "ec2_instance_id:i-foo").and_return([[], nil, nil])
135
- expect(Chef::Search::Query).to receive(:new).and_return(double_search)
136
- @knife_ec2_delete.run
137
- end
138
- end
139
- end
140
- end
@@ -1,131 +0,0 @@
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/config.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 +0,0 @@
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,75 +0,0 @@
1
- require File.expand_path("../../spec_helper", __FILE__)
2
- require "fog/aws"
3
-
4
- describe Chef::Knife::S3Source do
5
- before(:each) do
6
- @bucket_name = "mybucket"
7
- @test_file_path = "path/file.pem"
8
- @test_file_content = "TEST CONTENT\n"
9
-
10
- Fog.mock!
11
-
12
- {
13
- aws_access_key_id: "aws_access_key_id",
14
- aws_secret_access_key: "aws_secret_access_key",
15
- }.each do |key, value|
16
- Chef::Config[:knife][key] = value
17
- end
18
-
19
- fog = Fog::Storage::AWS.new(
20
- aws_access_key_id: "aws_access_key_id",
21
- aws_secret_access_key: "aws_secret_access_key"
22
- )
23
- test_dir_obj = fog.directories.create("key" => @bucket_name)
24
- test_file_obj = test_dir_obj.files.create("key" => @test_file_path)
25
- test_file_obj.body = @test_file_content
26
- test_file_obj.save
27
-
28
- @s3_connection = double(Fog::Storage::AWS)
29
- @s3_source = Chef::Knife::S3Source.new
30
- end
31
-
32
- context "for http URL format" do
33
- it "converts URI to path with leading / removed" do
34
- @s3_source.url = "http://s3.amazonaws.com/#{@bucket_name}/#{@test_file_path}"
35
- @s3_source.instance_eval { path }
36
- expect(@s3_source.instance_eval { path }).to eq(@test_file_path)
37
- end
38
-
39
- it "correctly retrieves the bucket name from the URI" do
40
- @s3_source.url = "http://s3.amazonaws.com/#{@bucket_name}/#{@test_file_path}"
41
- @s3_source.instance_eval { bucket }
42
- expect(@s3_source.instance_eval { bucket }).to eq(@bucket_name)
43
- end
44
-
45
- it "gets back the correct bucket contents" do
46
- @s3_source.url = "http://s3.amazonaws.com/#{@bucket_name}/#{@test_file_path}"
47
- expect(@s3_source.body).to eq(@test_file_content)
48
- end
49
-
50
- it "gets back a bucket object with bucket_obj" do
51
- @s3_source.url = "http://s3.amazonaws.com/#{@bucket_name}/#{@test_file_path}"
52
- @s3_source.instance_eval { bucket_obj }
53
- expect(@s3_source.instance_eval { bucket_obj }).to be_kind_of(Fog::Storage::AWS::Directory)
54
- end
55
- end
56
-
57
- context "for s3 URL format" do
58
- it "correctly retrieves the bucket name from the URI" do
59
- @s3_source.url = "s3://#{@bucket_name}/#{@test_file_path}"
60
- @s3_source.instance_eval { bucket }
61
- expect(@s3_source.instance_eval { bucket }).to eq(@bucket_name)
62
- end
63
-
64
- it "gets back the correct bucket contents" do
65
- @s3_source.url = "s3://#{@bucket_name}/#{@test_file_path}"
66
- expect(@s3_source.body).to eq(@test_file_content)
67
- end
68
-
69
- it "gets back a bucket object with bucket_obj" do
70
- @s3_source.url = "s3://#{@bucket_name}/#{@test_file_path}"
71
- @s3_source.instance_eval { bucket_obj }
72
- expect(@s3_source.instance_eval { bucket_obj }).to be_kind_of(Fog::Storage::AWS::Directory)
73
- end
74
- end
75
- end