ef52a0d2b8006089051603408d387d3e1eb9bb80
[tests.git] / system / TestPool.py
1 #
2 # Thierry Parmentelat - INRIA Sophia Antipolis 
3 #
4 # pool class
5
6 # allows to pick an available IP among a pool
7 #
8 # input is expressed as a list of tuples ('hostname_or_ip',user_data)
9 # can be searched iteratively
10 # e.g.
11 # pool = [ (hostname1,ip1,user_data1),  (hostname2,ip2,user_data2),  
12 #          (hostname3,ip3,user_data2),  (hostname4,ip4,user_data4) ]
13 # assuming that ip1 and ip3 are taken (pingable), then we'd get
14 # pool=TestPool(pool)
15 # pool.next_free() -> entry2
16 # pool.next_free() -> entry4
17 # pool.next_free() -> None
18 # that is, even if ip2 is not busy/pingable when the second next_free() is issued
19
20 import commands
21 import utils
22
23 class TestPool:
24
25     def __init__ (self, pool, options):
26         self.pool=pool
27         self.options=options
28         self.busy=[]
29
30     # let's be flexible
31     def locate (self, hostname_or_ip, busy=False):
32         for (h,i,u) in self.pool:
33             if h.find(hostname_or_ip)>=0  or i.find(hostname_or_ip)>=0 :
34                 if busy:
35                     self.busy.append(h)
36                 return (h,i,u)
37         return None
38
39     def next_free (self):
40         # if preferred is provided, let's re-order
41         for (host,ip,user_data) in self.pool:
42             if host in self.busy:
43                 continue
44             utils.header('TestPool : checking %s'%host)
45             if not TestPool.check_ping (host):
46                 utils.header('%s is available'%host)
47                 self.busy.append(host)
48                 return (host,ip,user_data)
49             else:
50                 self.busy.append(host)
51         return None
52
53 # OS-dependent ping option (support for macos, for convenience)
54     ping_timeout_option = None
55 # checks whether a given hostname/ip responds to ping
56     @staticmethod
57     def check_ping (hostname):
58         if not TestPool.ping_timeout_option:
59             (status,osname) = commands.getstatusoutput("uname -s")
60             if status != 0:
61                 raise Exception, "TestPool: Cannot figure your OS name"
62             if osname == "Linux":
63                 TestPool.ping_timeout_option="-w"
64             elif osname == "Darwin":
65                 TestPool.ping_timeout_option="-t"
66
67         command="ping -c 1 %s 1 %s"%(TestPool.ping_timeout_option,hostname)
68         (status,output) = commands.getstatusoutput(command)
69         return status == 0