Beispiel #1
0
class TestMesosCollector(CollectorTestCase):
    def setUp(self):
        config = get_collector_config('MesosCollector', {})

        self.collector = MesosCollector(config, None)

    def test_import(self):
        self.assertTrue(MesosCollector)

    @patch.object(Collector, 'publish')
    def test_should_work_with_real_data(self, publish_mock):
        def se(url):
            if url == 'http://*****:*****@patch.object(Collector, 'publish')
    def test_should_fail_gracefully(self, publish_mock):
        patch_urlopen = patch(
            'urllib2.urlopen',
            Mock(return_value=self.getFixture('metrics_blank')))

        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()

        self.assertPublishedMany(publish_mock, {})

    def test_http(self):
        self.collector.config['host'] = 'localhost'
        self.assertEqual('http://localhost:5050/metrics/snapshot',
                         self.collector._get_url())

    def test_https(self):
        self.collector.config['host'] = 'https://localhost'
        self.assertEqual('https://localhost:5050/metrics/snapshot',
                         self.collector._get_url())

    def get_metrics(self):
        return {
            'master.cpus_percent': 0.762166666666667,
            'master.cpus_total': 120,
            'master.cpus_used': 91.46,
            'master.disk_percent': 0.0317975447795468,
            'master.disk_total': 12541440,
            'master.disk_used': 398787
        }
Beispiel #2
0
class TestMesosCollector(CollectorTestCase):

    def setUp(self):
        config = get_collector_config('MesosCollector', {})

        self.collector = MesosCollector(config, None)

    def test_import(self):
        self.assertTrue(MesosCollector)

    @patch.object(Collector, 'publish')
    def test_should_work_with_real_data(self, publish_mock):
        def se(url):
            if url == 'http://*****:*****@patch.object(Collector, 'publish')
    def test_should_fail_gracefully(self, publish_mock):
        patch_urlopen = patch('urllib2.urlopen', Mock(
                              return_value=self.getFixture('metrics_blank')))

        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()

        self.assertPublishedMany(publish_mock, {})

    def test_http(self):
        self.collector.config['host'] = 'localhost'
        self.assertEqual('http://localhost:5050/metrics/snapshot',
                         self.collector._get_url())

    def test_https(self):
        self.collector.config['host'] = 'https://localhost'
        self.assertEqual('https://localhost:5050/metrics/snapshot',
                         self.collector._get_url())

    def get_metrics(self):
        return {
            'master.cpus_percent': 0.762166666666667,
            'master.cpus_total': 120,
            'master.cpus_used': 91.46,
            'master.disk_percent': 0.0317975447795468,
            'master.disk_total': 12541440,
            'master.disk_used': 398787
        }
