knife-openstack 0.10.0 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.travis.yml +6 -0
  4. data/CHANGELOG.md +20 -22
  5. data/Gemfile +11 -1
  6. data/README.md +6 -4
  7. data/Rakefile +33 -0
  8. data/knife-openstack.gemspec +7 -8
  9. data/lib/chef/knife/cloud/openstack_server_create_options.rb +75 -0
  10. data/lib/chef/knife/cloud/openstack_service.rb +62 -0
  11. data/lib/chef/knife/cloud/openstack_service_options.rb +50 -0
  12. data/lib/chef/knife/openstack_flavor_list.rb +37 -51
  13. data/lib/chef/knife/openstack_group_list.rb +38 -45
  14. data/lib/chef/knife/openstack_helpers.rb +30 -0
  15. data/lib/chef/knife/openstack_image_list.rb +42 -59
  16. data/lib/chef/knife/openstack_network_list.rb +25 -21
  17. data/lib/chef/knife/openstack_server_create.rb +166 -452
  18. data/lib/chef/knife/openstack_server_delete.rb +26 -106
  19. data/lib/chef/knife/openstack_server_list.rb +37 -59
  20. data/lib/chef/knife/openstack_server_show.rb +57 -0
  21. data/lib/knife-openstack/version.rb +1 -1
  22. data/spec/functional/flavor_list_func_spec.rb +45 -0
  23. data/spec/functional/group_list_func_spec.rb +67 -0
  24. data/spec/functional/image_list_func_spec.rb +51 -0
  25. data/spec/functional/network_list_func_spec.rb +44 -0
  26. data/spec/functional/server_create_func_spec.rb +118 -0
  27. data/spec/functional/server_delete_func_spec.rb +84 -0
  28. data/spec/functional/server_list_func_spec.rb +95 -0
  29. data/spec/functional/server_show_func_spec.rb +46 -0
  30. data/spec/integration/cleanup.rb +91 -0
  31. data/spec/integration/config/environment.yml.sample +13 -0
  32. data/spec/integration/openstack_spec.rb +618 -0
  33. data/spec/spec_helper.rb +126 -0
  34. data/spec/unit/openstack_flavor_list_spec.rb +30 -0
  35. data/spec/unit/openstack_group_list_spec.rb +43 -0
  36. data/spec/unit/openstack_image_list_spec.rb +32 -0
  37. data/spec/unit/openstack_network_list_spec.rb +39 -0
  38. data/spec/unit/openstack_server_create_spec.rb +344 -182
  39. data/spec/unit/openstack_server_delete_spec.rb +43 -0
  40. data/spec/unit/openstack_server_list_spec.rb +32 -0
  41. data/spec/unit/openstack_server_show_spec.rb +42 -0
  42. data/spec/unit/openstack_service_spec.rb +89 -0
  43. data/spec/unit/validate_spec.rb +55 -0
  44. metadata +95 -51
  45. data/lib/chef/knife/openstack_base.rb +0 -182
