File: testpoller.py

package info (click to toggle)
lava-coordinator 0.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 244 kB
  • ctags: 114
  • sloc: python: 1,237; xml: 144; sh: 76; makefile: 25
file content (748 lines) | stat: -rw-r--r-- 33,082 bytes parent folder | download | duplicates (3)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  testpoller.py
#
#  Copyright 2013 Neil Williams <codehelp@debian.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#

import unittest
import logging
import sys
import uuid
import json
from lava.coordinator import LavaCoordinator


bundle_sample = {
    "test_runs": [
        {"software_context": {
            "sources": [
                {"branch_url": "git://git.linaro.org/people/neilwilliams/multinode-yaml.git",
                 "branch_vcs": "git", "project_name":
                    "multinode-yaml", "branch_revision": "f61e707c6d3da75d90735a75e6dc6aca55f1142b"}],
            "image": {"name": ""}, "packages": [
            {"version": "1:2.0.16-1+deb7u1", "name": "acpid"},
            {"version": "1:1.2.7.dfsg-13", "name": "zlib1g"}]},
         "attachments": [
             {"content": "", "pathname": "stdout.log", "mime_type": "text/plain"},
             {"content": "", "pathname": "testdef.yaml", "mime_type": "text/plain"},
             {"content": "MAo=", "pathname": "return_code", "mime_type": "text/plain"},
             {"content": "", "pathname": "run.sh", "mime_type": "text/plain"}],
         "analyzer_assigned_date": "2013-08-13T19:27:41Z",
            "time_check_performed": False,
            "test_results": [
                {"result": "pass", "attributes": {},
                 "attachments": [], "test_case_id": "linux-linaro-ubuntu-pwd"},
                {"result": "pass", "attributes": {}, "attachments": [], "test_case_id": "multinode-role-output"},
                {"result": "pass", "attributes": {}, "attachments": [], "test_case_id": "multinode-lava-network"}],
            "testdef_metadata": {
                "description": "Basic MultiNode test commands for Linux Linaro ubuntu Images",
                "format": "Lava-Test Test Definition 1.0",
                "url": "git://git.linaro.org/people/neilwilliams/multinode-yaml.git",
                "version": "f61e707c6d3da75d90735a75e6dc6aca55f1142b", "location": "GIT"},
            "hardware_context": {
                "devices": [
                    {"attributes": {"power management": "", "cpuid level": "4", "model": "2", "wp": "yes"},
                     "description": "Processor #0"}]},
            "analyzer_assigned_uuid": "2eb4898b-ba33-42e1-ab71-025f18feef81", "attributes":
            {"target_group": "da4ed985-80e9-43bf-acd5-4e18d03300b9", "target":
                "multinode-kvm01", "target.hostname": "multinode-kvm01", "target.device_version": "1.0",
                "role": "felix", "target.device_type": "kvm", "logging_level": "DEBUG", "group_size": "2"},
            "test_id": "smoke-tests-multinode"}], "format": "Dashboard Bundle Format 1.6"}


class TestSignals(object):

    message_str = ''

    def formatString(self, reply):
        if type(reply) is dict:
            for target, messages in reply.items():
                for key, value in messages.items():
                    self.message_str += " %s:%s=%s" % (target, key, value)
        return self.message_str

    def checkMessage(self, reply):
        if reply is not None:
            self.log = logging.getLogger("testCase")
            self.log.info("\t<LAVA_TEST_COMPLETE%s>" % self.formatString(reply))


