Merge lp:~canonical-platform-qa/ubuntu-ui-toolkit/fixture_unset_env into lp:ubuntu-ui-toolkit/staging

Proposed by Leo Arias on 2015-05-12
Status: Merged
Approved by: Christian Dywan on 2015-07-06
Approved revision: 1517
Merged at revision: 1548
Proposed branch: lp:~canonical-platform-qa/ubuntu-ui-toolkit/fixture_unset_env
Merge into: lp:ubuntu-ui-toolkit/staging
Diff against target: 264 lines (+87/-144)
2 files modified
tests/autopilot/ubuntuuitoolkit/fixture_setup.py (+6/-2)
tests/autopilot/ubuntuuitoolkit/tests/test_fixture_setup.py (+81/-142)
To merge this branch: bzr merge lp:~canonical-platform-qa/ubuntu-ui-toolkit/fixture_unset_env
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve on 2015-07-06
Zoltan Balogh (community) 2015-05-12 Approve on 2015-06-30
Vincent Ladeuil (community) 2015-05-12 Approve on 2015-05-15
Review via email: mp+258906@code.launchpad.net

Commit Message

In the initctl test fixture, add the option to unset an environment variable.

To post a comment you must log in.
Leo Arias (elopio) wrote :

I've pushed an updated version. Thanks!

Vincent Ladeuil (vila) wrote :

Scenarios keys, while descriptive, should stay short, see inline comment.

On the overall, and I realize this MP doesn't introduce the issue, the scenarios look far too complex.

https://bazaar.launchpad.net/~canonical-platform-qa/uci-tests/trunk/view/head:/ucitests/tests/test_fixtures.py TestEnv attempts to test something similar with far less code and no scenarios.

May be I'm missing something about what is tested in this MP but the difference between the two is worth discussing.

review: Needs Fixing
Leo Arias (elopio) wrote :

Yes, I didn't like to write this test. I have an idea that might make it better. Back to work in progress.

Leo Arias (elopio) wrote :

Back to needs review. Please take another look vila.

Vincent Ladeuil (vila) wrote :

Far better !

There is still a lot of duplicated code in the tests that should either go to setUp or specific helpers, mostly around defining and running the inner test and check it was successful but the test setup itself could benefit from a helper to highlight the test specifics more clearly (setting or unsetting the env var IIRC).

I tried to find how to run the tests locally before doing that refactoring myself but get lost >-/ Can you help ?

Overall it's already pretty clear so you can land as is but I think that can be made crystal clear without too much effort.

review: Approve
Leo Arias (elopio) wrote :

Yes, you don't need to do much as this is only python:

bzr branch lp:~canonical-platform-qa/ubuntu-ui-toolkit/fixture_unset_env
cd fixture_unset_env/tests/autopilot
autopilot3 run ubuntuuitoolkit.tests.test_fixture_setup

I want crystal clear, so please show me the refactorings you have in mind if you have some time.

Vincent Ladeuil (vila) wrote :

@Leo: I've pushed the refactoring I was talking about, feel free to revisit.

The resulting diff is not that clear so looking at the branch history may be easier to understand how I got there.

The end result really focus on what the tests do, pushing down (and sharing !) the implementation details into the helpers.

The tests now can ignore that 'testenvvarforfixture' is used internally and focus on what happens to the variable.

This also ensures that all the tests share the details on how they test without the risk of divergence if the tests are modified later.

Funnily enough, this refactoring revealed that one addCleanup() call was useless (see history).

Leo Arias (elopio) wrote :

Wow, 3 lines and 4 lines. Thanks Vila!

