def test_get_samples(self):
        next_value = iter((
            virt_inspector.MemoryUsageStats(usage=1.0),
            virt_inspector.MemoryUsageStats(usage=2.0),
        ))

        def inspect_memory_usage(instance, duration):
            return six.next(next_value)

        (self.inspector.inspect_memory_usage) = mock.Mock(
            side_effect=inspect_memory_usage)

        mgr = manager.AgentManager()
        pollster = memory.MemoryUsagePollster()

        def _verify_memory_metering(expected_memory_mb):
            cache = {}
            samples = list(pollster.get_samples(mgr, cache, [self.instance]))
            self.assertEqual(1, len(samples))
            self.assertEqual(set(['memory.usage']),
                             set([s.name for s in samples]))
            self.assertEqual(expected_memory_mb, samples[0].volume)

        _verify_memory_metering(1.0)
        _verify_memory_metering(2.0)
Exemple #2
0
    def test_get_samples_with_empty_stats(self):
        def inspect_memory_usage(instance, duration):
            raise virt_inspector.NoDataException()

        self.inspector.inspect_memory_usage = mock.Mock(
            side_effect=inspect_memory_usage)

        mgr = manager.AgentManager(0, self.CONF)
        pollster = memory.MemoryUsagePollster(self.CONF)

        def all_samples():
            return list(pollster.get_samples(mgr, {}, [self.instance]))

        self.assertRaises(plugin_base.PollsterPermanentError, all_samples)
Exemple #3
0
    def test_get_samples(self):
        next_value = iter((
            virt_inspector.MemoryUsageStats(usage=1.0),
            virt_inspector.MemoryUsageStats(usage=2.0),
            virt_inspector.InstanceNoDataException(),
            virt_inspector.InstanceShutOffException(),
        ))

        def inspect_memory_usage(instance, duration):
            value = next(next_value)
            if isinstance(value, virt_inspector.MemoryUsageStats):
                return value
            else:
                raise value

        self.inspector.inspect_memory_usage = mock.Mock(
            side_effect=inspect_memory_usage)

        mgr = manager.AgentManager(0, self.CONF)
        pollster = memory.MemoryUsagePollster(self.CONF)

        @mock.patch('ceilometer.compute.pollsters.memory.LOG')
        def _verify_memory_metering(expected_count, expected_memory_mb,
                                    expected_warnings, mylog):
            samples = list(pollster.get_samples(mgr, {}, [self.instance]))
            self.assertEqual(expected_count, len(samples))
            if expected_count > 0:
                self.assertEqual(set(['memory.usage']),
                                 set([s.name for s in samples]))
                self.assertEqual(expected_memory_mb, samples[0].volume)
            else:
                self.assertEqual(expected_warnings, mylog.warning.call_count)
            self.assertEqual(0, mylog.exception.call_count)

        _verify_memory_metering(1, 1.0, 0)
        _verify_memory_metering(1, 2.0, 0)
        _verify_memory_metering(0, 0, 1)
        _verify_memory_metering(0, 0, 0)