Update Cluster State Library to report all states.
[integration/test.git] / test / csit / libraries / ClusterStateLibrary.py
index 615c5c185b9283337476d31fea10d11d770b52c2..58240e084f04cae818dddbab284bf68e1d49bca7 100644 (file)
@@ -10,7 +10,7 @@ import json
 import sys
 
 
-def getClusterRoles(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips):
+def getClusterRoles(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=3, port=8181, *ips):
     """Given a shardname (e.g. shard-inventory-config), number of shards and bunch of ips
 
     determines what role each ip has in an Akka (Raft based) cluster
@@ -21,16 +21,18 @@ def getClusterRoles(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesI
     for ip in ips:
         i = 1
         dict[ip] = None
-
+        print "numOfShards => " + str(numOfShards)
         while i <= numOfShards:
             shardMemberName = "member-" + str(i) + "-" + shardName
             j = 1
-
-            while j <= numOfTries:
+            print 'j => ' + str(j)
+            print 'numOfTries => ' + str(numOfTries)
+            while int(j) <= int(numOfTries):
                 print("Try number " + str(j))
                 try:
-                    print("finding if" + ip + "is leader for shardName =" + shardMemberName)
+                    print("getting role of " + ip + "  for shardName = " + shardMemberName)
                     url = SettingsLibrary.getJolokiaURL(ip, str(port), str(i), shardName)
+                    print url
                     resp = UtilLibrary.get(url)
                     print(resp)
                     if resp.status_code != 200:
@@ -41,11 +43,7 @@ def getClusterRoles(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesI
                     if 'value' in data:
                         dataValue = data['value']
                         print("datavalue RaftState is", dataValue['RaftState'])
-                        if dataValue['RaftState'] == 'Follower':
-                            dict[ip] = 'Follower'
-                            break
-                        elif dataValue['RaftState'] == 'Leader':
-                            dict[ip] = 'Leader'
+                        dict[ip] = dataValue['RaftState']
                 except:
                     e = sys.exc_info()[0]
                     print("Try" + str(j) + ":An error occurred when finding leader on" + ip +
@@ -55,21 +53,22 @@ def getClusterRoles(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesI
                     continue
                 finally:
                     j = j + 1
-
             if dict[ip] is not None:
                 break
             i = i + 1
-
     return dict
 
 
-def isLeader(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress):
-    """Given a shardname (e.g. shard-inventory-config), number of shards and an ip determines if its a leader"""
+def isRole(role,  shardName, ipAddress, numOfShards=3, numOfRetries=1, sleepFor=3, port=8181):
+    """Given a role (Leader, Follower, Candidate, or IsolatedLeader),
+    shardname (e.g. shard-inventory-config), controller IP address,
+    and number of shards on the controller,this function determines if the controller,
+    has that role for the specified shard.
+    """
     ip = getClusterRoles(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress)
     print(ip)
-    if ip[ipAddress] == 'Leader':
+    if ip[ipAddress] == role:
         return True
-
     return False
 
 
@@ -80,7 +79,6 @@ def getLeader(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=
         for ip in dict.keys():
             if dict[ip] == 'Leader':
                 return ip
-
     return None
 
 
@@ -97,29 +95,18 @@ def getFollowers(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSe
         if (len(result) == (len(ips) - 1)):
             break
         sleep(1)
-
     return result
 
 
-def isFollower(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress):
-    """Given a shardname (e.g. shard-inventory-config), number of shards and an ip determines if its a leader"""
-    ip = getClusterRoles(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress)
-    print(ip)
-    if ip[ipAddress] == 'Follower':
-        return True
-
-    return False
-
-
 def testGetClusterRoles():
     dict = getClusterRoles("shard-inventory-config", 3, 1, 1, 8181,
                            "10.194.126.116", "10.194.126.117", "10.194.126.118")
     print(dict)
 
     for ip in dict.keys():
-        if isLeader("shard-inventory-config", 3, 1, 1, 8181, ip):
+        if isRole("Leader", "shard-inventory-config", 3, 1, 1, 8181, ip):
             print(ip + " is Leader")
-        elif isFollower("shard-inventory-config", 3, 1, 1, 8181, ip):
+        elif isRole("Follower", "shard-inventory-config", 3, 1, 1, 8181, ip):
             print(ip + " is follower")
         else:
             print(ip + " seems to have value " + str(dict[ip]))