Пример #1
0
    def test_collect_stats(self):
        """``CollectorThread`` 'collect_stats' queries for data, then uploads to InfluxDB"""
        fake_stat_collector = MagicMock()
        fake_stat_collector.counter_name = 'someStat'
        fake_stat_collector.query.return_value = {123456789: 42}
        collector = vsphere_collectors.CollectorThread(self.vcenter,
                                                       self.influx,
                                                       'someThingInVMware')
        collector._stats = ['someStat']
        collector.collectors = [fake_stat_collector]

        collector.collect_stats()
        _, the_kwargs = self.influx.write.call_args
        expected = {
            'fields': {
                'someStat': 42
            },
            'tags': {
                'name': 'someThingInVMware',
                'kind': 'None'
            },
            'timestamp': 123456789
        }

        self.assertEqual(the_kwargs, expected)
Пример #2
0
    def test_init(self):
        """``CollectorThread`` '__init__' params have not changed"""
        collector = vsphere_collectors.CollectorThread(self.vcenter,
                                                       self.influx,
                                                       'someThingInVMware')

        self.assertTrue(
            isinstance(collector, vsphere_collectors.CollectorThread))
Пример #3
0
    def test_entity(self):
        """``CollectorThread`` the entity property returns the VMware object"""
        collector = vsphere_collectors.CollectorThread(self.vcenter,
                                                       self.influx,
                                                       'someThingInVMware')
        collector.find_entity = lambda: 'someEntity'

        entity = collector.entity()
        expected = 'someEntity'

        self.assertEqual(entity, expected)
Пример #4
0
    def test_setup_collectors(self):
        """``CollectorThread`` 'setup_collectors' sets the 'collectors' attr on the object"""
        collector = vsphere_collectors.CollectorThread(self.vcenter,
                                                       self.influx,
                                                       'someThingInVMware')

        collector.collectors = MagicMock(
        )  # explictly setting it here, so we can test if it gets changed
        collector.setup_collectors()
        expected = []

        self.assertEqual(collector.collectors, expected)
Пример #5
0
    def test_run(self, fake_time):
        """``CollectorThread`` 'run' collects stats, then sleeps"""
        fake_time.time.return_value = 1
        fake_collect_stats = MagicMock()
        collector = vsphere_collectors.CollectorThread(self.vcenter,
                                                       self.influx,
                                                       'someThingInVMware')
        collector.collect_stats = fake_collect_stats

        collector.start()
        collector.keep_running = False
        collector.join()

        self.assertTrue(fake_time.sleep.called)
Пример #6
0
    def test_collect_stats_setup(self):
        """``CollectorThread`` 'collect_stats' will setup the collectors if needed"""
        fake_stat_collector = MagicMock()
        fake_stat_collector.counter_name = 'someStat'
        fake_stat_collector.query.return_value = {123456789: 42}
        fake_setup_collectors = MagicMock()
        fake_setup_collectors.return_value = [fake_stat_collector]
        collector = vsphere_collectors.CollectorThread(self.vcenter,
                                                       self.influx,
                                                       'someThingInVMware')
        collector._stats = ['someStat']
        collector.setup_collectors = fake_setup_collectors

        collector.collect_stats()

        self.assertTrue(fake_setup_collectors.called)
Пример #7
0
    def test_entity_cached(self):
        """``CollectorThread`` the entity property caches the object, instead of looking it up constantly"""
        fake_find_entity = MagicMock()
        collector = vsphere_collectors.CollectorThread(self.vcenter,
                                                       self.influx,
                                                       'someThingInVMware')
        collector.find_entity = fake_find_entity

        collector.entity()
        collector.entity()
        collector.entity()
        collector.entity()

        actual_calls = fake_find_entity.call_count
        expected_calls = 1

        self.assertEqual(actual_calls, expected_calls)
Пример #8
0
    def test_run_no_sleep(self, fake_time):
        """``CollectorThread`` 'run' sleeps for zero seconds if the collection took longer than the loop_interval"""
        fake_time.time.side_effect = [x * 500 for x in range(500)]
        fake_collect_stats = MagicMock()
        collector = vsphere_collectors.CollectorThread(self.vcenter,
                                                       self.influx,
                                                       'someThingInVMware')
        collector.collect_stats = fake_collect_stats

        collector.start()
        collector.keep_running = False
        collector.join()

        the_args, _ = fake_time.sleep.call_args
        slept_for = the_args[0]
        expected = 0

        self.assertEqual(slept_for, expected)