| OLD | NEW |
| 1 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import json | 5 import json |
| 6 import os.path | 6 import os.path |
| 7 import shutil | 7 import shutil |
| 8 import tempfile | 8 import tempfile |
| 9 import unittest | 9 import unittest |
| 10 | 10 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 self.mock_ownservice = self.mock_ownservice_ctor.return_value | 29 self.mock_ownservice = self.mock_ownservice_ctor.return_value |
| 30 self.mock_ownservice.start.return_value = True | 30 self.mock_ownservice.start.return_value = True |
| 31 self.mock_ownservice.has_version_changed.return_value = False | 31 self.mock_ownservice.has_version_changed.return_value = False |
| 32 | 32 |
| 33 self.cw = config_watcher.ConfigWatcher( | 33 self.cw = config_watcher.ConfigWatcher( |
| 34 self.config_directory, | 34 self.config_directory, |
| 35 42, | 35 42, |
| 36 43, | 36 43, |
| 37 '/state', | 37 '/state', |
| 38 '/rootdir', | 38 '/rootdir', |
| 39 '/cloudtail', |
| 39 sleep_fn=self.mock_sleep) | 40 sleep_fn=self.mock_sleep) |
| 40 | 41 |
| 41 def tearDown(self): | 42 def tearDown(self): |
| 42 mock.patch.stopall() | 43 mock.patch.stopall() |
| 43 | 44 |
| 44 shutil.rmtree(self.config_directory) | 45 shutil.rmtree(self.config_directory) |
| 45 | 46 |
| 46 def _set_config(self, name, contents, mtime=None): | 47 def _set_config(self, name, contents, mtime=None): |
| 47 filename = os.path.join(self.config_directory, name) | 48 filename = os.path.join(self.config_directory, name) |
| 48 with open(filename, 'w') as fh: | 49 with open(filename, 'w') as fh: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 63 self.mock_ownservice.has_version_changed.return_value = True | 64 self.mock_ownservice.has_version_changed.return_value = True |
| 64 | 65 |
| 65 self.cw._iteration() | 66 self.cw._iteration() |
| 66 self.assertTrue(self.cw._stop) | 67 self.assertTrue(self.cw._stop) |
| 67 | 68 |
| 68 def test_add(self): | 69 def test_add(self): |
| 69 self._set_config('foo.json', '{"name": "foo"}') | 70 self._set_config('foo.json', '{"name": "foo"}') |
| 70 | 71 |
| 71 self.cw._iteration() | 72 self.cw._iteration() |
| 72 | 73 |
| 73 self.mock_thread_ctor.assert_called_once_with(43, '/state', {'name': 'foo'}) | 74 self.mock_thread_ctor.assert_called_once_with( |
| 75 43, '/state', {'name': 'foo'}, '/cloudtail') |
| 74 self.mock_thread.start.assert_called_once_with() | 76 self.mock_thread.start.assert_called_once_with() |
| 75 self.mock_thread.start_service.assert_called_once_with() | 77 self.mock_thread.start_service.assert_called_once_with() |
| 76 | 78 |
| 77 def test_add_invalid_json(self): | 79 def test_add_invalid_json(self): |
| 78 self._set_config('foo.json', '{"name": ') | 80 self._set_config('foo.json', '{"name": ') |
| 79 | 81 |
| 80 self.cw._iteration() | 82 self.cw._iteration() |
| 81 | 83 |
| 82 self.assertFalse(self.mock_thread_ctor.called) | 84 self.assertFalse(self.mock_thread_ctor.called) |
| 83 | 85 |
| 84 def test_add_filename_does_not_match_name(self): | 86 def test_add_filename_does_not_match_name(self): |
| 85 self._set_config('foo.json', '{"name": "bar"}') | 87 self._set_config('foo.json', '{"name": "bar"}') |
| 86 | 88 |
| 87 self.cw._iteration() | 89 self.cw._iteration() |
| 88 | 90 |
| 89 self.mock_thread_ctor.assert_called_once_with(43, '/state', {'name': 'bar'}) | 91 self.mock_thread_ctor.assert_called_once_with( |
| 92 43, '/state', {'name': 'bar'}, '/cloudtail') |
| 90 self.mock_thread.start.assert_called_once_with() | 93 self.mock_thread.start.assert_called_once_with() |
| 91 self.mock_thread.start_service.assert_called_once_with() | 94 self.mock_thread.start_service.assert_called_once_with() |
| 92 | 95 |
| 93 def test_add_duplicate_name(self): | 96 def test_add_duplicate_name(self): |
| 94 self._set_config('foo.json', '{"name": "foo"}') | 97 self._set_config('foo.json', '{"name": "foo"}') |
| 95 self.cw._iteration() | 98 self.cw._iteration() |
| 96 self.assertEqual(1, self.mock_thread_ctor.call_count) | 99 self.assertEqual(1, self.mock_thread_ctor.call_count) |
| 97 | 100 |
| 98 self._set_config('bar.json', '{"name": "foo"}') | 101 self._set_config('bar.json', '{"name": "foo"}') |
| 99 self.cw._iteration() | 102 self.cw._iteration() |
| 100 self.assertEqual(1, self.mock_thread_ctor.call_count) | 103 self.assertEqual(1, self.mock_thread_ctor.call_count) |
| 101 | 104 |
| 102 def test_change(self): | 105 def test_change(self): |
| 103 self._set_config('foo.json', '{"name": "foo"}', 100) | 106 self._set_config('foo.json', '{"name": "foo"}', 100) |
| 104 | 107 |
| 105 self.cw._iteration() | 108 self.cw._iteration() |
| 106 self.mock_thread_ctor.assert_called_once_with(43, '/state', {'name': 'foo'}) | 109 self.mock_thread_ctor.assert_called_once_with( |
| 110 43, '/state', {'name': 'foo'}, '/cloudtail') |
| 107 | 111 |
| 108 self._set_config('foo.json', '{"name": "foo", "args": [1, 2, 3]}', 200) | 112 self._set_config('foo.json', '{"name": "foo", "args": [1, 2, 3]}', 200) |
| 109 | 113 |
| 110 self.cw._iteration() | 114 self.cw._iteration() |
| 111 self.mock_thread.restart_with_new_config.assert_called_once_with( | 115 self.mock_thread.restart_with_new_config.assert_called_once_with( |
| 112 {'name': 'foo', 'args': [1, 2, 3]}) | 116 {'name': 'foo', 'args': [1, 2, 3]}) |
| 113 | 117 |
| 114 def test_change_duplicate_name(self): | 118 def test_change_duplicate_name(self): |
| 115 self._set_config('foo.json', '{"name": "foo"}', 100) | 119 self._set_config('foo.json', '{"name": "foo"}', 100) |
| 116 self.cw._iteration() | 120 self.cw._iteration() |
| (...skipping 15 matching lines...) Expand all Loading... |
| 132 | 136 |
| 133 def test_add_bad_config_then_change(self): | 137 def test_add_bad_config_then_change(self): |
| 134 self._set_config('foo.json', '{"name": ', 100) | 138 self._set_config('foo.json', '{"name": ', 100) |
| 135 | 139 |
| 136 self.cw._iteration() | 140 self.cw._iteration() |
| 137 self.assertFalse(self.mock_thread_ctor.called) | 141 self.assertFalse(self.mock_thread_ctor.called) |
| 138 | 142 |
| 139 self._set_config('foo.json', '{"name": "foo"}', 200) | 143 self._set_config('foo.json', '{"name": "foo"}', 200) |
| 140 | 144 |
| 141 self.cw._iteration() | 145 self.cw._iteration() |
| 142 self.mock_thread_ctor.assert_called_once_with(43, '/state', {'name': 'foo'}) | 146 self.mock_thread_ctor.assert_called_once_with( |
| 147 43, '/state', {'name': 'foo'}, '/cloudtail') |
| 143 self.mock_thread.start.assert_called_once_with() | 148 self.mock_thread.start.assert_called_once_with() |
| 144 self.mock_thread.start_service.assert_called_once_with() | 149 self.mock_thread.start_service.assert_called_once_with() |
| 145 | 150 |
| 146 def test_add_bad_config_then_remove(self): | 151 def test_add_bad_config_then_remove(self): |
| 147 self._set_config('foo.json', '{"name": ', 100) | 152 self._set_config('foo.json', '{"name": ', 100) |
| 148 | 153 |
| 149 self.cw._iteration() | 154 self.cw._iteration() |
| 150 self.assertFalse(self.mock_thread_ctor.called) | 155 self.assertFalse(self.mock_thread_ctor.called) |
| 151 | 156 |
| 152 self._remove_config('foo.json') | 157 self._remove_config('foo.json') |
| 153 | 158 |
| 154 self.cw._iteration() | 159 self.cw._iteration() |
| 155 self.assertFalse(self.mock_thread_ctor.called) | 160 self.assertFalse(self.mock_thread_ctor.called) |
| 156 | 161 |
| 157 def test_add_good_config_then_make_bad(self): | 162 def test_add_good_config_then_make_bad(self): |
| 158 self._set_config('foo.json', '{"name": "foo"}', 100) | 163 self._set_config('foo.json', '{"name": "foo"}', 100) |
| 159 | 164 |
| 160 self.cw._iteration() | 165 self.cw._iteration() |
| 161 self.mock_thread_ctor.assert_called_once_with(43, '/state', {'name': 'foo'}) | 166 self.mock_thread_ctor.assert_called_once_with( |
| 167 43, '/state', {'name': 'foo'}, '/cloudtail') |
| 162 self.mock_thread.start.assert_called_once_with() | 168 self.mock_thread.start.assert_called_once_with() |
| 163 self.mock_thread.start_service.assert_called_once_with() | 169 self.mock_thread.start_service.assert_called_once_with() |
| 164 | 170 |
| 165 self._set_config('foo.json', '{"name": ', 200) | 171 self._set_config('foo.json', '{"name": ', 200) |
| 166 | 172 |
| 167 self.cw._iteration() | 173 self.cw._iteration() |
| 168 self.mock_thread.stop_service.assert_called_once_with() | 174 self.mock_thread.stop_service.assert_called_once_with() |
| 169 | 175 |
| 170 def test_add_bad_config_then_touch(self): | 176 def test_add_bad_config_then_touch(self): |
| 171 self._set_config('foo.json', '{"name": }', 100) | 177 self._set_config('foo.json', '{"name": }', 100) |
| 172 | 178 |
| 173 self.cw._iteration() | 179 self.cw._iteration() |
| 174 self.assertFalse(self.mock_thread_ctor.called) | 180 self.assertFalse(self.mock_thread_ctor.called) |
| 175 | 181 |
| 176 self._set_config('foo.json', '{"name": ', 200) | 182 self._set_config('foo.json', '{"name": ', 200) |
| 177 | 183 |
| 178 self.cw._iteration() | 184 self.cw._iteration() |
| 179 self.assertFalse(self.mock_thread_ctor.called) | 185 self.assertFalse(self.mock_thread_ctor.called) |
| 180 | 186 |
| 181 def test_remove_config(self): | 187 def test_remove_config(self): |
| 182 self._set_config('foo.json', '{"name": "foo"}', 100) | 188 self._set_config('foo.json', '{"name": "foo"}', 100) |
| 183 | 189 |
| 184 self.cw._iteration() | 190 self.cw._iteration() |
| 185 self.mock_thread_ctor.assert_called_once_with(43, '/state', {'name': 'foo'}) | 191 self.mock_thread_ctor.assert_called_once_with( |
| 192 43, '/state', {'name': 'foo'}, '/cloudtail') |
| 186 self.mock_thread.start.assert_called_once_with() | 193 self.mock_thread.start.assert_called_once_with() |
| 187 self.mock_thread.start_service.assert_called_once_with() | 194 self.mock_thread.start_service.assert_called_once_with() |
| 188 | 195 |
| 189 self._remove_config('foo.json') | 196 self._remove_config('foo.json') |
| 190 | 197 |
| 191 self.cw._iteration() | 198 self.cw._iteration() |
| 192 self.mock_thread.stop_service.assert_called_once_with() | 199 self.mock_thread.stop_service.assert_called_once_with() |
| 193 | 200 |
| 194 def test_remove_and_readd_config(self): | 201 def test_remove_and_readd_config(self): |
| 195 self._set_config('foo.json', '{"name": "foo"}', 100) | 202 self._set_config('foo.json', '{"name": "foo"}', 100) |
| 196 | 203 |
| 197 self.cw._iteration() | 204 self.cw._iteration() |
| 198 self.mock_thread_ctor.assert_called_once_with(43, '/state', {'name': 'foo'}) | 205 self.mock_thread_ctor.assert_called_once_with( |
| 206 43, '/state', {'name': 'foo'}, '/cloudtail') |
| 199 self.mock_thread.start.assert_called_once_with() | 207 self.mock_thread.start.assert_called_once_with() |
| 200 self.mock_thread.start_service.assert_called_once_with() | 208 self.mock_thread.start_service.assert_called_once_with() |
| 201 | 209 |
| 202 self._remove_config('foo.json') | 210 self._remove_config('foo.json') |
| 203 | 211 |
| 204 self.cw._iteration() | 212 self.cw._iteration() |
| 205 self.mock_thread.stop_service.assert_called_once_with() | 213 self.mock_thread.stop_service.assert_called_once_with() |
| 206 | 214 |
| 207 self._set_config('foo.json', '{"name": "foo"}', 100) | 215 self._set_config('foo.json', '{"name": "foo"}', 100) |
| 208 | 216 |
| 209 self.cw._iteration() | 217 self.cw._iteration() |
| 210 self.assertEqual(1, self.mock_thread_ctor.call_count) | 218 self.assertEqual(1, self.mock_thread_ctor.call_count) |
| 211 self.assertEqual(1, self.mock_thread.start_service.call_count) | 219 self.assertEqual(1, self.mock_thread.start_service.call_count) |
| 212 self.mock_thread.restart_with_new_config.assert_called_once_with( | 220 self.mock_thread.restart_with_new_config.assert_called_once_with( |
| 213 {'name': 'foo'}) | 221 {'name': 'foo'}) |
| 214 | 222 |
| 215 def test_run_stop(self): | 223 def test_run_stop(self): |
| 216 self._set_config('foo.json', '{"name": "foo"}', 100) | 224 self._set_config('foo.json', '{"name": "foo"}', 100) |
| 217 | 225 |
| 218 self.cw._iteration() | 226 self.cw._iteration() |
| 219 self.mock_thread_ctor.assert_called_once_with(43, '/state', {'name': 'foo'}) | 227 self.mock_thread_ctor.assert_called_once_with( |
| 228 43, '/state', {'name': 'foo'}, '/cloudtail') |
| 220 | 229 |
| 221 def sleep_impl(_duration): | 230 def sleep_impl(_duration): |
| 222 self.cw.stop() | 231 self.cw.stop() |
| 223 self.mock_sleep.side_effect = sleep_impl | 232 self.mock_sleep.side_effect = sleep_impl |
| 224 | 233 |
| 225 self.cw.run() | 234 self.cw.run() |
| 226 | 235 |
| 227 self.mock_sleep.assert_called_once_with(42) | 236 self.mock_sleep.assert_called_once_with(42) |
| 228 self.mock_thread.stop.assert_called_once_with() | 237 self.mock_thread.stop.assert_called_once_with() |
| 229 self.assertFalse(self.mock_thread.stop_service.called) | 238 self.assertFalse(self.mock_thread.stop_service.called) |
| 230 | 239 |
| 231 def test_run_stop_bad_config(self): | 240 def test_run_stop_bad_config(self): |
| 232 self._set_config('foo.json', '{"name": ') | 241 self._set_config('foo.json', '{"name": ') |
| 233 | 242 |
| 234 self.cw._iteration() | 243 self.cw._iteration() |
| 235 self.assertFalse(self.mock_thread_ctor.called) | 244 self.assertFalse(self.mock_thread_ctor.called) |
| 236 | 245 |
| 237 def sleep_impl(_duration): | 246 def sleep_impl(_duration): |
| 238 self.cw.stop() | 247 self.cw.stop() |
| 239 self.mock_sleep.side_effect = sleep_impl | 248 self.mock_sleep.side_effect = sleep_impl |
| 240 | 249 |
| 241 self.cw.run() | 250 self.cw.run() |
| 242 | 251 |
| 243 self.mock_sleep.assert_called_once_with(42) | 252 self.mock_sleep.assert_called_once_with(42) |
| 244 self.assertFalse(self.mock_thread_ctor.called) | 253 self.assertFalse(self.mock_thread_ctor.called) |
| OLD | NEW |