Add entity owner leader isolation-rejoin test
[integration/test.git] / csit / libraries / ClusterManagement.robot
1 *** Settings ***
2 Documentation     Resource housing Keywords common to several suites for cluster functional testing.
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 ...               This resource holds private state (in suite variables),
12 ...               which is generated once at Setup.
13 ...               The state includes IP addresses and Http (RequestsLibrary) sessions.
14 ...               Most functionality deals with stopping/starting controllers
15 ...               and finding leaders/followers for a Shard.
16 ...
17 ...               odl-jolokia is assumed to be installed.
18 ...
19 ...               Keywords are ordered from friendly ones to fiddly ones.
20 ...               TODO: Figure out more deterministic but still user-friendly ordering.
21 ...
22 ...               TODO: Unify capitalization of Leaders and Followers.
23 ...
24 ...               TODO: Move Keywords related to iptables manipulation from ClusterKeywords
25 ...               here, or to separate Resource.
26 Library           RequestsLibrary    # for Create_Session and To_Json
27 Library           Collections
28 Resource          ${CURDIR}/TemplatedRequests.robot    # for Get_As_Json_From_Uri
29 Resource          ${CURDIR}/Utils.robot    # for Run_Command_On_Controller
30
31 *** Variables ***
32 ${JAVA_HOME}      ${EMPTY}    # releng/builder scripts should provide correct value
33 ${JOLOKIA_CONF_SHARD_MANAGER_URI}    jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-config,type=DistributedConfigDatastore
34 ${JOLOKIA_OPER_SHARD_MANAGER_URI}    jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-operational,type=DistributedOperationalDatastore
35 ${JOLOKIA_READ_URI}    jolokia/read/org.opendaylight.controller
36 ${RESTCONF_MODULES_DIR}    ${CURDIR}/../variables/restconf/modules
37
38 *** Keywords ***
39 ClusterManagement_Setup
40     [Documentation]    Detect repeated call, or detect number of members and initialize derived suite variables.
41     # Avoid multiple initialization by several downstream libraries.
42     ${already_done} =    BuiltIn.Get_Variable_Value    \${ClusterManagement__has_setup_run}    False
43     BuiltIn.Return_From_Keyword_If    ${already_done}
44     BuiltIn.Set_Suite_Variable    \${ClusterManagement__has_setup_run}    True
45     ${cluster_size} =    BuiltIn.Get_Variable_Value    \${NUM_ODL_SYSTEM}    1
46     ${status}    ${possibly_int_of_members} =    BuiltIn.Run_Keyword_And_Ignore_Error    BuiltIn.Convert_To_Integer    ${cluster_size}
47     ${int_of_members} =    BuiltIn.Set_Variable_If    '${status}' != 'PASS'    ${1}    ${possibly_int_of_members}
48     ClusterManagement__Compute_Derived_Variables    int_of_members=${int_of_members}
49
50 Kill_Members_From_List_Or_All
51     [Arguments]    ${member_index_list}=${EMPTY}    ${confirm}=True
52     [Documentation]    If the list is empty, kill all ODL instances. Otherwise, kill members based on present indices.
53     ...    If \${confirm} is True, sleep 1 second and verify killed instances are not there anymore.
54     ${command} =    BuiltIn.Set_Variable    ps axf | grep java | grep karaf | awk '{print \"kill -9 \" $1}' | sh
55     Run_Command_On_List_Or_All    command=${command}    member_index_list=${member_index_list}
56     BuiltIn.Return_From_Keyword_If    not ${confirm}
57     # TODO: Convert to WUKS with configurable timeout if it turns out 1 second is not enough.
58     BuiltIn.Sleep    1s    Kill -9 closes open files, which may take longer than ssh overhead, but not long enough to warrant WUKS.
59     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
60     : FOR    ${index}    IN    @{index_list}
61     \    Verify_Karaf_Is_Not_Running_On_Member    member_index=${index}
62
63 Isolate_Member_From_List_Or_All
64     [Arguments]    ${isolate_member_index}    ${member_index_list}=${EMPTY}
65     [Documentation]    If the list is empty, isolate member from all ODL instances. Otherwise, isolate member based on present indices.
66     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
67     ${source} =    Get From Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${isolate_member_index}
68     : FOR    ${index}    IN    @{index_list}
69     \    ${destination} =    Get From Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${index}
70     \    ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -I OUTPUT -p all --source ${source} --destination ${destination} -j DROP
71     \    Run Keyword If    "${index}" != "${isolate_member_index}"    Run_Command_On_Member    command=${command}    member_index=${isolate_member_index}
72     ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -L -n
73     ${output} =    Run_Command_On_Member    command=${command}    member_index=${isolate_member_index}
74     Log    ${output}
75
76 Rejoin_Member_From_List_Or_All
77     [Arguments]    ${rejoin_member_index}    ${member_index_list}=${EMPTY}
78     [Documentation]    If the list is empty, rejoin member from all ODL instances. Otherwise, rejoin member based on present indices.
79     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
80     ${source} =    Get From Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${rejoin_member_index}
81     : FOR    ${index}    IN    @{index_list}
82     \    ${destination} =    Get From Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${index}
83     \    ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -D OUTPUT -p all --source ${source} --destination ${destination} -j DROP
84     \    Run Keyword If    "${index}" != "${rejoin_member_index}"    Run_Command_On_Member    command=${command}    member_index=${rejoin_member_index}
85     ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -L -n
86     ${output} =    Run_Command_On_Member    command=${command}    member_index=${rejoin_member_index}
87     Log    ${output}
88
89 Flush_Iptables_From_List_Or_All
90     [Arguments]    ${member_index_list}=${EMPTY}
91     [Documentation]    If the list is empty, flush IPTables in all ODL instances. Otherwise, flush member based on present indices.
92     ${command} =    BuiltIn.Set_Variable    sudo iptables -v -F
93     ${output} =    Run_Command_On_List_Or_All    command=${command}    member_index_list=${member_index_list}
94
95 ClusterManagement__Build_List
96     [Arguments]    ${member}
97     ${member_int}=    BuiltIn.Convert_To_Integer    ${member}
98     ${index_list}=    BuiltIn.Create_List    ${member_int}
99     [Return]    ${index_list}
100
101 Kill_Single_Member
102     [Arguments]    ${member}    ${confirm}=True
103     [Documentation]    Convenience keyword that kills the specified member of the cluster.
104     ${index_list}=    ClusterManagement__Build_List    ${member}
105     Kill_Members_From_List_Or_All    ${index_list}    ${confirm}
106
107 Clean_Journals_And_Snapshots_On_List_Or_All
108     [Arguments]    ${member_index_list}=${EMPTY}    ${karaf_home}=${WORKSPACE}${/}${BUNDLEFOLDER}
109     [Documentation]    Delete journal and snapshots directories on every node listed (or all).
110     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
111     ${command} =    Set Variable    rm -rf "${karaf_home}/journal" "${karaf_home}/snapshots"
112     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
113     \    Run_Command_On_Member    command=${command}    member_index=${index}
114
115 Start_Members_From_List_Or_All
116     [Arguments]    ${member_index_list}=${EMPTY}    ${wait_for_sync}=True    ${timeout}=300s    ${karaf_home}=${WORKSPACE}${/}${BUNDLEFOLDER}    ${export_java_home}=${JAVA_HOME}
117     [Documentation]    If the list is empty, start all cluster members. Otherwise, start members based on present indices.
118     ...    If ${wait_for_sync}, wait for cluster sync on listed members.
119     ...    Optionally karaf_home can be overriden. Optionally specific JAVA_HOME is used for starting.
120     ${base_command} =    BuiltIn.Set_Variable    ${karaf_home}/bin/start
121     ${command} =    BuiltIn.Set_Variable_If    "${export_java_home}"    export JAVA_HOME="${export_java_home}"; ${base_command}    ${base_command}
122     Run_Command_On_List_Or_All    command=${command}    member_index_list=${member_index_list}
123     BuiltIn.Return_From_Keyword_If    not ${wait_for_sync}
124     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    1s    Check_Cluster_Is_In_Sync    member_index_list=${member_index_list}
125     # TODO: Do we also want to check Shard Leaders here?
126
127 Start_Single_Member
128     [Arguments]    ${member}    ${wait_for_sync}=True    ${timeout}=300s
129     [Documentation]    Convenience keyword that starts the specified member of the cluster.
130     ${index_list}=    ClusterManagement__Build_List    ${member}
131     Start_Members_From_List_Or_All    ${index_list}    ${wait_for_sync}    ${timeout}
132
133 Verify_Leader_Exists_For_Each_Shard
134     [Arguments]    ${shard_name_list}    ${shard_type}=operational    ${member_index_list}=${EMPTY}    ${verify_restconf}=True
135     [Documentation]    For each shard name, call Get_Leader_And_Followers_For_Shard.
136     ...    Not much logic there, but single Keyword is useful when using BuiltIn.Wait_Until_Keyword_Succeeds.
137     : FOR    ${shard_name}    IN    @{shard_name_list}
138     \    Get_Leader_And_Followers_For_Shard    shard_name=${shard_name}    shard_type=${shard_type}    validate=True    member_index_list=${member_index_list}    verify_restconf=${verify_restconf}
139
140 Get_Leader_And_Followers_For_Shard
141     [Arguments]    ${shard_name}=default    ${shard_type}=operational    ${validate}=True    ${member_index_list}=${EMPTY}    ${verify_restconf}=True
142     [Documentation]    Get role lists, validate there is one leader, return the leader and list of followers.
143     ...    Optionally, issue GET to a simple restconf URL to make sure subsequent operations will not encounter 503.
144     ${leader_list}    ${follower_list} =    Get_State_Info_For_Shard    shard_name=${shard_name}    shard_type=${shard_type}    validate=True    member_index_list=${member_index_list}
145     ...    verify_restconf=${verify_restconf}
146     ${leader_count} =    BuiltIn.Get_Length    ${leader_list}
147     BuiltIn.Run_Keyword_If    ${leader_count} < 1    BuiltIn.Fail    No leader found.
148     BuiltIn.Length_Should_Be    ${leader_list}    ${1}    Too many Leaders.
149     ${leader} =    Collections.Get_From_List    ${leader_list}    0
150     [Return]    ${leader}    ${follower_list}
151
152 Resolve_Http_Session_For_Member
153     [Arguments]    ${member_index}
154     [Documentation]    Return RequestsLibrary session alias pointing to node of given index.
155     ${session} =    BuiltIn.Set_Variable    ClusterManagement__session_${member_index}
156     [Return]    ${session}
157
158 Get_State_Info_For_Shard
159     [Arguments]    ${shard_name}=default    ${shard_type}=operational    ${validate}=False    ${member_index_list}=${EMPTY}    ${verify_restconf}=False
160     [Documentation]    Return lists of Leader and Follower member indices from a given member index list
161     ...    (or from the full list if empty). If \${shard_type} is not 'config', 'operational' is assumed.
162     ...    If \${validate}, Fail if raft state is not Leader or Follower (for example on Candidate).
163     ...    The biggest difference from Get_Leader_And_Followers_For_Shard
164     ...    is that no check on number of Leaders is performed.
165     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
166     # TODO: Support alternative capitalization of 'config'?
167     ${ds_type} =    BuiltIn.Set_Variable_If    '${shard_type}' != 'config'    operational    config
168     ${leader_list} =    BuiltIn.Create_List
169     ${follower_list} =    BuiltIn.Create_List
170     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
171     \    ${raft_state} =    Get_Raft_State_Of_Shard_At_Member    shard_name=${shard_name}    shard_type=${ds_type}    member_index=${index}    verify_restconf=${verify_restconf}
172     \    BuiltIn.Run_Keyword_If    'Follower' == '${raft_state}'    Collections.Append_To_List    ${follower_list}    ${index}
173     \    ...    ELSE IF    'Leader' == '${raft_state}'    Collections.Append_To_List    ${leader_list}    ${index}
174     \    ...    ELSE IF    ${validate}    BuiltIn.Fail    Unrecognized Raft state: ${raft_state}
175     [Return]    ${leader_list}    ${follower_list}
176
177 Check_Cluster_Is_In_Sync
178     [Arguments]    ${member_index_list}=${EMPTY}
179     [Documentation]    Fail if no-sync is detected on a member from list (or any).
180     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
181     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
182     \    ${status} =    Get_Sync_Status_Of_Member    member_index=${index}
183     \    BuiltIn.Continue_For_Loop_If    'True' == '${status}'
184     \    BuiltIn.Fail    Index ${index} has incorrect status: ${status}
185
186 Verify_Karaf_Is_Not_Running_On_Member
187     [Arguments]    ${member_index}
188     [Documentation]    Fail if non-zero karaf instances are counted on member of given index.
189     ${count} =    Count_Running_Karafs_On_Member    member_index=${member_index}
190     BuiltIn.Should_Be_Equal    0    ${count}    Found running Karaf count: ${count}
191
192 Verify_Single_Karaf_Is_Running_On_Member
193     [Arguments]    ${member_index}
194     [Documentation]    Fail if number of karaf instances on member of given index is not one.
195     ${count} =    Count_Running_Karafs_On_Member    member_index=${member_index}
196     BuiltIn.Should_Be_Equal    1    ${count}    Wrong number of Karafs running: ${count}
197
198 Run_Command_On_List_Or_All
199     [Arguments]    ${command}    ${member_index_list}=${EMPTY}
200     [Documentation]    Cycle through indices (or all), run command on each.
201     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
202     : FOR    ${index}    IN    @{index_list}
203     \    Run_Command_On_Member    command=${command}    member_index=${index}
204
205 Get_Sync_Status_Of_Member
206     [Arguments]    ${member_index}
207     [Documentation]    Obtain IP, two GETs from jolokia URIs, return combined sync status as string.
208     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
209     ${conf_text} =    Get_As_Json_From_Uri    uri=${JOLOKIA_CONF_SHARD_MANAGER_URI}    session=${session}
210     ${conf_status} =    ClusterManagement__Parse_Sync_Status    shard_manager_text=${conf_text}
211     BuiltIn.Return_From_Keyword_If    'False' == ${conf_status}    False
212     ${oper_text} =    Get_As_Json_From_Uri    uri=${JOLOKIA_OPER_SHARD_MANAGER_URI}    session=${session}
213     ${oper_status} =    ClusterManagement__Parse_Sync_Status    shard_manager_text=${oper_text}
214     [Return]    ${oper_status}
215
216 Run_Command_On_Member
217     [Arguments]    ${command}    ${member_index}
218     [Documentation]    Obtain IP, call Utils and return output. This does not preserve active ssh session.
219     ${member_ip} =    Collections.Get_From_Dictionary    dictionary=${ClusterManagement__index_to_ip_mapping}    key=${member_index}
220     ${output} =    Utils.Run_Command_On_Controller    ${member_ip}    ${command}
221     [Return]    ${output}
222
223 Count_Running_Karafs_On_Member
224     [Arguments]    ${member_index}
225     [Documentation]    Remotely execute grep for karaf process, return count as string.
226     ${command} =    BuiltIn.Set_Variable    ps axf | grep karaf | grep -v grep | wc -l
227     ${count} =    Run_Command_On_Member    command=${command}    member_index=${member_index}
228     [Return]    ${count}
229
230 Get_Raft_State_Of_Shard_At_Member
231     [Arguments]    ${shard_name}    ${shard_type}    ${member_index}    ${verify_restconf}=False
232     [Documentation]    Send request to Jolokia on indexed member, return extracted Raft status.
233     ...    Optionally, check restconf works.
234     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
235     # TODO: Does the used URI tend to generate large data which floods log.html?
236     BuiltIn.Run_Keyword_If    ${verify_restconf}    TemplatedRequests.Get_As_Json_Templated    session=${session}    folder=${RESTCONF_MODULES_DIR}    verify=False
237     ${type_class} =    Resolve_Shard_Type_Class    shard_type=${shard_type}
238     ${uri} =    BuiltIn.Set_Variable    ${JOLOKIA_READ_URI}:Category=Shards,name=member-${member_index}-shard-${shard_name}-${shard_type},type=${type_class}
239     ${data_text} =    TemplatedRequests.Get_As_Json_From_Uri    uri=${uri}    session=${session}
240     ${data_object} =    RequestsLibrary.To_Json    ${data_text}
241     ${value} =    Collections.Get_From_Dictionary    ${data_object}    value
242     ${raft_state} =    Collections.Get_From_Dictionary    ${value}    RaftState
243     [Return]    ${raft_state}
244
245 Resolve_Shard_Type_Class
246     [Arguments]    ${shard_type}
247     [Documentation]    Simple lookup for class name corresponding to desired type.
248     BuiltIn.Run_Keyword_If    '${shard_type}' == 'config'    BuiltIn.Return_From_Keyword    DistributedConfigDatastore
249     ...    ELSE IF    '${shard_type}' == 'operational'    BuiltIn.Return_From_Keyword    DistributedOperationalDatastore
250     BuiltIn.Fail    Unrecognized shard type: ${shard_type}
251
252 ClusterManagement__Parse_Sync_Status
253     [Arguments]    ${shard_manager_text}
254     [Documentation]    Return sync status parsed out of given text. Called twice by Get_Sync_Status_Of_Member.
255     BuiltIn.Log    ${shard_manager_text}
256     ${manager_object} =    RequestsLibrary.To_Json    ${shard_manager_text}
257     ${value_object} =    Collections.Get_From_Dictionary    dictionary=${manager_object}    key=value
258     ${sync_status} =    Collections.Get_From_Dictionary    dictionary=${value_object}    key=SyncStatus
259     [Return]    ${sync_status}
260
261 ClusterManagement__Given_Or_Internal_Index_List
262     [Arguments]    ${given_list}=${EMPTY}
263     [Documentation]    Utility to allow \${EMPTY} as default argument value, as the internal list is computed at runtime.
264     ${given_length} =    BuiltIn.Get_Length    ${given_list}
265     ${return_list} =    BuiltIn.Set_Variable_If    ${given_length} > 0    ${given_list}    ${ClusterManagement__member_index_list}
266     [Return]    ${return_list}
267
268 ClusterManagement__Compute_Derived_Variables
269     [Arguments]    ${int_of_members}
270     [Documentation]    Construct index list, session list and IP mapping, publish them as suite variables.
271     @{member_index_list} =    BuiltIn.Create_List
272     @{session_list} =    BuiltIn.Create_List
273     &{index_to_ip_mapping} =    BuiltIn.Create_Dictionary
274     : FOR    ${index}    IN RANGE    1    ${int_of_members+1}
275     \    ClusterManagement__Include_Member_Index    ${index}    ${member_index_list}    ${session_list}    ${index_to_ip_mapping}
276     BuiltIn.Set_Suite_Variable    \${ClusterManagement__member_index_list}    ${member_index_list}
277     BuiltIn.Set_Suite_Variable    \${ClusterManagement__index_to_ip_mapping}    ${index_to_ip_mapping}
278     BuiltIn.Set_Suite_Variable    \${ClusterManagement__session_list}    ${session_list}
279
280 ClusterManagement__Include_Member_Index
281     [Arguments]    ${index}    ${member_index_list}    ${session_list}    ${index_to_ip_mapping}
282     [Documentation]    Add a corresponding item based on index into the last three arguments.
283     ...    Create the Http session whose alias is added to list.
284     Collections.Append_To_List    ${member_index_list}    ${index}
285     ${member_ip} =    BuiltIn.Set_Variable    ${ODL_SYSTEM_${index}_IP}
286     # ${index} is int (not string) so "key=value" syntax does not work in the following line.
287     Collections.Set_To_Dictionary    ${index_to_ip_mapping}    ${index}    ${member_ip}
288     # Http session, with ${AUTH}, without headers.
289     ${session_alias} =    Resolve_Http_Session_For_Member    member_index=${index}
290     RequestsLibrary.Create_Session    ${session_alias}    http://${member_ip}:${RESTCONFPORT}    auth=${AUTH}    max_retries=0
291     Collections.Append_To_List    ${session_list}    ${session_alias}