class TestSocket(object):

    response = None
    header = True
    log = None
    message = None
    passes = 0
    signalHandler = None

    def __init__(self):
        self.log = logging.getLogger("testCase")
        self.signalHandler = TestSignals()

    def send(self, data):
        if self.header:
            self.header = False
            assert(int(data, 16) < 0xFFFE)
            self.log.info("\tCoordinator header: %d bytes" % int(data, 16))
        else:
            try:
                json_data = json.loads(data)
            except ValueError:
                assert False
            if not self.response:
                assert(json_data['response'] == "nack")
                self.header = True
                return
            assert 'response' in json_data
            self.log.info("\tCoordinator response: '%s'" % json_data['response'])
            self.log.info("\tdebug: %s" % json.dumps(json_data))
            assert(json_data['response'] == self.response)
            self.passes += 1
            if self.message:
                # we are expecting a message back.
                assert 'message' in json_data
                self.log.info("\tCoordinator received a message: '%s'" % (json.dumps(json_data['message'])))
                assert(json_data['message'] == self.message)
                self.passes += 1
            else:
                # actual calls will discriminate between dict and string replies
                # according to the call prototype itself
                if "message" in json_data:
                    if type(json_data['message']) is dict:
                        self.log.info("\tCould have expected a message: '%s'" % json.dumps(json_data['message']))
                    else:
                        self.log.info("\t<LAVA_TEST_REPLY %s>" % json_data['message'])
                self.passes += 1
            self.header = True

    def close(self):
        self.log.info("\tCoordinator closing.")

    def clearPasses(self):
        self.passes = 0

    def logPasses(self):
        if self.passes == 1:
            self.log.info("\tCoordinator: %d socket test passed" % self.passes)
        else:
            self.log.info("\tCoordinator: %d socket tests passed" % self.passes)

    def prepare(self, name):
        self.response = name
        if self.response:
            self.log.info("\tCoordinator: expecting a response: '%s'" % self.response)

    def validate(self, message):
        self.message = message
        if self.message:
            self.log.info("\tCoordinator: expecting a message: '%s'" % json.dumps(self.message))
        self.signalHandler.checkMessage(self.message)


class TestCoordinator(LavaCoordinator):

    running = True
    json_data = None
    group_name = None
    group_size = 0
    client_name = None
    conn = None
    log = None

    def __init__(self):
        super(LavaCoordinator, self).__init__()
        self.group_name = str(uuid.uuid4())
        self.conn = TestSocket()
        self.log = logging.getLogger("testCase")
        self.log.info("")
        self.json_data = {"request": "testing"}
        self.client_name = "testpoller"
        self.log.info("\tStarting test with %s %d %d %s" %
                      (json.dumps(self.json_data), self.rpc_delay,
                       self.blocksize, self.host))
        self.expectResponse(None)

    def newGroup(self, size):
        self.group_name = str(uuid.uuid4())
        self.group_size = size
        self.log = logging.getLogger("testCase")
        self.log.info("\tGroup name %s" % self.group_name)

    # sets up TestSocket for the correct assertions
    def expectResponse(self, test_name):
        self.conn.prepare(test_name)

    def expectMessage(self, message):
        self.conn.validate(message)

    def addClient(self, client_name):
        self.conn.response = "ack"
        self.client_name = client_name
        self.log = logging.getLogger("testCase")
        ret = self._updateData({"client_name": client_name,
                                "group_size": self.group_size,
                                "role": "tester",
                                "hostname": "localhost",
                                "group_name": self.group_name})
        self.log.info("\tAdded client_name '%s'. group size now: %d" %
                      (client_name, len(self.group['clients'])))
        self.log.info("\tCurrent client_name: '%s'" % self.client_name)
        return ret

    def addClientRole(self, client_name, role):
        self.conn.response = "ack"
        self.client_name = client_name
        self.log = logging.getLogger("testCase")
        ret = self._updateData({"client_name": client_name,
                                "group_size": self.group_size,
                                "role": role,
                                "hostname": "localhost",
                                "group_name": self.group_name})
        self.log.info("\tAdded client_name '%s' with role '%s'. group size now: %d" %
                      (client_name, role, len(self.group['clients'])))
        self.log.info("\tCurrent client_name: '%s'" % self.client_name)
        return ret


class TestPoller(unittest.TestCase):

    coord = None
    role = None

    def setUp(self):
        self.coord = TestCoordinator()

    def _wrapMessage(self, message, role):
        base_msg = {
            "timeout": 90,
            "client_name": self.coord.client_name,
            "group_name": self.coord.group_name,
            "role": role,
        }
        base_msg.update(message)
        # uncomment to get verbose output
