VERSION: Disable git snapshot for the 4.1.9 release.
[Samba.git] / selftest / target / __init__.py
blob4c95c74895cb884d0faf9e4b797b6128301a5c96
1 # target.py -- Targets
2 # Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org>
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.
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.
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.
20 """Selftest target management."""
22 __all__ = ['Target', 'Environment', 'EnvironmentManager']
25 class EnvironmentDown(Exception):
26 """Indicates an environment has gone down."""
28 def __init__(self, msg):
29 super(EnvironmentDown, self).__init__("environment went down: %s" % msg)
32 class UnsupportedEnvironment(Exception):
33 """Indicates a particular environment is not supported."""
35 def __init__(self, target, envname):
36 super(UnsupportedEnvironment, self).__init__(
37 "Target %s does not support environment %s" % (target, envname))
40 class Target(object):
41 """A target for Samba tests."""
43 def setup_env(self, name, prefix):
44 """Setup an environment.
46 :param name: name of the environment
47 :param prefix: directory to create it in
48 """
49 raise NotImplementedError(self.setup_env)
52 class Environment(object):
53 """An environment for Samba tests.
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 """
59 def check(self):
60 """Check if this environment is still up and running.
62 :return: Boolean indicating whether environment is still running
63 """
64 raise NotImplementedError(self.check)
66 def get_log(self):
67 """Retrieve the last log for this environment.
69 :return: String with log
70 """
71 raise NotImplementedError(self.get_log)
73 def teardown(self):
74 """Tear down an environment.
76 """
77 raise NotImplementedError(self.teardown)
79 def get_vars(self):
80 """Retrieve the environment variables for this environment.
82 :return: Dictionary with string -> string values
83 """
84 raise NotImplementedError(self.get_vars)
87 class NoneEnvironment(Environment):
88 """Empty environment.
89 """
91 def check(self):
92 return True
94 def get_log(self):
95 return ""
97 def teardown(self):
98 return
100 def get_vars(self):
101 return {}
104 class NoneTarget(Target):
105 """Target that can only provide the 'none' environment."""
107 name = "none"
109 def setup_env(self, envname, prefix):
110 raise UnsupportedEnvironment(self.name, envname)
113 class EnvironmentManager(object):
114 """Manager of environments."""
116 def __init__(self, target):
117 self.target = target
118 self.running_envs = {}
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)
126 def getlog_env(self, envname):
127 env = self.get_running_env(envname)
128 return env.get_log()
130 def check_env(self, envname):
131 """Check if an environment is still up.
133 :param envname: Environment to check
135 env = self.get_running_env(envname)
136 return env.check()
138 def teardown_env(self, envname):
139 """Tear down an environment.
141 :param envname: Name of the environment
143 env = self.get_running_env(envname)
144 env.teardown()
145 del self.running_envs[envname]
147 def teardown_all(self):
148 """Teardown all environments."""
149 for env in self.running_envs.iterkeys():
150 self.teardown_env(env)
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
159 env = self.target.setup_env(envname, prefix)
160 if env is None:
161 return None
163 self.running_envs[envname] = env
165 return env