ns_lib: new env variable to set NS instantiation timeout for testing
[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 *** Variables ***
14 ${success_return_code}   0
15 ${ns_launch_pol_time}   30sec
16 ${ns_delete_max_wait_time}   4min
17 ${ns_delete_pol_time}   15sec
18 ${ns_action_max_wait_time}   1min
19 ${ns_action_pol_time}   15sec
20 ${vnf_scale_max_wait_time}   5min
21 ${vnf_scale_pol_time}   30sec
22 # If env variable "OSM_NS_TIMEOUT" exists, it prevails over the default timeout
23 ${default_ns_launch_max_wait_time}=   Get Environment Variable    OSM_NS_TIMEOUT   default=5min
24
25
26 *** Keywords ***
27 Create Network Service
28     [Arguments]   ${nsd}   ${vim_name}   ${ns_name}   ${ns_config}   ${publickey}  ${ns_launch_max_wait_time}=${default_ns_launch_max_wait_time}  ${config_file}=${EMPTY}
29
30     ${config_attr}   Set Variable If   '${ns_config}'!='${EMPTY}'   --config '${ns_config}'   \
31     ${sshkeys_attr}   Set Variable If   '${publickey}'!='${EMPTY}'   --ssh_keys ${publickey}   \
32     ${config_file_attr}   Set Variable If   '${config_file}'!='${EMPTY}'   --config_file '${config_file}'   \
33
34     ${ns_id}=   Instantiate Network Service   ${ns_name}   ${nsd}   ${vim_name}   ${config_attr} ${sshkeys_attr} ${config_file_attr}
35     log   ${ns_id}
36
37     WAIT UNTIL KEYWORD SUCCEEDS   ${ns_launch_max_wait_time}   ${ns_launch_pol_time}   Check For NS Instance To Configured   ${ns_name}
38     Check For NS Instance For Failure   ${ns_name}
39     [Return]  ${ns_id}
40
41
42 Instantiate Network Service
43     [Arguments]   ${ns_name}   ${nsd}   ${vim_name}   ${ns_extra_args}
44
45     ${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}
46     log   ${stdout}
47     Should Be Equal As Integers   ${rc}   ${success_return_code}
48     [Return]  ${stdout}
49
50
51 Get Vnf Management Ip Address
52     [Arguments]   ${ns_id}   ${vnf_member_index}
53
54     Should Not Be Empty   ${ns_id}
55     Should Not Be Empty   ${vnf_member_index}
56     ${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
57     log   ${stdout}
58     Should Be Equal As Integers   ${rc}   ${success_return_code}
59     [Return]  ${stdout}
60
61
62 Get Vnf Id
63     [Arguments]   ${ns_id}   ${vnf_member_index}
64
65     Should Not Be Empty   ${ns_id}
66     Should Not Be Empty   ${vnf_member_index}
67     ${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
68     log   ${stdout}
69     Should Be Equal As Integers   ${rc}   ${success_return_code}
70     [Return]  ${stdout}
71
72
73 Get Ns Vnf List
74     [Arguments]   ${ns_id}
75
76     Should Not Be Empty   ${ns_id}
77     @{vnf_list_string}=   Run and Return RC and Output   osm vnf-list | grep ${ns_id} | awk '{print $2}' 2>&1
78     # Returns a String of vnf_id and needs to be converted into a list
79     @{vnf_list} =  Split String    ${vnf_list_string}[1]
80     Log List    ${vnf_list}
81     [Return]  @{vnf_list}
82
83
84 Get Ns Ip List
85     [Arguments]   @{vnf_list}
86
87     should not be empty   @{vnf_list}
88     @{temp_list}=    Create List
89     FOR   ${vnf_id}   IN   @{vnf_list}
90         log   ${vnf_id}
91         @{vnf_ip_list}   Get Vnf Ip List   ${vnf_id}
92         @{temp_list}=   Combine Lists   ${temp_list}    ${vnf_ip_list}
93     END
94     should not be empty   ${temp_list}
95     [return]  @{temp_list}
96
97
98 Get Vnf Ip List
99     [arguments]   ${vnf_id}
100
101     should not be empty   ${vnf_id}
102     @{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
103     # returns a string of ip addresses and needs to be converted into a list
104     should not be empty   ${vnf_ip_list_string}[1]
105     @{vnf_ip_list} =  split string    ${vnf_ip_list_string}[1]
106     log list    ${vnf_ip_list}
107     should not be empty   ${vnf_ip_list}
108     [return]  @{vnf_ip_list}
109
110
111 Check For Ns Instance To Configured
112     [arguments]  ${ns_name}
113
114     ${rc}   ${stdout}=   run and return rc and output   osm ns-list --filter name="${ns_name}"
115     log   ${stdout}
116     Should Be Equal As Integers   ${rc}   ${success_return_code}
117     Should Contain Any   ${stdout}   READY   BROKEN
118
119 Check For NS Instance For Failure
120     [Arguments]  ${ns_name}
121
122     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list --filter name="${ns_name}"
123     log   ${stdout}
124     Should Be Equal As Integers   ${rc}   ${success_return_code}
125     Should Not Contain   ${stdout}   BROKEN
126
127 Check For NS Instance To Be Deleted
128     [Arguments]  ${ns}
129
130     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list | awk '{print $2}' | grep ${ns}
131     Should Not Be Equal As Strings   ${stdout}   ${ns}
132
133 Delete NS
134     [Documentation]  Delete ns
135     [Arguments]  ${ns}
136
137     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-delete ${ns}
138     log   ${stdout}
139     Should Be Equal As Integers   ${rc}   ${success_return_code}
140
141     WAIT UNTIL KEYWORD SUCCEEDS  ${ns_delete_max_wait_time}   ${ns_delete_pol_time}   Check For NS Instance To Be Deleted   ${ns}
142
143 Execute NS Action
144     [Documentation]     Execute an action over the desired NS.
145     ...                 Parameters are given to this function in key=value format (one argument per key/value pair).
146     ...                 Return the ID of the operation associated to the executed action.
147     ...                 Examples of execution:
148     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}
149     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}  \${param1}=\${value1}  \${param2}=\${value2}
150
151     [Arguments]  ${ns_name}  ${ns_action}  ${vnf_member_index}  @{action_params}
152
153     ${params}=  Set Variable  ${EMPTY}
154     FOR  ${param}  IN  @{action_params}
155         ${match}  ${param_name}  ${param_value} =  Should Match Regexp  ${param}  (.+)=(.+)  msg=Syntax error in parameters
156         ${params}=  Catenate  SEPARATOR=  ${params}  "${param_name}":"${param_value}",
157     END
158     ${osm_ns_action_command}=  Set Variable  osm ns-action --action_name ${ns_action} --vnf_name ${vnf_member_index}
159     ${osm_ns_action_command}=  Run Keyword If  '${params}'!='${EMPTY}'  Catenate  ${osm_ns_action_command}  --params '{${params}}'
160     ...  ELSE  Set Variable  ${osm_ns_action_command}
161     ${osm_ns_action_command}=  Catenate  ${osm_ns_action_command}  ${ns_name}
162     ${rc}  ${stdout}=  Run and Return RC and Output  ${osm_ns_action_command}
163     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
164     Wait Until Keyword Succeeds  ${ns_action_max_wait_time}  ${ns_action_pol_time}  Check For NS Operation Completed  ${stdout}
165     [Return]  ${stdout}
166
167
168 Execute NS K8s Action
169     [Documentation]     Execute an action over the desired K8s NS.
170     ...                 Parameters are given to this function in key=value format (one argument per key/value pair).
171     ...                 Return the ID of the operation associated to the executed action.
172     ...                 Examples of execution:
173     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}
174     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}  \${param1}=\${value1}  \${param2}=\${value2}
175
176     [Arguments]  ${ns_name}  ${ns_action}  ${vnf_member_index}  ${kdu_name}  @{action_params}
177
178     ${params}=  Set Variable  ${EMPTY}
179     FOR  ${param}  IN  @{action_params}
180         ${match}  ${param_name}  ${param_value} =  Should Match Regexp  ${param}  (.+)=(.+)  msg=Syntax error in parameters
181         ${params}=  Catenate  SEPARATOR=  ${params}  "${param_name}":"${param_value}",
182     END
183     ${osm_ns_action_command}=  Set Variable  osm ns-action --action_name ${ns_action} --vnf_name ${vnf_member_index} --kdu_name ${kdu_name}
184     ${osm_ns_action_command}=  Run Keyword If  '${params}'!='${EMPTY}'  Catenate  ${osm_ns_action_command}  --params '{${params}}'
185     ...  ELSE  Set Variable  ${osm_ns_action_command}
186     ${osm_ns_action_command}=  Catenate  ${osm_ns_action_command}  ${ns_name}
187     ${rc}  ${stdout}=  Run and Return RC and Output  ${osm_ns_action_command}
188     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
189     Wait Until Keyword Succeeds  ${ns_action_max_wait_time}  ${ns_action_pol_time}  Check For NS Operation Completed  ${stdout}
190     [Return]  ${stdout}
191
192
193 Execute Manual VNF Scale
194     [Documentation]     Execute a manual VNF Scale action.
195     ...                 The parameter 'scale_type' must be SCALE_IN or SCALE_OUT.
196     ...                 Return the ID of the operation associated to the executed scale action.
197
198     [Arguments]  ${ns_name}  ${vnf_member_index}  ${scaling_group}  ${scale_type}
199
200     Should Contain Any  ${scale_type}  SCALE_IN  SCALE_OUT  msg=Unknown scale type: ${scale_type}  values=False
201     ${osm_vnf_scale_command}=  Set Variable  osm vnf-scale --scaling-group ${scaling_group}
202     ${osm_vnf_scale_command}=  Run Keyword If  '${scale_type}'=='SCALE_IN'  Catenate  ${osm_vnf_scale_command}  --scale-in
203     ...  ELSE  Catenate  ${osm_vnf_scale_command}  --scale-out
204     ${osm_vnf_scale_command}=  Catenate  ${osm_vnf_scale_command}  ${ns_name}  ${vnf_member_index}
205     ${rc}  ${stdout}=  Run and Return RC and Output  ${osm_vnf_scale_command}
206     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
207     Wait Until Keyword Succeeds  ${ns_action_max_wait_time}  ${ns_action_pol_time}  Check For NS Operation Completed  ${stdout}
208     [Return]  ${stdout}
209
210
211 Get Operations List
212     [Arguments]  ${ns_name}
213
214     ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-op-list ${ns_name}
215     log  ${stdout}
216     log  ${rc}
217     Should Be Equal As Integers  ${rc}  ${success_return_code}
218
219
220 Check For NS Operation Completed
221     [Documentation]     Check wheter the status of the desired operation is "COMPLETED" or not.
222
223     [Arguments]  ${ns_operation_id}
224
225     ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-op-show ${ns_operation_id} --literal | yq r - operationState
226     log  ${stdout}
227     Should Be Equal As Integers  ${rc}  ${success_return_code}
228     Should Contain  ${stdout}  COMPLETED  msg=Timeout waiting for ns-action with id ${ns_operation_id}  values=False
229
230
231 Get Ns Vnfr Ids
232     [Documentation]     Return a list with the IDs of the VNF records of a NS instance.
233
234     [Arguments]  ${ns_id}
235
236     ${rc}  ${stdout}=  Run and Return RC and Output  osm vnf-list | grep ${ns_id} | awk '{print $2}' 2>&1
237     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
238     @{vdur} =  Split String  ${stdout}
239     [Return]  @{vdur}
240
241
242 Get Vnf Vdur Names
243     [Documentation]     Return a list with the names of the VDU records of a VNF instance.
244
245     [Arguments]  ${vnf_id}
246
247     ${rc}  ${stdout}=  Run and Return RC and Output  osm vnf-show ${vnf_id} --literal | yq r - vdur.*.name
248     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
249     @{vdur} =  Split String  ${stdout}
250     [Return]  @{vdur}
251