knife-cloud 1.0.0.rc.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +33 -0
  3. data/.travis.yml +7 -0
  4. data/CHANGELOG.md +11 -0
  5. data/CONTRIBUTING.md +5 -0
  6. data/Gemfile +9 -0
  7. data/LICENSE +201 -0
  8. data/README.md +420 -0
  9. data/Rakefile +35 -0
  10. data/knife-cloud.gemspec +27 -0
  11. data/lib/chef/knife/cloud/chefbootstrap/bootstrap_distribution.rb +31 -0
  12. data/lib/chef/knife/cloud/chefbootstrap/bootstrap_options.rb +191 -0
  13. data/lib/chef/knife/cloud/chefbootstrap/bootstrap_protocol.rb +69 -0
  14. data/lib/chef/knife/cloud/chefbootstrap/bootstrapper.rb +78 -0
  15. data/lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb +179 -0
  16. data/lib/chef/knife/cloud/chefbootstrap/unix_distribution.rb +31 -0
  17. data/lib/chef/knife/cloud/chefbootstrap/windows_distribution.rb +32 -0
  18. data/lib/chef/knife/cloud/chefbootstrap/winrm_bootstrap_protocol.rb +85 -0
  19. data/lib/chef/knife/cloud/command.rb +101 -0
  20. data/lib/chef/knife/cloud/exceptions.rb +38 -0
  21. data/lib/chef/knife/cloud/fog/options.rb +29 -0
  22. data/lib/chef/knife/cloud/fog/service.rb +200 -0
  23. data/lib/chef/knife/cloud/helpers.rb +39 -0
  24. data/lib/chef/knife/cloud/list_resource_command.rb +97 -0
  25. data/lib/chef/knife/cloud/list_resource_options.rb +21 -0
  26. data/lib/chef/knife/cloud/server/create_command.rb +165 -0
  27. data/lib/chef/knife/cloud/server/create_options.rb +80 -0
  28. data/lib/chef/knife/cloud/server/delete_command.rb +68 -0
  29. data/lib/chef/knife/cloud/server/delete_options.rb +42 -0
  30. data/lib/chef/knife/cloud/server/list_command.rb +84 -0
  31. data/lib/chef/knife/cloud/server/list_options.rb +43 -0
  32. data/lib/chef/knife/cloud/server/options.rb +39 -0
  33. data/lib/chef/knife/cloud/server/show_command.rb +55 -0
  34. data/lib/chef/knife/cloud/server/show_options.rb +36 -0
  35. data/lib/chef/knife/cloud/service.rb +91 -0
  36. data/lib/knife-cloud/version.rb +6 -0
  37. data/lib/test/fixtures/knife.rb +9 -0
  38. data/lib/test/fixtures/validation.pem +27 -0
  39. data/lib/test/knife-utils/helper.rb +39 -0
  40. data/lib/test/knife-utils/knife_test_utils.rb +40 -0
  41. data/lib/test/knife-utils/matchers.rb +29 -0
  42. data/lib/test/knife-utils/test_bed.rb +56 -0
  43. data/lib/test/templates/chef-full-chef-zero.erb +67 -0
  44. data/lib/test/templates/windows-chef-client-msi.erb +231 -0
  45. data/lib/test/templates/windows-shell.erb +77 -0
  46. data/spec/resource_spec_helper.rb +49 -0
  47. data/spec/server_command_common_spec_helper.rb +48 -0
  48. data/spec/spec_helper.rb +25 -0
  49. data/spec/support/shared_examples_for_command.rb +35 -0
  50. data/spec/support/shared_examples_for_servercreatecommand.rb +144 -0
  51. data/spec/support/shared_examples_for_serverdeletecommand.rb +77 -0
  52. data/spec/support/shared_examples_for_service.rb +85 -0
  53. data/spec/unit/bootstrap_protocol_spec.rb +70 -0
  54. data/spec/unit/bootstrapper_spec.rb +171 -0
  55. data/spec/unit/cloud_command_spec.rb +35 -0
  56. data/spec/unit/command_spec.rb +49 -0
  57. data/spec/unit/fog_service_spec.rb +138 -0
  58. data/spec/unit/list_resource_command_spec.rb +136 -0
  59. data/spec/unit/server_create_command_spec.rb +198 -0
  60. data/spec/unit/server_delete_command_spec.rb +25 -0
  61. data/spec/unit/server_list_command_spec.rb +119 -0
  62. data/spec/unit/server_show_command_spec.rb +64 -0
  63. data/spec/unit/service_spec.rb +46 -0
  64. data/spec/unit/ssh_bootstrap_protocol_spec.rb +116 -0
  65. data/spec/unit/unix_distribution_spec.rb +37 -0
  66. data/spec/unit/windows_distribution_spec.rb +37 -0
  67. data/spec/unit/winrm_bootstrap_protocol_spec.rb +106 -0
  68. metadata +248 -0