Zoltan Balogh (bzoltan) :
review: Approve
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/autopilot/ubuntuuitoolkit/fixture_setup.py'
2--- tests/autopilot/ubuntuuitoolkit/fixture_setup.py 2015-04-14 21:02:06 +0000
3+++ tests/autopilot/ubuntuuitoolkit/fixture_setup.py 2015-05-15 10:13:47 +0000
4@@ -127,8 +127,12 @@
5 super().setUp()
6 for variable, value in self.variables.items():
7 self._add_variable_cleanup(variable)
8- self.environment.set_initctl_env_var(
9- variable, value, global_=self.global_)
10+ if value is None:
11+ self.environment.unset_initctl_env_var(
12+ variable, global_=self.global_)
13+ else:
14+ self.environment.set_initctl_env_var(
15+ variable, value, global_=self.global_)
16
17 def _add_variable_cleanup(self, variable):
18 if self.environment.is_initctl_env_var_set(
19
20=== modified file 'tests/autopilot/ubuntuuitoolkit/tests/test_fixture_setup.py'
21--- tests/autopilot/ubuntuuitoolkit/tests/test_fixture_setup.py 2015-04-14 21:02:06 +0000
22+++ tests/autopilot/ubuntuuitoolkit/tests/test_fixture_setup.py 2015-05-15 10:13:47 +0000
23@@ -170,160 +170,99 @@
24 self.application.select_single('Label', objectName='testLabel')
25
26
27-class InitctlEnvironmentVariableTestCase(testtools.TestCase):
28-
29- def test_use_initctl_environment_variable_with_unset_variable(self):
30+class InitctlEnvironmentVariableTestCase(testscenarios.TestWithScenarios):
31+
32+ scenarios = [
33+ ('global_variable', {'global_': True}),
34+ ('local_variable', {'global_': False})
35+ ]
36+
37+ def set_original_value(self, value):
38+ self.addCleanup(
39+ environment.unset_initctl_env_var, 'testenvvarforfixture',
40+ global_=self.global_)
41+ environment.set_initctl_env_var('testenvvarforfixture',
42+ value, global_=self.global_)
43+
44+ def create_fixture(self, value):
45+ self.initctl_env_var = fixture_setup.InitctlEnvironmentVariable(
46+ testenvvarforfixture=value, global_=self.global_)
47+
48+ def assertValueIs(self, expected_value):
49+ self.assertEqual(
50+ expected_value,
51+ environment.get_initctl_env_var(
52+ 'testenvvarforfixture', global_=self.global_))
53+
54+ def assertVariableIsNotSet(self):
55+ self.assertFalse(
56+ environment.is_initctl_env_var_set(
57+ 'testenvvarforfixture', global_=self.global_))
58+
59+ def assertTestIsSuccessful(self, expected_value, test_name):
60+ result = testtools.TestResult()
61+
62+ class TestWithInitctlEnvVar(testtools.TestCase):
63+ def setUp(inner):
64+ super().setUp()
65+ inner.useFixture(self.initctl_env_var)
66+
67+ def test_value_set(inner):
68+ self.assertValueIs(expected_value)
69+
70+ def test_variable_not_set(inner):
71+ self.assertVariableIsNotSet()
72+
73+ TestWithInitctlEnvVar(test_name).run(result)
74+ self.assertTrue(
75+ result.wasSuccessful(), 'Failed to set the environment variable.')
76+
77+ def test_use_initctl_environment_variable_to_set_unexisting_variable(self):
78 """Test the initctl env var fixture when the var is unset.
79
80 During the test, the new value must be in place.
81 After the test, the variable must be unset again.
82
83 """
84- initctl_env_var = fixture_setup.InitctlEnvironmentVariable(
85- testenvvarforfixture='test value')
86-
87- result = testtools.TestResult()
88-
89- def inner_test():
90- class TestWithInitctlEnvVar(testtools.TestCase):
91- def test_it(self):
92- self.useFixture(initctl_env_var)
93- self.assertEqual(
94- 'test value',
95- environment.get_initctl_env_var(
96- 'testenvvarforfixture'))
97- return TestWithInitctlEnvVar('test_it')
98-
99- inner_test().run(result)
100-
101- self.assertTrue(
102- result.wasSuccessful(), 'Failed to set the environment variable.')
103- self.assertFalse(
104- environment.is_initctl_env_var_set('testenvvarforfixture'))
105-
106- def test_use_initctl_environment_variable_with_set_variable(self):
107+ self.create_fixture('test value')
108+ self.assertTestIsSuccessful('test value', 'test_value_set')
109+ self.assertVariableIsNotSet()
110+
111+ def test_use_initctl_environment_variable_to_set_existing_variable(self):
112 """Test the initctl env var fixture when the var is unset.
113
114 During the test, the new value must be in place.
115 After the test, the old value must be set again.
116
117 """
118- self.addCleanup(
119- environment.unset_initctl_env_var, 'testenvvarforfixture')
120- environment.set_initctl_env_var(
121- 'testenvvarforfixture', 'original test value')
122-
123- initctl_env_var = fixture_setup.InitctlEnvironmentVariable(
124- testenvvarforfixture='new test value')
125-
126- result = testtools.TestResult()
127-
128- def inner_test():
129- class TestWithInitctlEnvVar(testtools.TestCase):
130- def test_it(self):
131- self.useFixture(initctl_env_var)
132- self.assertEqual(
133- 'new test value',
134- environment.get_initctl_env_var(
135- 'testenvvarforfixture'))
136- return TestWithInitctlEnvVar('test_it')
137-
138- inner_test().run(result)
139-
140- self.assertTrue(
141- result.wasSuccessful(), 'Failed to set the environment variable.')
142- self.assertEqual(
143- 'original test value',
144- environment.get_initctl_env_var('testenvvarforfixture'))
145-
146-
147-class InitctlGlobalEnvironmentVariableTestCase(
148- testscenarios.TestWithScenarios):
149-
150- scenarios = [
151- ('global unset variable', {
152- 'is_variable_set': False,
153- 'variable_value': 'dummy',
154- 'global_value': 'value',
155- 'expected_calls': [
156- mock.call.is_initctl_env_var_set(
157- 'testenvvarforfixture', global_='value'),
158- mock.call.set_initctl_env_var(
159- 'testenvvarforfixture', 'new test value', global_='value'),
160- mock.call.unset_initctl_env_var(
161- 'testenvvarforfixture', global_='value')
162- ]
163- }),
164- ('global set variable', {
165- 'is_variable_set': True,
166- 'variable_value': 'original_value',
167- 'global_value': 'value',
168- 'expected_calls': [
169- mock.call.is_initctl_env_var_set(
170- 'testenvvarforfixture', global_='value'),
171- mock.call.get_initctl_env_var(
172- 'testenvvarforfixture', global_='value'),
173- mock.call.set_initctl_env_var(
174- 'testenvvarforfixture', 'new test value', global_='value'),
175- mock.call.set_initctl_env_var(
176- 'testenvvarforfixture', 'original_value', global_='value')
177- ]
178- }),
179- ('default unset variable', {
180- 'is_variable_set': False,
181- 'variable_value': 'dummy',
182- 'global_value': 'default',
183- 'expected_calls': [
184- mock.call.is_initctl_env_var_set(
185- 'testenvvarforfixture', global_=False),
186- mock.call.set_initctl_env_var(
187- 'testenvvarforfixture', 'new test value', global_=False),
188- mock.call.unset_initctl_env_var(
189- 'testenvvarforfixture', global_=False)
190- ]
191- }),
192- ('global set variable', {
193- 'is_variable_set': True,
194- 'variable_value': 'original_value',
195- 'global_value': 'default',
196- 'expected_calls': [
197- mock.call.is_initctl_env_var_set(
198- 'testenvvarforfixture', global_=False),
199- mock.call.get_initctl_env_var(
200- 'testenvvarforfixture', global_=False),
201- mock.call.set_initctl_env_var(
202- 'testenvvarforfixture', 'new test value', global_=False),
203- mock.call.set_initctl_env_var(
204- 'testenvvarforfixture', 'original_value', global_=False)
205- ]
206- }),
207- ]
208-
209- def test_use_initctl_environment_variable_with_global_unset_variable(self):
210- if self.global_value == 'default':
211- initctl_env_var = fixture_setup.InitctlEnvironmentVariable(
212- testenvvarforfixture='new test value')
213- else:
214- initctl_env_var = fixture_setup.InitctlEnvironmentVariable(
215- testenvvarforfixture='new test value',
216- global_=self.global_value)
217-
218- mock_env = mock.Mock()
219- initctl_env_var.environment = mock_env
220- mock_env.is_initctl_env_var_set.return_value = self.is_variable_set
221- mock_env.get_initctl_env_var.return_value = self.variable_value
222-
223- def inner_test():
224- class TestWithInitctlEnvVar(testtools.TestCase):
225- def test_it(self):
226- self.useFixture(initctl_env_var)
227-
228- return TestWithInitctlEnvVar('test_it')
229-
230- inner_test().run()
231-
232- self.assertEquals(
233- self.expected_calls, mock_env.mock_calls)
234+ self.set_original_value('original test value')
235+ self.create_fixture('new test value')
236+ self.assertTestIsSuccessful('new test value', 'test_value_set')
237+ self.assertValueIs('original test value')
238+
239+ def test_use_initctl_environment_variable_to_unset_existing_variable(self):
240+ """Test the initctl env var fixture to unset a variable.
241+
242+ During the test, the variable must be unset.
243+ After the test, the old value must be set again.
244+
245+ """
246+ self.set_original_value('original test value')
247+ self.create_fixture(None)
248+ self.assertTestIsSuccessful(None, 'test_variable_not_set')
249+ self.assertValueIs('original test value',)
250+
251+ def test_use_initctl_environment_variable_to_unset_nonexisting_variable(
252+ self):
253+ """Test the initctl env var fixture to unset a variable.
254+
255+ During the test, the variable must be unset.
256+ After the test, the variable must remain unset.
257+
258+ """
259+ self.create_fixture(None)
260+ self.assertTestIsSuccessful(None, 'test_variable_not_set')
261+ self.assertVariableIsNotSet()
262
263
264 class FakeHomeTestCase(testtools.TestCase):

Subscribers

People subscribed via source and target branches