~ubuntu-branches/ubuntu/vivid/ceilometer/vivid

« back to all changes in this revision

Viewing changes to ceilometer/tests/hardware/pollsters/base.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-03-06 14:44:28 UTC
  • mto: (28.1.1 utopic-proposed) (1.2.1)
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: package-import@ubuntu.com-20140306144428-rvphsh4igwyulzf0
Tags: upstream-2014.1~b3
Import upstream version 2014.1~b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
#
 
3
# Copyright © 2013 Intel Corp
 
4
#
 
5
# Authors: Lianhao Lu <lianhao.lu@intel.com>
 
6
#
 
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
8
# not use this file except in compliance with the License. You may obtain
 
9
# a copy of the License at
 
10
#
 
11
#      http://www.apache.org/licenses/LICENSE-2.0
 
12
#
 
13
# Unless required by applicable law or agreed to in writing, software
 
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
16
# License for the specific language governing permissions and limitations
 
17
# under the License.
 
18
 
 
19
import fixtures
 
20
import mock
 
21
 
 
22
from ceilometer.central import manager
 
23
from ceilometer.hardware.inspector import base as inspector_base
 
24
from ceilometer.tests import base as test_base
 
25
 
 
26
 
 
27
class FakeInspector(inspector_base.Inspector):
 
28
    CPU = inspector_base.CPUStats(cpu_1_min=0.99,
 
29
                                  cpu_5_min=0.77,
 
30
                                  cpu_15_min=0.55)
 
31
    DISK = (inspector_base.Disk(device='/dev/sda1', path='/'),
 
32
            inspector_base.DiskStats(size=1000, used=90))
 
33
    MEMORY = inspector_base.MemoryStats(total=1000, used=90)
 
34
    NET = (inspector_base.Interface(name='test.teest',
 
35
                                    mac='001122334455',
 
36
                                    ip='10.0.0.2'),
 
37
           inspector_base.InterfaceStats(bandwidth=1000,
 
38
                                         rx_bytes=90,
 
39
                                         tx_bytes=80,
 
40
                                         error=1))
 
41
 
 
42
    def inspect_cpu(self, host):
 
43
        yield self.CPU
 
44
 
 
45
    def inspect_disk(self, host):
 
46
        yield self.DISK
 
47
 
 
48
    def inspect_memory(self, host):
 
49
        yield self.MEMORY
 
50
 
 
51
    def inspect_network(self, host):
 
52
        yield self.NET
 
53
 
 
54
 
 
55
class TestPollsterBase(test_base.BaseTestCase):
 
56
    def faux_get_inspector(url, namespace=None):
 
57
        return FakeInspector()
 
58
 
 
59
    def setUp(self):
 
60
        super(TestPollsterBase, self).setUp()
 
61
        self.host = ["test://test"]
 
62
        self.useFixture(fixtures.MonkeyPatch(
 
63
            'ceilometer.hardware.inspector.get_inspector',
 
64
            self.faux_get_inspector))
 
65
 
 
66
    @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
 
67
    def _check_get_samples(self, factory, name,
 
68
                           expected_value, expected_type):
 
69
        mgr = manager.AgentManager()
 
70
        pollster = factory()
 
71
        cache = {}
 
72
        samples = list(pollster.get_samples(mgr, cache, self.host))
 
73
        self.assertTrue(samples)
 
74
        self.assertIn(pollster.CACHE_KEY, cache)
 
75
        self.assertIn(self.host[0], cache[pollster.CACHE_KEY])
 
76
 
 
77
        self.assertEqual(set([s.name for s in samples]),
 
78
                         set([name]))
 
79
        match = [s for s in samples if s.name == name]
 
80
        self.assertEqual(match[0].volume, expected_value)
 
81
        self.assertEqual(match[0].type, expected_type)