3
from landscape.monitor.computerinfo import ComputerInfo
4
from landscape.tests.helpers import LandscapeTest, MakePathHelper, MonitorHelper
5
from landscape.tests.mocker import ANY
12
class ComputerInfoTest(LandscapeTest):
14
helpers = [MonitorHelper, MakePathHelper]
16
sample_memory_info = """
34
CommitLimit: 2396060 kB
35
Committed_AS: 1166936 kB
37
VmallocTotal: 114680 kB
39
VmallocChunk: 107432 kB
43
LandscapeTest.setUp(self)
44
self.lsb_release_filename = self.make_path("""\
47
DISTRIB_CODENAME=dapper
48
DISTRIB_DESCRIPTION="Ubuntu 6.06.1 LTS"
51
def test_get_hostname(self):
52
self.mstore.set_accepted_types(["computer-info"])
53
plugin = ComputerInfo(get_hostname=get_hostname,
54
lsb_release_filename=self.lsb_release_filename)
55
self.monitor.add(plugin)
57
messages = self.mstore.get_pending_messages()
58
self.assertEquals(len(messages), 1)
59
self.assertEquals(messages[0]["type"], "computer-info")
60
self.assertEquals(messages[0]["hostname"], "ooga")
62
def test_get_real_hostname(self):
63
self.mstore.set_accepted_types(["computer-info"])
64
plugin = ComputerInfo()
65
self.monitor.add(plugin)
67
messages = self.mstore.get_pending_messages()
68
self.assertEquals(len(messages), 1)
69
self.assertEquals(messages[0]["type"], "computer-info")
70
self.assertNotEquals(len(messages[0]["hostname"]), 0)
71
self.assertTrue(re.search("\w", messages[0]["hostname"]))
73
def test_only_report_changed_hostnames(self):
74
self.mstore.set_accepted_types(["computer-info"])
75
plugin = ComputerInfo(get_hostname=get_hostname)
76
self.monitor.add(plugin)
78
messages = self.mstore.get_pending_messages()
79
self.assertEquals(len(messages), 1)
81
messages = self.mstore.get_pending_messages()
82
self.assertEquals(len(messages), 1)
84
def test_report_changed_hostnames(self):
85
def hostname_factory(hostnames=["ooga", "wubble", "wubble"]):
87
while i < len(hostnames):
91
self.mstore.set_accepted_types(["computer-info"])
92
plugin = ComputerInfo(get_hostname=hostname_factory().next)
93
self.monitor.add(plugin)
96
messages = self.mstore.get_pending_messages()
97
self.assertEquals(len(messages), 1)
98
self.assertEquals(messages[0]["hostname"], "ooga")
101
messages = self.mstore.get_pending_messages()
102
self.assertEquals(len(messages), 2)
103
self.assertEquals(messages[1]["hostname"], "wubble")
105
def test_get_total_memory(self):
106
self.mstore.set_accepted_types(["computer-info"])
107
meminfo_filename = self.make_path(self.sample_memory_info)
108
plugin = ComputerInfo(meminfo_file=meminfo_filename)
109
self.monitor.add(plugin)
111
messages = self.mstore.get_pending_messages()
112
message = messages[0]
113
self.assertEquals(message["type"], "computer-info")
114
self.assertEquals(message["total-memory"], 1510)
115
self.assertEquals(message["total-swap"], 1584)
117
def test_get_real_total_memory(self):
118
self.mstore.set_accepted_types(["computer-info"])
119
meminfo_filename = self.make_path(self.sample_memory_info)
120
plugin = ComputerInfo()
121
self.monitor.add(plugin)
123
message = self.mstore.get_pending_messages()[0]
124
self.assertEquals(message["type"], "computer-info")
125
self.assertTrue(isinstance(message["total-memory"], int))
126
self.assertTrue(isinstance(message["total-swap"], int))
128
def test_wb_report_changed_total_memory(self):
129
self.mstore.set_accepted_types(["computer-info"])
130
plugin = ComputerInfo()
131
self.monitor.add(plugin)
133
plugin._get_memory_info = lambda: (1510, 1584)
135
message = self.mstore.get_pending_messages()[0]
136
self.assertEquals(message["total-memory"], 1510)
137
self.assertTrue("total-swap" in message)
139
plugin._get_memory_info = lambda: (2048, 1584)
141
message = self.mstore.get_pending_messages()[1]
142
self.assertEquals(message["total-memory"], 2048)
143
self.assertTrue("total-swap" not in message)
145
def test_wb_report_changed_total_swap(self):
146
self.mstore.set_accepted_types(["computer-info"])
147
plugin = ComputerInfo()
148
self.monitor.add(plugin)
150
plugin._get_memory_info = lambda: (1510, 1584)
152
message = self.mstore.get_pending_messages()[0]
153
self.assertEquals(message["total-swap"], 1584)
154
self.assertTrue("total-memory" in message)
156
plugin._get_memory_info = lambda: (1510, 2048)
158
message = self.mstore.get_pending_messages()[1]
159
self.assertEquals(message["total-swap"], 2048)
160
self.assertTrue("total-memory" not in message)
162
def test_get_distribution(self):
164
Various details about the distribution should be reported by
165
the plugin. This test ensures that the right kinds of details
166
end up in messages produced by the plugin.
168
self.mstore.set_accepted_types(["distribution-info"])
169
plugin = ComputerInfo()
170
self.monitor.add(plugin)
173
message = self.mstore.get_pending_messages()[0]
174
self.assertEquals(message["type"], "distribution-info")
175
self.assertTrue("distributor-id" in message)
176
self.assertTrue("description" in message)
177
self.assertTrue("release" in message)
178
self.assertTrue("code-name" in message)
180
def test_get_sample_distribution(self):
182
Sample data is used to ensure that expected values end up in
183
the distribution data reported by the plugin.
185
self.mstore.set_accepted_types(["distribution-info"])
186
plugin = ComputerInfo(lsb_release_filename=self.lsb_release_filename)
187
self.monitor.add(plugin)
190
message = self.mstore.get_pending_messages()[0]
191
self.assertEquals(message["type"], "distribution-info")
192
self.assertEquals(message["distributor-id"], "Ubuntu")
193
self.assertEquals(message["description"], "Ubuntu 6.06.1 LTS")
194
self.assertEquals(message["release"], "6.06")
195
self.assertEquals(message["code-name"], "dapper")
197
def test_report_once(self):
199
Distribution data shouldn't be reported unless it's changed
200
since the last time it was reported.
202
self.mstore.set_accepted_types(["distribution-info"])
203
plugin = ComputerInfo()
204
self.monitor.add(plugin)
207
messages = self.mstore.get_pending_messages()
208
self.assertEquals(len(messages), 1)
209
self.assertTrue(messages[0]["type"], "distribution-info")
212
messages = self.mstore.get_pending_messages()
213
self.assertEquals(len(messages), 1)
215
def test_wb_report_changed_distribution(self):
217
When distribution data changes, the new data should be sent to
220
self.mstore.set_accepted_types(["distribution-info"])
221
plugin = ComputerInfo(lsb_release_filename=self.lsb_release_filename)
222
self.monitor.add(plugin)
225
message = self.mstore.get_pending_messages()[0]
226
self.assertEquals(message["type"], "distribution-info")
227
self.assertEquals(message["distributor-id"], "Ubuntu")
228
self.assertEquals(message["description"], "Ubuntu 6.06.1 LTS")
229
self.assertEquals(message["release"], "6.06")
230
self.assertEquals(message["code-name"], "dapper")
232
plugin._lsb_release_filename = self.make_path("""\
235
DISTRIB_CODENAME=edgy
236
DISTRIB_DESCRIPTION="Ubuntu 6.10"
239
message = self.mstore.get_pending_messages()[1]
240
self.assertEquals(message["type"], "distribution-info")
241
self.assertEquals(message["distributor-id"], "Ubuntu")
242
self.assertEquals(message["description"], "Ubuntu 6.10")
243
self.assertEquals(message["release"], "6.10")
244
self.assertEquals(message["code-name"], "edgy")
246
def test_unknown_distribution_key(self):
247
self.mstore.set_accepted_types(["distribution-info"])
248
lsb_release_filename = self.make_path("""\
251
DISTRIB_CODENAME=edgy
252
DISTRIB_DESCRIPTION="Ubuntu 6.10"
253
DISTRIB_NEW_UNEXPECTED_KEY=ooga
255
plugin = ComputerInfo(lsb_release_filename=lsb_release_filename)
256
self.monitor.add(plugin)
259
message = self.mstore.get_pending_messages()[0]
260
self.assertEquals(message["type"], "distribution-info")
261
self.assertEquals(message["distributor-id"], "Ubuntu")
262
self.assertEquals(message["description"], "Ubuntu 6.10")
263
self.assertEquals(message["release"], "6.10")
264
self.assertEquals(message["code-name"], "edgy")
266
def test_resynchronize(self):
268
If a reactor event "resynchronize" is received, messages for
269
all computer info should be generated.
271
self.mstore.set_accepted_types(["distribution-info", "computer-info"])
272
meminfo_filename = self.make_path(self.sample_memory_info)
273
plugin = ComputerInfo(get_hostname=get_hostname,
274
meminfo_file=meminfo_filename,
275
lsb_release_filename=self.lsb_release_filename)
276
self.monitor.add(plugin)
278
self.reactor.fire("resynchronize")
280
computer_info = {"type": "computer-info", "hostname": "ooga",
281
"timestamp": 0, "total-memory": 1510,
283
dist_info = {"type": "distribution-info",
284
"code-name": "dapper", "description": "Ubuntu 6.06.1 LTS",
285
"distributor-id": "Ubuntu", "release": "6.06"}
286
self.assertMessages(self.mstore.get_pending_messages(),
287
[computer_info, dist_info,
288
computer_info, dist_info])
290
def test_computer_info_call_on_accepted(self):
291
plugin = ComputerInfo()
292
self.monitor.add(plugin)
294
remote_broker_mock = self.mocker.replace(self.remote)
295
remote_broker_mock.send_message(ANY, urgent=True)
298
self.mstore.set_accepted_types(["computer-info"])
299
self.reactor.fire(("message-type-acceptance-changed", "computer-info"),
302
def test_distribution_info_call_on_accepted(self):
303
plugin = ComputerInfo()
304
self.monitor.add(plugin)
306
remote_broker_mock = self.mocker.replace(self.remote)
307
remote_broker_mock.send_message(ANY, urgent=True)
310
self.mstore.set_accepted_types(["distribution-info"])
311
self.reactor.fire(("message-type-acceptance-changed",
312
"distribution-info"),
315
def test_message_if_not_accepted(self):
317
Don't add any messages at all if the broker isn't currently
318
accepting their type.
320
plugin = ComputerInfo()
321
self.monitor.add(plugin)
322
self.reactor.advance(self.monitor.step_size * 2)
323
self.monitor.exchange()
325
self.mstore.set_accepted_types(["distribution-info", "computer-info"])
326
self.assertMessages(list(self.mstore.get_pending_messages()), [])