~ahasenack/landscape-client/landscape-client-1.5.5-0ubuntu0.9.04.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import tempfile

from landscape.monitor.temperature import Temperature
from landscape.lib.tests.test_sysstats import ThermalZoneTest
from landscape.tests.helpers import MonitorHelper
from landscape.tests.mocker import ANY


class TemperatureTestWithSampleData(ThermalZoneTest):
    """Tests for the temperature plugin."""

    helpers = [MonitorHelper]

    def setUp(self):
        """Initialize test helpers and create a sample thermal zone."""
        super(TemperatureTestWithSampleData, self).setUp()
        self.mstore.set_accepted_types(["temperature"])
        self.write_thermal_zone("ZONE1", "50 C")

    def test_wb_disabled_with_no_thermal_zones(self):
        """
        When no thermal zones are available /proc/acpi/thermal_zone
        will be empty.  In this case, the plugin won't register itself
        to respond to client events such as exchange.
        """
        thermal_zone_path = tempfile.mkdtemp()
        os.rmdir(thermal_zone_path)
        plugin = Temperature(thermal_zone_path=thermal_zone_path)
        self.assertEquals(plugin._thermal_zones, [])

    def test_no_messages_without_thermal_zones(self):
        """
        Messages should never be generated by the plugin when no
        thermal zones are available.
        """
        thermal_zone_path = self.make_dir()
        plugin = Temperature(interval=1, thermal_zone_path=thermal_zone_path)
        self.monitor.add(plugin)
        self.reactor.advance(self.monitor.step_size)
        self.assertEquals(len(self.mstore.get_pending_messages()), 0)

    def test_disjointed_thermal_zone_temperature_changes(self):
        """
        Changing data needs to be tracked according to the thermal
        zone the data is for.  This test ensures that the plugin
        creates messages with changes reported correctly.
        """
        self.write_thermal_zone("ZONE2", "50 C")
        plugin = Temperature(thermal_zone_path=self.thermal_zone_path,
                             create_time=self.reactor.time)
        step_size = self.monitor.step_size
        self.monitor.add(plugin)

        self.reactor.advance(step_size)

        self.write_thermal_zone("ZONE2", "56 C")
        self.reactor.advance(step_size)

        messages = list(plugin.create_messages())
        self.assertEquals(len(messages), 2)

        self.assertEquals(messages[0]["thermal-zone"], "ZONE1")
        self.assertEquals(len(messages[0]["temperatures"]), 2)
        self.assertEquals(messages[0]["temperatures"][0],
                          (step_size, 50.0))
        self.assertEquals(messages[0]["temperatures"][1],
                          (step_size * 2, 50.0))

        self.assertEquals(messages[1]["thermal-zone"], "ZONE2")
        self.assertEquals(len(messages[1]["temperatures"]), 2)
        self.assertEquals(messages[1]["temperatures"][0],
                          (step_size, 50.0))
        self.assertEquals(messages[1]["temperatures"][1],
                          (step_size * 2, 56.0))

    def test_messaging_flushes(self):
        """
        Duplicate message should never be created.  If no data is
        available, a message with an empty C{temperatures} list is
        expected.
        """
        plugin = Temperature(thermal_zone_path=self.thermal_zone_path,
                             create_time=self.reactor.time)
        self.monitor.add(plugin)

        self.reactor.advance(self.monitor.step_size)

        messages = plugin.create_messages()
        self.assertEquals(len(messages), 1)

        messages = plugin.create_messages()
        self.assertEquals(len(messages), 0)

    def test_never_exchange_empty_messages(self):
        """
        The plugin will only create messages when data is available.
        If no data is available when an exchange occurs no messages
        should not be queued.
        """
        self.write_thermal_zone("ZONE2", "50 C")
        plugin = Temperature(thermal_zone_path=self.thermal_zone_path,
                             create_time=self.reactor.time)
        step_size = self.monitor.step_size
        self.monitor.add(plugin)
        self.assertEquals(len(self.mstore.get_pending_messages()), 0)

    def test_exchange_messages(self):
        """
        The temperature plugin queues message when an exchange
        happens.  Each message should be aligned to a step boundary;
        messages collected bewteen exchange periods should be
        delivered in a single message.
        """
        self.write_thermal_zone("ZONE2", "50 C")
        plugin = Temperature(thermal_zone_path=self.thermal_zone_path,
                             create_time=self.reactor.time)
        step_size = self.monitor.step_size
        self.monitor.add(plugin)
        self.reactor.advance(step_size)
        self.monitor.exchange()

        self.assertMessages(self.mstore.get_pending_messages(),
                            [{"type": "temperature",
                              "thermal-zone": "ZONE1",
                              "temperatures": [(step_size, 50.0)]},
                             {"type": "temperature",
                              "thermal-zone": "ZONE2",
                              "temperatures": [(step_size, 50.0)]}])

    def test_no_messages_on_bad_values(self):
        """
        If the temperature is in an unknown format, the plugin won't
        break and no messages are sent.
        """
        self.write_thermal_zone("ZONE1", "UNKNOWN C")
        plugin = Temperature(thermal_zone_path=self.thermal_zone_path,
                             create_time=self.reactor.time)
        step_size = self.monitor.step_size
        self.monitor.add(plugin)
        self.reactor.advance(step_size)
        self.monitor.exchange()

        self.assertMessages(self.mstore.get_pending_messages(), [])

    def test_call_on_accepted(self):
        plugin = Temperature(thermal_zone_path=self.thermal_zone_path,
                             create_time=self.reactor.time)
        self.monitor.add(plugin)

        self.reactor.advance(plugin.registry.step_size)

        remote_broker_mock = self.mocker.replace(self.remote)
        remote_broker_mock.send_message(ANY, urgent=True)
        self.mocker.replay()

        self.reactor.fire(("message-type-acceptance-changed", "temperature"),
                          True)

    def test_no_message_if_not_accepted(self):
        """
        Don't add any messages at all if the broker isn't currently
        accepting their type.
        """
        self.mstore.set_accepted_types([])
        plugin = Temperature(thermal_zone_path=self.thermal_zone_path,
                             create_time=self.reactor.time)
        self.monitor.add(plugin)
        self.reactor.advance(self.monitor.step_size * 2)
        self.monitor.exchange()

        self.mstore.set_accepted_types(["temperature"])
        self.assertMessages(list(self.mstore.get_pending_messages()), [])