selftest: Give Samba4 processes a little longer to clean up
[nivanova/samba-autobuild/.git] / selftest / target / __init__.py
1 # target.py -- Targets
2 # Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 3
7 # of the License or (at your option) any later version of
8 # the License.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA  02110-1301, USA.
19
20 """Selftest target management."""
21
22 __all__ = ['Target', 'Environment', 'EnvironmentManager']
23
24
25 class EnvironmentDown(Exception):
26     """Indicates an environment has gone down."""
27
28     def __init__(self, msg):
29         super(EnvironmentDown, self).__init__("environment went down: %s" % msg)
30
31
32 class UnsupportedEnvironment(Exception):
33     """Indicates a particular environment is not supported."""
34
35     def __init__(self, target, envname):
36         super(UnsupportedEnvironment, self).__init__(
37             "Target %s does not support environment %s" % (target, envname))
38
39
40 class Target(object):
41     """A target for Samba tests."""
42
43     def setup_env(self, name, prefix):
44         """Setup an environment.
45
46         :param name: name of the environment
47         :param prefix: directory to create it in
48         """
49         raise NotImplementedError(self.setup_env)
50
51
52 class Environment(object):
53     """An environment for Samba tests.
54
55     Tests often need to run against a server with particular things set up,
56     a "environment". This environment is provided by the test target.
57     """
58
59     def check(self):
60         """Check if this environment is still up and running.
61
62         :return: Boolean indicating whether environment is still running
63         """
64         raise NotImplementedError(self.check)
65
66     def get_log(self):
67         """Retrieve the last log for this environment.
68
69         :return: String with log
70         """
71         raise NotImplementedError(self.get_log)
72
73     def teardown(self):
74         """Tear down an environment.
75
76         """
77         raise NotImplementedError(self.teardown)
78
79     def get_vars(self):
80         """Retrieve the environment variables for this environment.
81
82         :return: Dictionary with string -> string values
83         """
84         raise NotImplementedError(self.get_vars)
85
86
87 class NoneEnvironment(Environment):
88     """Empty environment.
89     """
90
91     def check(self):
92         return True
93
94     def get_log(self):
95         return ""
96
97     def teardown(self):
98         return
99
100     def get_vars(self):
101         return {}
102
103
104 class NoneTarget(Target):
105     """Target that can only provide the 'none' environment."""
106
107     name = "none"
108
109     def setup_env(self, envname, prefix):
110         raise UnsupportedEnvironment(self.name, envname)
111
112
113 class EnvironmentManager(object):
114     """Manager of environments."""
115
116     def __init__(self, target):
117         self.target = target
118         self.running_envs = {}
119
120     def get_running_env(self, name):
121         envname = name.split(":")[0]
122         if envname == "none":
123             return NoneEnvironment()
124         return self.running_envs.get(envname)
125
126     def getlog_env(self, envname):
127         env = self.get_running_env(envname)
128         return env.get_log()
129
130     def check_env(self, envname):
131         """Check if an environment is still up.
132
133         :param envname: Environment to check
134         """
135         env = self.get_running_env(envname)
136         return env.check()
137
138     def teardown_env(self, envname):
139         """Tear down an environment.
140
141         :param envname: Name of the environment
142         """
143         env = self.get_running_env(envname)
144         env.teardown()
145         del self.running_envs[envname]
146
147     def teardown_all(self):
148         """Teardown all environments."""
149         for env in self.running_envs.iterkeys():
150             self.teardown_env(env)
151
152     def setup_env(self, envname, prefix):
153         running_env = self.get_running_env(envname)
154         if running_env is not None:
155             if not running_env.check():
156                 raise EnvironmentDown(running_env.get_log())
157             return running_env
158
159         env = self.target.setup_env(envname, prefix)
160         if env is None:
161             return None
162
163         self.running_envs[envname] = env
164
165         return env