@@ -0,0 +1,44 @@
1
+ #
2
+ # Author:: Ameya Varade (<ameya.varade@clogeny.com>)
3
+ # Copyright:: Copyright (c) 2014 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require 'spec_helper'
19
+ require 'chef/knife/openstack_network_list'
20
+ require 'chef/knife/cloud/openstack_service'
21
+ require 'support/shared_examples_for_command'
22
+
23
+ describe Chef::Knife::Cloud::OpenstackNetworkList do
24
+ let (:instance) {Chef::Knife::Cloud::OpenstackNetworkList.new}
25
+
26
+ context "functionality" do
27
+ before do
28
+ resources = [ TestResource.new({:id => "resource-1", :name => "external", :tenant_id => "1", :shared => true}),
29
+ TestResource.new({:id => "resource-2", :name => "internal", :tenant_id => "2", :shared => false})
30
+ ]
31
+ allow(instance).to receive(:query_resource).and_return(resources)
32
+ allow(instance).to receive(:puts)
33
+ allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new)
34
+ allow(instance).to receive(:validate!)
35
+ end
36
+
37
+ it "lists formatted list of network resources" do
38
+ expect(instance.ui).to receive(:list).with(["Name", "ID", "Tenant", "Shared",
39
+ "external", "resource-1", "1", "true",
40
+ "internal", "resource-2", "2", "false"], :uneven_columns_across, 4)
41
+ instance.run
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,118 @@
1
+ #
2
+ # Author:: Mukta Aphale (<mukta.aphale@clogeny.com>)
3
+ # Author:: Kaustubh Deorukhkar (<kaustubh@clogeny.com>)
4
+ # Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>)
5
+ # Author:: Prabhu Das (<prabhu.das@clogeny.com>)
6
+ # Author:: Ameya Varade (<ameya.varade@clogeny.com>)
7
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
8
+ # License:: Apache License, Version 2.0
9
+ #
10
+ # Licensed under the Apache License, Version 2.0 (the "License");
11
+ # you may not use this file except in compliance with the License.
12
+ # You may obtain a copy of the License at
13
+ #
14
+ # http://www.apache.org/licenses/LICENSE-2.0
15
+ #
16
+ # Unless required by applicable law or agreed to in writing, software
17
+ # distributed under the License is distributed on an "AS IS" BASIS,
18
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ # See the License for the specific language governing permissions and
20
+ # limitations under the License.
21
+
22
+ require File.expand_path('../../spec_helper', __FILE__)
23
+
24
+ describe Chef::Knife::Cloud::OpenstackServerCreate do
25
+
26
+ before do
27
+ @knife_openstack_create = Chef::Knife::Cloud::OpenstackServerCreate.new
28
+ {
29
+ :image => 'image',
30
+ :openstack_username => 'openstack_username',
31
+ :openstack_password => 'openstack_password',
32
+ :openstack_auth_url => 'openstack_auth_url',
33
+ :server_create_timeout => 1000
34
+ }.each do |key, value|
35
+ Chef::Config[:knife][key] = value
36
+ end
37
+
38
+ @openstack_service = Chef::Knife::Cloud::OpenstackService.new
39
+ allow(@openstack_service).to receive(:msg_pair)
40
+ allow(@openstack_service).to receive(:print)
41
+ image = Object.new
42
+ allow(image).to receive(:id).and_return('image_id')
43
+ allow(@openstack_service).to receive(:get_image).and_return(image)
44
+ flavor = Object.new
45
+ allow(flavor).to receive(:id).and_return('flavor_id')
46
+ allow(@openstack_service).to receive(:get_flavor).and_return(flavor)
47
+
48
+ allow(@knife_openstack_create).to receive(:create_service_instance).and_return(@openstack_service)
49
+ allow(@knife_openstack_create).to receive(:puts)
50
+ @new_openstack_server = double()
51
+
52
+ @openstack_server_attribs = { :name => 'Mock Server',
53
+ :id => 'id-123456',
54
+ :key_name => 'key_name',
55
+ :flavor => 'flavor_id',
56
+ :image => 'image_id',
57
+ :addresses => {
58
+ 'public' => [{'addr' => '75.101.253.10'}],
59
+ 'private' => [{'addr' => '10.251.75.20'}]
60
+ },
61
+ :password => 'password'
62
+ }
63
+
64
+ @openstack_server_attribs.each_pair do |attrib, value|
65
+ allow(@new_openstack_server).to receive(attrib).and_return(value)
66
+ end
67
+ end
68
+
69
+ describe "run" do
70
+ before(:each) do
71
+ allow(@knife_openstack_create).to receive(:validate_params!)
72
+ allow(Fog::Compute::OpenStack).to receive_message_chain(:new, :servers, :create).and_return(@new_openstack_server)
73
+ @knife_openstack_create.config[:openstack_floating_ip] = '-1'
74
+ allow(@new_openstack_server).to receive(:wait_for)
75
+ end
76
+
77
+ context "for Linux" do
78
+ before do
79
+ @config = {:openstack_floating_ip=>"-1", :bootstrap_ip_address => "75.101.253.10", :ssh_password=>"password"}
80
+ @knife_openstack_create.config[:distro] = 'chef-full'
81
+ @bootstrapper = Chef::Knife::Cloud::Bootstrapper.new(@config)
82
+ @ssh_bootstrap_protocol = Chef::Knife::Cloud::SshBootstrapProtocol.new(@config)
83
+ @unix_distribution = Chef::Knife::Cloud::UnixDistribution.new(@config)
84
+ allow(@ssh_bootstrap_protocol).to receive(:send_bootstrap_command)
85
+ end
86
+
87
+ it "Creates an OpenStack instance and bootstraps it" do
88
+ expect(Chef::Knife::Cloud::Bootstrapper).to receive(:new).with(@config).and_return(@bootstrapper)
89
+ allow(@bootstrapper).to receive(:bootstrap).and_call_original
90
+ expect(@bootstrapper).to receive(:create_bootstrap_protocol).and_return(@ssh_bootstrap_protocol)
91
+ expect(@bootstrapper).to receive(:create_bootstrap_distribution).and_return(@unix_distribution)
92
+ expect(@openstack_service).to receive(:server_summary).exactly(2).times
93
+ @knife_openstack_create.run
94
+ end
95
+ end
96
+
97
+ context "for Windows" do
98
+ before do
99
+ @config = {:openstack_floating_ip=>"-1", :image_os_type => 'windows', :bootstrap_ip_address => "75.101.253.10", :bootstrap_protocol => 'winrm', :ssh_password=>"password"}
100
+ @knife_openstack_create.config[:image_os_type] = 'windows'
101
+ @knife_openstack_create.config[:bootstrap_protocol] = 'winrm'
102
+ @knife_openstack_create.config[:distro] = 'windows-chef-client-msi'
103
+ @bootstrapper = Chef::Knife::Cloud::Bootstrapper.new(@config)
104
+ @winrm_bootstrap_protocol = Chef::Knife::Cloud::WinrmBootstrapProtocol.new(@config)
105
+ @windows_distribution = Chef::Knife::Cloud::WindowsDistribution.new(@config)
106
+ end
107
+ it "Creates an OpenStack instance for Windows and bootstraps it" do
108
+ expect(Chef::Knife::Cloud::Bootstrapper).to receive(:new).with(@config).and_return(@bootstrapper)
109
+ allow(@bootstrapper).to receive(:bootstrap).and_call_original
110
+ expect(@bootstrapper).to receive(:create_bootstrap_protocol).and_return(@winrm_bootstrap_protocol)
111
+ expect(@bootstrapper).to receive(:create_bootstrap_distribution).and_return(@windows_distribution)
112
+ allow(@winrm_bootstrap_protocol).to receive(:send_bootstrap_command)
113
+ expect(@openstack_service).to receive(:server_summary).exactly(2).times
114
+ @knife_openstack_create.run
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,84 @@
1
+ #
2
+ # Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>)
3
+ # Author:: Prabhu Das (<prabhu.das@clogeny.com>)
4
+ # Author:: Ameya Varade (<ameya.varade@clogeny.com>)
5
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
6
+ # License:: Apache License, Version 2.0
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+
20
+ require File.expand_path('../../spec_helper', __FILE__)
21
+ require 'chef/knife/openstack_server_delete'
22
+ require 'chef/knife/cloud/openstack_service'
23
+
24
+ describe Chef::Knife::Cloud::OpenstackServerDelete do
25
+
26
+ before do
27
+ @openstack_connection = double(Fog::Compute::OpenStack)
28
+ @chef_node = double(Chef::Node)
29
+ @chef_client = double(Chef::ApiClient)
30
+ @knife_openstack_delete = Chef::Knife::Cloud::OpenstackServerDelete.new
31
+ {
32
+ :openstack_username => 'openstack_username',
33
+ :openstack_password => 'openstack_password',
34
+ :openstack_auth_url => 'openstack_auth_url'
35
+ }.each do |key, value|
36
+ Chef::Config[:knife][key] = value
37
+ end
38
+
39
+ @openstack_service = Chef::Knife::Cloud::OpenstackService.new
40
+ allow(@openstack_service).to receive(:msg_pair)
41
+ allow(@knife_openstack_delete).to receive(:create_service_instance).and_return(@openstack_service)
42
+ allow(@knife_openstack_delete.ui).to receive(:warn)
43
+ allow(@knife_openstack_delete.ui).to receive(:confirm)
44
+ @openstack_servers = double()
45
+ @running_openstack_server = double()
46
+ @openstack_server_attribs = { :name => 'Mock Server',
47
+ :id => 'id-123456',
48
+ :flavor => 'flavor_id',
49
+ :image => 'image_id',
50
+ :addresses => {
51
+ 'public' => [{'addr' => '75.101.253.10'}],
52
+ 'private' => [{'addr' => '10.251.75.20'}]
53
+ }
54
+ }
55
+
56
+ @openstack_server_attribs.each_pair do |attrib, value|
57
+ allow(@running_openstack_server).to receive(attrib).and_return(value)
58
+ end
59
+ @knife_openstack_delete.name_args = ['test001']
60
+ end
61
+
62
+ describe "run" do
63
+ it "deletes an OpenStack instance." do
64
+ expect(@openstack_servers).to receive(:get).and_return(@running_openstack_server)
65
+ expect(@openstack_connection).to receive(:servers).and_return(@openstack_servers)
66
+ expect(Fog::Compute::OpenStack).to receive(:new).and_return(@openstack_connection)
67
+ expect(@running_openstack_server).to receive(:destroy)
68
+ @knife_openstack_delete.run
69
+ end
70
+
71
+ it "deletes the instance along with the node and client on the chef-server when --purge is given as an option." do
72
+ @knife_openstack_delete.config[:purge] = true
73
+ expect(Chef::Node).to receive(:load).and_return(@chef_node)
74
+ expect(@chef_node).to receive(:destroy)
75
+ expect(Chef::ApiClient).to receive(:load).and_return(@chef_client)
76
+ expect(@chef_client).to receive(:destroy)
77
+ expect(@openstack_servers).to receive(:get).and_return(@running_openstack_server)
78
+ expect(@openstack_connection).to receive(:servers).and_return(@openstack_servers)
79
+ expect(Fog::Compute::OpenStack).to receive(:new).and_return(@openstack_connection)
80
+ expect(@running_openstack_server).to receive(:destroy)
81
+ @knife_openstack_delete.run
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,95 @@
1
+ #
2
+ # Author:: Mukta Aphale (<mukta.aphale@clogeny.com>)
3
+ # Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>)
4
+ # Author:: Prabhu Das (<prabhu.das@clogeny.com>)
5
+ # Author:: Ameya Varade (<ameya.varade@clogeny.com>)
6
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
7
+ # License:: Apache License, Version 2.0
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+
21
+ require 'spec_helper'
22
+ require 'chef/knife/openstack_server_list'
23
+ require 'chef/knife/cloud/openstack_service'
24
+
25
+ describe Chef::Knife::Cloud::OpenstackServerList do
26
+ let (:instance) {Chef::Knife::Cloud::OpenstackServerList.new}
27
+
28
+ context "functionality" do
29
+ before do
30
+ @resources = [ TestResource.new({:id => "resource-1", :name => "ubuntu01", :availability_zone => "test zone", :addresses => {"public"=>[{"version"=>4, "addr"=>"172.31.6.132"}], "private"=>[{"version"=>4, "addr"=>"172.31.6.133"}]}, :flavor => {"id" => "1"}, :image => {"id" => "image1"}, :key_name => "keypair", :state => "ACTIVE"}),
31
+ TestResource.new({:id => "resource-2", :name => "windows2008",:availability_zone => "test zone", :addresses => {"public"=>[{"version"=>4, "addr"=>"172.31.6.132"}]}, :flavor => {"id" => "id2"}, :image => {"id" => "image2"}, :key_name => "keypair", :state => "ACTIVE"}),
32
+ TestResource.new({:id => "resource-3-err", :name => "windows2008", :availability_zone => "test zone", :addresses => {"public"=>[], "private"=>[]}, :flavor => {"id" => "id2"}, :image => {"id" => "image2"}, :key_name => "keypair", :state => "ERROR"})
33
+ ]
34
+ allow(instance).to receive(:query_resource).and_return(@resources)
35
+ allow(instance).to receive(:puts)
36
+ allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::FogService.new)
37
+ allow(instance).to receive(:validate!)
38
+ end
39
+
40
+ it "lists formatted list of resources" do
41
+ expect(instance.ui).to receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone",
42
+ "ubuntu01", "resource-1", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone",
43
+ "windows2008", "resource-2", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", "windows2008", "resource-3-err", nil, nil, "id2", "image2", "keypair", "ERROR", "test zone"], :uneven_columns_across, 9)
44
+ instance.run
45
+ end
46
+
47
+ context "when chef-data and chef-node-attribute set" do
48
+ before(:each) do
49
+ @resources.push(TestResource.new({:id => "server-4", :name => "server-4", :availability_zone => "test zone", :addresses => {"public"=>[{"version"=>4, "addr"=>"172.31.6.132"}], "private"=>[{"version"=>4, "addr"=>"172.31.6.133"}]}, :flavor => {"id" => "1"}, :image => {"id" => "image1"}, :key_name => "keypair", :state => "ACTIVE"}))
50
+ @node = TestResource.new({:id => "server-4", :name => "server-4", :chef_environment => "_default", :fqdn => "testfqdnnode.us", :run_list => [], :tags => [], :platform => "ubuntu", :platform_family => "debian"})
51
+ allow(Chef::Node).to receive(:list).and_return({"server-4" => @node})
52
+ instance.config[:chef_data] = true
53
+ end
54
+
55
+ it "lists formatted list of resources on chef data option set" do
56
+ expect(instance.ui).to receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform",
57
+ "server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "server-4", "_default", "testfqdnnode.us", "[]", "[]", "ubuntu",
58
+ "ubuntu01", "resource-1", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "",
59
+ "windows2008", "resource-2", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "",
60
+ "windows2008", "resource-3-err", nil, nil, "id2", "image2", "keypair", "ERROR", "test zone", "", "", "", "", "", ""], :uneven_columns_across, 15)
61
+ instance.run
62
+ end
63
+
64
+ it "lists formatted list of resources on chef-data and chef-node-attribute option set" do
65
+ instance.config[:chef_node_attribute] = "platform_family"
66
+ expect(@node).to receive(:attribute?).with("platform_family").and_return(true)
67
+ expect(instance.ui).to receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform", "platform_family",
68
+ "server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "server-4", "_default", "testfqdnnode.us", "[]", "[]", "ubuntu", "debian",
69
+ "ubuntu01", "resource-1", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", "",
70
+ "windows2008", "resource-2", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", "",
71
+ "windows2008", "resource-3-err", nil, nil, "id2", "image2", "keypair", "ERROR", "test zone", "", "", "", "", "", "", ""], :uneven_columns_across, 16)
72
+ instance.run
73
+ end
74
+
75
+ it "raise error on invalid chef-node-attribute set" do
76
+ instance.config[:chef_node_attribute] = "invalid_attribute"
77
+ expect(instance.ui).to receive(:fatal)
78
+ expect(@node).to receive(:attribute?).with("invalid_attribute").and_return(false)
79
+ expect(instance.ui).to receive(:error).with("The Node does not have a invalid_attribute attribute.")
80
+ expect { instance.run }.to raise_error
81
+ end
82
+
83
+ it "not display chef-data on chef-node-attribute set but chef-data option missing" do
84
+ instance.config[:chef_data] = false
85
+ instance.config[:chef_node_attribute] = "platform_family"
86
+ expect(instance.ui).to receive(:list).with(["Name", "Instance ID", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone",
87
+ "server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone",
88
+ "ubuntu01", "resource-1", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone",
89
+ "windows2008", "resource-2", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone",
90
+ "windows2008", "resource-3-err", nil, nil, "id2", "image2", "keypair", "ERROR", "test zone"], :uneven_columns_across, 9)
91
+ instance.run
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,46 @@
1
+ #
2
+ # Author:: Ameya Varade (<ameya.varade@clogeny.com>)
3
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require 'spec_helper'
19
+ require 'chef/knife/openstack_server_show'
20
+ require 'chef/knife/cloud/openstack_service'
21
+
22
+ describe Chef::Knife::Cloud::OpenstackServerShow do
23
+
24
+ context "functionality" do
25
+ before do
26
+ @instance = Chef::Knife::Cloud::OpenstackServerShow.new
27
+ Chef::Config[:knife][:instance_id] = "instance_id"
28
+ @openstack_service = Chef::Knife::Cloud::OpenstackService.new
29
+ allow(@openstack_service).to receive(:msg_pair)
30
+ allow(@openstack_service).to receive(:print)
31
+ allow_message_expectations_on_nil
32
+ server = Object.new
33
+ conn = Object.new
34
+ conn.define_singleton_method(:servers){ }
35
+ allow(@openstack_service).to receive(:connection).and_return(conn)
36
+ expect(@openstack_service.connection.servers).to receive(:get).and_return(server)
37
+ allow(@instance).to receive(:create_service_instance).and_return(@openstack_service)
38
+ allow(@instance).to receive(:validate!)
39
+ expect(@openstack_service).to receive(:server_summary)
40
+ end
41
+
42
+ it "runs server show successfully" do
43
+ @instance.run
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,91 @@
1
+ # Copyright: Copyright (c) 2013-2014 Chef Software, Inc.
2
+ # License: Apache License, Version 2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>)
17
+
18
+ require 'mixlib/shellout'
19
+
20
+ module CleanupTestResources
21
+ def self.validate_params
22
+ unset_env_var = []
23
+
24
+ # OPENSTACK_USERNAME, OPENSTACK_PASSWORD and OPENSTACK_AUTH_URL are mandatory params to run knife openstack commands.
25
+ %w(OPENSTACK_USERNAME OPENSTACK_PASSWORD OPENSTACK_AUTH_URL).each do |os_env_var|
26
+ if ENV[os_env_var].nil?
27
+ unset_env_var << os_env_var
28
+ end
29
+ end
30
+
31
+ err_msg = "\nPlease set #{unset_env_var.join(', ')} environment"
32
+ err_msg = err_msg + ( unset_env_var.length > 1 ? " variables " : " variable " ) + "to cleanup test resources."
33
+ if ! unset_env_var.empty?
34
+ puts err_msg
35
+ exit 1
36
+ end
37
+ end
38
+
39
+ # Use Mixlib::ShellOut to run knife openstack commands.
40
+ def self.run(command_line)
41
+ shell_out = Mixlib::ShellOut.new("#{command_line}")
42
+ shell_out.timeout = 3000
43
+ shell_out.run_command
44
+ return shell_out
45
+ end
46
+
47
+ # Use knife openstack to delete servers.
48
+ def self.cleanup_resources
49
+
50
+ delete_resources = []
51
+
52
+ # Openstack credentials use during knife openstack command run.
53
+ openstack_creds = "--openstack-username '#{ENV['OPENSTACK_USERNAME']}' --openstack-password '#{ENV['OPENSTACK_PASSWORD']}' --openstack-api-endpoint #{ENV['OPENSTACK_AUTH_URL']}"
54
+
55
+ # List all servers in openstack using knife openstack server list command.
56
+ list_command = "knife openstack server list #{openstack_creds}"
57
+ list_output = run(list_command)
58
+
59
+ # Check command exitstatus. Non zero exitstatus indicates command execution fails.
60
+ if list_output.exitstatus != 0
61
+ puts "Cleanup Test Resources failed. Please check Openstack user name, password and auth url are correct. Error: #{list_output.stderr}."
62
+ exit list_output.exitstatus
63
+ else
64
+ servers = list_output.stdout
65
+ end
66
+
67
+ # We use "os-integration-test-<platform>-<randomNumber>" pattern for server name during integration tests run. So use "os-integration-test-" pattern to find out servers created during integration tests run.
68
+ servers.each_line do |line|
69
+ if line.include?("os-integration-test-") || (line.include?("openstack-") && line.include?("opscode-ci-ssh"))
70
+ # Extract and add instance id of server to delete_resources list.
71
+ delete_resources << {"id" => line.split(" ").first, "name" => line.split(" ")[1]}
72
+ end
73
+ end
74
+
75
+ # Delete servers
76
+ delete_resources.each do |resource|
77
+ delete_command = "knife openstack server delete #{resource['id']} #{openstack_creds} --yes"
78
+ delete_output = run(delete_command)
79
+
80
+ # check command exitstatus. Non zero exitstatus indicates command execution fails.
81
+ if delete_output.exitstatus != 0
82
+ puts "Unable to delete server #{resource['name']}: #{resource['id']}. Error: #{delete_output.stderr}."
83
+ else
84
+ puts "Deleted server #{resource['name']}: #{resource['id']}."
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ CleanupTestResources.validate_params
91
+ CleanupTestResources.cleanup_resources