2d410d29814d222ce5224a8299ef62d275051c5d
[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
39     ...    ${session}
40     ...    url=${PC_NW_TOPOLOGY}=${topology}?content=nonconfig
41     ...    expected_status=any
42     IF    ${response.status_code} != 200
43         Fail    Get on ${topology} returned status code ${response.status_code} with message: ${response.text}
44     END
45     RETURN    ${response.text}
46
47 Get_Ipv4_Topology_Count
48     [Documentation]    Get topology. If not fail, return number of prefixes in the topology.
49     [Arguments]    ${session}=operational    ${topology}=example-ipv4-topology
50     ${topology} =    Get_Ipv4_Topology    session=${session}    topology=${topology}
51     # Triple quotes are precaution against formatted output.
52     ${prefix_count} =    Builtin.Evaluate    len(re.findall('"prefix":"', '''${topology}'''))    modules=re
53     RETURN    ${prefix_count}
54
55 Get_Ipv4_Topology_Count_With_Shards_Check
56     [Documentation]    Get topology after the shards stability check passes. If not fail, return number of prefixes in the topology.
57     [Arguments]    ${shards_list}    ${details_exp}    ${session}=operational    ${topology}=example-ipv4-topology
58     ${details_actual} =    ShardStability.Shards_Stability_Get_Details    ${shards_list}    http_timeout=125
59     ShardStability.Shards_Stability_Compare_Same    ${details_actual}    stateless_details=${details_exp}
60     ${topology} =    Get_Ipv4_Topology    session=${session}    topology=${topology}
61     # Triple quotes are precaution against formatted output.
62     ${prefix_count} =    Builtin.Evaluate    len(re.findall('"prefix":"', '''${topology}'''))    modules=re
63     RETURN    ${prefix_count}
64
65 Check_Ipv4_Topology_Count
66     [Documentation]    Check that the count of prefixes matches the expected count. Fails if it does not. In either case, collect garbage.
67     [Arguments]    ${expected_count}    ${session}=operational    ${topology}=example-ipv4-topology
68     ${actual_count} =    ScalarClosures.Run_Keyword_And_Collect_Garbage
69     ...    Get_Ipv4_Topology_Count
70     ...    session=${session}
71     ...    topology=${topology}
72     BuiltIn.Should_Be_Equal_As_Strings    ${actual_count}    ${expected_count}
73
74 Check_Ipv4_Topology_Is_Empty
75     [Documentation]    Example_Ipv4_Topology has to give status 200 with zero prefixes.
76     ...
77     ...    Functional suites should use a more strict Keyword which tests for the whole JSON structure.
78     [Arguments]    ${session}=operational    ${topology}=example-ipv4-topology
79     Check_Ipv4_Topology_Count    0    session=${session}    topology=${topology}
80
81 Wait_For_Ipv4_Topology_Prefixes_To_Become_Stable
82     [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.
83     [Arguments]    ${timeout}=60s    ${period}=5s    ${repetitions}=1    ${excluded_count}=-1    ${session}=operational    ${topology}=example-ipv4-topology
84     ...    ${shards_list}=${EMPTY}    ${shards_details}=${EMPTY}
85     # This is very similar to ChangeCounter keyword, but attempt to extract common code would increase overall code size.
86     IF    """${shards_list}"""=="""${EMPTY}"""
87         ${getter} =    ScalarClosures.Closure_From_Keyword_And_Arguments
88         ...    Get_Ipv4_Topology_Count
89         ...    session=${session}
90         ...    topology=${topology}
91     ELSE
92         ${getter} =    ScalarClosures.Closure_From_Keyword_And_Arguments
93         ...    Get_Ipv4_Topology_Count_With_Shards_Check
94         ...    ${shards_list}
95         ...    ${shards_details}
96         ...    session=${session}
97         ...    topology=${topology}
98     END
99     ${validator} =    ScalarClosures.Closure_From_Keyword_And_Arguments
100     ...    WaitUtils.Excluding_Stability_Safe_Stateful_Validator_As_Keyword
101     ...    state_holder
102     ...    data_holder
103     ...    excluded_value=${excluded_count}
104     ${result} =    WaitUtils.Wait_For_Getter_Error_Or_Safe_Stateful_Validator_Consecutive_Success
105     ...    timeout=${timeout}
106     ...    period=${period}
107     ...    count=${repetitions}
108     ...    getter=${getter}
109     ...    safe_validator=${validator}
110     ...    initial_state=${excluded_count}
111     RETURN    ${result}