@@ -0,0 +1,144 @@
1
+ #
2
+ # Author:: Mukta Aphale (<mukta.aphale@clogeny.com>)
3
+ # Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>)
4
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'spec_helper'
20
+ require 'chef/knife/cloud/server/create_command'
21
+
22
+ def get_mock_resource(id)
23
+ obj = Object.new
24
+ allow(obj).to receive(:id).and_return(id)
25
+ obj
26
+ end
27
+
28
+ shared_examples_for Chef::Knife::Cloud::ServerCreateCommand do |instance|
29
+ before do
30
+ instance.service = double
31
+ allow(instance.ui).to receive(:fatal)
32
+
33
+ allow(instance.service).to receive(:get_image).and_return(get_mock_resource('image_id'))
34
+
35
+ allow(instance.service).to receive(:get_flavor).and_return(get_mock_resource('flavor_id'))
36
+ end
37
+
38
+ describe "#before_exec_command" do
39
+ it "calls create_server_dependencies" do
40
+ expect(instance.service).to receive(:create_server_dependencies)
41
+ instance.before_exec_command
42
+ end
43
+ it "delete_server_dependencies on any error" do
44
+ allow(instance).to receive(:execute_command)
45
+ allow(instance).to receive(:after_exec_command)
46
+ allow(instance).to receive(:validate!)
47
+ allow(instance).to receive(:validate_params!)
48
+ instance.service = Chef::Knife::Cloud::Service.new
49
+ allow(instance).to receive(:create_service_instance).and_return(instance.service)
50
+ allow(instance.service).to receive(:get_image).and_return(get_mock_resource('image_id'))
51
+ allow(instance.service).to receive(:get_flavor).and_return(get_mock_resource('flavor_id'))
52
+ allow(instance.service).to receive(:create_server_dependencies).and_raise(Chef::Knife::Cloud::CloudExceptions::ServerCreateDependenciesError)
53
+ expect(instance.service).to receive(:delete_server_dependencies)
54
+ expect(instance.service).to_not receive(:delete_server_on_failure)
55
+ expect(instance).to receive(:exit)
56
+ instance.run
57
+ end
58
+ end
59
+
60
+ describe "#execute_command" do
61
+ it "calls create_server" do
62
+ expect(instance.service).to receive(:create_server).and_return(true)
63
+ expect(instance.service).to receive(:server_summary)
64
+ instance.execute_command
65
+ end
66
+
67
+ it "delete_server_dependencies on any error" do
68
+ allow(instance).to receive(:before_exec_command)
69
+ allow(instance).to receive(:after_exec_command)
70
+ allow(instance).to receive(:validate!)
71
+ allow(instance).to receive(:validate_params!)
72
+ instance.service = Chef::Knife::Cloud::Service.new
73
+ allow(instance).to receive(:create_service_instance).and_return(instance.service)
74
+ allow(instance.service).to receive(:create_server).and_raise(Chef::Knife::Cloud::CloudExceptions::ServerCreateError)
75
+ expect(instance.service).to receive(:delete_server_dependencies)
76
+ expect(instance.service).to_not receive(:delete_server_on_failure)
77
+ expect(instance).to receive(:exit)
78
+ instance.run
79
+ end
80
+ end
81
+
82
+ describe "#bootstrap" do
83
+ it "execute with correct method calls" do
84
+ @bootstrap = Object.new
85
+ allow(@bootstrap).to receive(:bootstrap)
86
+ allow(instance.ui).to receive(:info)
87
+ allow(Chef::Knife::Cloud::Bootstrapper).to receive(:new).and_return(@bootstrap)
88
+ expect(instance).to receive(:before_bootstrap).ordered
89
+ expect(instance).to receive(:after_bootstrap).ordered
90
+ instance.bootstrap
91
+ end
92
+ end
93
+
94
+ describe "#after_bootstrap" do
95
+ it "display server summary" do
96
+ expect(instance.service).to receive(:server_summary)
97
+ instance.after_bootstrap
98
+ end
99
+ end
100
+
101
+ describe "#get_node_name" do
102
+ it "auto generates chef_node_name" do
103
+ instance.config[:bootstrap_protocol] = 'ssh'
104
+ instance.config[:ssh_password] = 'password'
105
+ instance.config[:image_os_type] = 'linux'
106
+ instance.config[:chef_node_name_prefix] = 'os'
107
+ expect(instance).to receive(:get_node_name).and_call_original
108
+ instance.validate_params!
109
+ expect(instance.config[:chef_node_name]).to be =~ /os-*/
110
+ end
111
+
112
+ it "auto generates unique chef_node_name" do
113
+ node_names = []
114
+ instance.config[:bootstrap_protocol] = 'ssh'
115
+ instance.config[:ssh_password] = 'password'
116
+ instance.config[:image_os_type] = 'linux'
117
+ instance.config[:chef_node_name_prefix] = 'os'
118
+ 5.times do
119
+ instance.config[:chef_node_name] = nil
120
+ instance.validate_params!
121
+ expect(node_names).to_not include(instance.config[:chef_node_name])
122
+ node_names.push(instance.config[:chef_node_name])
123
+ end
124
+ end
125
+ end
126
+
127
+ describe "#cleanup_on_failure" do
128
+ it "delete server dependencies on delete_server_on_failure set" do
129
+ instance.config[:delete_server_on_failure] = true
130
+ instance.service = Chef::Knife::Cloud::Service.new
131
+ expect(instance.service).to receive(:delete_server_dependencies)
132
+ expect(instance.service).to receive(:delete_server_on_failure)
133
+ instance.cleanup_on_failure
134
+ end
135
+
136
+ it "don't delete server dependencies on delete_server_on_failure option is missing" do
137
+ instance.config[:delete_server_on_failure] = false
138
+ Chef::Config[:knife].delete(:delete_server_on_failure)
139
+ expect(instance.service).to_not receive(:delete_server_dependencies)
140
+ expect(instance.service).to_not receive(:delete_server_on_failure)
141
+ instance.cleanup_on_failure
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,77 @@
1
+ #
2
+ # Author:: Mukta Aphale (<mukta.aphale@clogeny.com>)
3
+ # Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>)
4
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'chef/knife/cloud/server/delete_command'
20
+
21
+ shared_examples_for Chef::Knife::Cloud::ServerDeleteCommand do |instance|
22
+ describe "#delete_from_chef" do
23
+ it "expects chef warning message when purge option is disabled" do
24
+ server_name = "testserver"
25
+ expect(instance.ui).to receive(:warn).with("Corresponding node and client for the #{server_name} server were not deleted and remain registered with the Chef Server")
26
+ instance.delete_from_chef(server_name)
27
+ end
28
+
29
+ it "deletes chef node and client when purge option is enabled" do
30
+ instance.config[:purge] = true
31
+ server_name = "testserver"
32
+ expect(instance).to receive(:destroy_item).with(Chef::Node, server_name, "node").ordered
33
+ expect(instance).to receive(:destroy_item).with(Chef::ApiClient, server_name, "client").ordered
34
+ instance.delete_from_chef(server_name)
35
+ end
36
+
37
+ it "deletes chef node specified with node-name option overriding the instance server_name" do
38
+ instance.config[:purge] = true
39
+ server_name = "testserver"
40
+ chef_node_name = "testnode"
41
+ instance.config[:chef_node_name] = chef_node_name
42
+ expect(instance).to receive(:destroy_item).with(Chef::Node, chef_node_name, "node").ordered
43
+ expect(instance).to receive(:destroy_item).with(Chef::ApiClient, chef_node_name, "client").ordered
44
+ instance.delete_from_chef(chef_node_name)
45
+ end
46
+ end
47
+
48
+ describe "#execute_command" do
49
+ it "execute with correct method calls" do
50
+ instance::name_args = ["testserver"]
51
+ instance.service = double
52
+ expect(instance.service).to receive(:delete_server).ordered
53
+ expect(instance).to receive(:delete_from_chef).ordered
54
+ instance.execute_command
55
+ end
56
+ end
57
+
58
+ describe "#destroy_item" do
59
+ it "destroy chef node" do
60
+ node_name = "testnode"
61
+ test_obj = Object.new
62
+ allow(Chef::Node).to receive(:load).and_return(test_obj)
63
+ allow(test_obj).to receive(:destroy)
64
+ expect(instance.ui).to receive(:warn).with("Deleted node #{node_name}")
65
+ instance.destroy_item(Chef::Node, node_name, "node")
66
+ end
67
+
68
+ it "destroy chef client" do
69
+ client_name = "testclient"
70
+ test_obj = Object.new
71
+ allow(Chef::ApiClient).to receive(:load).and_return(test_obj)
72
+ allow(test_obj).to receive(:destroy)
73
+ expect(instance.ui).to receive(:warn).with("Deleted client #{client_name}")
74
+ instance.destroy_item(Chef::ApiClient, client_name, "client")
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,85 @@
1
+ #
2
+ # Author:: Mukta Aphale (<mukta.aphale@clogeny.com>)
3
+ # Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>)
4
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'chef/knife/cloud/service'
20
+
21
+ shared_examples_for Chef::Knife::Cloud::Service do |instance|
22
+
23
+ describe "#connection" do
24
+ it "creates a connection to fog service." do
25
+ expect(instance).to receive(:add_api_endpoint)
26
+ expect(Fog::Compute).to receive(:new)
27
+ instance.connection
28
+ end
29
+ end
30
+
31
+ describe "#delete_server" do
32
+ it "deletes the server." do
33
+ server = double()
34
+ allow(instance).to receive(:puts)
35
+ allow(instance).to receive_message_chain(:connection, :servers, :get).and_return(server)
36
+ expect(server).to receive(:name).ordered
37
+ expect(server).to receive(:id).ordered
38
+ allow(instance).to receive_message_chain(:ui, :confirm)
39
+ expect(server).to receive(:destroy).ordered
40
+ instance.delete_server(:server_name)
41
+ end
42
+
43
+ it "throws error message when the server cannot be located." do
44
+ server_name = "invalid_server_name"
45
+ error_message = "Could not locate server '#{server_name}'."
46
+ allow(instance).to receive_message_chain(:connection, :servers, :get).and_return(nil)
47
+ allow(instance).to receive_message_chain(:ui, :error).with(error_message)
48
+ expect { instance.delete_server(server_name) }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ServerDeleteError)
49
+ end
50
+ end
51
+
52
+ describe "#create_server" do
53
+ before do
54
+ allow(instance).to receive(:puts)
55
+ allow(instance).to receive(:print)
56
+ end
57
+
58
+ it "creates the server." do
59
+ server = double()
60
+ allow(instance).to receive_message_chain(:connection, :servers, :create).and_return(server)
61
+ allow(instance).to receive_message_chain(:ui, :color)
62
+ expect(server).to receive(:wait_for)
63
+ instance.create_server({:server_create_timeout => 600})
64
+ end
65
+ end
66
+
67
+ describe "#get_server" do
68
+ it "return server." do
69
+ server = double()
70
+ allow(instance).to receive_message_chain(:connection, :servers, :create).and_return(server)
71
+ expect(instance.connection.servers).to receive(:get)
72
+ instance.get_server("instance_id")
73
+ end
74
+ end
75
+
76
+ describe "#server_summary" do
77
+ it "show server details." do
78
+ server = double()
79
+ instance.ui = double()
80
+ expect(instance.ui).to receive(:list)
81
+ expect(server).to receive(:id)
82
+ instance.server_summary(server, [{:label => 'Instance ID', :key => 'id'}])
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,70 @@
1
+ #
2
+ # Author:: Siddheshwar More (<siddheshwar.more@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/cloud/chefbootstrap/bootstrap_protocol'
20
+ require 'chef/knife/bootstrap'
21
+
22
+ describe Chef::Knife::Cloud::BootstrapProtocol do
23
+ before do
24
+ @config = {:bootstrap_protocol => 'ssh'}
25
+ @instance = Chef::Knife::Cloud::BootstrapProtocol.new(@config)
26
+ end
27
+
28
+ context "BootstrapProtocol initializer" do
29
+ it "asks for compulsory properties while creating @instance" do
30
+ expect {Chef::Knife::Cloud::BootstrapProtocol.new}.to raise_error(ArgumentError)
31
+ end
32
+
33
+ it "creating @instance" do
34
+ expect {Chef::Knife::Cloud::BootstrapProtocol.new(@config)}.to_not raise_error
35
+ expect(Chef::Knife::Cloud::BootstrapProtocol.new(@config).class).to eq(Chef::Knife::Cloud::BootstrapProtocol)
36
+ end
37
+ end
38
+
39
+ describe "#send_bootstrap_command" do
40
+ it "execute with correct method calls" do
41
+ @instance.bootstrap = double()
42
+ expect(@instance).to receive(:wait_for_server_ready).ordered
43
+ expect(@instance).to receive(:init_bootstrap_options).ordered
44
+ expect(@instance.bootstrap).to receive(:run)
45
+ @instance.send_bootstrap_command
46
+ end
47
+ end
48
+
49
+ describe "#init_bootstrap_options" do
50
+ it "set correct bootstrap config" do
51
+ @config[:bootstrap_ip_address] = "127.0.0.1"
52
+ @config[:chef_node_name] = "testnode"
53
+ @config[:environment] = "_default"
54
+ @config[:first_boot_attributes] = "{\"foo\":\"bar\"}"
55
+ @config[:secret] = "secret"
56
+ @config[:secret_file] = "secret_file"
57
+ @config[:template_file] = "../template_file"
58
+ allow(@config).to receive(:locate_config_value).and_return({})
59
+ @instance.bootstrap = Chef::Knife::Bootstrap.new
60
+ @instance.init_bootstrap_options
61
+ expect(@instance.bootstrap.name_args).to eq(@config[:bootstrap_ip_address])
62
+ expect(@instance.bootstrap.config[:chef_node_name]).to eq(@config[:chef_node_name])
63
+ expect(@instance.bootstrap.config[:environment]).to eq(@config[:environment])
64
+ expect(@instance.bootstrap.config[:first_boot_attributes]).to eq(@config[:first_boot_attributes])
65
+ expect(@instance.bootstrap.config[:secret]).to eq(@config[:secret])
66
+ expect(@instance.bootstrap.config[:secret_file]).to eq(@config[:secret_file])
67
+ expect(@instance.bootstrap.config[:template_file]).to eq(@config[:template_file])
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,171 @@
1
+ #
2
+ # Author:: Prabhu Das (<prabhu.das@clogeny.com>)
3
+ # Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>)
4
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'spec_helper'
20
+ require 'chef/knife/cloud/chefbootstrap/bootstrapper'
21
+ require 'chef/knife/bootstrap_windows_ssh'
22
+
23
+ describe Chef::Knife::Cloud::Bootstrapper do
24
+ before(:each) do
25
+ @config = {:bootstrap_protocol => 'ssh'}
26
+ @instance = Chef::Knife::Cloud::Bootstrapper.new(@config)
27
+ end
28
+
29
+ context "Bootstrapper initializer" do
30
+ it "asks for compulsory properties while creating instance" do
31
+ expect {Chef::Knife::Cloud::Bootstrapper.new}.to raise_error(ArgumentError)
32
+ end
33
+
34
+ it "creating instance" do
35
+ expect {Chef::Knife::Cloud::Bootstrapper.new(@config)}.to_not raise_error
36
+ expect(Chef::Knife::Cloud::Bootstrapper.new(@config).class).to eq(Chef::Knife::Cloud::Bootstrapper)
37
+ end
38
+ end
39
+
40
+ describe "#bootstrap Linux machine with ssh" do
41
+ it "executes with correct method calls" do
42
+ @ssh_bootstrap_protocol = Chef::Knife::Cloud::SshBootstrapProtocol.new(@config)
43
+ allow(@instance).to receive(:create_bootstrap_protocol).and_return(@ssh_bootstrap_protocol)
44
+ @unix_distribution = Chef::Knife::Cloud::UnixDistribution.new(@config)
45
+ expect(@instance).to receive(:create_bootstrap_protocol).ordered
46
+ allow(@instance).to receive(:create_bootstrap_distribution).and_return(@unix_distribution)
47
+ expect(@ssh_bootstrap_protocol).to receive(:send_bootstrap_command).ordered
48
+ @instance.bootstrap
49
+ end
50
+ end
51
+
52
+ describe "#bootstrap Windows machine with winrm" do
53
+ it "executes with correct method calls" do
54
+ @winrm_bootstrap_protocol = Chef::Knife::Cloud::WinrmBootstrapProtocol.new(@config)
55
+ allow(@instance).to receive(:create_bootstrap_protocol).and_return(@winrm_bootstrap_protocol)
56
+ @windows_distribution = Chef::Knife::Cloud::WindowsDistribution.new(@config)
57
+ expect(@instance).to receive(:create_bootstrap_protocol).ordered
58
+ allow(@instance).to receive(:create_bootstrap_distribution).and_return(@windows_distribution)
59
+ expect(@winrm_bootstrap_protocol).to receive(:send_bootstrap_command).ordered
60
+ @instance.bootstrap
61
+ end
62
+ end
63
+
64
+ describe "#create_bootstrap_distribution" do
65
+ context "when image_os_type set to windows" do
66
+ before(:each) do
67
+ @config[:image_os_type] = "windows"
68
+ end
69
+
70
+ it "instantiates Windows Distribution class." do
71
+ expect(Chef::Knife::Cloud::WindowsDistribution).to receive(:new).with(@config)
72
+ @instance.create_bootstrap_distribution
73
+ end
74
+
75
+ it "doesn't instantiate Unix Distribution class." do
76
+ expect(Chef::Knife::Cloud::UnixDistribution).to_not receive(:new)
77
+ @instance.create_bootstrap_distribution
78
+ end
79
+
80
+ end
81
+
82
+ context "when image_os_type set to linux" do
83
+ before(:each) do
84
+ @config[:image_os_type] = "linux"
85
+ end
86
+
87
+ it "instantiates Unix Distribution class." do
88
+ expect(Chef::Knife::Cloud::UnixDistribution).to receive(:new).with(@config)
89
+ @instance.create_bootstrap_distribution
90
+ end
91
+
92
+ it "doesn't instantiate Windows Distribution class." do
93
+ expect(Chef::Knife::Cloud::WindowsDistribution).to_not receive(:new)
94
+ @instance.create_bootstrap_distribution
95
+ end
96
+ end
97
+
98
+ context "when image_os_type set to invalid" do
99
+ before(:each) do
100
+ @config[:image_os_type] = "invalid"
101
+ end
102
+
103
+ it "raise bootstrap error" do
104
+ ui = double()
105
+ expect(@instance).to receive(:ui).and_return(ui)
106
+ allow(ui).to receive(:fatal)
107
+ expect { @instance.create_bootstrap_distribution }.to raise_error(Chef::Knife::Cloud::CloudExceptions::BootstrapError, "Invalid bootstrap distribution. image_os_type should be either windows or linux.")
108
+ end
109
+ end
110
+ end
111
+
112
+
113
+ describe "#create_bootstrap_protocol" do
114
+ context "when bootstrap_protocol set to ssh" do
115
+ before(:each) do
116
+ @config[:bootstrap_protocol] = "ssh"
117
+ end
118
+
119
+ it "instantiates SshBootstrapProtocol class." do
120
+ expect(Chef::Knife::Cloud::SshBootstrapProtocol).to receive(:new)
121
+ @instance.create_bootstrap_protocol
122
+ end
123
+
124
+ it "doesn't instantiate Windows Distribution class." do
125
+ expect(Chef::Knife::Cloud::WinrmBootstrapProtocol).to_not receive(:new)
126
+ @instance.create_bootstrap_protocol
127
+ end
128
+
129
+ end
130
+
131
+ context "when bootstrap_protocol set to winrm" do
132
+ before(:each) do
133
+ @config[:bootstrap_protocol] = "winrm"
134
+ end
135
+
136
+ it "instantiates WinrmBootstrapProtocol class." do
137
+ expect(Chef::Knife::Cloud::WinrmBootstrapProtocol).to receive(:new)
138
+ @instance.create_bootstrap_protocol
139
+ end
140
+
141
+ it "doesn't instantiate SshBootstrapProtocol class." do
142
+ expect(Chef::Knife::Cloud::SshBootstrapProtocol).to_not receive(:new)
143
+ @instance.create_bootstrap_protocol
144
+ end
145
+ end
146
+
147
+ context "when bootstrap_protocol set to nil." do
148
+ before do
149
+ @config[:bootstrap_protocol] = nil
150
+ end
151
+
152
+ it "instantiates SshBootstrapProtocol class." do
153
+ expect(Chef::Knife::Cloud::SshBootstrapProtocol).to receive(:new)
154
+ @instance.create_bootstrap_protocol
155
+ end
156
+ end
157
+
158
+ context "when bootstrap_protocol set to invalid." do
159
+ before do
160
+ @config[:bootstrap_protocol] = "invalid"
161
+ end
162
+
163
+ it "instantiates SshBootstrapProtocol class." do
164
+ ui = double()
165
+ expect(@instance).to receive(:ui).and_return(ui)
166
+ allow(ui).to receive(:fatal)
167
+ expect { @instance.create_bootstrap_protocol }.to raise_error(Chef::Knife::Cloud::CloudExceptions::BootstrapError, "Invalid bootstrap protocol.")
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,35 @@
1
+ #
2
+ # Author:: Mukta Aphale (<mukta.aphale@clogeny.com>)
3
+ # Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>)
4
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
20
+ require 'support/shared_examples_for_command'
21
+
22
+ describe Chef::Knife::Cloud::Command do
23
+ it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::Command.new
24
+
25
+ let (:instance) { Chef::Knife::Cloud::Command.new }
26
+
27
+ it "overrides execute_command" do
28
+ allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new)
29
+ expect {instance.run}.to raise_error(Chef::Exceptions::Override, "You must override execute_command in #{instance.to_s}")
30
+ end
31
+
32
+ it { expect {instance.run}.to raise_error(Chef::Exceptions::Override, "You must override create_service_instance in #{instance.to_s} to create cloud specific service") }
33
+
34
+ end
35
+
@@ -0,0 +1,49 @@
1
+ #
2
+ # Author:: Siddheshwar More (<siddheshwar.more@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 'support/shared_examples_for_command'
19
+
20
+ describe Chef::Knife::Cloud::Command do
21
+ it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::Command.new
22
+
23
+ describe "validate!" do
24
+ before(:each) do
25
+ # Here following options are used as a test data.
26
+ Chef::Config[:knife][:cloud_provider_username] = "cloud_provider_username"
27
+ Chef::Config[:knife][:cloud_provider_password] = "cloud_provider_password"
28
+ Chef::Config[:knife][:cloud_provider_auth_url] = "cloud_provider_auth_url"
29
+ @instance = Chef::Knife::Cloud::Command.new
30
+ allow(@instance.ui).to receive(:error)
31
+ end
32
+
33
+ after(:all) do
34
+ Chef::Config[:knife].delete(:cloud_provider_username)
35
+ Chef::Config[:knife].delete(:cloud_provider_password)
36
+ Chef::Config[:knife].delete(:cloud_provider_auth_url)
37
+ end
38
+
39
+ it "execute with success" do
40
+ expect { @instance.validate!(:cloud_provider_username, :cloud_provider_password, :cloud_provider_auth_url) }.to_not raise_error
41
+ end
42
+
43
+ it "raise_error on any mandatory option is missing" do
44
+ # delete cloud_provide_username option.
45
+ Chef::Config[:knife].delete(:cloud_provider_username)
46
+ expect { @instance.validate!(:cloud_provider_username, :cloud_provider_password, :cloud_provider_auth_url) }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError, " You did not provide a valid 'Cloud Provider Username' value..")
47
+ end
48
+ end
49
+ end