Beispiel #3
0
class TestMesosCollector(CollectorTestCase):
    STATE = {
        'attributes': {
            'group': 'test'
        },
        'frameworks': [{
            'executors': [{
                'id': 'thermos-1420503024922-docker-test-devel-hello_docker-0-46e0aa39-8691-414a-992c-919ea2d1003d',
                'directory': '/tmp',
                'resources': {
                    'cpus': 10,
                    'disk': 16
                },
                'tasks': [{
                    'executor_id': u'thermos-1420503024922-docker-test-devel-hello_docker-0-46e0aa39-8691-414a-992c-919ea2d1003d'
                }]
            }]
        }],
        'version': '0.22.1'
    }

    def get_state(self, major, minor, point):
        state = self.STATE.copy()
        state['version'] = '%s.%s.%s' % (major, minor, point)
        return state

    def setUp(self):
        config = get_collector_config('MesosCollector', {
            'hosts': 'localhost:8081',
            'collect_disk_usage': False
        })

        self.collector = MesosCollector(config, None)

    def test_import(self):
        self.assertTrue(MesosCollector)

    @patch.object(Collector, 'publish')
    def test_master_metrics(self, publish_mock):
        mm = self.getFixture('master_metrics')
        patch_urlopen = patch('urllib2.urlopen', Mock(return_value=mm))

        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()

        published_metrics = {
            'master_mem_used': 297300,
            'master_slave_registrations': 11,
            'system_mem_free_bytes': 1334034432
        }

        self.assertPublishedMany(publish_mock, published_metrics)

    @patch.object(Collector, 'publish')
    def test_no_publish_if_inactive(self, publish_mock):
        metrics = {
            'master/elected': 0,
            'master_mem_used': 297300,
            'master_slave_registrations': 11,
            'system_mem_free_bytes': 1334034432
        }

        patch_urlopen = patch('urllib2.urlopen',
            Mock(side_effect=lambda *args: StringIO(json.dumps(metrics))))

        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()

        self.assertUnpublishedMany(publish_mock, metrics)

    @patch.object(Collector, 'publish')
    def test_slave_metrics(self, publish_mock):
        fixtures = [self.getFixture('slave_metrics'),
                    StringIO(json.dumps(self.STATE)),
                    StringIO("[]")]
        patch_urlopen = patch('urllib2.urlopen',
                              Mock(side_effect=lambda *args: fixtures.pop(0)))

        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()

        published_metrics = {
            'slave_executors_terminated': 397,
            'slave_valid_status_updates': 1471
        }

        self.assertPublishedMany(publish_mock, published_metrics)

    @patch.object(Collector, 'publish')
    def test_task_metrics(self, publish_mock):
        fixtures = [StringIO("{}"),
                    StringIO(json.dumps(self.get_state(0, 22, 1))),
                    self.getFixture('slave_perf_1'),

                    StringIO("{}"),
                    StringIO(json.dumps(self.get_state(0, 22, 1))),
                    self.getFixture('slave_perf_2')]
        patch_urlopen = patch('urllib2.urlopen',
                              Mock(side_effect=lambda *args: fixtures.pop(0)))

        patch_urlopen.start()
        self.collector.collect()
        self.collector.collect()
        patch_urlopen.stop()

        expected_metrics = {
            'job_resources_used_user_cpu': 2.7,
            'job_resources_used_sys_cpu': 1,
            'job_resources_used_cpu': 3.7,
            'job_resources_used_mem_total': 79015936 / (1024 * 1024)
        }
        # reverse it so the calls that get checked are the last calls
        publish_mock.call_args_list.reverse()

        # Make sure the sources are right.
        for arg in publish_mock.call_args_list:
            self.assertEqual(arg[1]['source'],
                             'test.docker.devel.hello_docker.0')

        # Make sure the values are correct.
        self.assertPublishedMany(publish_mock, expected_metrics, 2)

        unexpected_metrics = {
            'job_resources_used_mem_file': 0,
            'job_resources_used_mem_resident': 0
        }

        self.assertUnpublishedMany(publish_mock, unexpected_metrics)

    @patch.object(Collector, 'publish')
    def test_task_memory_metrics(self, publish_mock):
        fixtures = [StringIO("{}"),
                    StringIO(json.dumps(self.get_state(0, 22, 1))),
                    self.getFixture('slave_perf_3')]

        patch_urlopen = patch('urllib2.urlopen',
                              Mock(side_effect=lambda *args: fixtures.pop(0)))

        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()

        expected_metrics = {
            'job_resources_used_mem_total': 79015936 / (1024 * 1024),
            'job_resources_used_mem_file': 22227836 / (1024 * 1024),
            'job_resources_used_mem_resident': 56788100 / (1024 * 1024)
        }

        self.assertPublishedMany(publish_mock, expected_metrics)

    @patch.object(Collector, 'publish')
    def test_task_indirect_disk_metrics(self, publish_mock):
        fixtures = [StringIO("{}"),
                    StringIO(json.dumps(self.get_state(0, 22, 1))),
                    self.getFixture('slave_perf_3')]

        patch_urlopen = patch('urllib2.urlopen',
                              Mock(side_effect=lambda *args: fixtures.pop(0)))

        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()

        expected_metrics = {
            'job_resources_used_disk_percent': 20.0,
            'job_resources_used_disk_mb': 2
        }

        self.assertPublishedMany(publish_mock, expected_metrics)

    @patch.object(Collector, 'publish')
    def test_task_direct_disk_metrics(self, publish_mock):
        config = get_collector_config('MesosCollector', {
            'hosts': 'localhost:8081',
            'collect_disk_usage': True
        })

        self.collector = MesosCollector(config, None)

        fixtures = [StringIO("{}"),
                    StringIO(json.dumps(self.get_state(0, 22, 1))),
                    self.getFixture('slave_perf_3')]

        patch_urlopen = patch('urllib2.urlopen',
                              Mock(side_effect=lambda *args: fixtures.pop(0)))

        patch_communicate = patch(
            'subprocess.Popen.communicate',
            Mock(return_value=('4096\t/mnt/path\n', '')))

        patch_communicate.start()
        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()
        patch_communicate.stop()

        expected_metrics = {
            'job_resources_used_disk_percent': 25.0,
            'job_resources_used_disk_mb': 4
        }

        self.assertPublishedMany(publish_mock, expected_metrics)

    @patch.object(Collector, 'publish')
    def test_unit_conversion(self, publish_mock):
        config = get_collector_config('MesosCollector', {
            'hosts': 'localhost:8081',
            'collect_disk_usage': False,
            'byte_unit': ['kb']
        })

        self.collector = MesosCollector(config, None)

        fixtures = [StringIO("{}"),
                    StringIO(json.dumps(self.get_state(0, 24, 0))),
                    self.getFixture('slave_perf_4')]

        patch_urlopen = patch('urllib2.urlopen',
                              Mock(side_effect=lambda *args: fixtures.pop(0)))

        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()

        expected_metrics = {
            'job_resources_used_disk_percent': 25.0,
            'job_resources_used_disk_kb': 4096,
            'job_resources_used_mem_anon_kb': 48516,
            'job_resources_used_mem_cache_kb': 9236,
            'job_resources_used_mem_critical_pressure_counter': 0,
            'job_resources_used_mem_file_kb': 9236,
            'job_resources_used_mem_limit_kb': 1179648,
            'job_resources_used_mem_low_pressure_counter': 0,
            'job_resources_used_mem_mapped_file_kb': 3808,
            'job_resources_used_mem_medium_pressure_counter': 0,
            'job_resources_used_mem_rss_kb': 48516,
            'job_resources_used_mem_swap_kb': 0,
            'job_resources_used_mem_total_kb': 58204,
            'job_resources_used_mem_unevictable_kb': 0,
        }

        self.assertPublishedMany(publish_mock, expected_metrics)

    def test_test(self):
        major, minor, point = MesosCollector.get_mesos_version(self.STATE)
        self.assertEqual(major, 0)
        self.assertEqual(minor, 22)
        self.assertEqual(point, 1)
