Make ClusterManagement keywords preserve active SSH connection
[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 ${JOLOKIA_CONF_SHARD_MANAGER_URI}    jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-config,type=DistributedConfigDatastore
33 ${JOLOKIA_OPER_SHARD_MANAGER_URI}    jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-operational,type=DistributedOperationalDatastore
34 ${JOLOKIA_READ_URI}    jolokia/read/org.opendaylight.controller
35 ${KARAF_HOME}     ${WORKSPACE}${/}${BUNDLEFOLDER}
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     ${status}    ${possibly_int_of_members} =    BuiltIn.Run_Keyword_And_Ignore_Error    BuiltIn.Convert_To_Integer    ${NUM_ODL_SYSTEM}
46     ${int_of_members} =    BuiltIn.Set_Variable_If    '${status}' != 'PASS'    ${1}    ${possibly_int_of_members}
47     ClusterManagement__Compute_Derived_Variables    int_of_members=${int_of_members}
48
49 Kill_Members_From_List_Or_All
50     [Arguments]    ${member_index_list}=${EMPTY}    ${confirm}=True
51     [Documentation]    If the list is empty, kill all ODL instances. Otherwise, kill members based on present indices.
52     ...    If \${confirm} is True, sleep 1 second and verify killed instances are not there anymore.
53     ${command} =    BuiltIn.Set_Variable    ps axf | grep karaf | grep -v grep | awk '{print \"kill -9 \" $1}' | sh
54     Run_Command_On_List_Or_All    command=${command}    member_index_list=${member_index_list}
55     BuiltIn.Return_From_Keyword_If    not ${confirm}
56     # TODO: Convert to WUKS with configurable timeout if it turns out 1 second is not enough.
57     BuiltIn.Sleep    1s    Kill -9 closes open files, which may take longer than ssh overhead, but not long enough to warrant WUKS.
58     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
59     : FOR    ${index}    IN    @{index_list}
60     \    Verify_Karaf_Is_Not_Running_On_Member    member_index=${index}
61
62 ClusterManagement__Build_List
63     [Arguments]    ${member}
64     ${member_int}=    BuiltIn.Convert_To_Integer    ${member}
65     ${index_list}=    BuiltIn.Create_List    ${member_int}
66     [Return]    ${index_list}
67
68 Kill_Single_Member
69     [Arguments]    ${member}    ${confirm}=True
70     [Documentation]    Convenience keyword that kills the specified member of the cluster.
71     ${index_list}=    ClusterManagement__Build_List    ${member}
72     Kill_Members_From_List_Or_All    ${index_list}    ${confirm}
73
74 Clean_Journals_And_Snapshots_On_List_Or_All
75     [Arguments]    ${member_index_list}=${EMPTY}
76     [Documentation]    Delete journal and snapshots directories on every node listed (or all).
77     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
78     ${command} =    Set Variable    rm -rf "${KARAF_HOME}/journal" "${KARAF_HOME}/snapshots"
79     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
80     \    Run_Command_On_Member    command=${command}    member_index=${index}
81
82 Start_Members_From_List_Or_All
83     [Arguments]    ${member_index_list}=${EMPTY}    ${wait_for_sync}=True    ${timeout}=300s
84     [Documentation]    If the list is empty, start all cluster members. Otherwise, start members based on present indices.
85     ...    If ${wait_for_sync}, wait for cluster sync on listed members.
86     ${command} =    BuiltIn.Set_Variable    ${KARAF_HOME}/bin/start
87     Run_Command_On_List_Or_All    command=${command}    member_index_list=${member_index_list}
88     BuiltIn.Return_From_Keyword_If    not ${wait_for_sync}
89     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    1s    Check_Cluster_Is_In_Sync    member_index_list=${member_index_list}
90     # TODO: Do we also want to check Shard Leaders here?
91
92 Start_Single_Member
93     [Arguments]    ${member}    ${wait_for_sync}=True    ${timeout}=300s
94     [Documentation]    Convenience keyword that starts the specified member of the cluster.
95     ${index_list}=    ClusterManagement__Build_List    ${member}
96     Start_Members_From_List_Or_All    ${index_list}    ${wait_for_sync}    ${timeout}
97
98 Verify_Leader_Exists_For_Each_Shard
99     [Arguments]    ${shard_name_list}    ${shard_type}=operational    ${member_index_list}=${EMPTY}    ${verify_restconf}=True
100     [Documentation]    For each shard name, call Get_Leader_And_Followers_For_Shard.
101     ...    Not much logic there, but single Keyword is useful when using BuiltIn.Wait_Until_Keyword_Succeeds.
102     : FOR    ${shard_name}    IN    @{shard_name_list}
103     \    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}
104
105 Get_Leader_And_Followers_For_Shard
106     [Arguments]    ${shard_name}=default    ${shard_type}=operational    ${validate}=True    ${member_index_list}=${EMPTY}    ${verify_restconf}=True
107     [Documentation]    Get role lists, validate there is one leader, return the leader and list of followers.
108     ...    Optionally, issue GET to a simple restconf URL to make sure subsequent operations will not encounter 503.
109     ${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}
110     ...    verify_restconf=${verify_restconf}
111     ${leader_count} =    BuiltIn.Get_Length    ${leader_list}
112     BuiltIn.Run_Keyword_If    ${leader_count} < 1    BuiltIn.Fail    No leader found.
113     BuiltIn.Length_Should_Be    ${leader_list}    ${1}    Too many Leaders.
114     ${leader} =    Collections.Get_From_List    ${leader_list}    0
115     [Return]    ${leader}    ${follower_list}
116
117 Resolve_Http_Session_For_Member
118     [Arguments]    ${member_index}
119     [Documentation]    Return RequestsLibrary session alias pointing to node of given index.
120     ${session} =    BuiltIn.Set_Variable    ClusterManagement__session_${member_index}
121     [Return]    ${session}
122
123 Get_State_Info_For_Shard
124     [Arguments]    ${shard_name}=default    ${shard_type}=operational    ${validate}=False    ${member_index_list}=${EMPTY}    ${verify_restconf}=False
125     [Documentation]    Return lists of Leader and Follower member indices from a given member index list
126     ...    (or from the full list if empty). If \${shard_type} is not 'config', 'operational' is assumed.
127     ...    If \${validate}, Fail if raft state is not Leader or Follower (for example on Candidate).
128     ...    The biggest difference from Get_Leader_And_Followers_For_Shard
129     ...    is that no check on number of Leaders is performed.
130     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
131     # TODO: Support alternative capitalization of 'config'?
132     ${ds_type} =    BuiltIn.Set_Variable_If    '${shard_type}' != 'config'    operational    config
133     ${leader_list} =    BuiltIn.Create_List
134     ${follower_list} =    BuiltIn.Create_List
135     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
136     \    ${raft_state} =    Get_Raft_State_Of_Shard_At_Member    shard_name=${shard_name}    shard_type=${ds_type}    member_index=${index}    verify_restconf=${verify_restconf}
137     \    BuiltIn.Run_Keyword_If    'Follower' == '${raft_state}'    Collections.Append_To_List    ${follower_list}    ${index}
138     \    ...    ELSE IF    'Leader' == '${raft_state}'    Collections.Append_To_List    ${leader_list}    ${index}
139     \    ...    ELSE IF    ${validate}    BuiltIn.Fail    Unrecognized Raft state: ${raft_state}
140     [Return]    ${leader_list}    ${follower_list}
141
142 Check_Cluster_Is_In_Sync
143     [Arguments]    ${member_index_list}=${EMPTY}
144     [Documentation]    Fail if no-sync is detected on a member from list (or any).
145     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
146     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
147     \    ${status} =    Get_Sync_Status_Of_Member    member_index=${index}
148     \    # The previous line may have failed already. If not, check status.
149     \    BuiltIn.Continue_For_Loop_If    'True' == '${status}'
150     \    BuiltIn.Fail    Index ${index} has incorrect status: ${status}
151
152 Verify_Karaf_Is_Not_Running_On_Member
153     [Arguments]    ${member_index}
154     [Documentation]    Fail if non-zero karaf instances are counted on member of given index.
155     ${count} =    Count_Running_Karafs_On_Member    member_index=${member_index}
156     BuiltIn.Should_Be_Equal    0    ${count}    Found running Karaf count: ${count}
157
158 Verify_Single_Karaf_Is_Running_On_Member
159     [Arguments]    ${member_index}
160     [Documentation]    Fail if number of karaf instances on member of given index is not one.
161     ${count} =    Count_Running_Karafs_On_Member    member_index=${member_index}
162     BuiltIn.Should_Be_Equal    1    ${count}    Wrong number of Karafs running: ${count}
163
164 Run_Command_On_List_Or_All
165     [Arguments]    ${command}    ${member_index_list}=${EMPTY}
166     [Documentation]    Cycle through indices (or all), run command on each.
167     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
168     : FOR    ${index}    IN    @{index_list}
169     \    Run_Command_On_Member    command=${command}    member_index=${index}
170
171 Get_Sync_Status_Of_Member
172     [Arguments]    ${member_index}
173     [Documentation]    Obtain IP, two GETs from jolokia URIs, return combined sync status as string.
174     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
175     ${conf_text} =    Get_As_Json_From_Uri    uri=${JOLOKIA_CONF_SHARD_MANAGER_URI}    session=${session}
176     ${conf_status} =    ClusterManagement__Parse_Sync_Status    shard_manager_text=${conf_text}
177     BuiltIn.Return_From_Keyword_If    'False' == ${conf_status}    False
178     ${oper_text} =    Get_As_Json_From_Uri    uri=${JOLOKIA_OPER_SHARD_MANAGER_URI}    session=${session}
179     ${oper_status} =    ClusterManagement__Parse_Sync_Status    shard_manager_text=${oper_text}
180     [Return]    ${oper_status}
181
182 Run_Command_On_Member
183     [Arguments]    ${command}    ${member_index}
184     [Documentation]    Obtain IP, call Utils and return output. This does not preserve active ssh session.
185     ${member_ip} =    Collections.Get_From_Dictionary    dictionary=${ClusterManagement__index_to_ip_mapping}    key=${member_index}
186     ${output} =    Utils.Run_Command_On_Controller    ${member_ip}    ${command}
187     [Return]    ${output}
188
189 Count_Running_Karafs_On_Member
190     [Arguments]    ${member_index}
191     [Documentation]    Remotely execute grep for karaf process, return count as string.
192     ${command} =    BuiltIn.Set_Variable    ps axf | grep karaf | grep -v grep | wc -l
193     ${count} =    Run_Command_On_Member    command=${command}    member_index=${member_index}
194     [Return]    ${count}
195
196 Get_Raft_State_Of_Shard_At_Member
197     [Arguments]    ${shard_name}    ${shard_type}    ${member_index}    ${verify_restconf}=False
198     [Documentation]    Send request to Jolokia on indexed member, return extracted Raft status.
199     ...    Optionally, check restconf works.
200     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
201     # TODO: Does the used URI tend to generate large data which floods log.html?
202     BuiltIn.Run_Keyword_If    ${verify_restconf}    TemplatedRequests.Get_As_Json_Templated    session=${session}    folder=${RESTCONF_MODULES_DIR}    verify=False
203     ${type_class} =    Resolve_Shard_Type_Class    shard_type=${shard_type}
204     ${uri} =    BuiltIn.Set_Variable    ${JOLOKIA_READ_URI}:Category=Shards,name=member-${member_index}-shard-${shard_name}-${shard_type},type=${type_class}
205     ${data_text} =    TemplatedRequests.Get_As_Json_From_Uri    uri=${uri}    session=${session}
206     ${data_object} =    RequestsLibrary.To_Json    ${data_text}
207     ${value} =    Collections.Get_From_Dictionary    ${data_object}    value
208     ${raft_state} =    Collections.Get_From_Dictionary    ${value}    RaftState
209     [Return]    ${raft_state}
210
211 Resolve_Shard_Type_Class
212     [Arguments]    ${shard_type}
213     [Documentation]    Simple lookup for class name corresponding to desired type.
214     BuiltIn.Run_Keyword_If    '${shard_type}' == 'config'    BuiltIn.Return_From_Keyword    DistributedConfigDatastore
215     ...    ELSE IF    '${shard_type}' == 'operational'    BuiltIn.Return_From_Keyword    DistributedOperationalDatastore
216     BuiltIn.Fail    Unrecognized shard type: ${shard_type}
217
218 ClusterManagement__Parse_Sync_Status
219     [Arguments]    ${shard_manager_text}
220     [Documentation]    Return sync status parsed out of given text. Called twice by Get_Sync_Status_Of_Member.
221     BuiltIn.Log    ${shard_manager_text}
222     ${manager_object} =    RequestsLibrary.To_Json    ${shard_manager_text}
223     ${value_object} =    Collections.Get_From_Dictionary    dictionary=${manager_object}    key=value
224     ${sync_status} =    Collections.Get_From_Dictionary    dictionary=${value_object}    key=SyncStatus
225     [Return]    ${sync_status}
226
227 ClusterManagement__Given_Or_Internal_Index_List
228     [Arguments]    ${given_list}=${EMPTY}
229     [Documentation]    Utility to allow \${EMPTY} as default argument value, as the internal list is computed at runtime.
230     ${given_length} =    BuiltIn.Get_Length    ${given_list}
231     ${return_list} =    BuiltIn.Set_Variable_If    ${given_length} > 0    ${given_list}    ${ClusterManagement__member_index_list}
232     [Return]    ${return_list}
233
234 ClusterManagement__Compute_Derived_Variables
235     [Arguments]    ${int_of_members}
236     [Documentation]    Construct index list, session list and IP mapping, publish them as suite variables.
237     @{member_index_list} =    BuiltIn.Create_List
238     @{session_list} =    BuiltIn.Create_List
239     &{index_to_ip_mapping} =    BuiltIn.Create_Dictionary
240     : FOR    ${index}    IN RANGE    1    ${int_of_members+1}
241     \    ClusterManagement__Include_Member_Index    ${index}    ${member_index_list}    ${session_list}    ${index_to_ip_mapping}
242     BuiltIn.Set_Suite_Variable    \${ClusterManagement__member_index_list}    ${member_index_list}
243     BuiltIn.Set_Suite_Variable    \${ClusterManagement__index_to_ip_mapping}    ${index_to_ip_mapping}
244     BuiltIn.Set_Suite_Variable    \${ClusterManagement__session_list}    ${session_list}
245
246 ClusterManagement__Include_Member_Index
247     [Arguments]    ${index}    ${member_index_list}    ${session_list}    ${index_to_ip_mapping}
248     [Documentation]    Add a corresponding item based on index into the last three arguments.
249     ...    Create the Http session whose alias is added to list.
250     Collections.Append_To_List    ${member_index_list}    ${index}
251     ${member_ip} =    BuiltIn.Set_Variable    ${ODL_SYSTEM_${index}_IP}
252     # ${index} is int (not string) so "key=value" syntax does not work in the following line.
253     Collections.Set_To_Dictionary    ${index_to_ip_mapping}    ${index}    ${member_ip}
254     # Http session, with ${AUTH}, without headers.
255     ${session_alias} =    Resolve_Http_Session_For_Member    member_index=${index}
256     RequestsLibrary.Create_Session    ${session_alias}    http://${member_ip}:${RESTCONFPORT}    auth=${AUTH}    max_retries=0
257     Collections.Append_To_List    ${session_list}    ${session_alias}