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 init_env(): master_list = os.environ.get('MESOS_MASTER', '').split(',') carbon_host = os.environ.get('CARBON_HOST', None) graphite_prefix = os.environ.get('GRAPHITE_PREFIX', None) carbon_pickle = os.environ.get('CARBON_PICKLE', 'False') singularity_host = os.environ.get('SINGULARITY_HOST', None) carbon_port = os.environ.get('CARBON_PORT', '2003') dry_run = os.environ.get('DRY_RUN', 'False') dry_run = str_to_bool(dry_run) carbon_pickle = str_to_bool(carbon_pickle) def config_print(): print("=" * 80) print("MESOS MASTERS: %s" % master_list) print("CARBON: %s" % carbon_host) print("GRAPHITE PREFIX: %s" % graphite_prefix) print("CARBON PICKLE: %s" % carbon_pickle) print("SINGULARITY HOST: %s" % singularity_host) print("DRY RUN (TEST MODE): %s" % dry_run) print("=" * 80) if not all([master_list, carbon_host, graphite_prefix]): print('ERROR : One or more configuration env not set') print('MESOS_MASTERS, CARBON, and GRAPHITE_PREFIX needs to be set') config_print() sys.exit(0) config_print() assert (isinstance(master_list, list)) with Timer("mesos init"): mesos = Mesos(master_list) with Timer("carbon init"): carbon = Carbon(carbon_host, graphite_prefix, port=int(carbon_port), pickle=carbon_pickle, dry_run=dry_run) if singularity_host: with Timer("singularity init"): singularity = Singularity(singularity_host) return (mesos, carbon, singularity, carbon_pickle)
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.'))