2dafb53d85f5ce895da6be8c33be2f070267dc45
[integration/test.git] / csit / libraries / SFC / SfcKeywords.robot
1 *** Settings ***
2 Documentation     Common Keywords for the SFC Test suites.
3 Library           Collections
4 Resource          ../../../libraries/Utils.robot
5
6 *** Variables ***
7
8 *** Keywords ***
9 Post Elements To URI As JSON
10     [Arguments]    ${uri}    ${data}
11     ${resp} =    RequestsLibrary.Post Request    session    ${uri}    data=${data}    headers=${headers}
12     BuiltIn.Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
13
14 Get JSON Elements From URI
15     [Arguments]    ${uri}
16     ${resp} =    RequestsLibrary.Get Request    session    ${uri}
17     [Return]    ${resp.json()}
18
19 Check Classifier Flows
20     ${flowList} =    DockerSfc.Get Flows In Docker Containers
21     BuiltIn.log    ${flowList}
22     BuiltIn.Should Contain Match    ${flowList}    *actions=pop_nsh*
23     BuiltIn.Should Contain Match    ${flowList}    *actions=push_nsh*
24
25 Check Service Function Types Added
26     [Arguments]    ${elements}
27     [Documentation]    Check that the service function types are updated with the service function names
28     Utils.Check For Elements At URI    ${SERVICE_FUNCTION_TYPES_URI}    ${elements}
29
30 Check Service Function Types Removed
31     [Arguments]    ${elements}
32     [Documentation]    Check that the service function names are removed from the service function types
33     Utils.Check For Elements Not At URI    ${SERVICE_FUNCTION_TYPES_URI}    ${elements}
34
35 Check Empty Service Function Paths State
36     [Documentation]    Check that the service function paths state is empty after deleting SFPs
37     Utils.Check For Specific Number Of Elements At URI    ${SERVICE_FUNCTION_PATHS_STATE_URI}    service-function-path-state    0
38
39 Check Rendered Service Path Created
40     [Arguments]    ${sfp_name}
41     [Documentation]    Check that the Rendered Service Path is created
42     ${sfp_name_elements_list} =    BuiltIn.Create List    ${sfp_name}
43     Utils.Check For Elements At URI    ${SERVICE_FUNCTION_PATHS_STATE_URI}    ${sfp_name_elements_list}
44
45 Check Rendered Service Path Deleted
46     [Arguments]    ${sfp_name}
47     [Documentation]    Check that the Rendered Service Path is deleted
48     ${sfp_name_elements_list} =    BuiltIn.Create List    ${sfp_name}
49     Utils.Check For Elements Not At URI    ${SERVICE_FUNCTION_PATHS_STATE_URI}    ${sfp_name_elements_list}
50
51 Get Rendered Service Path Name
52     [Arguments]    ${sfp_name}    ${get_reverse}=False
53     [Documentation]    Given an SFP name, do a get on ${SERVICE_FUNCTION_PATH_STATE_URI} to get the RSP name
54     ${resp} =    RequestsLibrary.Get Request    session    ${SERVICE_FUNCTION_PATH_STATE_URI}${sfp_name}
55     BuiltIn.Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
56     # should be like this: {"service-function-path-state":[{"name":"SFC1-100","sfp-rendered-service-path":[{"name":"SFC1-100-Path-183"}]}]}
57     ${sfp_rendered_service_path_list} =    Collections.Get_From_Dictionary    ${resp.json()}    service-function-path-state
58     ${sfp_rendered_service_path_dict} =    Collections.Get_From_List    ${sfp_rendered_service_path_list}    0
59     ${sfp_state_list} =    Collections.Get_From_Dictionary    ${sfp_rendered_service_path_dict}    sfp-rendered-service-path
60     ${list_length} =    BuiltIn.Get Length    ${sfp_state_list}
61     # The "sfp-rendered-service-path" will only have 1 or 2 entries, depending on chain symmetry config.
62     # The RSP name will be "<SfpName>-Path-<PathId>" and the optional symmetric name will be <SfpName>-Path-<PathId>-Reverse"
63     ${value} =    BuiltIn.Set Variable    None
64     : FOR    ${i}    IN RANGE    ${list_length}
65     \    ${rsp_name_dict} =    Collections.Get From List    ${sfp_state_list}    ${i}
66     \    ${name} =    Collections.Get_From_Dictionary    ${rsp_name_dict}    name
67     \    @{matches} =    String.Get Regexp Matches    ${name}    .*Reverse$
68     \    ${matches_length} =    BuiltIn.Get Length    ${matches}
69     \    ${value} =    BuiltIn.Set Variable If    "${get_reverse}" == "False" and 0 == ${matches_length}    ${name}    "${get_reverse}" == "True" and 0 < ${matches_length}    ${name}
70     \    ...    "${value}" != "None"    ${value}
71     [Return]    ${value}
72
73 Create Sfp And Wait For Rsp Creation
74     [Arguments]    ${sfp_file_name}    ${created_sfps}
75     [Documentation]    Given an SFP name, create it and wait for the associated RSPs to be created
76     Utils.Add Elements To URI From File And Verify    ${SERVICE_FUNCTION_PATHS_URI}    ${sfp_file_name}
77     BuiltIn.Run Keyword If    len(${created_sfps}) > 0    BuiltIn.Wait Until Keyword Succeeds    60s    2s    Utils.Check For Elements At URI    ${SERVICE_FUNCTION_PATHS_STATE_URI}
78     ...    ${created_sfps}
79
80 Delete Sfp And Wait For Rsps Deletion
81     [Arguments]    ${sfp_name}
82     [Documentation]    Given an SFP name, delete it and wait for the associated SFP state and RSPs to be deleted
83     Utils.Remove All Elements At URI And Verify    ${SERVICE_FUNCTION_PATH_URI}${sfp_name}
84     BuiltIn.Wait Until Keyword Succeeds    60s    2s    Check Rendered Service Path Deleted    ${sfp_name}
85
86 Delete All Sfps And Wait For Rsps Deletion
87     [Documentation]    Delete all SFPs and wait for the RSPs to be deleted
88     Utils.Remove All Elements At URI And Verify    ${SERVICE_FUNCTION_PATHS_URI}
89     BuiltIn.Wait Until Keyword Succeeds    60s    2s    Check Empty Service Function Paths State
90
91 Switch Ips In Json Files
92     [Arguments]    ${json_dir}    ${container_names}
93     ${normalized_dir}=    OperatingSystem.Normalize Path    ${json_dir}/*.json
94     : FOR    ${cont_name}    IN    @{container_names}
95     \    ${cont_ip} =    Get Docker IP    ${cont_name}
96     \    OperatingSystem.Run    sudo sed -i 's/${cont_name}/${cont_ip}/g' ${normalized_dir}