forked from ManageIQ/manageiq-automation_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiq_ae_state_machine_retry_spec.rb
285 lines (254 loc) · 11.1 KB
/
miq_ae_state_machine_retry_spec.rb
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
describe "MiqAeStateMachineRetry" do
include Spec::Support::AutomationHelper
before do
@method_name = 'MY_RETRY_METHOD'
@method_instance = 'MY_RETRY_INSTANCE'
@retry_class = 'MY_RETRY_CLASS'
@domain = 'SPEC_DOMAIN'
@namespace = 'NS1'
@state_class = 'MY_STATE_MACHINE'
@state_instance = 'MY_STATE_INSTANCE'
@max_retries = 1
@root_class = "TOP_OF_THE_WORLD"
@root_instance = "EVEREST"
@user = FactoryBot.create(:user_with_group)
@automate_args = {:namespace => @namespace,
:class_name => @root_class,
:instance_name => @root_instance,
:user_id => @user.id,
:miq_group_id => @user.current_group_id,
:tenant_id => @user.current_tenant.id,
:automate_message => 'create'}
EvmSpecHelper.create_guid_miq_server_zone
clear_domain
end
def clear_domain
MiqAeDomain.find_by_name(@domain).try(:destroy)
end
def perpetual_retry_script
<<-'RUBY'
$evm.root['ae_result'] = 'retry'
RUBY
end
alias_method :retry_script, :perpetual_retry_script
def perpetual_restart_script_with_nextstate
<<-'RUBY'
$evm.root['ae_result'] = 'restart'
$evm.root['ae_next_state'] = 'state2'
RUBY
end
def perpetual_restart_script
<<-'RUBY'
$evm.log("info", "Setting restart for state")
$evm.root['ae_result'] = 'restart'
RUBY
end
def simpleton_script
<<-'RUBY'
$evm.root['ae_result'] = 'ok'
RUBY
end
def retry_server_affinity_script
<<-'RUBY'
$evm.root['ae_result'] = 'retry'
$evm.root['ae_retry_server_affinity'] = true
RUBY
end
def method_script_state_var
<<-'RUBY'
root = $evm.object("/")
if $evm.state_var_exist?(:gravy) && $evm.get_state_var('gravy') == 'train'
root['finished'] = true
$evm.set_state_var(:three, 3)
$evm.set_state_var('one', $evm.get_state_var(:one) + 1)
status = 'ok'
else
$evm.set_state_var(:one, 0)
$evm.set_state_var(:two, 2)
$evm.set_state_var('gravy', 'train')
status = 'retry'
end
case status
when 'retry'
root['ae_result'] = 'retry'
root['ae_retry_interval'] = '1.minute'
when 'ok'
root['ae_result'] = 'ok'
end
exit MIQ_OK
RUBY
end
def setup_model(method_script, max_time = nil)
dom = FactoryBot.create(:miq_ae_domain, :enabled => true, :name => @domain)
ns = FactoryBot.create(:miq_ae_namespace, :parent_id => dom.id, :name => @namespace)
@ns_fqname = ns.fqname
create_retry_class(:namespace => @ns_fqname, :name => @retry_class, :method_script => method_script)
create_state_class({:namespace => @ns_fqname, :name => @state_class}, max_time)
create_root_class(:namespace => @ns_fqname, :name => @root_class)
end
def create_retry_class(attrs = {})
method_script = attrs.delete(:method_script)
ae_fields = {'execute' => {:aetype => 'method', :datatype => 'string'}}
ae_instances = {@method_instance => {'execute' => {:value => @method_name}}}
ae_methods = {@method_name => {:scope => 'instance', :location => 'inline',
:data => method_script,
:language => 'ruby', 'params' => {}}}
FactoryBot.create(:miq_ae_class, :with_instances_and_methods,
attrs.merge('ae_fields' => ae_fields,
'ae_instances' => ae_instances,
'ae_methods' => ae_methods))
end
def create_state_class(attrs = {}, max_time = nil)
ae_fields = {'state1' => {:aetype => 'state', :datatype => 'string',
:max_retries => @max_retries, :message => 'create',
:max_time => max_time}}
fqname = "/#{@domain}/#{@namespace}/#{@retry_class}/#{@method_instance}"
ae_instances = {@state_instance => {'state1' => {:value => fqname}}}
FactoryBot.create(:miq_ae_class, :with_instances_and_methods,
attrs.merge('ae_fields' => ae_fields,
'ae_methods' => {},
'ae_instances' => ae_instances))
end
def create_root_class(attrs = {})
ae_fields = {'rel1' => {:aetype => 'relationship', :datatype => 'string'}}
fqname = "/#{@domain}/#{@namespace}/#{@state_class}/#{@state_instance}"
ae_instances = {@root_instance => {'rel1' => {:value => fqname}}}
FactoryBot.create(:miq_ae_class, :with_instances_and_methods,
attrs.merge('ae_fields' => ae_fields,
'ae_methods' => {},
'ae_instances' => ae_instances))
end
def create_method_class(attrs, script1, script2, script3)
ae_fields = {'execute' => {:aetype => 'method', :datatype => 'string'}}
ae_instances = {'meth1' => {'execute' => {:value => 'meth1'}},
'meth2' => {'execute' => {:value => 'meth2'}},
'meth3' => {'execute' => {:value => 'meth3'}}}
ae_methods = {'meth1' => {:scope => 'instance', :location => 'inline',
:data => script1,
:language => 'ruby', 'params' => {}},
'meth2' => {:scope => 'instance', :location => 'inline',
:data => script2,
:language => 'ruby', 'params' => {}},
'meth3' => {:scope => 'instance', :location => 'inline',
:data => script3,
:language => 'ruby', 'params' => {}}}
FactoryBot.create(:miq_ae_class, :with_instances_and_methods,
attrs.merge('ae_fields' => ae_fields,
'ae_instances' => ae_instances,
'ae_methods' => ae_methods))
end
def create_multi_state_class(attrs = {})
ae_fields = {'state1' => {:aetype => 'state', :datatype => 'string', :priority => 1},
'state2' => {:aetype => 'state', :datatype => 'string', :priority => 2},
'state3' => {:aetype => 'state', :datatype => 'string', :priority => 3}}
fq1 = "/#{@domain}/#{@namespace}/#{@retry_class}/meth1"
fq2 = "/#{@domain}/#{@namespace}/#{@retry_class}/meth2"
fq3 = "/#{@domain}/#{@namespace}/#{@retry_class}/meth3"
ae_instances = {@state_instance => {'state1' => {:value => fq1},
'state2' => {:value => fq2},
'state3' => {:value => fq3}}}
FactoryBot.create(:miq_ae_class, :with_instances_and_methods,
attrs.merge('ae_fields' => ae_fields,
'ae_methods' => {},
'ae_instances' => ae_instances))
end
def create_restart_model(script1, script2, script3)
dom = FactoryBot.create(:miq_ae_domain, :name => @domain)
ns = FactoryBot.create(:miq_ae_namespace, :parent => dom, :name => @namespace)
@ns_fqname = ns.fqname
create_multi_state_class(:namespace => @ns_fqname, :name => @state_class)
attrs = {:namespace => @ns_fqname, :name => @retry_class}
create_method_class(attrs, script1, script2, script3)
create_root_class(:namespace => @ns_fqname, :name => @root_class)
end
it "check persistent hash" do
setup_model(method_script_state_var)
expected = {'three' => 3, 'one' => 1, 'two' => 2, 'gravy' => 'train'}
send_ae_request_via_queue(@automate_args)
status, _message, ws = deliver_ae_request_from_queue
expect(status).not_to eq(MiqQueue::STATUS_ERROR)
expect(ws).not_to be_nil
expect(MiqQueue.count).to eq(2)
status, _message, ws = deliver_ae_request_from_queue
expect(status).not_to eq(MiqQueue::STATUS_ERROR)
expect(ws.persist_state_hash).to eq(expected)
expect(ws.root.attributes['finished']).to be_truthy
end
it "check max retries" do
setup_model(perpetual_retry_script)
send_ae_request_via_queue(@automate_args)
(@max_retries + 2).times do
status, _message, ws = deliver_ae_request_from_queue
expect(status).not_to eq(MiqQueue::STATUS_ERROR)
expect(ws).not_to be_nil
end
expect(deliver_ae_request_from_queue).to be_nil
end
it "check max_time" do
max_time = 2
setup_model(perpetual_retry_script, max_time)
send_ae_request_via_queue(@automate_args)
status, _message, ws = deliver_ae_request_from_queue
expect(status).not_to eq(MiqQueue::STATUS_ERROR)
expect(ws).to be_truthy
Timecop.travel(max_time + 1) do
status, _message, ws = deliver_ae_request_from_queue
expect(status).not_to eq(MiqQueue::STATUS_ERROR)
expect(ws).to be_truthy
end
Timecop.travel(max_time * 2 + 2) do
status, _message, ws = deliver_ae_request_from_queue
expect(status).not_to be
end
end
it "check restart without next state" do
create_restart_model(simpleton_script, simpleton_script, perpetual_restart_script)
send_ae_request_via_queue(@automate_args)
status, _message, ws = deliver_ae_request_from_queue
expect(status).not_to eq(MiqQueue::STATUS_ERROR)
expect(ws).not_to be_nil
q = MiqQueue.where(:state => 'ready').first
expect(q.args[0][:state]).to eql('state1')
end
it "check restart with next state" do
create_restart_model(simpleton_script, simpleton_script, perpetual_restart_script_with_nextstate)
send_ae_request_via_queue(@automate_args)
status, _message, ws = deliver_ae_request_from_queue
expect(status).not_to eq(MiqQueue::STATUS_ERROR)
expect(ws).not_to be_nil
q = MiqQueue.where(:state => 'ready').first
expect(q.args[0][:state]).to eql('state2')
end
it "retry with server affinity set" do
setup_model(retry_server_affinity_script)
send_ae_request_via_queue(@automate_args)
status, _message, ws = deliver_ae_request_from_queue
expect(status).not_to eq(MiqQueue::STATUS_ERROR)
expect(ws).not_to be_nil
expect(MiqQueue.count).to eq(2)
q = MiqQueue.where(:state => 'ready').first
expect(q[:server_guid]).to eql(MiqServer.my_guid)
end
it "retry without server affinity set" do
setup_model(retry_script)
send_ae_request_via_queue(@automate_args)
status, _message, ws = deliver_ae_request_from_queue
expect(status).not_to eq(MiqQueue::STATUS_ERROR)
expect(ws).not_to be_nil
expect(MiqQueue.count).to eq(2)
q = MiqQueue.where(:state => 'ready').first
expect(q[:server_guid]).to be_nil
end
it "it can preserve old state data" do
ae_state_data = {'old' => 1}
setup_model(retry_script)
send_ae_request_via_queue(@automate_args.merge(:ae_state_data => ae_state_data.to_yaml))
status, _message, ws = deliver_ae_request_from_queue
expect(status).not_to eq(MiqQueue::STATUS_ERROR)
expect(ws).not_to be_nil
expect(MiqQueue.count).to eq(2)
q = MiqQueue.where(:state => 'ready').first
expect(q[:server_guid]).to be_nil
expect(YAML.load(q.args.first[:ae_state_data])).to eq(ae_state_data)
end
end