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
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,
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
))
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
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.
60 """Check if this environment is still up and running.
62 :return: Boolean indicating whether environment is still running
64 raise NotImplementedError(self
.check
)
67 """Retrieve the last log for this environment.
69 :return: String with log
71 raise NotImplementedError(self
.get_log
)
74 """Tear down an environment.
77 raise NotImplementedError(self
.teardown
)
80 """Retrieve the environment variables for this environment.
82 :return: Dictionary with string -> string values
84 raise NotImplementedError(self
.get_vars
)
87 class NoneEnvironment(Environment
):
104 class NoneTarget(Target
):
105 """Target that can only provide the 'none' environment."""
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
):
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
)
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
)
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
)
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())
159 env
= self
.target
.setup_env(envname
, prefix
)
163 self
.running_envs
[envname
] = env