06de53b9d4a899e2efc937fa582c6a786b553612
[integration/test.git] / csit / libraries / DevstackUtils.robot
1 *** Settings ***
2 Documentation     General Utils library. This library has broad scope, it can be used by any robot system tests.
3 Library           SSHLibrary
4 Library           String
5 Library           DateTime
6 Library           Process
7 Library           Collections
8 Library           RequestsLibrary
9 Library           ./UtilLibrary.py
10 Resource          KarafKeywords.robot
11 Resource          OpenStackOperations.robot
12 Resource          SSHKeywords.robot
13 Resource          ../variables/Variables.robot
14
15 *** Variables ***
16 ${default_devstack_prompt_timeout}    10s
17 ${DEVSTACK_SYSTEM_PASSWORD}    \    # set to empty, but provide for others to override if desired
18 ${blacklist_file}    /tmp/blacklist.txt
19 @{stable/newton_exclusion_regexes}    ${EMPTY}
20 @{stable/ocata_exclusion_regexes}    ${EMPTY}
21 @{stable/pike_exclusion_regexes}    ${EMPTY}
22 @{master_exclusion_regexes}    ${EMPTY}
23 ${tempest_dir}    /opt/stack/tempest
24 ${tempest_config_file}    ${tempest_dir}/etc/tempest.conf
25 ${external_physical_network}    physnet1
26 # Parameter values below are based on releng/builder - changing them requires updates in releng/builder as well
27 ${TEMPEST_TIMEOUT}    420s
28 ${OS_CNTL_CONN_ID}    None
29 ${OS_CMP1_CONN_ID}    None
30 ${OS_CMP2_CONN_ID}    None
31 ${OS_CNTL_IP}     ${EMPTY}
32 ${OS_CMP1_IP}     ${EMPTY}
33 ${OS_CMP2_IP}     ${EMPTY}
34 @{OS_ALL_IPS}     @{EMPTY}
35 @{OS_CMP_IPS}     @{EMPTY}
36
37 *** Keywords ***
38 Run Tempest Tests
39     [Arguments]    ${tempest_regex}    ${timeout}=${TEMPEST_TIMEOUT}    ${debug}=False
40     Run Keyword If    "${debug}"=="False"    Run Tempest Tests Without Debug    ${tempest_regex}    timeout=${timeout}
41     Run Keyword If    "${debug}"=="True"    Run Tempest Tests With Debug    ${tempest_regex}    timeout=${timeout}
42     Run Keyword If    "${debug}"!="True" and "${debug}"!="False"    Fail    debug argument must be True or False
43
44 Run Tempest Tests Without Debug
45     [Arguments]    ${tempest_regex}    ${tempest_directory}=${tempest_dir}    ${timeout}=${TEMPEST_TIMEOUT}
46     [Documentation]    Using ostestr will allow us to (by default) run tests in paralllel.
47     ...    Because of the parallel fashion, we must ensure there is no pause on teardown so that flag in tempest.conf is
48     ...    explicitly set to False.
49     Return From Keyword If    "skip_if_${OPENSTACK_BRANCH}" in @{TEST_TAGS}
50     Return From Keyword If    "skip_if_${SECURITY_GROUP_MODE}" in @{TEST_TAGS}
51     ${tempest_conn_id}=    SSHLibrary.Open Connection    ${OS_CONTROL_NODE_IP}    prompt=${DEFAULT_LINUX_PROMPT_STRICT}
52     SSHKeywords.Flexible SSH Login    ${OS_USER}    ${DEVSTACK_SYSTEM_PASSWORD}
53     Write Commands Until Prompt    source ${DEVSTACK_DEPLOY_PATH}/openrc admin admin
54     Write Commands Until Prompt    cd ${tempest_directory}
55     SSHLibrary.Read
56     Tempest Conf Modify Pause On Test Teardown    False
57     SSHLibrary.Set Client Configuration    timeout=${timeout}
58     # There are tons of deprecation error messages when we use ostestr in our CSIT environment (openstack via devstack)
59     # The robot log files are very large and one culprit is all these deprecation warnings. If we redirect stderr to
60     # /dev/null we should be able to ignore them. We will miss any other errors, however.
61     ${output}=    Write Commands Until Prompt    ostestr --regex ${tempest_regex} 2>/dev/null    timeout=${timeout}
62     Log    ${output}
63     SSHLibrary.Close Connection
64     Should Contain    ${output}    Failed: 0
65
66 Run Tempest Tests With Debug
67     [Arguments]    ${tempest_regex}    ${tempest_directory}=${tempest_dir}    ${timeout}=${TEMPEST_TIMEOUT}
68     [Documentation]    After setting pause_teardown=True in tempest.conf, use the python -m testtools.run module to execute
69     ...    a single tempest test case. We need to run only one tempest test case at a time as there will
70     ...    be potentional for an unkown number of debug pdb() prompts to catch and continue if we are running multiple
71     ...    test cases with a single command. Essentially, this keyword only handles one breakpoint at a single teardown.
72     Return From Keyword If    "skip_if_${OPENSTACK_BRANCH}" in @{TEST_TAGS}
73     Return From Keyword If    "skip_if_${SECURITY_GROUP_MODE}" in @{TEST_TAGS}
74     ${tempest_conn_id}=    SSHLibrary.Open Connection    ${OS_CONTROL_NODE_IP}    prompt=${DEFAULT_LINUX_PROMPT_STRICT}
75     SSHKeywords.Flexible SSH Login    ${OS_USER}    ${DEVSTACK_SYSTEM_PASSWORD}
76     Write Commands Until Prompt    source ${DEVSTACK_DEPLOY_PATH}/openrc admin admin
77     Write Commands Until Prompt    cd ${tempest_directory}
78     SSHLibrary.Read
79     Tempest Conf Modify Pause On Test Teardown    True
80     SSHLibrary.Set Client Configuration    timeout=${timeout}
81     SSHLibrary.Write    python -m testtools.run ${tempest_regex}
82     ${output}=    SSHLibrary.Read Until Regexp    ${DEFAULT_LINUX_PROMPT_STRICT}|pdb.set_trace()
83     Log    ${output}
84     Show Debugs
85     Get Test Teardown Debugs
86     SSHLibrary.Switch Connection    ${tempest_conn_id}
87     SSHLibrary.Write    continue
88     ${output}=    SSHLibrary.Read Until Regexp    ${DEFAULT_LINUX_PROMPT_STRICT}|pdb.set_trace()
89     Log    ${output}
90     SSHLibrary.Write    continue
91     ${output}=    SSHLibrary.Read Until Prompt
92     Log    ${output}
93     SSHLibrary.Close Connection
94     Should Contain    ${output}    OK
95     Should Not Contain    ${output}    FAILED
96
97 Suite Setup
98     OpenStackOperations.OpenStack Suite Setup
99     Log In To Tempest Executor And Setup Test Environment
100
101 Log In To Tempest Executor And Setup Test Environment
102     [Documentation]    Initialize SetupUtils, open SSH connection to a devstack system and source the openstack
103     ...    credentials needed to run the tempest tests. The (sometimes empty) tempest blacklist file will be created
104     ...    and pushed to the tempest executor.
105     Create Blacklist File
106     # Tempest tests need an existing external network in order to create routers.
107     Create Network    ${EXTERNAL_NET_NAME}    --external --default --provider-network-type flat --provider-physical-network ${PUBLIC_PHYSICAL_NETWORK}
108     Create Subnet    ${EXTERNAL_NET_NAME}    ${EXTERNAL_SUBNET_NAME}    ${EXTERNAL_SUBNET}    --gateway ${EXTERNAL_GATEWAY} --allocation-pool ${EXTERNAL_SUBNET_ALLOCATION_POOL}
109     List Networks
110     ${control_node_conn_id}=    SSHLibrary.Open Connection    ${OS_CONTROL_NODE_IP}    prompt=${DEFAULT_LINUX_PROMPT_STRICT}
111     SSHKeywords.Flexible SSH Login    ${OS_USER}
112     Write Commands Until Prompt    source ${DEVSTACK_DEPLOY_PATH}/openrc admin admin
113     Write Commands Until Prompt    sudo rm -rf /opt/stack/tempest/.testrepository
114     ${net_id}=    Get Net Id    ${EXTERNAL_NET_NAME}
115     Tempest Conf Add External Network And Floating Network Name    ${net_id}
116
117 Tempest Conf Add External Network And Floating Network Name
118     [Arguments]    ${external_network_id}
119     [Documentation]    Tempest will be run with a config file - this function will add the
120     ...    given external network ID to the configuration file.
121     Modify Config In File On Existing SSH Connection    ${tempest_config_file}    set    network    public_network_id    ${external_network_id}
122     Modify Config In File On Existing SSH Connection    ${tempest_config_file}    set    DEFAULT    debug    False
123     Modify Config In File On Existing SSH Connection    ${tempest_config_file}    set    DEFAULT    log_level    INFO
124     Modify Config In File On Existing SSH Connection    ${tempest_config_file}    set    network    floating_network_name    ${EXTERNAL_NET_NAME}
125     Write Commands Until Prompt    sudo cat ${tempest_config_file}
126     Write Commands Until Prompt    sudo chmod 777 ${tempest_config_file}
127
128 Tempest Conf Modify Pause On Test Teardown
129     [Arguments]    ${pause_flag}
130     [Documentation]    Sets the DEFAULT section flag for pausing the test teardown. If True the tempest test case
131     ...    being executed will break to a pdb() debug shell when it hits it's teardown() function.
132     Modify Config In File On Existing SSH Connection    ${tempest_config_file}    set    DEFAULT    pause_teardown    ${pause_flag}
133
134 Modify Config In File On Existing SSH Connection
135     [Arguments]    ${config_file}    ${modifier}    ${config_section}    ${config_key}    ${config_value}=${EMPTY}
136     [Documentation]    uses crudini to populate oslo cofg file.
137     # this keyword is only one line so seems like extra overhead, but this may be a good candidate to move
138     # to a library at some point, when/if other suites need to use it, so wanted to make it generic.
139     Write Commands Until Prompt    sudo -E crudini --${modifier} ${config_file} ${config_section} ${config_key} ${config_value}
140
141 Create Blacklist File
142     [Documentation]    For each exclusion regex in the required @{${OPENSTACK_BRANCH}_exclusion_regexes} list a new
143     ...    line will be created in the required ${blacklist_file} location. This file is pushed to the OS_CONTROL_NODE
144     ...    which is assumed to be the tempest executor.
145     OperatingSystem.Create File    ${blacklist_file}
146     : FOR    ${exclusion}    IN    @{${OPENSTACK_BRANCH}_exclusion_regexes}
147     \    OperatingSystem.Append To File    ${blacklist_file}    ${exclusion}\n
148     Log File    ${blacklist_file}
149     SSHKeywords.Copy File To Remote System    ${OS_CONTROL_NODE_IP}    ${blacklist_file}    ${blacklist_file}
150
151 Open Connection
152     [Arguments]    ${name}    ${ip}
153     ${conn_id} =    SSHLibrary.Open Connection    ${ip}    prompt=${DEFAULT_LINUX_PROMPT}
154     SSHKeywords.Flexible SSH Login    ${OS_USER}    ${DEVSTACK_SYSTEM_PASSWORD}
155     BuiltIn.Set Suite Variable    \${${name}}    ${conn_id}
156     [Return]    ${conn_id}
157
158 Devstack Suite Setup
159     [Arguments]    ${odl_ip}=${ODL_SYSTEM_IP}
160     [Documentation]    Open connections to the nodes
161     Get DevStack Nodes Data
162     Create Session    session    http://${odl_ip}:${RESTCONFPORT}    auth=${AUTH}    headers=${HEADERS}
163     SSHLibrary.Set Default Configuration    timeout=${default_devstack_prompt_timeout}
164     Run Keyword If    0 < ${NUM_OS_SYSTEM}    Open Connection    OS_CNTL_CONN_ID    ${OS_CONTROL_NODE_IP}
165     Run Keyword If    1 < ${NUM_OS_SYSTEM}    Open Connection    OS_CMP1_CONN_ID    ${OS_COMPUTE_1_IP}
166     Run Keyword If    2 < ${NUM_OS_SYSTEM}    Open Connection    OS_CMP2_CONN_ID    ${OS_COMPUTE_2_IP}
167
168 Write Commands Until Prompt
169     [Arguments]    ${cmd}    ${timeout}=${default_devstack_prompt_timeout}
170     [Documentation]    quick wrapper for Write and Read Until Prompt Keywords to make test cases more readable
171     SSHLibrary.Set Client Configuration    timeout=${timeout}
172     SSHLibrary.Read
173     SSHLibrary.Write    ${cmd};echo Command Returns $?
174     ${output}=    SSHLibrary.Read Until Prompt
175     [Return]    ${output}
176
177 Write Commands Until Prompt And Log
178     [Arguments]    ${cmd}    ${timeout}=${default_devstack_prompt_timeout}
179     [Documentation]    quick wrapper for Write and Read Until Prompt Keywords to make test cases more readable
180     ${output} =    Write Commands Until Prompt    ${cmd}    ${timeout}
181     Log    ${output}
182     [Return]    ${output}
183
184 Log Devstack Nodes Data
185     ${output} =    BuiltIn.Catenate    SEPARATOR=\n    OS_CNTL_HOSTNAME: ${OS_CNTL_HOSTNAME} - OS_CNTL_IP: ${OS_CNTL_IP} - OS_CONTROL_NODE_IP: ${OS_CONTROL_NODE_IP}    OS_CMP1_HOSTNAME: ${OS_CMP1_HOSTNAME} - OS_CMP1_IP: ${OS_CMP1_IP} - OS_COMPUTE_1_IP: ${OS_COMPUTE_1_IP}    OS_CMP2_HOSTNAME: ${OS_CMP2_HOSTNAME} - OS_CMP2_IP: ${OS_CMP2_IP} - OS_COMPUTE_2_IP: ${OS_COMPUTE_2_IP}    OS_ALL_IPS: @{OS_ALL_IPS}
186     ...    OS_CMP_IPS: @{OS_CMP_IPS}
187     BuiltIn.Log    DevStack Nodes Data:\n${output}
188
189 Get DevStack Hostnames
190     [Documentation]    Assign hostname global variables for DevStack nodes
191     ${OS_CNTL_HOSTNAME} =    OpenStackOperations.Get Hypervisor Hostname From IP    ${OS_CNTL_IP}
192     ${OS_CMP1_HOSTNAME} =    OpenStackOperations.Get Hypervisor Hostname From IP    ${OS_CMP1_IP}
193     ${OS_CMP2_HOSTNAME} =    OpenStackOperations.Get Hypervisor Hostname From IP    ${OS_CMP2_IP}
194     BuiltIn.Set Suite Variable    ${OS_CNTL_HOSTNAME}
195     BuiltIn.Set Suite Variable    ${OS_CMP1_HOSTNAME}
196     BuiltIn.Set Suite Variable    ${OS_CMP2_HOSTNAME}
197
198 Set Node Data For AllinOne Setup
199     [Documentation]    Assign global variables for DevStack nodes where the Control Node enables Compute service also.
200     BuiltIn.Set Suite Variable    @{OS_ALL_IPS}    ${OS_CNTL_IP}
201
202 Set Node Data For Control And Compute Node Setup
203     [Documentation]    Assign global variables for DevStack nodes where the control node is also the compute
204     BuiltIn.Set Suite Variable    ${OS_CMP1_IP}    ${OS_CNTL_IP}
205     BuiltIn.Set Suite Variable    ${OS_CMP2_IP}    ${OS_COMPUTE_1_IP}
206     BuiltIn.Set Suite Variable    @{OS_ALL_IPS}    ${OS_CNTL_IP}    ${OS_CMP2_IP}
207     BuiltIn.Set Suite Variable    @{OS_CMP_IPS}    ${OS_CMP1_IP}    ${OS_CMP2_IP}
208
209 Set Node Data For Control Only Node Setup
210     [Documentation]    Assign global variables for DevStack nodes where the control node is different than the compute
211     BuiltIn.Set Suite Variable    ${OS_CMP1_IP}    ${OS_COMPUTE_1_IP}
212     BuiltIn.Set Suite Variable    ${OS_CMP2_IP}    ${OS_COMPUTE_2_IP}
213     BuiltIn.Set Suite Variable    @{OS_ALL_IPS}    ${OS_CNTL_IP}    ${OS_CMP1_IP}    ${OS_CMP2_IP}
214     BuiltIn.Set Suite Variable    @{OS_CMP_IPS}    ${OS_CMP1_IP}    ${OS_CMP2_IP}
215
216 Get DevStack Nodes Data
217     [Documentation]    Assign global variables for DevStack nodes
218     BuiltIn.Set Suite Variable    ${OS_CNTL_IP}    ${OS_CONTROL_NODE_IP}
219     Run Keyword If    ${NUM_OS_SYSTEM} == 1    Set Node Data For AllinOne Setup
220     ...    ELSE IF    ${NUM_OS_SYSTEM} == 2    Set Node Data For Control And Compute Node Setup
221     ...    ELSE IF    ${NUM_OS_SYSTEM} == 3    Set Node Data For Control Only Node Setup
222     Get DevStack Hostnames
223     Log Devstack Nodes Data