Add test cases for new API to netconf/call-home suite
[integration/test.git] / csit / libraries / NetconfCallHome.robot
1 *** Settings ***
2 Library           SSHLibrary
3 Library           RequestsLibrary
4 Resource          SSHKeywords.robot
5 Resource          ../variables/Variables.robot
6
7 *** Variables ***
8 ${mount_point_url}    /restconf/operational/network-topology:network-topology/topology/topology-netconf/
9 ${device_status}    /restconf/operational/odl-netconf-callhome-server:netconf-callhome-server
10 ${whitelist}      /restconf/config/odl-netconf-callhome-server:netconf-callhome-server/allowed-devices
11 ${global_config_url}    /restconf/config/odl-netconf-callhome-server:netconf-callhome-server/global/credentials
12 ${substring1}     "netconf-node-topology:connection-status":"connected"
13 ${substring2}     "node-id":"netopeer2"
14 ${substring3}     "netconf-node-topology:available-capabilities"
15
16 *** Keywords ***
17 Check Device status
18     [Arguments]    ${status}    ${id}=netopeer2
19     [Documentation]    Checks the operational device status.
20     @{expectedValues}    Create List    "unique-id":"${id}"    "callhome-status:device-status":"${status}"
21     Run Keyword If    '${status}'=='FAILED_NOT_ALLOWED' or '${status}'=='FAILED_AUTH_FAILURE'    Remove Values From List    ${expectedValues}    "unique-id":"${id}"
22     Utils.Check For Elements At URI    ${device_status}    ${expectedValues}
23
24 Apply SSH-based Call-Home configuration
25     [Documentation]    Upload netopeer2 configuration files needed for SSH transport
26     SSHLibrary.Put File    ${CURDIR}/../variables/netconf/callhome/configuration-files/ssh/ietf-netconf-server.xml
27     ...    configuration-files/ietf-netconf-server.xml
28     SSHLibrary.Put File    ${CURDIR}/../variables/netconf/callhome/configuration-files/ssh/ietf-keystore.xml
29     ...    configuration-files/ietf-keystore.xml
30
31 Register global credentials for SSH call-home devices (APIv1)
32     [Arguments]    ${username}    ${password}
33     [Documentation]    Set global credentials for SSH call-home devices
34     ${template}    OperatingSystem.Get File    ${CREATE_GLOBAL_CREDENTIALS_REQ}
35     ${body}    Replace String    ${template}    {username}    ${username}
36     ${body}    Replace String    ${body}    {password}    ${password}
37     ${resp}    RequestsLibrary.Put Request    session    ${global_config_url}    data=${body}    headers=${HEADERS}
38     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
39
40 Register SSH call-home device in ODL controller (APIv1)
41     [Arguments]    ${device_name}    ${hostkey}    ${username}=${EMPTY}    ${password}=${EMPTY}
42     [Documentation]    Registration call-home device with SSH transport
43     Run Keyword If    '${username}' == '${EMPTY}' or '${password}' == '${EMPTY}'    Get create device request without credentials template (APIv1)
44     ...    ELSE    Get create device request template (APIv1)
45     ${body}    Replace String    ${template}    {device_name}    ${device_name}
46     ${body}    Replace String    ${body}    {username}    ${username}
47     ${body}    Replace String    ${body}    {password}    ${password}
48     ${body}    Replace String    ${body}    {hostkey}    ${hostkey}
49     ${resp}    RequestsLibrary.Post Request    session    ${whitelist}    data=${body}    headers=${HEADERS}
50     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
51
52 Get create device request template (APIv1)
53     ${template}    OperatingSystem.Get File    ${CREATE_SSH_DEVICE_REQ_V1}
54     Set Test Variable    ${template}
55
56 Get create device request without credentials template (APIv1)
57     ${template}    OperatingSystem.Get File    ${CREATE_SSH_DEVICE_REQ_V1_HOST_KEY_ONLY}
58     Set Test Variable    ${template}
59
60 Register SSH call-home device in ODL controller (APIv2)
61     [Arguments]    ${device_name}    ${hostkey}    ${username}=${EMPTY}    ${password}=${EMPTY}
62     [Documentation]    Registration call-home device with SSH transport using latest models
63     Run Keyword If    '${username}' == '${EMPTY}' or '${password}' == '${EMPTY}'    Get create device request without credentials template (APIv2)
64     ...    ELSE    Get create device request template (APIv2)
65     ${body}    Replace String    ${template}    {device_name}    ${device_name}
66     ${body}    Replace String    ${body}    {username}    ${username}
67     ${body}    Replace String    ${body}    {password}    ${password}
68     ${body}    Replace String    ${body}    {hostkey}    ${hostkey}
69     ${resp}    RequestsLibrary.Post Request    session    ${whitelist}    data=${body}    headers=${HEADERS}
70     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
71
72 Get create device request template (APIv2)
73     ${template}    OperatingSystem.Get File    ${CREATE_SSH_DEVICE_REQ_V2}
74     Set Test Variable    ${template}
75
76 Get create device request without credentials template (APIv2)
77     ${template}    OperatingSystem.Get File    ${CREATE_SSH_DEVICE_REQ_V2_HOST_KEY_ONLY}
78     Set Test Variable    ${template}
79
80 Pull Netopeer2 Docker Image
81     [Documentation]    Pulls the netopeer image from the docker repository.
82     ${stdout}    ${stderr}    ${rc}=    SSHLibrary.Execute Command    docker pull sysrepo/sysrepo-netopeer2:latest    return_stdout=True    return_stderr=True
83     ...    return_rc=True
84     ${stdout}    ${stderr}    ${rc}=    SSHLibrary.Execute Command    docker images    return_stdout=True    return_stderr=True
85     ...    return_rc=True
86
87 Install Docker Compose on tools system
88     [Documentation]    Install docker-compose on tools system.
89     ${netopeer_conn_id} =    SSHKeywords.Open_Connection_To_Tools_System
90     Builtin.Set Suite Variable    ${netopeer_conn_id}
91     SSHLibrary.Write    sudo curl -L "https://github.com/docker/compose/releases/download/1.11.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
92     ${output}=    Wait Until Keyword Succeeds    30s    2s    SSHLibrary.Read_Until_Prompt
93     ${stdout}    ${stderr}    ${rc}=    SSHLibrary.Execute Command    sudo chmod +x /usr/local/bin/docker-compose    return_stdout=True    return_stderr=True
94     ...    return_rc=True
95
96 Uninstall Docker Compose on tools system
97     [Documentation]    Uninstall docker-compose on tools system
98     ${stdout}    ${stderr}    ${rc}=    SSHLibrary.Execute Command    pip uninstall docker-compose    return_stdout=True    return_stderr=True
99     ...    return_rc=True
100
101 Test Setup
102     [Documentation]    Opens session towards ODL controller, set configuration folder, generates a new host key for the container
103     RequestsLibrary.Create_Session    session    http://${ODL_SYSTEM_IP}:${RESTCONFPORT}    auth=${AUTH}
104     SSHLibrary.Execute_Command    rm -rf ./configuration-files && mkdir configuration-files
105     SSHLibrary.Execute_Command    ssh-keygen -q -t rsa -b 2048 -N '' -f ./configuration-files/ssh_host_rsa_key
106     ${public_key}    SSHLibrary.Execute_Command    cat configuration-files/ssh_host_rsa_key.pub | awk '{print $2}'
107     Set Test Variable    ${NETOPEER_PUB_KEY}    ${public_key}
108
109 Test Teardown
110     [Documentation]    Tears down the docker running netopeer and deletes entry from the whitelist.
111     ${stdout}    ${stderr}    ${rc}=    SSHLibrary.Execute Command    docker-compose logs    return_stdout=True    return_stderr=True
112     ...    return_rc=True
113     Log    ${stdout}
114     ${stdout}    ${stderr}    ${rc}=    SSHLibrary.Execute Command    docker-compose down    return_stdout=True    return_stderr=True
115     ...    return_rc=True
116     ${stdout}    ${stderr}    ${rc}=    SSHLibrary.Execute Command    docker ps -a    return_stdout=True    return_stderr=True
117     ...    return_rc=True
118     SSHLibrary.Execute_Command    rm -rf ./configuration-files
119     ${resp} =    RequestsLibrary.Delete_Request    session    ${whitelist}
120
121 Suite Setup
122     [Documentation]    Get the suite ready for callhome test cases.
123     Install Docker Compose on tools system
124     Pull Netopeer2 Docker Image
125     SSHLibrary.Put File    ${CURDIR}/../variables/netconf/callhome/docker-compose.yaml    .
126     SSHLibrary.Put File    ${CURDIR}/../variables/netconf/callhome/init_configuration.sh    .
127     SSHLibrary.Execute_Command    sed -i -e 's/ODL_SYSTEM_IP/${ODL_SYSTEM_IP}/g' docker-compose.yaml
128     ${netconf_mount_expected_values}    Create list    ${substring1}    ${substring2}    ${substring3}
129     Set Suite Variable    ${netconf_mount_expected_values}
130     Set Suite Variable    ${CREATE_SSH_DEVICE_REQ_V1}    ${CURDIR}/../variables/netconf/callhome/json/apiv1/create_device.json
131     Set Suite Variable    ${CREATE_SSH_DEVICE_REQ_V1_HOST_KEY_ONLY}    ${CURDIR}/../variables/netconf/callhome/json/apiv1/create_device_hostkey_only.json
132     Set Suite Variable    ${CREATE_GLOBAL_CREDENTIALS_REQ}    ${CURDIR}/../variables/netconf/callhome/json/apiv1/create_global_credentials.json
133     Set Suite Variable    ${CREATE_SSH_DEVICE_REQ_V2}    ${CURDIR}/../variables/netconf/callhome/json/apiv2/create_ssh_device.json
134     Set Suite Variable    ${CREATE_SSH_DEVICE_REQ_V2_HOST_KEY_ONLY}    ${CURDIR}/../variables/netconf/callhome/json/apiv2/create_device_hostkey_only.json
135
136 Suite Teardown
137     [Documentation]    Tearing down the setup.
138     Uninstall Docker Compose on tools system
139     RequestsLibrary.Delete_All_Sessions
140     SSHLibrary.Close_All_Connections