Beispiel #4
0
class TestMesosCollector(CollectorTestCase):

    def setUp(self):
        config = get_collector_config('MesosCollector', {})

        self.collector = MesosCollector(config, None)

    def test_import(self):
        self.assertTrue(MesosCollector)

    def test_import2(self):
        self.assertTrue(self.collector.config['path'], 'mesos')

    @patch.object(Collector, 'publish')
    def test_should_work_for_master_with_real_data(self, publish_mock):
        returns = self.getFixture('master_metrics_snapshot.json')
        urlopen_mock = patch('urllib2.urlopen', Mock(
            side_effect=lambda *args: returns))

        urlopen_mock.start()
        self.collector.collect()
        urlopen_mock.stop()

        # check how many fixtures were consumed
        self.assertEqual(urlopen_mock.new.call_count, 1)

        metrics = {
            'master.elected': (1, 0),
            "system.mem_free_bytes": (5663678464.1, 0),
            "registrar.state_store_ms.p9999": (17.8412544, 6)
        }

        self.assertPublishedMany(publish_mock, metrics)

    @patch.object(Collector, 'publish')
    def test_should_work_for_slave_with_real_data(self, publish_mock):
        config = get_collector_config('MesosCollector', {'master': False})
        self.collector = MesosCollector(config, None)
        self.assertEqual(self.collector.master, False)

        returns = [
            self.getFixture('master_metrics_snapshot.json'),
            self.getFixture('slave_metrics_state.json'),
            self.getFixture('slave_monitor_statistics.json')
        ]

        urlopen_mock = patch('urllib2.urlopen', Mock(
            side_effect=lambda *args: returns.pop(0)))

        urlopen_mock.start()
        self.collector.collect()
        urlopen_mock.stop()

        # check how many fixtures were consumed
        self.assertEqual(urlopen_mock.new.call_count, 3)

        metrics = {
            'master.elected': 1,
            'system.mem_free_bytes': 5663678464.1,
            'registrar.state_store_ms.p9999': (17.8412544, 6),
            'staged_tasks': 20,
            'failed_tasks': 6,
            'finished_tasks': 1,
            'frameworks.marathon-0_7_6.executors.task_name.'
            '09b6f20c-b6a9-11e4-99f6-fa163ef210c0.cpus_limit': (0.6, 1),
            'frameworks.marathon-0_7_6.executors.task_name.'
            '06247c78-b6a9-11e4-99f6-fa163ef210c0.cpus_limit': (1.1, 1),
            'frameworks.marathon-0_7_6.executors.task_name.'
            'cpus_limit': (1.7, 1),
            'frameworks.marathon-0_7_6.executors.task_name.'
            'instances_count': (2, 0),
            'frameworks.marathon-0_7_6.executors.'
            'com_domain_group_anotherApp.mem_mapped_file_bytes': 45056,
            'frameworks.marathon-0_7_6.executors.task_name.'
            'mem_percent': (0.19, 2)
        }

        self.setDocExample(collector=self.collector.__class__.__name__,
                           metrics=metrics,
                           defaultpath=self.collector.config['path'])
        self.assertPublishedMany(publish_mock, metrics)

    @patch.object(Collector, 'publish')
    def test_should_compute_cpus_utilisation(self, publish_mock):
        self.fixture_cpu_utilisation(publish_mock)

        metrics = {
            'frameworks.marathon-0_7_6.executors.task_name.'
            '09b6f20c-b6a9-11e4-99f6-fa163ef210c0.cpus_utilisation': 0.25,
            'frameworks.marathon-0_7_6.executors.task_name.'
            '06247c78-b6a9-11e4-99f6-fa163ef210c0.cpus_utilisation': 0.25,
            'frameworks.marathon-0_7_6.executors.task_name.'
            'cpus_utilisation': 0.5,
        }

        self.assertPublishedMany(publish_mock, metrics)

    @patch.object(Collector, 'publish')
    def test_should_fail_gracefully(self, publish_mock):
        patch_urlopen = patch('urllib2.urlopen', Mock(
                              return_value=self.getFixture('metrics_blank')))

        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()

        self.assertPublishedMany(publish_mock, {})

    @patch.object(Collector, 'publish')
    def test_should_compute_cpus_percent(self, publish_mock):
        self.fixture_cpu_utilisation(publish_mock)

        self.assertPublished(
            publish_mock,
            'frameworks.marathon-0_7_6.executors.task_name.cpus_percent',
            0.5/1.7)

    def fixture_cpu_utilisation(self, publish_mock):
        config = get_collector_config('MesosCollector', {'master': False})
        self.collector = MesosCollector(config, None)
        self.assertEqual(self.collector.master, False)
        # we need 2 collect calls to see new metrics
        returns = [
            self.getFixture('master_metrics_snapshot.json'),
            self.getFixture('slave_metrics_state.json'),
            self.getFixture(
                'slave_monitor_statistics_cpus_utilisation_next.json'),
            self.getFixture('master_metrics_snapshot.json'),
            self.getFixture('slave_metrics_state.json'),
            self.getFixture('slave_monitor_statistics_cpus_utilisation.json'),
        ]
        urlopen_mock = patch('urllib2.urlopen', Mock(
            side_effect=lambda *args: returns.pop(0)))
        urlopen_mock.start()
        self.collector.collect()
        publish_mock.reset_mock()
        self.collector.collect()
        urlopen_mock.stop()

    def test_http(self):
        self.collector.config['host'] = 'localhost'
        self.assertEqual('http://localhost:5050/metrics/snapshot',
                         self.collector._get_url("metrics/snapshot"))

    def test_https(self):
        self.collector.config['host'] = 'https://localhost'
        self.assertEqual('https://localhost:5050/metrics/snapshot',
                         self.collector._get_url("metrics/snapshot"))

    def test_sum_statistics(self):
        metrics_1 = {'cpu': 50, 'mem': 30, 'loadavg': 1}
        metrics_2 = {'cpu': 10, 'mem': 30, 'network': 10}
        self.assertEqual(self.collector._sum_statistics(metrics_1, metrics_2),
                         {'mem': 60, 'loadavg': 1, 'network': 10, 'cpu': 60})
