xref: /openbmc/openbmc-test-automation/lib/connection_client.robot (revision b5c119e5bd886b889a4f0f2d84d2ca2cc549e9a8)
14d6c1dabSGeorge Keishing*** Settings ***
24d6c1dabSGeorge KeishingDocumentation     This module is for SSH connection override to QEMU
34d6c1dabSGeorge Keishing...               based openbmc systems.
44d6c1dabSGeorge Keishing
5ffcf02a1SGeorge KeishingLibrary           SSHLibrary   timeout=30 seconds
64d6c1dabSGeorge KeishingLibrary           OperatingSystem
7442f0ef7SrootLibrary           Collections
84d6c1dabSGeorge Keishing
94d6c1dabSGeorge Keishing*** Variables ***
104d6c1dabSGeorge Keishing
114d6c1dabSGeorge Keishing*** Keywords ***
124d6c1dabSGeorge KeishingOpen Connection And Log In
13442f0ef7Sroot    [Documentation]  Opens a connection with the given arguments, and logs in.
14442f0ef7Sroot    ...  Defaults to logging into the BMC.
15442f0ef7Sroot    [Arguments]  ${username}=${OPENBMC_USERNAME}
16442f0ef7Sroot    ...          ${password}=${OPENBMC_PASSWORD}  &{connection_args}
17442f0ef7Sroot
18442f0ef7Sroot    # username          The username to log into the connection with.
19442f0ef7Sroot    # password          The password to log into the connection with.
20442f0ef7Sroot    # connection_args   A dictionary of acceptable inputs to the Open Connection
21442f0ef7Sroot    #                   keyword. This includes, but is not limited to, the
22442f0ef7Sroot    #                   following:
23442f0ef7Sroot    #                   host, alias, port, timeout, newline, prompt, term_type,
2416b3c7bfSGeorge Keishing    #                   width, height, path_separator, encoding
25442f0ef7Sroot    #                   (For more information, please visit the SSHLibrary doc)
26442f0ef7Sroot
27442f0ef7Sroot    #                   Of the above arguments to Open Connection, this keyword
28442f0ef7Sroot    #                   will provide the following default values:
29442f0ef7Sroot    #                   host             ${OPENBMC_HOST}
30442f0ef7Sroot
31442f0ef7Sroot    # If no host was provided, add ${OPENBMC_HOST} to the dictionary
32442f0ef7Sroot    ${has_host}=  Run Keyword and Return Status
33442f0ef7Sroot    ...           Dictionary Should Contain Key  ${connection_args}  host
34442f0ef7Sroot    Run Keyword If  ${has_host} == ${FALSE}
35442f0ef7Sroot    ...             Set To Dictionary  ${connection_args}  host=${OPENBMC_HOST}
36442f0ef7Sroot
37442f0ef7Sroot    Run Keyword If
38442f0ef7Sroot    ...   '${SSH_PORT}' != '${EMPTY}' and '${HTTPS_PORT}' != '${EMPTY}'
394d6c1dabSGeorge Keishing    ...   User input SSH and HTTPs Ports
404d6c1dabSGeorge Keishing
41442f0ef7Sroot    # Check to see if a port to connect to was provided.
42442f0ef7Sroot    ${has_port}=  Run Keyword and Return Status
43442f0ef7Sroot    ...           Dictionary Should Contain Key  ${connection_args}  port
444d6c1dabSGeorge Keishing
45442f0ef7Sroot    # If the ${SSH_PORT} is set and no port was provided, add the defined port
46442f0ef7Sroot    # to the dictionary and open the connection. Otherwise, open the connection
47442f0ef7Sroot    # with the either the provided port or the default port.
48442f0ef7Sroot    Run Keyword If  '${SSH_PORT}' != '${EMPTY}' and ${has_port} == ${FALSE}
49442f0ef7Sroot    ...            Run Keywords
50442f0ef7Sroot    ...            Set To Dictionary  ${connection_args}  port=${SSH_PORT}  AND
51314cf85aSGeorge Keishing    ...            SSHLibrary.Open connection  &{connection_args}
52314cf85aSGeorge Keishing    ...   ELSE  Run Keyword   SSHLibrary.Open connection  &{connection_args}
53442f0ef7Sroot
54939ac389SGeorge Keishing    SSHLibrary.Login  ${username}  ${password}
554d6c1dabSGeorge Keishing
564346a413SGeorge KeishingOpen Connection for SCP
57f4a807bfSJoy Onyerikwu    [Documentation]  Open a connection for SCP.
58*b5c119e5SGeorge Keishing    Import Library      SCPLibrary      AS       scp
594346a413SGeorge Keishing    Run Keyword If  '${SSH_PORT}' == '${EMPTY}'  scp.Open connection  ${OPENBMC_HOST}
604346a413SGeorge Keishing    ...  username=${OPENBMC_USERNAME}  password=${OPENBMC_PASSWORD}
614346a413SGeorge Keishing    ...  ELSE   Run Keyword    scp.Open connection  ${OPENBMC_HOST}  port=${SSH_PORT}
624346a413SGeorge Keishing    ...  username=${OPENBMC_USERNAME}  password=${OPENBMC_PASSWORD}
634346a413SGeorge Keishing
644346a413SGeorge Keishing
654d6c1dabSGeorge KeishingUser input SSH and HTTPs Ports
664d6c1dabSGeorge Keishing    [Documentation]   Update the global SSH and HTTPs port variable for QEMU
674d6c1dabSGeorge Keishing    ${port_num}=    Convert To Integer    ${SSH_PORT}
684d6c1dabSGeorge Keishing    ${SSH_PORT}=    Replace Variables     ${port_num}
694d6c1dabSGeorge Keishing
704d6c1dabSGeorge Keishing    ${https_num}=   Convert To Integer    ${HTTPS_PORT}
7189a86922SSridevi Ramesh    Set Global Variable     ${AUTH_URI}    https://${OPENBMC_HOST}:${https_num}
72c9c9cdeaSLeah McNutt
73c9c9cdeaSLeah McNuttValidate Or Open Connection
74c9c9cdeaSLeah McNutt    [Documentation]  Checks for an open connection to a host or alias.
75c9c9cdeaSLeah McNutt    [Arguments]  ${alias}=None  ${host}=${EMPTY}  &{connection_args}
76c9c9cdeaSLeah McNutt
77c9c9cdeaSLeah McNutt    # alias            The alias of the connection to validate.
78c9c9cdeaSLeah McNutt    # host             The DNS name or IP of the host to validate.
7928e403b8SGunnar Mills    # connection_args  A dictionary of arguments to pass to Open Connection
80c9c9cdeaSLeah McNutt    #                  and Log In (see above) if the connection is not open. May
81c9c9cdeaSLeah McNutt    #                  contain, but does not need to contain, the host or alias.
82c9c9cdeaSLeah McNutt
83c9c9cdeaSLeah McNutt    # Check to make sure we have an alias or host to search for.
84c9c9cdeaSLeah McNutt    Run Keyword If  '${host}' == '${EMPTY}'  Should Not Be Equal  ${alias}  None
85c9c9cdeaSLeah McNutt    ...  msg=Need to provide a host or an alias.  values=False
86c9c9cdeaSLeah McNutt
87c9c9cdeaSLeah McNutt    # Search the dictionary to see if it includes the host and alias.
88c9c9cdeaSLeah McNutt    ${host_exists}=  Run Keyword and Return Status
89c9c9cdeaSLeah McNutt    ...              Dictionary Should Contain Key  ${connection_args}  host
90c9c9cdeaSLeah McNutt    ${alias_exists}=  Run Keyword and Return Status
91c9c9cdeaSLeah McNutt    ...               Dictionary Should Contain Key  ${connection_args}  alias
92c9c9cdeaSLeah McNutt
93c9c9cdeaSLeah McNutt    # Add the alias and host back into the dictionary of connection arguments,
94c9c9cdeaSLeah McNutt    # if needed.
95c9c9cdeaSLeah McNutt    Run Keyword If  '${host}' != '${EMPTY}' and ${host_exists} == ${FALSE}
96c9c9cdeaSLeah McNutt    ...             Set to Dictionary  ${connection_args}  host  ${host}
97c9c9cdeaSLeah McNutt    Run Keyword If  '${alias}' != 'None' and ${alias_exists} == ${FALSE}
98c9c9cdeaSLeah McNutt    ...             Set to Dictionary  ${connection_args}  alias  ${alias}
99c9c9cdeaSLeah McNutt
100c9c9cdeaSLeah McNutt    @{open_connections}=  Get Connections
101c9c9cdeaSLeah McNutt    # If there are no open connections, open one and return.
102c9c9cdeaSLeah McNutt    Run Keyword If  '${open_connections}' == '[]'
103c9c9cdeaSLeah McNutt    ...             Open Connection and Log In  &{connection_args}
104c9c9cdeaSLeah McNutt    Return From Keyword If  '${open_connections}' == '[]'
105c9c9cdeaSLeah McNutt
106c9c9cdeaSLeah McNutt    # Connect to the alias or host that matches. If both are given, only connect
107c9c9cdeaSLeah McNutt    # to a connection that has both.
10820ccfc71SMarissa Garza    FOR  ${connection}  IN  @{open_connections}
10920ccfc71SMarissa Garza        Log  ${connection}
11020ccfc71SMarissa Garza        ${alias_match}=  Evaluate  '${alias}' == '${connection.alias}'
11120ccfc71SMarissa Garza        ${host_match}=  Evaluate  '${host}' == '${connection.host}'
11220ccfc71SMarissa Garza        ${given_alias}=  Evaluate  '${alias}' != 'None'
11320ccfc71SMarissa Garza        ${no_alias}=  Evaluate  '${alias}' == 'None'
11420ccfc71SMarissa Garza        ${given_host}=  Evaluate  '${host}' != '${EMPTY}'
11520ccfc71SMarissa Garza        ${no_host}=  Evaluate  '${host}' == '${EMPTY}'
11620ccfc71SMarissa Garza        Run Keyword If
117c9c9cdeaSLeah McNutt        ...    ${given_alias} and ${given_host} and ${alias_match} and ${host_match}
118c9c9cdeaSLeah McNutt        ...    Run Keywords
119c9c9cdeaSLeah McNutt        ...      Switch Connection  ${alias}  AND
120c9c9cdeaSLeah McNutt        ...      Log to Console  Found connection. Switched to ${alias} ${host}  AND
121c9c9cdeaSLeah McNutt        ...      Return From Keyword If  ${alias_match} and ${host_match}
122c9c9cdeaSLeah McNutt        ...    ELSE  Run Keyword If
123c9c9cdeaSLeah McNutt        ...      ${given_alias} and ${no_host} and ${alias_match}
124c9c9cdeaSLeah McNutt        ...      Run Keywords
125c9c9cdeaSLeah McNutt        ...        Switch Connection  ${alias}  AND
126c9c9cdeaSLeah McNutt        ...        Log to Console  Found connection. Switched to: ${alias}  AND
127c9c9cdeaSLeah McNutt        ...        Return From Keyword If  ${alias_match}
128c9c9cdeaSLeah McNutt        ...    ELSE  Run Keyword If
129c9c9cdeaSLeah McNutt        ...       ${given_host} and ${no_alias} and ${host_match}
130c9c9cdeaSLeah McNutt        ...       Run Keywords
131c9c9cdeaSLeah McNutt        ...         Switch Connection  ${connection.index}  AND
132c9c9cdeaSLeah McNutt        ...         Log to Console  Found Connection. Switched to: ${host}  AND
133c9c9cdeaSLeah McNutt        ...         Return From Keyword If  ${host_match}
13420ccfc71SMarissa Garza    END
135c9c9cdeaSLeah McNutt    # If no connections are found, open a connection with the provided args.
136c9c9cdeaSLeah McNutt    Log  No connection with provided arguments.  Opening a connection.
137c9c9cdeaSLeah McNutt    Open Connection and Log In  &{connection_args}
1381a52c995SVijay
1391a52c995SVijay
1401a52c995SVijayClear System Entry From Knownhosts
1411a52c995SVijay    [Documentation]   Delete OPENBMC_HOST entry from known_hosts file.
1421a52c995SVijay    ${cmd}=  Set Variable  sed '/${OPENBMC_HOST}/d' -i ~/.ssh/known_hosts
1431a52c995SVijay    ${rc}  ${output}=  Run and Return RC and Output  ${cmd}
1441a52c995SVijay
145