Bug 2327 fix to verify ipaddress in sol003_02 testsuite
[osm/tests.git] / robot-systest / lib / openstack_lib.resource
1 *** Comments ***
2 #   Licensed under the Apache License, Version 2.0 (the "License");
3 #   you may not use this file except in compliance with the License.
4 #   You may obtain a copy of the License at
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
8 #   Unless required by applicable law or agreed to in writing, software
9 #   distributed under the License is distributed on an "AS IS" BASIS,
10 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 #   See the License for the specific language governing permissions and
12 #   limitations under the License.
13
14
15 *** Settings ***
16 Documentation   Library providing keywords to interact with Openstack with openstack client.
17
18 Library   OperatingSystem
19 Library   String
20
21 Resource   ns_lib.resource
22 Resource   vnf_lib.resource
23
24
25 *** Variables ***
26 ${SUCCESS_RETURN_CODE}   0
27
28
29 *** Keywords ***
30 Get Server Flavor ID
31     [Documentation]   Get from Openstack the flavor id of a server/VM passed as argument.
32     [Arguments]   ${server_id}
33     Should Not Be Empty   ${server_id}
34     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack server show ${server_id} | grep flavor | awk 'NR>1{print $1}' RS='(' FS=')'
35     Log   ${stdout}
36     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
37     RETURN   ${stdout}
38
39 Get Flavor Properties
40     [Documentation]   Get from Openstack the properties of a flavor passed as argument.
41     [Arguments]   ${flavor_id}
42     Should Not Be Empty   ${flavor_id}
43     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack flavor show ${flavor_id} | grep properties | awk -F '|' '{print $3}'
44     Log   ${stdout}
45     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
46     RETURN   ${stdout}
47
48 Check NS Servers In VIM
49     [Documentation]   Check if the number of servers in Openstack by filtering by NS name is as expected.
50     [Arguments]   ${ns_name}   ${number}
51     Should Not Be Empty   ${ns_name}
52     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack server list | grep ${ns_name} | awk '{print $4}'
53     Log   ${stdout}
54     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
55     @{servers}=   Split String   ${stdout}
56     ${n_servers}=   Get Length   ${servers}
57     Should Be Equal As Integers   ${number}   ${n_servers}
58
59 Check If Volume Bootable
60     [Documentation]   Check the volume properties and return whether it is bootable or not.
61     [Arguments]   ${volume_id}
62     Should Not Be Empty   ${volume_id}
63     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack volume show ${volume_id} | grep bootable | awk '{print $4}'
64     Log   ${stdout}
65     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
66     RETURN   ${stdout}
67
68 Get Project Quota
69     [Documentation]   Get a field (ram, cores, ports, etc.) from quota in current project.
70     [Arguments]   ${field}
71     Should Not Be Empty   ${field}
72     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack quota show -f yaml | yq '.[] | select(.Resource=="${field}") | .Limit'
73     Log   ${stdout}
74     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
75     RETURN   ${stdout}
76
77 Get Server Property
78     [Documentation]   Get a field (flavor, image, volumes_attached, etc.) from a server.
79     [Arguments]   ${server_id}   ${field}
80     Should Not Be Empty   ${server_id}
81     Should Not Be Empty   ${field}
82     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack server show ${server_id} -c ${field} -f value
83     Log   ${stdout}
84     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
85     RETURN   ${stdout}
86
87 Check VM In Server Group
88     [Documentation]   Check if a given server is included in an Openstack server group
89     [Arguments]   ${server_id}   ${server_group_name}
90     Should Not Be Empty   ${server_group_name}
91     Should Not Be Empty   ${server_id}
92     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack server group show ${server_group_name} -c members -f value
93     Log   ${stdout}
94     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
95     Should Contain   ${stdout}   ${server_id}
96
97 Set Volume Id
98     [Documentation]   Sets the root volume ID and ordinary volume ID by checking bootable property of volumes
99     [Arguments]   ${volume_match}   ${index}
100     Should Not Be Empty   ${volume_match}   ${index}
101     ${bootable}=   Check If Volume Bootable   ${volume_match}[${index}]
102     IF   '${bootable}'=='true'
103         Set Suite Variable   ${ROOT_VOL_ID}   ${volume_match}[${index}]
104         IF   not ${index}
105             Set Suite Variable   ${ORDINARY_VOL_ID}   ${volume_match}[1]
106         ELSE
107             Set Suite Variable   ${ORDINARY_VOL_ID}   ${volume_match}[0]
108         END
109     END
110
111 Get Persistent Volumes Attached To Vm
112     [Documentation]   Sets the root volume ID and ordinary volume ID by checking bootable property of volumes
113     [Arguments]   ${ns_id}
114     Variable Should Exist   ${ns_id}   msg=NS is not available
115     ${vnfs_list}=   Get Ns Vnf List   ${ns_id}
116     ${vim_id}=   Get VNF VIM ID   ${vnfs_list}[0]
117     ${volumes_attached}=   Get Server Property   ${vim_id}   volumes_attached
118     Log   ${volumes_attached}
119     ${stdout}=   Get Regexp Matches   ${volumes_attached}   '([0-9a-f\-]+)'   1
120     Log   ${stdout}
121     RETURN   ${stdout}
122
123 Check If Volume Exists
124     [Documentation]   Checks if a volume id exists
125     [Arguments]   ${volume_id}
126     Should Not Be Empty   ${volume_id}
127     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack volume list | grep ${volume_id} | wc -l
128     Log   ${stdout}
129     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
130     RETURN   ${stdout}
131
132 Delete Volume
133     [Documentation]   Delete a volume by its identifier
134     [Arguments]   ${volume_id}
135     Should Not Be Empty   ${volume_id}
136     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack volume delete ${volume_id}
137     Log   ${stdout}
138     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
139     RETURN   ${stdout}
140
141 Get Subnet
142     [Documentation]   Get subnet information in JSON format
143     [Arguments]   ${subnet_id}
144     Should Not Be Empty   ${subnet_id}
145     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack subnet show ${subnet_id} -f json
146     Log   ${stdout}
147     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
148     RETURN   ${stdout}
149
150 Stop Server
151     [Documentation]   Stop a server
152     [Arguments]   ${server_id}
153     Should Not Be Empty   ${server_id}
154     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack server stop ${server_id}
155     Log   ${stdout}
156     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
157     RETURN   ${stdout}
158
159 Halt Server
160     [Documentation]   Reset a server
161     [Arguments]   ${server_id}
162     Should Not Be Empty   ${server_id}
163     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack server set --state error ${server_id}
164     Log   ${stdout}
165     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
166     RETURN   ${stdout}
167
168 Delete Server
169     [Documentation]   Delete a server
170     [Arguments]   ${server_id}
171     Should Not Be Empty   ${server_id}
172     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack server delete ${server_id}
173     Log   ${stdout}
174     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
175     RETURN   ${stdout}
176
177 Create Flavor
178     [Documentation]   Create a Flavor
179     [Arguments]   ${flavor_name}   ${cpu}=1   ${ram}=1024   ${disk}=10
180     Should Not Be Empty   ${flavor_name}
181     ${rc}   ${flavor_id}=   Run And Return Rc And Output   openstack flavor create ${flavor_name} --ram ${ram} --disk ${disk} --vcpus ${cpu} -c id -f value
182     Log   ${flavor_id}
183     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
184     RETURN   ${flavor_id}
185
186 Delete Flavor
187     [Documentation]   Delete a Flavor
188     [Arguments]   ${flavor_id}
189     Should Not Be Empty   ${flavor_id}
190     ${rc}   ${stdout}=   Run And Return Rc And Output   openstack flavor delete ${flavor_id}
191     Log   ${stdout}
192     Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
193     RETURN   ${stdout}
194
195 Openstack Server List
196     [Documentation]   Get the openstack server list
197     [Arguments]   ${ns_name}
198     ${result}=   Run And Return Rc And Output   openstack server list | grep ${ns_name} | awk 'index($0, "ipv6=") {gsub(/,$/, ""); gsub("ipv6=", ""); print $8, $9}'
199     ${rc}=   Get From List   ${result}   0
200     ${Ip}=   Get From List   ${result}   1
201     Log   ${Ip}
202     Should Be Equal As Integers   ${rc}   ${success_return_code}
203     RETURN   ${Ip}
204
205 Source Openrc Command
206     [Documentation]   Keyword to source openstack tenant
207     Write   source openrc