OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 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 argparse | 5 import argparse |
6 import json | 6 import json |
7 import requests | 7 import requests |
8 import tempfile | 8 import tempfile |
9 import unittest | 9 import unittest |
10 | 10 |
11 import mock | 11 import mock |
12 | 12 |
13 from testing_support import auto_stub | 13 from testing_support import auto_stub |
14 | 14 |
15 from infra_libs.ts_mon import config | 15 from infra_libs.ts_mon import config |
16 from infra_libs.ts_mon import interface | 16 from infra_libs.ts_mon import interface |
17 from infra_libs.ts_mon import standard_metrics | 17 from infra_libs.ts_mon.common import standard_metrics |
18 from infra_libs.ts_mon.test import stubs | 18 from infra_libs.ts_mon.test import stubs |
19 | 19 |
20 | 20 |
21 class GlobalsTest(auto_stub.TestCase): | 21 class GlobalsTest(auto_stub.TestCase): |
22 | 22 |
23 def setUp(self): | 23 def setUp(self): |
24 super(GlobalsTest, self).setUp() | 24 super(GlobalsTest, self).setUp() |
25 self.mock(interface, 'state', stubs.MockState()) | 25 self.mock(interface, 'state', stubs.MockState()) |
26 self.mock(config, 'load_machine_config', lambda x: {}) | 26 self.mock(config, 'load_machine_config', lambda x: {}) |
27 | 27 |
28 def tearDown(self): | 28 def tearDown(self): |
29 # It's important to call close() before un-setting the mock state object, | 29 # It's important to call close() before un-setting the mock state object, |
30 # because any FlushThread started by the test is stored in that mock state | 30 # because any FlushThread started by the test is stored in that mock state |
31 # and needs to be stopped before running any other tests. | 31 # and needs to be stopped before running any other tests. |
32 interface.close() | 32 interface.close() |
33 super(GlobalsTest, self).tearDown() | 33 super(GlobalsTest, self).tearDown() |
34 | 34 |
35 @mock.patch('requests.get') | 35 @mock.patch('requests.get') |
36 @mock.patch('socket.getfqdn') | 36 @mock.patch('socket.getfqdn') |
37 @mock.patch('infra_libs.ts_mon.monitors.ApiMonitor') | 37 @mock.patch('infra_libs.ts_mon.monitors.ApiMonitor') |
38 @mock.patch('infra_libs.ts_mon.targets.DeviceTarget') | 38 @mock.patch('infra_libs.ts_mon.common.targets.DeviceTarget') |
39 def test_default_monitor_args(self, fake_target, fake_monitor, fake_fqdn, | 39 def test_default_monitor_args(self, fake_target, fake_monitor, fake_fqdn, |
40 fake_get): | 40 fake_get): |
41 singleton = mock.Mock() | 41 singleton = mock.Mock() |
42 fake_monitor.return_value = singleton | 42 fake_monitor.return_value = singleton |
43 fake_target.return_value = singleton | 43 fake_target.return_value = singleton |
44 fake_fqdn.return_value = 'slave1-a1.reg.tld' | 44 fake_fqdn.return_value = 'slave1-a1.reg.tld' |
45 fake_get.return_value.side_effect = requests.exceptions.ConnectionError | 45 fake_get.return_value.side_effect = requests.exceptions.ConnectionError |
46 p = argparse.ArgumentParser() | 46 p = argparse.ArgumentParser() |
47 config.add_argparse_options(p) | 47 config.add_argparse_options(p) |
48 args = p.parse_args([ | 48 args = p.parse_args([ |
49 '--ts-mon-credentials', '/path/to/creds.p8.json', | 49 '--ts-mon-credentials', '/path/to/creds.p8.json', |
50 '--ts-mon-endpoint', | 50 '--ts-mon-endpoint', |
51 'https://www.googleapis.com/acquisitions/v1_mon_shared/storage']) | 51 'https://www.googleapis.com/acquisitions/v1_mon_shared/storage']) |
52 config.process_argparse_options(args) | 52 config.process_argparse_options(args) |
53 fake_monitor.assert_called_once_with( | 53 fake_monitor.assert_called_once_with( |
54 '/path/to/creds.p8.json', | 54 '/path/to/creds.p8.json', |
55 'https://www.googleapis.com/acquisitions/v1_mon_shared/storage', | 55 'https://www.googleapis.com/acquisitions/v1_mon_shared/storage', |
56 use_instrumented_http=True) | 56 use_instrumented_http=True) |
57 self.assertIs(interface.state.global_monitor, singleton) | 57 self.assertIs(interface.state.global_monitor, singleton) |
58 fake_target.assert_called_once_with('reg', '1', 'slave1-a1') | 58 fake_target.assert_called_once_with('reg', '1', 'slave1-a1') |
59 self.assertIs(interface.state.default_target, singleton) | 59 self.assertIs(interface.state.default_target, singleton) |
60 self.assertEquals(args.ts_mon_flush, 'auto') | 60 self.assertEquals(args.ts_mon_flush, 'auto') |
61 self.assertIsNotNone(interface.state.flush_thread) | 61 self.assertIsNotNone(interface.state.flush_thread) |
62 self.assertTrue(standard_metrics.up.get()) | 62 self.assertTrue(standard_metrics.up.get()) |
63 | 63 |
64 @mock.patch('requests.get') | 64 @mock.patch('requests.get') |
65 @mock.patch('socket.getfqdn') | 65 @mock.patch('socket.getfqdn') |
66 @mock.patch('infra_libs.ts_mon.monitors.ApiMonitor') | 66 @mock.patch('infra_libs.ts_mon.monitors.ApiMonitor') |
67 @mock.patch('infra_libs.ts_mon.targets.DeviceTarget') | 67 @mock.patch('infra_libs.ts_mon.common.targets.DeviceTarget') |
68 def test_fallback_monitor_args(self, fake_target, fake_monitor, fake_fqdn, | 68 def test_fallback_monitor_args(self, fake_target, fake_monitor, fake_fqdn, |
69 fake_get): | 69 fake_get): |
70 singleton = mock.Mock() | 70 singleton = mock.Mock() |
71 fake_monitor.return_value = singleton | 71 fake_monitor.return_value = singleton |
72 fake_target.return_value = singleton | 72 fake_target.return_value = singleton |
73 fake_fqdn.return_value = 'foo' | 73 fake_fqdn.return_value = 'foo' |
74 fake_get.return_value.side_effect = requests.exceptions.ConnectionError | 74 fake_get.return_value.side_effect = requests.exceptions.ConnectionError |
75 p = argparse.ArgumentParser() | 75 p = argparse.ArgumentParser() |
76 config.add_argparse_options(p) | 76 config.add_argparse_options(p) |
77 args = p.parse_args([ | 77 args = p.parse_args([ |
78 '--ts-mon-credentials', '/path/to/creds.p8.json', | 78 '--ts-mon-credentials', '/path/to/creds.p8.json', |
79 '--ts-mon-endpoint', | 79 '--ts-mon-endpoint', |
80 'https://www.googleapis.com/acquisitions/v1_mon_shared/storage']) | 80 'https://www.googleapis.com/acquisitions/v1_mon_shared/storage']) |
81 config.process_argparse_options(args) | 81 config.process_argparse_options(args) |
82 fake_monitor.assert_called_once_with( | 82 fake_monitor.assert_called_once_with( |
83 '/path/to/creds.p8.json', | 83 '/path/to/creds.p8.json', |
84 'https://www.googleapis.com/acquisitions/v1_mon_shared/storage', | 84 'https://www.googleapis.com/acquisitions/v1_mon_shared/storage', |
85 use_instrumented_http=True) | 85 use_instrumented_http=True) |
86 self.assertIs(interface.state.global_monitor, singleton) | 86 self.assertIs(interface.state.global_monitor, singleton) |
87 fake_target.assert_called_once_with('', '', 'foo') | 87 fake_target.assert_called_once_with('', '', 'foo') |
88 self.assertIs(interface.state.default_target, singleton) | 88 self.assertIs(interface.state.default_target, singleton) |
89 | 89 |
90 @mock.patch('socket.getfqdn') | 90 @mock.patch('socket.getfqdn') |
91 @mock.patch('infra_libs.ts_mon.monitors.ApiMonitor') | 91 @mock.patch('infra_libs.ts_mon.monitors.ApiMonitor') |
92 @mock.patch('infra_libs.ts_mon.targets.DeviceTarget') | 92 @mock.patch('infra_libs.ts_mon.common.targets.DeviceTarget') |
93 def test_manual_flush(self, fake_target, fake_monitor, fake_fqdn): | 93 def test_manual_flush(self, fake_target, fake_monitor, fake_fqdn): |
94 singleton = mock.Mock() | 94 singleton = mock.Mock() |
95 fake_monitor.return_value = singleton | 95 fake_monitor.return_value = singleton |
96 fake_target.return_value = singleton | 96 fake_target.return_value = singleton |
97 fake_fqdn.return_value = 'foo' | 97 fake_fqdn.return_value = 'foo' |
98 p = argparse.ArgumentParser() | 98 p = argparse.ArgumentParser() |
99 config.add_argparse_options(p) | 99 config.add_argparse_options(p) |
100 args = p.parse_args(['--ts-mon-flush', 'manual']) | 100 args = p.parse_args(['--ts-mon-flush', 'manual']) |
101 config.process_argparse_options(args) | 101 config.process_argparse_options(args) |
102 self.assertIsNone(interface.state.flush_thread) | 102 self.assertIsNone(interface.state.flush_thread) |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 singleton = mock.Mock() | 164 singleton = mock.Mock() |
165 fake_monitor.return_value = singleton | 165 fake_monitor.return_value = singleton |
166 p = argparse.ArgumentParser() | 166 p = argparse.ArgumentParser() |
167 config.add_argparse_options(p) | 167 config.add_argparse_options(p) |
168 args = p.parse_args(['--ts-mon-endpoint', 'file://foo.txt']) | 168 args = p.parse_args(['--ts-mon-endpoint', 'file://foo.txt']) |
169 config.process_argparse_options(args) | 169 config.process_argparse_options(args) |
170 fake_monitor.assert_called_once_with('foo.txt') | 170 fake_monitor.assert_called_once_with('foo.txt') |
171 self.assertIs(interface.state.global_monitor, singleton) | 171 self.assertIs(interface.state.global_monitor, singleton) |
172 | 172 |
173 @mock.patch('infra_libs.ts_mon.monitors.ApiMonitor') | 173 @mock.patch('infra_libs.ts_mon.monitors.ApiMonitor') |
174 @mock.patch('infra_libs.ts_mon.targets.DeviceTarget') | 174 @mock.patch('infra_libs.ts_mon.common.targets.DeviceTarget') |
175 def test_device_args(self, fake_target, _fake_monitor): | 175 def test_device_args(self, fake_target, _fake_monitor): |
176 singleton = mock.Mock() | 176 singleton = mock.Mock() |
177 fake_target.return_value = singleton | 177 fake_target.return_value = singleton |
178 p = argparse.ArgumentParser() | 178 p = argparse.ArgumentParser() |
179 config.add_argparse_options(p) | 179 config.add_argparse_options(p) |
180 args = p.parse_args(['--ts-mon-credentials', '/path/to/creds.p8.json', | 180 args = p.parse_args(['--ts-mon-credentials', '/path/to/creds.p8.json', |
181 '--ts-mon-target-type', 'device', | 181 '--ts-mon-target-type', 'device', |
182 '--ts-mon-device-region', 'reg', | 182 '--ts-mon-device-region', 'reg', |
183 '--ts-mon-device-network', 'net', | 183 '--ts-mon-device-network', 'net', |
184 '--ts-mon-device-hostname', 'host']) | 184 '--ts-mon-device-hostname', 'host']) |
185 config.process_argparse_options(args) | 185 config.process_argparse_options(args) |
186 fake_target.assert_called_once_with('reg', 'net', 'host') | 186 fake_target.assert_called_once_with('reg', 'net', 'host') |
187 self.assertIs(interface.state.default_target, singleton) | 187 self.assertIs(interface.state.default_target, singleton) |
188 | 188 |
189 @mock.patch('infra_libs.ts_mon.monitors.ApiMonitor') | 189 @mock.patch('infra_libs.ts_mon.monitors.ApiMonitor') |
190 @mock.patch('infra_libs.ts_mon.targets.TaskTarget') | 190 @mock.patch('infra_libs.ts_mon.common.targets.TaskTarget') |
191 def test_task_args(self, fake_target, _fake_monitor): | 191 def test_task_args(self, fake_target, _fake_monitor): |
192 singleton = mock.Mock() | 192 singleton = mock.Mock() |
193 fake_target.return_value = singleton | 193 fake_target.return_value = singleton |
194 p = argparse.ArgumentParser() | 194 p = argparse.ArgumentParser() |
195 config.add_argparse_options(p) | 195 config.add_argparse_options(p) |
196 args = p.parse_args(['--ts-mon-credentials', '/path/to/creds.p8.json', | 196 args = p.parse_args(['--ts-mon-credentials', '/path/to/creds.p8.json', |
197 '--ts-mon-target-type', 'task', | 197 '--ts-mon-target-type', 'task', |
198 '--ts-mon-task-service-name', 'serv', | 198 '--ts-mon-task-service-name', 'serv', |
199 '--ts-mon-task-job-name', 'job', | 199 '--ts-mon-task-job-name', 'job', |
200 '--ts-mon-task-region', 'reg', | 200 '--ts-mon-task-region', 'reg', |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 246 |
247 def test_load_machine_config_bad(self): | 247 def test_load_machine_config_bad(self): |
248 with tempfile.NamedTemporaryFile() as fh: | 248 with tempfile.NamedTemporaryFile() as fh: |
249 fh.write('not a json file') | 249 fh.write('not a json file') |
250 fh.flush() | 250 fh.flush() |
251 with self.assertRaises(ValueError): | 251 with self.assertRaises(ValueError): |
252 config.load_machine_config(fh.name) | 252 config.load_machine_config(fh.name) |
253 | 253 |
254 def test_load_machine_config_not_exists(self): | 254 def test_load_machine_config_not_exists(self): |
255 self.assertEquals({}, config.load_machine_config('does not exist')) | 255 self.assertEquals({}, config.load_machine_config('does not exist')) |
OLD | NEW |