Do not use Get Request in PrefixCounting.robot
[integration/test.git] / csit / libraries / PrefixCounting.robot
1 *** Settings ***
2 Documentation       Robot keyword library (Resource) for common BGP actions concerned with counting prefixes.
3 ...
4 ...                 Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
5 ...
6 ...                 This program and the accompanying materials are made available under the
7 ...                 terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 ...                 and is available at http://www.eclipse.org/legal/epl-v10.html
9 ...
10 ...
11 ...                 Currently, all keywords count prefixes only in ${topology}.
12 ...                 Prefix is identified by simplistic regular expression on JSON data.
13 ...
14 ...                 This resource assumes that RequestsLibrary has open a connection named "operational"
15 ...                 which points to (an analogue of) http://<ip-addr>:${RESTCONFPORT}
16 ...                 or user has to provide a similar session.
17
18 Library             RequestsLibrary
19 Resource            ${CURDIR}/ShardStability.robot
20 Resource            ${CURDIR}/WaitUtils.robot
21 Resource            ${CURDIR}/ScalarClosures.robot
22
23
24 *** Variables ***
25 ${PC_NW_TOPOLOGY}       ${REST_API}/network-topology:network-topology/topology
26
27
28 *** Keywords ***
29 PC_Setup
30     [Documentation]    Call dependency setups and construct suite variables.
31     WaitUtils.WU_Setup    # includes ScalarClosures.SC_Setup
32
33 Get_Ipv4_Topology
34     [Documentation]    GET the ${topology} data, check status is 200, return the topology data.
35     ...
36     ...    Contrary to Utils.Get_Data_From_URI, this does not Log the (potentially huge) content.
37     [Arguments]    ${session}=operational    ${topology}=example-ipv4-topology
38     ${response} =    RequestsLibrary.GET On Session    ${session}    ${PC_NW_TOPOLOGY}=${topology}?content=nonconfig expected_status=any
39     IF    ${response.status_code} != 200
40         Fail    Get on ${topology} returned status code ${response.status_code} with message: ${response.text}
41     END
42     RETURN    ${response.text}
43
44 Get_Ipv4_Topology_Count
45     [Documentation]    Get topology. If not fail, return number of prefixes in the topology.
46     [Arguments]    ${session}=operational    ${topology}=example-ipv4-topology
47     ${topology} =    Get_Ipv4_Topology    session=${session}    topology=${topology}
48     # Triple quotes are precaution against formatted output.
49     ${prefix_count} =    Builtin.Evaluate    len(re.findall('"prefix":"', '''${topology}'''))    modules=re
50     RETURN    ${prefix_count}
51
52 Get_Ipv4_Topology_Count_With_Shards_Check
53     [Documentation]    Get topology after the shards stability check passes. If not fail, return number of prefixes in the topology.
54     [Arguments]    ${shards_list}    ${details_exp}    ${session}=operational    ${topology}=example-ipv4-topology
55     ${details_actual} =    ShardStability.Shards_Stability_Get_Details    ${shards_list}    http_timeout=125
56     ShardStability.Shards_Stability_Compare_Same    ${details_actual}    stateless_details=${details_exp}
57     ${topology} =    Get_Ipv4_Topology    session=${session}    topology=${topology}
58     # Triple quotes are precaution against formatted output.
59     ${prefix_count} =    Builtin.Evaluate    len(re.findall('"prefix":"', '''${topology}'''))    modules=re
60     RETURN    ${prefix_count}
61
62 Check_Ipv4_Topology_Count
63     [Documentation]    Check that the count of prefixes matches the expected count. Fails if it does not. In either case, collect garbage.
64     [Arguments]    ${expected_count}    ${session}=operational    ${topology}=example-ipv4-topology
65     ${actual_count} =    ScalarClosures.Run_Keyword_And_Collect_Garbage
66     ...    Get_Ipv4_Topology_Count
67     ...    session=${session}
68     ...    topology=${topology}
69     BuiltIn.Should_Be_Equal_As_Strings    ${actual_count}    ${expected_count}
70
71 Check_Ipv4_Topology_Is_Empty
72     [Documentation]    Example_Ipv4_Topology has to give status 200 with zero prefixes.
73     ...
74     ...    Functional suites should use a more strict Keyword which tests for the whole JSON structure.
75     [Arguments]    ${session}=operational    ${topology}=example-ipv4-topology
76     Check_Ipv4_Topology_Count    0    session=${session}    topology=${topology}
77
78 Wait_For_Ipv4_Topology_Prefixes_To_Become_Stable
79     [Documentation]    Each ${period} get prefix count. When called with shard list, the check is done before the count is get. After ${repetitions} of stable different from ${excluded_count} within ${timeout}, Return validator output. Fail early on getter error.
80     [Arguments]    ${timeout}=60s    ${period}=5s    ${repetitions}=1    ${excluded_count}=-1    ${session}=operational    ${topology}=example-ipv4-topology
81     ...    ${shards_list}=${EMPTY}    ${shards_details}=${EMPTY}
82     # This is very similar to ChangeCounter keyword, but attempt to extract common code would increase overall code size.
83     IF    """${shards_list}"""=="""${EMPTY}"""
84         ${getter} =    ScalarClosures.Closure_From_Keyword_And_Arguments
85         ...    Get_Ipv4_Topology_Count
86         ...    session=${session}
87         ...    topology=${topology}
88     ELSE
89         ${getter} =    ScalarClosures.Closure_From_Keyword_And_Arguments
90         ...    Get_Ipv4_Topology_Count_With_Shards_Check
91         ...    ${shards_list}
92         ...    ${shards_details}
93         ...    session=${session}
94         ...    topology=${topology}
95     END
96     ${validator} =    ScalarClosures.Closure_From_Keyword_And_Arguments
97     ...    WaitUtils.Excluding_Stability_Safe_Stateful_Validator_As_Keyword
98     ...    state_holder
99     ...    data_holder
100     ...    excluded_value=${excluded_count}
101     ${result} =    WaitUtils.Wait_For_Getter_Error_Or_Safe_Stateful_Validator_Consecutive_Success
102     ...    timeout=${timeout}
103     ...    period=${period}
104     ...    count=${repetitions}
105     ...    getter=${getter}
106     ...    safe_validator=${validator}
107     ...    initial_state=${excluded_count}
108     RETURN    ${result}