#        self.log = logging.getLogger("testCase")
#        self.log.info("\tmessage content: '%s'" % json.dumps(base_msg))
        return base_msg

    def _switch_client(self, name):
        self.coord.client_name = name

    def _cleanup(self):
        self.log = logging.getLogger("testCase")
        self.log.info("\tClearing group %s after test" % self.coord.group_name)
        old_name = self.coord.group_name
        self.coord.expectResponse("ack")
        self.coord.expectMessage(None)
        while self.coord.group_size > 0:
            self.coord._clearGroupData({"group_name": old_name})
            self.coord.group_size -= 1
        # clear the group name and data
        self.assertTrue(self.coord.group['group'] != old_name)
        self.assertTrue(self.coord.group['group'] == '')
        self.log.info("\tGroup %s cleared correctly." % old_name)
        self.coord.conn.clearPasses()

    def test_01_poll(self):
        """ Check that an empty message gives an empty response
        """
        self.coord.dataReceived({})

    def test_02_receive(self):
        """ Explicitly expect an empty response with an empty message
        """
        self.coord.expectResponse(None)
        self.coord.dataReceived({})

    def test_03_missing_client_name(self):
        """ Send a malformed message with no client_name, expect a warning
        """
        self.log = logging.getLogger("testCase")
        self.log.info("\tExpect warning of a missing client name in request")
        ret = self.coord._updateData({"group_name": self.coord.group_name})
        self.assertTrue(ret is None)

    def test_04_missing_group_size(self):
        """ Send a malformed message with no group_size, expect a warning.
        """
        self.log = logging.getLogger("testCase")
        self.log.info("\tExpect warning of new group without specifying the size of the group")
        ret = self.coord._updateData({
            "client_name": self.coord.client_name,
            "group_name": self.coord.group_name
        })
        self.assertTrue(ret is None)

    def test_05_start_group_incomplete(self):
        """ Create a group but fail to populate it with enough devices and cleanup
        """
        self.coord.group_name = str(uuid.uuid4())
        self.coord.group_size = 2
        self.coord.conn.response = "ack"
        self.coord.client_name = "incomplete"
        self.log = logging.getLogger("testCase")
        ret = self.coord._updateData(
            {"client_name": self.coord.client_name,
             "group_size": self.coord.group_size,
             "role": "tester",
             "hostname": "localhost",
             "group_name": self.coord.group_name})
        self.log.info("\tAdded client_name '%s'. group size now: %d" %
                      (self.coord.client_name, len(self.coord.group['clients'])))
        self.log.info("\tCurrent client_name: '%s'" % self.coord.client_name)
        self.coord.group_size = 1
        self.assertTrue(ret == "incomplete")
        self._cleanup()

    def test_06_start_group_complete(self):
        """ Create a group with enough devices and check for no errors.
        """
        self.coord.newGroup(2)
        ret = self.coord.addClient("completing")
        self.assertTrue(ret == "completing")
        ret = self.coord.addClient("completed")
        self.assertTrue(ret == "completed")
        self._cleanup()

    def test_07_lava_send_check(self):
        """ Create a deliberate typo of an API call and check for a warning.
        """
        self.coord.newGroup(2)
        self.coord.addClient("node_one")
        self.coord.addClient("node_two")
        self.log = logging.getLogger("testCase")
        self.log.info("\tExpect warning of an unrecognised request due to deliberate typo.")
        self.coord.expectResponse("nack")
        send_msg = {"request": "lava-send",
                    "messageID": "sending_test",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self.coord.expectResponse("ack")
        send_msg = {"request": "lava_send",
                    "messageID": "sending_test",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self._cleanup()

    def test_08_lava_send_keypair(self):
        """ lava-send key=value - expect an ack
        """
        self.coord.newGroup(2)
        self.coord.addClient("node one")
        self.coord.addClient("node two")
        send_msg = {"request": "lava_send",
                    "messageID": "keyvalue_test",
                    "message": {
                        "key": "value"
                    }}
        self.log = logging.getLogger("testCase")
        self.log.info("\tINF: simply send a message and check for ack")
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self._cleanup()

    def test_09_lava_wait_check(self):
        """ lava-wait check without key value pairs
        """
        self.coord.newGroup(2)
        self.coord.addClient("node_one")
        self.coord.addClient("node_two")
        self.coord.expectResponse("ack")
        send_msg = {"request": "lava_send",
                    "messageID": "sending_test",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        wait_msg = {"request": "lava_wait",
                    "messageID": "missing message",
                    "message": None}
        self.log = logging.getLogger("testCase")
        self.log.info("\tINF: wait for a message not already sent.")
        self.coord.expectResponse("wait")
        self.coord.dataReceived(self._wrapMessage(wait_msg, "tester"))
        self.coord.expectResponse("ack")
        self.log.info("\tINF: wait for a message which has already been sent.")
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self.coord.expectResponse("ack")
        self._cleanup()

    def test_10_lava_wait_keypair(self):
        """ lava-wait check with key=value
        """
        self.coord.newGroup(2)
        self.coord.addClient("node_one")
        self.coord.addClient("node_two")
        self.coord.expectResponse("ack")
        message = {"key": "value"}
        send_msg = {"request": "lava_send",
                    "messageID": "keyvalue_test",
                    "message": message}
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self.coord.expectResponse("ack")
        message = {self.coord.client_name: {"key": "value"}}
        self.coord.expectMessage(message)
        wait_msg = {"request": "lava_wait",
                    "messageID": "keyvalue_test",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(wait_msg, "tester"))
        self.coord.expectMessage(None)
        self._cleanup()

    def test_11_lava_wait_all(self):
        """ lava-wait-all check
        """
        self.coord.newGroup(2)
        self.coord.addClient("node_one")
        self.coord.addClient("node_two")
        self.coord.expectResponse("ack")
        send_msg = {"request": "lava_send",
                    "messageID": "waitall_test",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self.log = logging.getLogger("testCase")
        self.log.info("\tINF: send from node_two first, expect wait")
        wait_msg = {"request": "lava_wait_all",
                    "messageID": "waitall_test",
                    "message": None}
        self.coord.expectResponse("wait")
        self.coord.dataReceived(self._wrapMessage(wait_msg, "tester"))
        self.log.info("\tINF: test node_one waiting before sending a message itself")
        # FIXME: this may need to become a "nack" with the node outputting a warning
        self._switch_client("node_one")
        self.coord.expectResponse("wait")
        self.coord.dataReceived(self._wrapMessage(wait_msg, "tester"))
        self.log.info("\tINF: now allow node_one to send the right message")
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self.log.info("\tINF: test node_one after sending a message")
        self._switch_client("node_one")
        self.coord.expectResponse("ack")
        message = {"node_one": {}, "node_two": {}}
        self.coord.expectMessage(message)
        self.coord.dataReceived(self._wrapMessage(wait_msg, "tester"))
        self._cleanup()

    def test_12_lava_sync(self):
        """ lava-sync check
        """
        self.coord.newGroup(2)
        self.coord.addClient("node_one")
        self.coord.addClient("node_two")
        self.coord.expectResponse("wait")
        self.log = logging.getLogger("testCase")
        self.log.info("\tINF: %s requests a sync" % self.coord.client_name)
        sync_msg = {"request": "lava_sync",
                    "messageID": "waitall_test",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(sync_msg, "tester"))
        self._switch_client("node_one")
        self.log.info("\tINF: %s requests a sync" % self.coord.client_name)
        self.coord.expectResponse("wait")
        self.coord.dataReceived(self._wrapMessage(sync_msg, "tester"))
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(sync_msg, "tester"))
        self._switch_client("node_two")
        self.log.info("\tINF: %s requests a sync" % self.coord.client_name)
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(sync_msg, "tester"))
        self._cleanup()

    def test_13_lava_wait_all_role(self):
        """ lava-wait-all check with role limitation.
        """
        self.coord.newGroup(3)
        self.coord.addClientRole("client_one", "client")
        self.coord.addClientRole("client_two", "client")
        self.coord.addClientRole("server", "server")
        self.log = logging.getLogger("testCase")
        self._switch_client("client_two")
        self.log.info("\tINF: one client waiting before lava_send on any client")
        self.coord.expectResponse("nack")
        wait_msg = {"request": "lava_wait_all",
                    "messageID": "wait-all-role",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self.log.info("\tINF: Send a message to this group")
        send_msg = {"request": "lava_send",
                    "messageID": "wait-all-role",
                    "message": None}
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "client"))
        self.log.info("\tINF:one client waiting before lava_send on the other client")
        self.coord.expectResponse("wait")
        wait_msg = {"request": "lava_wait_all",
                    "messageID": "wait-all-role",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self._switch_client("server")
        # FIXME: this may need to become a "nack" with the node outputting a warning
        self.log.info("\tINF:server waiting before lava_send on the other client")
        self.coord.expectResponse("wait")
        wait_msg = {"request": "lava_wait_all",
                    "messageID": "wait-all-role",
                    "waitrole": "client",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(wait_msg, "server"))
        self._switch_client("client_one")
        self.log.info("\tINF:Send a message to this group")
        send_msg = {"request": "lava_send",
                    "messageID": "wait-all-role",
                    "message": None}
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "client"))
        wait_msg = {"request": "lava_wait_all",
                    "messageID": "wait-all-role",
                    "waitrole": "client",
                    "message": None}
        self.coord.expectResponse("ack")
        message = {"client_two": {}, "client_one": {}}
        self.coord.expectMessage(message)
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self._cleanup()

    def test_14_lava_wait_all_keypair(self):
        """ lava-wait-all with key value pairs
        """
        self.coord.newGroup(3)
        self.coord.addClientRole("client_one", "client")
        self.coord.addClientRole("client_two", "client")
        self.coord.addClientRole("server", "server")
        self.log = logging.getLogger("testCase")
        self._switch_client("client_two")
        self.coord.expectResponse("ack")
        message = {"key": "value"}
        send_msg = {"request": "lava_send",
                    "messageID": "keyvalue_test",
                    "message": message}
        self.coord.dataReceived(self._wrapMessage(send_msg, "client"))
        self.coord.expectResponse("wait")
        wait_msg = {"request": "lava_wait_all",
                    "messageID": "keyvalue_test",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self.log.info("\tINF: wait_all - so other clients need to send before we get the message")
        self._switch_client("client_one")
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "client"))
        self.coord.expectResponse("wait")
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self.log.info("\tINF: this is a wait_all without a role - so server must send too.")
        self._switch_client("server")
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "server"))
        message = {"client_two": {"key": "value"},
                   "client_one": {"key": "value"},
                   "server": {"key": "value"}}
        self.coord.expectResponse("ack")
        self.coord.expectMessage(message)
        self.coord.dataReceived(self._wrapMessage(wait_msg, "server"))
        self._cleanup()

    def test_15_lava_wait_all_role_keypair(self):
        """ lava-wait-all with key value pairs and role limitation.
        """
        self.coord.newGroup(3)
        self.coord.addClientRole("client_one", "client")
        self.coord.addClientRole("client_two", "client")
        self.coord.addClientRole("server", "server")
        self.log = logging.getLogger("testCase")
        self._switch_client("client_two")
        self.coord.expectResponse("ack")
        message = {"key": "value"}
        send_msg = {"request": "lava_send",
                    "messageID": "keyvalue_test",
                    "message": message}
        self.coord.dataReceived(self._wrapMessage(send_msg, "client"))
        self.coord.expectResponse("wait")
        wait_msg = {"request": "lava_wait_all",
                    "messageID": "keyvalue_test",
                    "waitrole": "client",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self.log.info("\tINF: wait_all - so other clients need to send before we get the message")
        self._switch_client("client_one")
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "client"))
        message = {"client_two": {"key": "value"},
                   "client_one": {"key": "value"}}
        self.coord.expectResponse("ack")
        self.coord.expectMessage(message)
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self.log.info("\tINF: this is a wait_all with a role - so server will be ignored.")
        self._switch_client("server")
        self.coord.expectMessage(None)
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "server"))
        self.log.info("\tINF: call to wait by the server was ignored.")
        self.coord.expectResponse("ack")
        self.log.info("\tINF: checking that the messageID is persistent.")
        message = {"client_two": {"key": "value"},
                   "client_one": {"key": "value"},
                   "server": {"key": "value"}}
        self.coord.expectMessage(message)
        self.coord.dataReceived(self._wrapMessage(wait_msg, "client"))
        self._cleanup()

    def test_16_lava_network(self):
        """ Simulate calls to lava-network using real data from multinode.validation.linaro.org
        at the node & coordinator level.
        """
        msg02 = {"message": {
            "hostname-full": "imx53-02.localdomain", "hostname": "imx53-02",
            "netmask": "Mask:255.255.0.0", "dns_1": "192.168.1.32",
            "default-gateway": "192.168.1.1", "ipv6": "addr:",
            "ipv4": "addr:192.168.106.189"},
            "request": "lava_send", "messageID": "network_info"}
        msg04 = {"message": {
            "hostname-full": "imx53-04.localdomain", "hostname": "imx53-04",
            "netmask": "Mask:255.255.0.0", "dns_1": "192.168.1.32",
            "default-gateway": "192.168.1.1", "ipv6": "addr:",
            "ipv4": "addr:192.168.106.180"},
            "request": "lava_send", "messageID": "network_info"}
        reply = {"imx53-02": {"hostname-full": "imx53-02.localdomain",
                              "hostname": "imx53-02", "netmask": "Mask:255.255.0.0",
                              "dns_1": "192.168.1.32", "default-gateway": "192.168.1.1",
                              "ipv6": "addr:", "ipv4": "addr:192.168.106.189"},
                 "imx53-04": {"hostname-full": "imx53-04.localdomain",
                              "hostname": "imx53-04", "netmask": "Mask:255.255.0.0",
                              "dns_1": "192.168.1.32", "default-gateway": "192.168.1.1",
                              "ipv6": "addr:", "ipv4": "addr:192.168.106.180"}}
        self.coord.newGroup(2)
        self.coord.addClientRole("imx53-02", "network")
        self.coord.addClientRole("imx53-04", "network")
        self.log = logging.getLogger("testCase")
        self.log = logging.getLogger("testCase")
        self.log.info("\tINF: Start by sending data for imx53-02 (broadcast)")
        self._switch_client("imx53-02")
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(msg02, "network"))
        self.log.info("\tINF: collect should wait until the other client sends.")
        wait_msg = {"request": "lava_wait_all",
                    "messageID": "network_info",
                    "message": None}
        self.coord.expectResponse("wait")
        self.coord.dataReceived(self._wrapMessage(wait_msg, "network"))
        self.coord.expectResponse("wait")
        self.coord.dataReceived(self._wrapMessage(wait_msg, "network"))
        self.coord.expectResponse("wait")
        self.coord.dataReceived(self._wrapMessage(wait_msg, "network"))
        self.log.info("\tINF: Send data for imx53-04")
        self._switch_client("imx53-04")
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(msg04, "network"))
        wait_msg = {"request": "lava_wait_all",
                    "messageID": "network_info",
                    "message": None}
        self.coord.expectResponse("ack")
        self.coord.expectMessage(reply)
        self.coord.dataReceived(self._wrapMessage(wait_msg, "network"))
        self._cleanup()

    def test_17_nack_check(self):
        """ Create a deliberate nack messageID and check for a warning.
        """
        self.coord.newGroup(2)
        self.coord.addClient("node_one")
        self.coord.addClient("node_two")
        self.log = logging.getLogger("testCase")
        self.coord.expectResponse("ack")
        send_msg = {"request": "lava_send",
                    "messageID": "nack",
                    "message": None}
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        wait_msg = {"request": "lava_wait",
                    "messageID": "nack",
                    "message": None}
        self.coord.expectMessage({"node_two": {}})
        self.coord.dataReceived(self._wrapMessage(wait_msg, "tester"))
        self._cleanup()

    def test_18_aggregation(self):
        """ Check that a syb_id zero waits for all pending result bundles
        """
        self.coord.newGroup(3)
        self.coord.addClient("controller")
        self.coord.addClient("node_one")
        self.coord.addClient("node_two")
        self.log = logging.getLogger("testCase")
        self._switch_client("controller")
        self.coord.expectResponse("nack")
        send_msg = {"request": "aggregate"}
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        send_msg["bundle"] = None
        self.coord.expectResponse("nack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        send_msg["sub_id"] = None
        self.coord.expectResponse("nack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        # It is OK to reuse the same bundle - only the database cares about duplicate assigned_uuid fields etc.
        send_msg['bundle'] = bundle_sample
        self.coord.expectResponse("nack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self.log.info("Setting a zero sub_id - expect wait")
        send_msg["sub_id"] = "10.0"
        for _ in range(6):
            self.coord.expectResponse("wait")
            self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self._switch_client("node_one")
        send_msg["sub_id"] = "10.1"
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self._switch_client("controller")
        send_msg["sub_id"] = "10.0"
        for _ in range(6):
            self.coord.expectResponse("wait")
            self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self._switch_client("node_two")
        send_msg["sub_id"] = "10.2"
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self._switch_client("controller")
        send_msg["sub_id"] = "10.0"
        for _ in range(self.coord.rpc_delay):
            self.coord.expectResponse("wait")
            self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self.coord.expectResponse("ack")
        self.coord.dataReceived(self._wrapMessage(send_msg, "tester"))
        self._cleanup()


def main():
    FORMAT = '%(msg)s'
    logging.basicConfig(format=FORMAT)
    logging.basicConfig(stream=sys.stderr)
    logging.getLogger("testCase").setLevel(logging.DEBUG)
    suite = unittest.TestLoader().loadTestsFromTestCase(TestPoller)
    runner = unittest.TextTestRunner(verbosity=2)
    res = runner.run(suite)
    if not res.wasSuccessful():
        sys.exit(1)
    return 0

if __name__ == '__main__':
    main()