Rework to use ClusterManagement's sessions
[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 Library           RequestsLibrary
18 Resource          ${CURDIR}/ShardStability.robot
19 Resource          ${CURDIR}/WaitUtils.robot
20 Resource          ${CURDIR}/ScalarClosures.robot
21
22 *** Variables ***
23 ${PC_NW_TOPOLOGY}    ${OPERATIONAL_API}/network-topology:network-topology/topology
24
25 *** Keywords ***
26 PC_Setup
27     [Documentation]    Call dependency setups and construct suite variables.
28     WaitUtils.WU_Setup    # includes ScalarClosures.SC_Setup
29
30 Get_Ipv4_Topology
31     [Arguments]    ${session}=operational    ${topology}=example-ipv4-topology
32     [Documentation]    GET the ${topology} data, check status is 200, return the topology data.
33     ...
34     ...    Contrary to Utils.Get_Data_From_URI, this does not Log the (potentially huge) content.
35     ${response} =    RequestsLibrary.Get_Request    ${session}    ${PC_NW_TOPOLOGY}/${topology}
36     Run_Keyword_If    ${response.status_code} != 200    Fail    Get on ${topology} returned status code ${response.status_code} with message: ${response.text}
37     [Return]    ${response.text}
38
39 Get_Ipv4_Topology_Count
40     [Arguments]    ${session}=operational    ${topology}=example-ipv4-topology
41     [Documentation]    Get topology. If not fail, return number of prefixes in the topology.
42     ${topology} =    Get_Ipv4_Topology    session=${session}    topology=${topology}
43     # Triple quotes are precaution against formatted output.
44     ${prefix_count} =    Builtin.Evaluate    len(re.findall('"prefix":"', '''${topology}'''))    modules=re
45     [Return]    ${prefix_count}
46
47 Get_Ipv4_Topology_Count_With_Shards_Check
48     [Arguments]    ${shards_list}    ${details_exp}    ${session}=operational    ${topology}=example-ipv4-topology
49     [Documentation]    Get topology after the shards stability check passes. If not fail, return number of prefixes in the topology.
50     ${details_actual}=    ShardStability.Shards_Stability_Get_Details    ${shards_list}    http_timeout=125
51     ShardStability.Shards_Stability_Compare_Same    ${details_actual}    stateless_details=${details_exp}
52     ${topology} =    Get_Ipv4_Topology    session=${session}    topology=${topology}
53     # Triple quotes are precaution against formatted output.
54     ${prefix_count} =    Builtin.Evaluate    len(re.findall('"prefix":"', '''${topology}'''))    modules=re
55     [Return]    ${prefix_count}
56
57 Check_Ipv4_Topology_Count
58     [Arguments]    ${expected_count}    ${session}=operational    ${topology}=example-ipv4-topology
59     [Documentation]    Check that the count of prefixes matches the expected count. Fails if it does not. In either case, collect garbage.
60     ${actual_count} =    ScalarClosures.Run_Keyword_And_Collect_Garbage    Get_Ipv4_Topology_Count    session=${session}    topology=${topology}
61     BuiltIn.Should_Be_Equal_As_Strings    ${actual_count}    ${expected_count}
62
63 Check_Ipv4_Topology_Is_Empty
64     [Arguments]    ${session}=operational    ${topology}=example-ipv4-topology
65     [Documentation]    Example_Ipv4_Topology has to give status 200 with zero prefixes.
66     ...
67     ...    Functional suites should use a more strict Keyword which tests for the whole JSON structure.
68     Check_Ipv4_Topology_Count    0    session=${session}    topology=${topology}
69
70 Wait_For_Ipv4_Topology_Prefixes_To_Become_Stable
71     [Arguments]    ${timeout}=60s    ${period}=5s    ${repetitions}=1    ${excluded_count}=-1    ${session}=operational    ${topology}=example-ipv4-topology
72     ...    ${shards_list}=${EMPTY}    ${shards_details}=${EMPTY}
73     [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.
74     # This is very similar to ChangeCounter keyword, but attempt to extract common code would increase overall code size.
75     ${getter} =    BuiltIn.Run_Keyword_If    """${shards_list}"""=="""${EMPTY}"""    ScalarClosures.Closure_From_Keyword_And_Arguments    Get_Ipv4_Topology_Count    session=${session}    topology=${topology}
76     ...    ELSE    ScalarClosures.Closure_From_Keyword_And_Arguments    Get_Ipv4_Topology_Count_With_Shards_Check    ${shards_list}    ${shards_details}    session=${session}
77     ...    topology=${topology}
78     ${validator} =    ScalarClosures.Closure_From_Keyword_And_Arguments    WaitUtils.Excluding_Stability_Safe_Stateful_Validator_As_Keyword    state_holder    data_holder    excluded_value=${excluded_count}
79     ${result} =    WaitUtils.Wait_For_Getter_Error_Or_Safe_Stateful_Validator_Consecutive_Success    timeout=${timeout}    period=${period}    count=${repetitions}    getter=${getter}    safe_validator=${validator}
80     ...    initial_state=${excluded_count}
81     [Return]    ${result}