make perftest: for performance testing
[sfrench/samba-autobuild/.git] / selftest / tests / test_target.py
1 # test_target.py -- The tests for selftest target code
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 """Tests for selftest.target."""
21
22 from selftest.target import (
23     Environment,
24     EnvironmentDown,
25     EnvironmentManager,
26     NoneEnvironment,
27     NoneTarget,
28     Target,
29     UnsupportedEnvironment,
30     )
31
32 from samba.tests import TestCase
33
34
35 class DummyEnvironment(Environment):
36
37     def __init__(self, name, prefix):
38         self.name = name
39         self.prefix = prefix
40         self.check_ret = True
41         self.log_ret = ""
42
43     def check(self):
44         return self.check_ret
45
46     def get_log(self):
47         return self.log_ret
48
49
50 class DummyTarget(Target):
51
52     def setup_env(self, name, prefix):
53         return DummyEnvironment(name, prefix)
54
55
56 class NoneEnvironmentTests(TestCase):
57
58     def setUp(self):
59         super(NoneEnvironmentTests, self).setUp()
60         self.env = NoneEnvironment()
61
62     def test_get_vars(self):
63         self.assertEquals({}, self.env.get_vars())
64
65     def test_check(self):
66         self.assertEquals(True, self.env.check())
67
68     def test_get_log(self):
69         self.assertEquals("", self.env.get_log())
70
71
72 class NoneTargetTests(TestCase):
73
74     def setUp(self):
75         super(NoneTargetTests, self).setUp()
76         self.target = NoneTarget()
77
78     def test_setup_env(self):
79         self.assertRaises(UnsupportedEnvironment, self.target.setup_env,
80             "something", "prefx")
81
82
83 class EnvironmentManagerTests(TestCase):
84
85     def setUp(self):
86         super(EnvironmentManagerTests, self).setUp()
87         self.mgr = EnvironmentManager(DummyTarget())
88
89     def test_none(self):
90         self.assertIs(
91             NoneEnvironment, type(self.mgr.setup_env("none", "prefix")))
92
93     def test_setup(self):
94         env = self.mgr.setup_env("something", "prefix")
95         self.assertEquals(env.name, "something")
96         self.assertEquals(env.prefix, "prefix")
97
98     def test_setup_reuses(self):
99         env1 = self.mgr.setup_env("something", "prefix")
100         env2 = self.mgr.setup_env("something", "prefix")
101         self.assertIs(env1, env2)
102
103     def test_setup_down(self):
104         env1 = self.mgr.setup_env("something", "prefix")
105         env1.check_ret = False
106         self.assertRaises(EnvironmentDown, self.mgr.setup_env, "something", "")
107
108     def test_check(self):
109         env = self.mgr.setup_env("something", "prefix")
110         self.assertTrue(env.check())
111         self.assertTrue(self.mgr.check_env("something"))
112         env.check_ret = False
113         self.assertFalse(env.check())
114         self.assertFalse(self.mgr.check_env("something"))
115
116     def test_get_log(self):
117         env = self.mgr.setup_env("something", "prefix")
118         self.assertEquals("", env.get_log())
119         self.assertEquals("", self.mgr.getlog_env("something"))
120         env.log_ret = 'bla'
121         self.assertEquals('bla', env.get_log())
122         self.assertEquals('bla', self.mgr.getlog_env("something"))
123
124     def test_get_running_env(self):
125         env = self.mgr.setup_env("something", "prefix")
126         self.assertIs(env, self.mgr.get_running_env("something"))
127
128     def test_get_running_env_nonexistent(self):
129         self.assertIs(None, self.mgr.get_running_env("something"))