Beispiel #5
0
class TestMesosCollector(CollectorTestCase):
    def setUp(self):
        config = get_collector_config('MesosCollector', {})

        self.collector = MesosCollector(config, None)

    def test_import(self):
        self.assertTrue(MesosCollector)

    def test_import2(self):
        self.assertTrue(self.collector.config['path'], 'mesos')

    @patch.object(Collector, 'publish')
    def test_should_work_for_master_with_real_data(self, publish_mock):
        returns = self.getFixture('master_metrics_snapshot.json')
        urlopen_mock = patch('urllib2.urlopen',
                             Mock(side_effect=lambda *args: returns))

        urlopen_mock.start()
        self.collector.collect()
        urlopen_mock.stop()

        # check how many fixtures were consumed
        self.assertEqual(urlopen_mock.new.call_count, 1)

        metrics = {
            'master.elected': (1, 0),
            "system.mem_free_bytes": (5663678464.1, 0),
            "registrar.state_store_ms.p9999": (17.8412544, 6)
        }

        self.assertPublishedMany(publish_mock, metrics)

    @patch.object(Collector, 'publish')
    def test_should_work_for_slave_with_real_data(self, publish_mock):
        config = get_collector_config('MesosCollector', {'master': False})
        self.collector = MesosCollector(config, None)
        self.assertEqual(self.collector.master, False)

        returns = [
            self.getFixture('master_metrics_snapshot.json'),
            self.getFixture('slave_metrics_state.json'),
            self.getFixture('slave_monitor_statistics.json')
        ]

        urlopen_mock = patch('urllib2.urlopen',
                             Mock(side_effect=lambda *args: returns.pop(0)))

        urlopen_mock.start()
        self.collector.collect()
        urlopen_mock.stop()

        # check how many fixtures were consumed
        self.assertEqual(urlopen_mock.new.call_count, 3)

        metrics = {
            'master.elected':
            1,
            'system.mem_free_bytes':
            5663678464.1,
            'registrar.state_store_ms.p9999': (17.8412544, 6),
            'staged_tasks':
            20,
            'failed_tasks':
            6,
            'finished_tasks':
            1,
            'frameworks.marathon-0_7_6.executors.task_name.'
            '09b6f20c-b6a9-11e4-99f6-fa163ef210c0.cpus_limit': (0.6, 1),
            'frameworks.marathon-0_7_6.executors.task_name.'
            '06247c78-b6a9-11e4-99f6-fa163ef210c0.cpus_limit': (1.1, 1),
            'frameworks.marathon-0_7_6.executors.task_name.'
            'cpus_limit': (1.7, 1),
            'frameworks.marathon-0_7_6.executors.task_name.'
            'instances_count': (2, 0),
            'frameworks.marathon-0_7_6.executors.'
            'com_domain_group_anotherApp.mem_mapped_file_bytes':
            45056,
            'frameworks.marathon-0_7_6.executors.task_name.'
            'mem_percent': (0.19, 2)
        }

        self.setDocExample(collector=self.collector.__class__.__name__,
                           metrics=metrics,
                           defaultpath=self.collector.config['path'])
        self.assertPublishedMany(publish_mock, metrics)

    @patch.object(Collector, 'publish')
    def test_should_compute_cpus_utilisation(self, publish_mock):
        self.fixture_cpu_utilisation(publish_mock)

        metrics = {
            'frameworks.marathon-0_7_6.executors.task_name.'
            '09b6f20c-b6a9-11e4-99f6-fa163ef210c0.cpus_utilisation':
            0.25,
            'frameworks.marathon-0_7_6.executors.task_name.'
            '06247c78-b6a9-11e4-99f6-fa163ef210c0.cpus_utilisation':
            0.25,
            'frameworks.marathon-0_7_6.executors.task_name.'
            'cpus_utilisation':
            0.5,
        }

        self.assertPublishedMany(publish_mock, metrics)

    @patch.object(Collector, 'publish')
    def test_should_fail_gracefully(self, publish_mock):
        patch_urlopen = patch(
            'urllib2.urlopen',
            Mock(return_value=self.getFixture('metrics_blank')))

        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()

        self.assertPublishedMany(publish_mock, {})

    @patch.object(Collector, 'publish')
    def test_should_compute_cpus_percent(self, publish_mock):
        self.fixture_cpu_utilisation(publish_mock)

        self.assertPublished(
            publish_mock,
            'frameworks.marathon-0_7_6.executors.task_name.cpus_percent',
            0.5 / 1.7)

    def fixture_cpu_utilisation(self, publish_mock):
        config = get_collector_config('MesosCollector', {'master': False})
        self.collector = MesosCollector(config, None)
        self.assertEqual(self.collector.master, False)
        # we need 2 collect calls to see new metrics
        returns = [
            self.getFixture('master_metrics_snapshot.json'),
            self.getFixture('slave_metrics_state.json'),
            self.getFixture(
                'slave_monitor_statistics_cpus_utilisation_next.json'),
            self.getFixture('master_metrics_snapshot.json'),
            self.getFixture('slave_metrics_state.json'),
            self.getFixture('slave_monitor_statistics_cpus_utilisation.json'),
        ]
        urlopen_mock = patch('urllib2.urlopen',
                             Mock(side_effect=lambda *args: returns.pop(0)))
        urlopen_mock.start()
        self.collector.collect()
        publish_mock.reset_mock()
        self.collector.collect()
        urlopen_mock.stop()

    def test_http(self):
        self.collector.config['host'] = 'localhost'
        self.assertEqual('http://localhost:5050/metrics/snapshot',
                         self.collector._get_url("metrics/snapshot"))

    def test_https(self):
        self.collector.config['host'] = 'https://localhost'
        self.assertEqual('https://localhost:5050/metrics/snapshot',
                         self.collector._get_url("metrics/snapshot"))

    def test_sum_statistics(self):
        metrics_1 = {'cpu': 50, 'mem': 30, 'loadavg': 1}
        metrics_2 = {'cpu': 10, 'mem': 30, 'network': 10}
        self.assertEqual(self.collector._sum_statistics(metrics_1, metrics_2),
                         {
                             'mem': 60,
                             'loadavg': 1,
                             'network': 10,
                             'cpu': 60
                         })