Feature 10909: Robot tests for healing
[osm/tests.git] / robot-systest / lib / ns_lib.robot
1 #   Licensed under the Apache License, Version 2.0 (the "License");
2 #   you may not use this file except in compliance with the License.
3 #   You may obtain a copy of the License at
4 #
5 #       http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #   Unless required by applicable law or agreed to in writing, software
8 #   distributed under the License is distributed on an "AS IS" BASIS,
9 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 #   See the License for the specific language governing permissions and
11 #   limitations under the License.
12
13 *** Settings ***
14 Documentation     Library to deploy and delete NS, and run operations on them.
15
16 Library   DateTime
17
18
19 *** Variables ***
20 ${success_return_code}   0
21 ${ns_launch_pol_time}   30sec
22 ${ns_delete_pol_time}   15sec
23 ${ns_action_max_wait_time}   1min
24 ${ns_action_pol_time}   15sec
25 ${vnf_scale_pol_time}   15sec
26 ${healing_pol_time}   15sec
27 ${vim_timeout_multiplier}   %{OSM_VIM_MULTIPLIER_TIMEOUT=1.0}
28
29
30 *** Keywords ***
31 Create Network Service
32     [Arguments]   ${nsd}   ${vim_name}   ${ns_name}   ${ns_config}   ${publickey}   ${ns_launch_max_wait_time}=5min   ${config_file}=${EMPTY}
33
34     ${ns_launch_max_wait_time}=   Convert Time   ${ns_launch_max_wait_time}   result_format=number
35     ${ns_launch_max_wait_time}=   Evaluate   ${ns_launch_max_wait_time} * ${vim_timeout_multiplier}
36     Log   ${ns_launch_max_wait_time}
37     ${config_attr}   Set Variable If   '${ns_config}'!='${EMPTY}'   --config '${ns_config}'   \
38     ${sshkeys_attr}   Set Variable If   '${publickey}'!='${EMPTY}'   --ssh_keys ${publickey}   \
39     ${config_file_attr}   Set Variable If   '${config_file}'!='${EMPTY}'   --config_file '${config_file}'   \
40
41     ${ns_id}=   Instantiate Network Service   ${ns_name}   ${nsd}   ${vim_name}   ${config_attr} ${sshkeys_attr} ${config_file_attr}
42     log   ${ns_id}
43
44     WAIT UNTIL KEYWORD SUCCEEDS   ${ns_launch_max_wait_time}   ${ns_launch_pol_time}   Check For NS Instance To Configured   ${ns_name}
45     Check For NS Instance For Failure   ${ns_name}
46     [Return]  ${ns_id}
47
48
49 Instantiate Network Service
50     [Arguments]   ${ns_name}   ${nsd}   ${vim_name}   ${ns_extra_args}
51
52     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-create --ns_name ${ns_name} --nsd_name ${nsd} --vim_account ${vim_name} ${ns_extra_args}
53     log   ${stdout}
54     Should Be Equal As Integers   ${rc}   ${success_return_code}
55     [Return]  ${stdout}
56
57
58 Update Network Service
59     [Arguments]   ${ns_id}   ${update_type}   ${ns_update_config}   ${ns_update_timeout}
60
61     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-update ${ns_id} --updatetype ${update_type} --config ${ns_update_config} --timeout ${ns_update_timeout} --wait
62     log   ${stdout}
63     Should Be Equal As Integers   ${rc}   ${success_return_code}
64     [Return]  ${stdout}
65
66
67 Get Vnf Management Ip Address
68     [Arguments]   ${ns_id}   ${vnf_member_index}
69
70     Should Not Be Empty   ${ns_id}
71     Should Not Be Empty   ${vnf_member_index}
72     ${rc}   ${stdout}=   Run and Return RC and Output   osm vnf-list --filter member-vnf-index-ref=${vnf_member_index} | grep ${ns_id} | awk '{print $14}' 2>&1
73     log   ${stdout}
74     Should Be Equal As Integers   ${rc}   ${success_return_code}
75     [Return]  ${stdout}
76
77
78 Get Vnf Id
79     [Arguments]   ${ns_id}   ${vnf_member_index}
80
81     Should Not Be Empty   ${ns_id}
82     Should Not Be Empty   ${vnf_member_index}
83     ${rc}   ${stdout}=   Run and Return RC and Output   osm vnf-list --filter member-vnf-index-ref=${vnf_member_index} | grep ${ns_id} | awk '{print $2}' 2>&1
84     log   ${stdout}
85     Should Be Equal As Integers   ${rc}   ${success_return_code}
86     [Return]  ${stdout}
87
88
89 Get Ns Id
90     [Arguments]   ${ns_name}
91
92     Should Not Be Empty   ${ns_name}
93     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list | grep ${ns_name} | awk '{print $4}' 2>&1
94     log   ${stdout}
95     Should Be Equal As Integers   ${rc}   ${success_return_code}
96     Should Not Be Empty   ${stdout}
97     [Return]   ${stdout}
98
99
100 Get Ns Vnf List
101     [Arguments]   ${ns_id}
102
103     Should Not Be Empty   ${ns_id}
104     @{vnf_list_string}=   Run and Return RC and Output   osm vnf-list | grep ${ns_id} | awk '{print $2}' 2>&1
105     # Returns a String of vnf_id and needs to be converted into a list
106     @{vnf_list} =  Split String    ${vnf_list_string}[1]
107     Log List    ${vnf_list}
108     [Return]  @{vnf_list}
109
110
111 Get Ns Ip List
112     [Arguments]   @{vnf_list}
113
114     should not be empty   @{vnf_list}
115     @{temp_list}=    Create List
116     FOR   ${vnf_id}   IN   @{vnf_list}
117         log   ${vnf_id}
118         @{vnf_ip_list}   Get Vnf Ip List   ${vnf_id}
119         @{temp_list}=   Combine Lists   ${temp_list}    ${vnf_ip_list}
120     END
121     should not be empty   ${temp_list}
122     [return]  @{temp_list}
123
124
125 Get Vnf Ip List
126     [arguments]   ${vnf_id}
127
128     should not be empty   ${vnf_id}
129     @{vnf_ip_list_string}=   run and return rc and output   osm vnf-list --filter id=${vnf_id} | grep -o '[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}' | sort -t: -u -k1,1 2>&1
130     # returns a string of ip addresses and needs to be converted into a list
131     should not be empty   ${vnf_ip_list_string}[1]
132     @{vnf_ip_list} =  split string    ${vnf_ip_list_string}[1]
133     log list    ${vnf_ip_list}
134     should not be empty   ${vnf_ip_list}
135     [return]  @{vnf_ip_list}
136
137
138 Check For Ns Instance To Configured
139     [arguments]  ${ns_name}
140
141     ${rc}   ${stdout}=   Run and Return RC and Output   openstack server list
142     log   ${stdout}
143     ${rc}   ${stdout}=   run and return rc and output   osm ns-list --filter name="${ns_name}"
144     log   ${stdout}
145     Should Be Equal As Integers   ${rc}   ${success_return_code}
146     Should Contain Any   ${stdout}   READY   BROKEN
147
148 Check For NS Instance For Failure
149     [Arguments]  ${ns_name}
150
151     ${rc}   ${stdout}=   Run and Return RC and Output   openstack server list
152     log   ${stdout}
153     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list --filter name="${ns_name}"
154     log   ${stdout}
155     Should Be Equal As Integers   ${rc}   ${success_return_code}
156     Should Not Contain   ${stdout}   BROKEN
157
158 Check For NS Instance To Be Deleted
159     [Arguments]  ${ns}
160
161     ${rc}   ${stdout}=   Run and Return RC and Output   openstack server list
162     log   ${stdout}
163     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list | awk '{print $2}' | grep ${ns}
164     Should Not Be Equal As Strings   ${stdout}   ${ns}
165
166 Delete NS
167     [Documentation]   Delete ns
168     [Arguments]   ${ns}   ${ns_delete_max_wait_time}=4min
169
170     ${ns_delete_max_wait_time}=   Convert Time   ${ns_delete_max_wait_time}   result_format=number
171     ${ns_delete_max_wait_time}=   Evaluate   ${ns_delete_max_wait_time} * ${vim_timeout_multiplier}
172     Log   ${ns_delete_max_wait_time}
173     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-delete ${ns}
174     Log   ${stdout}
175     Should Be Equal As Integers   ${rc}   ${success_return_code}
176
177     WAIT UNTIL KEYWORD SUCCEEDS  ${ns_delete_max_wait_time}   ${ns_delete_pol_time}   Check For NS Instance To Be Deleted   ${ns}
178
179 Execute NS Action
180     [Documentation]     Execute an action over the desired NS.
181     ...                 Parameters are given to this function in key=value format (one argument per key/value pair).
182     ...                 Return the ID of the operation associated to the executed action.
183     ...                 Examples of execution:
184     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}
185     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}  \${param1}=\${value1}  \${param2}=\${value2}
186
187     [Arguments]  ${ns_name}  ${ns_action}  ${vnf_member_index}  @{action_params}
188
189     ${params}=  Set Variable  ${EMPTY}
190     FOR  ${param}  IN  @{action_params}
191         ${match}  ${param_name}  ${param_value} =  Should Match Regexp  ${param}  (.+)=(.+)  msg=Syntax error in parameters
192         ${params}=  Catenate  SEPARATOR=  ${params}  "${param_name}":"${param_value}",
193     END
194     ${osm_ns_action_command}=  Set Variable  osm ns-action --action_name ${ns_action} --vnf_name ${vnf_member_index}
195     ${osm_ns_action_command}=  Run Keyword If  '${params}'!='${EMPTY}'  Catenate  ${osm_ns_action_command}  --params '{${params}}'
196     ...  ELSE  Set Variable  ${osm_ns_action_command}
197     ${osm_ns_action_command}=  Catenate  ${osm_ns_action_command}  ${ns_name}
198     ${rc}  ${stdout}=  Run and Return RC and Output  ${osm_ns_action_command}
199     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
200     Wait Until Keyword Succeeds  ${ns_action_max_wait_time}  ${ns_action_pol_time}  Check For NS Operation Ended  ${stdout}
201     Check For NS Operation Completed  ${stdout}
202     [Return]  ${stdout}
203
204
205 Execute NS K8s Action
206     [Documentation]     Execute an action over the desired K8s NS.
207     ...                 Parameters are given to this function in key=value format (one argument per key/value pair).
208     ...                 Return the ID of the operation associated to the executed action.
209     ...                 Examples of execution:
210     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}
211     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}  \${param1}=\${value1}  \${param2}=\${value2}
212
213     [Arguments]  ${ns_name}  ${ns_action}  ${vnf_member_index}  ${kdu_name}  @{action_params}
214
215     ${params}=  Set Variable  ${EMPTY}
216     FOR  ${param}  IN  @{action_params}
217         ${match}  ${param_name}  ${param_value} =  Should Match Regexp  ${param}  (.+)=(.+)  msg=Syntax error in parameters
218         ${params}=  Catenate  SEPARATOR=  ${params}  "${param_name}":"${param_value}",
219     END
220     ${osm_ns_action_command}=  Set Variable  osm ns-action --action_name ${ns_action} --vnf_name ${vnf_member_index} --kdu_name ${kdu_name}
221     ${osm_ns_action_command}=  Run Keyword If  '${params}'!='${EMPTY}'  Catenate  ${osm_ns_action_command}  --params '{${params}}'
222     ...  ELSE  Set Variable  ${osm_ns_action_command}
223     ${osm_ns_action_command}=  Catenate  ${osm_ns_action_command}  ${ns_name}
224     ${rc}  ${stdout}=  Run and Return RC and Output  ${osm_ns_action_command}
225     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
226     Wait Until Keyword Succeeds  ${ns_action_max_wait_time}  ${ns_action_pol_time}  Check For NS Operation Ended  ${stdout}
227     Check For NS Operation Completed  ${stdout}
228     [Return]  ${stdout}
229
230
231 Execute Manual VNF Scale
232     [Documentation]     Execute a manual VNF Scale action.
233     ...                 The parameter 'scale_type' must be SCALE_IN or SCALE_OUT.
234     ...                 Return the ID of the operation associated to the executed scale action.
235
236     [Arguments]   ${ns_name}   ${vnf_member_index}   ${scaling_group}   ${scale_type}   ${vnf_scale_max_wait_time}=2min
237
238     ${vnf_scale_max_wait_time}=   Convert Time   ${vnf_scale_max_wait_time}   result_format=number
239     ${vnf_scale_max_wait_time}=   Evaluate   ${vnf_scale_max_wait_time} * ${vim_timeout_multiplier}
240     Should Contain Any  ${scale_type}  SCALE_IN  SCALE_OUT  msg=Unknown scale type: ${scale_type}  values=False
241     ${osm_vnf_scale_command}=  Set Variable  osm vnf-scale --scaling-group ${scaling_group}
242     ${osm_vnf_scale_command}=  Run Keyword If  '${scale_type}'=='SCALE_IN'  Catenate  ${osm_vnf_scale_command}  --scale-in
243     ...  ELSE  Catenate  ${osm_vnf_scale_command}  --scale-out
244     ${osm_vnf_scale_command}=  Catenate  ${osm_vnf_scale_command}  ${ns_name}  ${vnf_member_index}
245     ${rc}  ${stdout}=  Run and Return RC and Output  ${osm_vnf_scale_command}
246     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
247     Wait Until Keyword Succeeds  ${vnf_scale_max_wait_time}  ${vnf_scale_pol_time}  Check For NS Operation Ended  ${stdout}
248     Check For NS Operation Completed  ${stdout}
249     [Return]  ${stdout}
250
251
252 Heal Network Service
253     [Documentation]     Execute healing operation over one NS.
254         ...             Return the ID of the operation associated to the executed healing action.
255
256     [Arguments]   ${ns_name}   ${params}   ${healing_max_wait_time}=10m
257
258     Should Not Be Empty   ${ns_name}
259     Should Not Be Empty   ${params}
260     ${healing_max_wait_time}=   Convert Time   ${healing_max_wait_time}   result_format=number
261     ${healing_max_wait_time}=   Evaluate   ${healing_max_wait_time} * ${vim_timeout_multiplier}
262     ${rc}  ${stdout}=  Run and Return RC and Output   osm ns-heal ${ns_name} ${params}
263     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
264     Wait Until Keyword Succeeds  ${healing_max_wait_time}  ${healing_pol_time}  Check For NS Operation Ended  ${stdout}
265     Check For NS Operation Completed  ${stdout}
266     [Return]  ${stdout}
267
268
269 Get Operations List
270     [Arguments]  ${ns_name}
271
272     Should Not Be Empty   ${ns_name}
273     ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-op-list ${ns_name}
274     log  ${stdout}
275     log  ${rc}
276     Should Be Equal As Integers  ${rc}  ${success_return_code}
277
278
279 Check For NS Operation Completed
280     [Documentation]     Check wheter the status of the desired operation is "COMPLETED" or not.
281
282     [Arguments]  ${ns_operation_id}
283
284     Should Not Be Empty   ${ns_operation_id}
285     ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-op-show ${ns_operation_id} --literal | yq .operationState | tr -d \\"
286     log  ${stdout}
287     Should Be Equal As Integers  ${rc}  ${success_return_code}
288     Should Contain  ${stdout}  COMPLETED  msg=The ns-action with id ${ns_operation_id} was not completed  values=False
289
290
291 Check For NS Operation Failed
292     [Documentation]     Check wheter the status of the desired operation is "FAILED" or not.
293
294     [Arguments]  ${ns_operation_id}
295
296     Should Not Be Empty   ${ns_operation_id}
297     ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-op-show ${ns_operation_id} --literal | yq .operationState | tr -d \\"
298     log  ${stdout}
299     Should Be Equal As Integers  ${rc}  ${success_return_code}
300     Should Contain  ${stdout}  FAILED  msg=Timeout waiting for ns-action with id ${ns_operation_id}  values=False
301
302
303 Check For NS Operation Ended
304     [Documentation]     Check wheter the status of the desired operation is "FAILED" or "COMPLETED".
305
306     [Arguments]  ${ns_operation_id}
307
308     Should Not Be Empty   ${ns_operation_id}
309     ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-op-show ${ns_operation_id} --literal | yq .operationState | tr -d \\"
310     log  ${stdout}
311     Should Be Equal As Integers  ${rc}  ${success_return_code}
312     Should Contain Any  ${stdout}  FAILED  COMPLETED  msg=Timeout waiting for ns-action with id ${ns_operation_id}  values=False
313
314
315 Get Ns Vnfr Ids
316     [Documentation]     Return a list with the IDs of the VNF records of a NS instance.
317
318     [Arguments]  ${ns_id}
319
320     Should Not Be Empty   ${ns_id}
321     ${rc}  ${stdout}=  Run and Return RC and Output  osm vnf-list | grep ${ns_id} | awk '{print $2}' 2>&1
322     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
323     @{vdur} =  Split String  ${stdout}
324     [Return]  @{vdur}
325
326
327 Get Vnf Vdur Names
328     [Documentation]     Return a list with the names of the VDU records of a VNF instance.
329
330     [Arguments]  ${vnf_id}
331
332     Should Not Be Empty   ${vnf_id}
333     ${rc}  ${stdout}=  Run and Return RC and Output  osm vnf-show ${vnf_id} --literal | yq .vdur[].name | tr -d \\"
334     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
335     @{vdur} =  Split String  ${stdout}
336     [Return]  @{vdur}
337
338
339 Get Vnf Kdu Replica Count
340     [Documentation]     Return the number of KDU replicas (empty if none) of a VNF instance.
341
342     [Arguments]   ${vnf_id}   ${kdu_name}
343
344     Should Not Be Empty   ${vnf_id}
345     Should Not Be Empty   ${kdu_name}
346     ${rc}  ${stdout}=  Run and Return RC and Output  osm vnf-show ${vnf_id} --kdu ${kdu_name} | yq .config.replicaCount | tr -d \\"
347     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
348     ${return} =   Set Variable If   '${stdout}' == 'null'   ${EMPTY}    ${stdout}
349     [Return]  ${return}
350
351
352 Get Application Names
353     [Documentation]    Return the list of the application of a VNF instance.
354
355     [Arguments]   ${ns_name}
356
357     Should Not Be Empty   ${ns_name}
358     ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-show ${ns_name} --literal | yq ._admin.deployed.VCA[].application | tr -d \\"
359     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
360     @{app_names} =  Split String  ${stdout}
361     [Return]  ${app_names}
362
363
364 Get VDU Affinity Group Name
365     [Documentation]   Return the affinity group name for a NF
366
367     [Arguments]   ${ns_name}   ${vnf_id}   ${vdur_id}
368
369     ${rc}   ${affinity_group_id}=   Run and Return RC and Output   osm vnf-show ${vnf_id} --literal | yq '.vdur[] | select(.id == "'${vdur_id}'")' | yq '."affinity-or-anti-affinity-group-id"[0]' | tr -d \\" 2>&1
370     log   ${affinity_group_id}
371     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${affinity_group_id}   values=False
372     ${rc}   ${affinity_group_name}=   Run and Return RC and Output   osm ns-show ${ns_name} --literal | yq '."affinity-or-anti-affinity-group"[] | select(.id == "'${affinity_group_id}'")' | yq '.name' | tr -d \\" 2>&1
373     log   ${affinity_group_name}
374     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${affinity_group_name}   values=False
375     [Return]   ${affinity_group_name}