def test_singularity_carbon(self): with requests_mock.Mocker(real_http=True) as m: m.register_uri('GET', 'http://server/api/state', json=self.state_api, status_code=200) m.register_uri('GET', 'http://server/api/requests', json=self.requests_api, status_code=200) m.register_uri('GET', 'http://server/api/disasters/stats', json=self.disaster_api, status_code=200) m.register_uri('GET', 'http://server/api/tasks/active', json=self.tasks_api, status_code=200) s = Singularity('server') s.update() q = queue.Queue() sc = SingularityCarbon(s, q) sc.flush_all() # Queue should be populated self.assertFalse(q.empty()) # Test plaintext protocol a = q.get() self.assertEqual(len(a.split()), 3) # Make sure that every metric has been captured q.put(a) all_metric_names = set(sc.metric_mapping.values()) metric_names_from_q = set() while True: try: a = q.get(block=None) except queue.Empty: break metric_names_from_q.add(a.split()[0]) self.assertEqual(all_metric_names, metric_names_from_q, metric_names_from_q) # Test pickle protocol q2 = queue.Queue() sc2 = SingularityCarbon(s, q2, pickle=True) sc2.flush_all() self.assertFalse(q2.empty()) a = q2.get() self.assertIsInstance(a, tuple) self.assertIsInstance(a[1], tuple)
def test_singularity_carbon(self): with requests_mock.Mocker(real_http=True) as m: m.register_uri('GET', 'http://server/api/state', json=self.state_api, status_code=200) m.register_uri('GET', 'http://server/api/requests', json=self.requests_api, status_code=200) m.register_uri('GET', 'http://server/api/disasters/stats', json=self.disaster_api, status_code=200) m.register_uri('GET', 'http://server/api/tasks/active', json=self.tasks_api, status_code=200) m.register_uri('GET', 'http://server/api/slaves?state=DECOMMISSIONED', json={}, status_code=200) s = Singularity('server') s.update() q = queue.Queue() sc = SingularityCarbon(s, q) sc.flush_all() # Queue should be populated self.assertFalse(q.empty()) # Test plaintext protocol a = q.get() self.assertEqual(len(a.split()), 3) # Make sure that every metric has been captured q.put(a) all_metric_names = set(sc.metric_mapping.values()) metric_names_from_q = set() while True: try: a = q.get(block=None) except queue.Empty: break metric_names_from_q.add(a.split()[0]) self.assertEqual(all_metric_names, metric_names_from_q, metric_names_from_q) # Test pickle protocol q2 = queue.Queue() sc2 = SingularityCarbon(s, q2, pickle=True) sc2.flush_all() self.assertFalse(q2.empty()) a = q2.get() self.assertIsInstance(a, tuple) self.assertIsInstance(a[1], tuple)
def test_get_singularity_lookup(self): with requests_mock.Mocker(real_http=True) as m: m.register_uri('GET', 'http://server/api/state', json=self.state_api, status_code=200) m.register_uri('GET', 'http://server/api/requests', json=self.requests_api, status_code=200) m.register_uri('GET', 'http://server/api/disasters/stats', json=self.disaster_api, status_code=200) m.register_uri('GET', 'http://server/api/tasks/active', json=self.tasks_api, status_code=200) m.register_uri('GET', 'http://server/api/slaves?state=DECOMMISSIONED', json={}, status_code=200) s = Singularity('server') s.update() mapping = s.get_singularity_lookup() self.assertIsInstance(mapping, dict) self.assertEqual(mapping['my-mesos-task'], 'my-request_2')
def test_send_alternate_executor_metrics(self): with requests_mock.Mocker(real_http=True) as m: tasks_api = [ { "taskId": { "requestId": "my-request", "deployId": "teamcity_2018_01_17T00_04_40", "startedAt": 1516147481669, "instanceNo": 2, "host": "mesos_slave21_qa_sf.qasql.opentable.com", "sanitizedHost": "mesos_slave21_qa_sf.qasql.opentable.com", "sanitizedRackId": "FIXME", "rackId": "FIXME", "id": "my-mesos-task" }, "mesosTask": { "taskId": { "value": "my-mesos-task" }, "name": "pp-promoted-inventory-service", } } ] res = [ { "executor_id": "my-mesos-task", "executor_name": "mytask command", "framework_id": "Singularity", "source": "mytask", "statistics": { "cpus_limit": 0.13, "cpus_system_time_secs": 22.56, "cpus_user_time_secs": 104.88, "mem_limit_bytes": 301989888, "mem_rss_bytes": 87113728, "timestamp": 1516124496.89259 } } ] m.register_uri('GET', 'http://slave1:5051/monitor/statistics.json', json=res, status_code=200) m.register_uri('GET', 'http://slave1:5051/metrics/snapshot', json={}, status_code=200) m.register_uri('GET', 'http://mesos1/slaves', json=self.slaves_api, status_code=200) m.register_uri('GET', 'http://mesos1/metrics/snapshot', json={'master/elected': 1}, status_code=200) m.register_uri('GET', 'http://server/api/state', json={}, status_code=200) m.register_uri('GET', 'http://server/api/requests', json=[], status_code=200) m.register_uri('GET', 'http://server/api/disasters/stats', json={}, status_code=200) m.register_uri('GET', 'http://server/api/tasks/active', json=tasks_api, status_code=200) s = Singularity('server') s.update() mesos = Mesos(master_list=['mesos1']) mesos.update() q = multiprocessing.Queue() mc = MesosCarbon(mesos, q, singularity=s) mc.send_alternate_executor_metrics() try: self.assertEqual(len(q.qsize()), 5) except NotImplementedError: # Not supported in Mac OS X pass a = q.get() self.assertTrue(a.split()[0].startswith('tasks.my-request.2.'))
def test_send_alternate_executor_metrics(self): with requests_mock.Mocker(real_http=True) as m: tasks_api = [ { "taskId": { "requestId": "my-request", "deployId": "teamcity_2018_01_17T00_04_40", "startedAt": 1516147481669, "instanceNo": 2, "host": "mesos_slave21_qa_sf.qasql.opentable.com", "sanitizedHost": "mesos_slave21_qa_sf.qasql.opentable.com", "sanitizedRackId": "FIXME", "rackId": "FIXME", "id": "my-mesos-task" }, "mesosTask": { "taskId": { "value": "my-mesos-task" }, "name": "pp-promoted-inventory-service", } } ] res = [ { "executor_id": "my-mesos-task", "executor_name": "mytask command", "framework_id": "Singularity", "source": "mytask", "statistics": { "cpus_limit": 0.13, "cpus_system_time_secs": 22.56, "cpus_user_time_secs": 104.88, "mem_limit_bytes": 301989888, "mem_rss_bytes": 87113728, "timestamp": 1516124496.89259 } } ] m.register_uri('GET', 'http://slave1:5051/monitor/statistics.json', json=res, status_code=200) m.register_uri('GET', 'http://slave1:5051/metrics/snapshot', json={}, status_code=200) m.register_uri('GET', 'http://mesos1/slaves', json=self.slaves_api, status_code=200) m.register_uri('GET', 'http://mesos1/metrics/snapshot', json={'master/elected': 1}, status_code=200) m.register_uri('GET', 'http://server/api/state', json={}, status_code=200) m.register_uri('GET', 'http://server/api/slaves?state=DECOMMISSIONED', json={}, status_code=200) m.register_uri('GET', 'http://server/api/requests', json=[], status_code=200) m.register_uri('GET', 'http://server/api/disasters/stats', json={}, status_code=200) m.register_uri('GET', 'http://server/api/tasks/active', json=tasks_api, status_code=200) s = Singularity('server') s.update() mesos = Mesos(master_list=['mesos1']) mesos.update() q = multiprocessing.Queue() mc = MesosCarbon(mesos, q, singularity=s) mc.send_alternate_executor_metrics() try: self.assertEqual(len(q.qsize()), 5) except NotImplementedError: # Not supported in Mac OS X pass a = q.get() self.assertTrue(a.split()[0].startswith('tasks.my-request.2.'))