-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathshared_examples_for_servercreatecommand.rb
More file actions
143 lines (129 loc) · 5.79 KB
/
shared_examples_for_servercreatecommand.rb
File metadata and controls
143 lines (129 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#
# Author:: Mukta Aphale (<mukta.aphale@clogeny.com>)
# Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>)
# Copyright:: Copyright (c) 2011-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "spec_helper"
require "chef/knife/cloud/server/create_command"
def get_mock_resource(id)
obj = Object.new
allow(obj).to receive(:id).and_return(id)
obj
end
shared_examples_for Chef::Knife::Cloud::ServerCreateCommand do |instance|
before do
instance.service = double
allow(instance.ui).to receive(:fatal)
allow(instance.service).to receive(:get_image).and_return(get_mock_resource("image_id"))
allow(instance.service).to receive(:get_flavor).and_return(get_mock_resource("flavor_id"))
end
describe "#before_exec_command" do
it "calls create_server_dependencies" do
expect(instance.service).to receive(:create_server_dependencies)
instance.before_exec_command
end
it "delete_server_dependencies on any error" do
allow(instance).to receive(:execute_command)
allow(instance).to receive(:after_exec_command)
allow(instance).to receive(:validate!)
allow(instance).to receive(:validate_params!)
instance.service = Chef::Knife::Cloud::Service.new(config: instance.config)
allow(instance).to receive(:create_service_instance).and_return(instance.service)
allow(instance.service).to receive(:get_image).and_return(get_mock_resource("image_id"))
allow(instance.service).to receive(:get_flavor).and_return(get_mock_resource("flavor_id"))
allow(instance.service).to receive(:create_server_dependencies).and_raise(Chef::Knife::Cloud::CloudExceptions::ServerCreateDependenciesError)
expect(instance.service).to receive(:delete_server_dependencies)
expect(instance.service).to_not receive(:delete_server_on_failure)
expect(instance).to receive(:exit)
instance.run
end
end
describe "#execute_command" do
it "calls create_server" do
expect(instance.service).to receive(:create_server).and_return(true)
expect(instance.service).to receive(:server_summary)
instance.execute_command
end
it "delete_server_dependencies on any error" do
allow(instance).to receive(:before_exec_command)
allow(instance).to receive(:after_exec_command)
allow(instance).to receive(:validate!)
allow(instance).to receive(:validate_params!)
instance.service = Chef::Knife::Cloud::Service.new(config: instance.config)
allow(instance).to receive(:create_service_instance).and_return(instance.service)
allow(instance.service).to receive(:create_server).and_raise(Chef::Knife::Cloud::CloudExceptions::ServerCreateError)
expect(instance.service).to receive(:delete_server_dependencies)
expect(instance.service).to_not receive(:delete_server_on_failure)
expect(instance).to receive(:exit)
instance.run
end
end
describe "#bootstrap" do
it "execute with correct method calls" do
@bootstrap = Object.new
allow(@bootstrap).to receive(:bootstrap)
allow(instance.ui).to receive(:info)
allow(Chef::Knife::Cloud::Bootstrapper).to receive(:new).and_return(@bootstrap)
expect(instance).to receive(:before_bootstrap).ordered
expect(instance).to receive(:after_bootstrap).ordered
instance.bootstrap
end
end
describe "#after_bootstrap" do
it "display server summary" do
expect(instance.service).to receive(:server_summary)
instance.after_bootstrap
end
end
describe "#get_node_name" do
it "auto generates chef_node_name" do
instance.config[:connection_protocol] = "ssh"
instance.config[:connection_password] = "password"
instance.config[:image_os_type] = "linux"
instance.config[:chef_node_name_prefix] = "os"
expect(instance).to receive(:get_node_name).and_call_original
instance.validate_params!
expect(instance.config[:chef_node_name]).to be =~ /os-*/
end
it "auto generates unique chef_node_name" do
node_names = []
instance.config[:connection_protocol] = "ssh"
instance.config[:connection_password] = "password"
instance.config[:image_os_type] = "linux"
instance.config[:chef_node_name_prefix] = "os"
5.times do
instance.config[:chef_node_name] = nil
instance.validate_params!
expect(node_names).to_not include(instance.config[:chef_node_name])
node_names.push(instance.config[:chef_node_name])
end
end
end
describe "#cleanup_on_failure" do
it "delete server dependencies on delete_server_on_failure set" do
instance.config[:delete_server_on_failure] = true
instance.service = Chef::Knife::Cloud::Service.new(config: instance.config)
expect(instance.service).to receive(:delete_server_dependencies)
expect(instance.service).to receive(:delete_server_on_failure)
instance.cleanup_on_failure
end
it "don't delete server dependencies on delete_server_on_failure option is missing" do
instance.config[:delete_server_on_failure] = false
expect(instance.service).to_not receive(:delete_server_dependencies)
expect(instance.service).to_not receive(:delete_server_on_failure)
instance.cleanup_on_failure
end
end
end