4c4473edb7f7c17725fb5912de253204f39c868f
[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,ip,user_data)
9 # that can be searched iteratively for a free slot
10 # TestPoolIP : look for a free IP address
11 # TestPoolQemu : look for a test_box with no qemu running
12 # e.g.
13 # pool = [ (hostname1,ip1,user_data1),  
14 #          (hostname2,ip2,user_data2),  
15 #          (hostname3,ip3,user_data2),  
16 #          (hostname4,ip4,user_data4) ]
17 # assuming that ip1 and ip3 are taken (pingable), then we'd get
18 # pool=TestPoolIP(pool)
19 # pool.next_free() -> entry2
20 # pool.next_free() -> entry4
21 # pool.next_free() -> None
22 # that is, even if ip2 is not busy/pingable when the second next_free() is issued
23
24 import commands
25 import utils
26
27 class TestPool:
28
29     def __init__ (self, pool, options,message):
30         self.pool=pool
31         self.options=options
32         self.busy=[]
33         self.message=message
34
35     # let's be flexible
36     def locate_entry (self, hostname_or_ip, busy=True):
37         for (h,i,u) in self.pool:
38             if h.find(hostname_or_ip)>=0  or (i and i.find(hostname_or_ip)>=0) :
39                 if busy:
40                     self.busy.append(h)
41                 return (h,i,u)
42         print '* TestPool.locate_entry: Could not locate entry for',hostname_or_ip
43         print '* in pool:'
44         for (h,i,u) in self.pool:
45             print "* \t",i,"\t",h
46         return None
47
48     def next_free (self):
49         if self.options.quiet:
50             print 'TestPool is looking for a %s'%self.message,
51         for (hostname,ip,user_data) in self.pool:
52             if hostname in self.busy:
53                 continue
54             if not self.options.quiet:
55                 utils.header('TestPool : checking %s'%hostname)
56             else:
57                 print '.',
58             if self.free_hostname(hostname):
59                 if not self.options.quiet:
60                     utils.header('%s is available'%hostname)
61                 else:
62                     print ''
63                 self.busy.append(hostname)
64                 return (hostname,ip,user_data)
65             else:
66                 self.busy.append(hostname)
67         raise Exception, "No space left in pool (%s)"%self.message
68
69 class TestPoolIP (TestPool):
70
71     def __init__ (self,pool,options):
72         TestPool.__init__(self,pool,options,"free IP address")
73
74     def free_hostname (self, hostname):
75         return not self.check_ping(hostname)
76
77 # OS-dependent ping option (support for macos, for convenience)
78     ping_timeout_option = None
79 # checks whether a given hostname/ip responds to ping
80     def check_ping (self,hostname):
81         if not TestPoolIP.ping_timeout_option:
82             (status,osname) = commands.getstatusoutput("uname -s")
83             if status != 0:
84                 raise Exception, "TestPool: Cannot figure your OS name"
85             if osname == "Linux":
86                 TestPoolIP.ping_timeout_option="-w"
87             elif osname == "Darwin":
88                 TestPoolIP.ping_timeout_option="-t"
89
90         if self.options.verbose:
91             utils.header ("TestPoolIP: pinging %s"%hostname)
92         command="ping -c 1 %s 1 %s"%(TestPoolIP.ping_timeout_option,hostname)
93         (status,output) = commands.getstatusoutput(command)
94         return status == 0
95
96 class TestPoolQemu (TestPool):
97     
98     def __init__ (self,pool,options):
99         TestPool.__init__(self,pool,options,"free qemu box")
100
101     def free_hostname (self, hostname):
102         return not self.busy_qemu(hostname)
103
104     # is there a qemu runing on that box already ?
105     def busy_qemu (self, hostname):
106         if self.options.verbose:
107             utils.header("TestPoolQemu: checking for running qemu instances on %s"%hostname)
108         command="ssh -o ConnectTimeout=5 root@%s ps -e -o cmd"%hostname
109         (status,output) = commands.getstatusoutput(command)
110         # if we fail to run that, let's assume we don't have ssh access, so
111         # we pretend the box is busy
112         if status!=0:
113             return True
114         elif output.find("qemu") >=0 :
115             return True
116         else:
117             return False