lin
2025-08-14 dae8bad597b6607a449b32bf76c523423f7720ed
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
#!/usr/bin/python
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
 
import base64
import mox
import unittest
 
import common
 
from autotest_lib.client.common_lib import utils
from autotest_lib.site_utils import cloud_console_client
from autotest_lib.site_utils import cloud_console_pb2 as cpcon
from autotest_lib.site_utils import pubsub_utils
 
class PubSubBasedClientTests(mox.MoxTestBase):
    """Tests for the 'PubSubBasedClient'."""
 
    def setUp(self):
        super(PubSubBasedClientTests, self).setUp()
        self._pubsub_client_mock = self.mox.CreateMock(
                pubsub_utils.PubSubClient)
        self._stubs = mox.stubout.StubOutForTesting()
        self._stubs.Set(cloud_console_client, '_create_pubsub_client',
                lambda x: self._pubsub_client_mock)
 
    def tearDown(self):
        self._stubs.UnsetAll()
        super(PubSubBasedClientTests, self).tearDown()
 
    def test_create_test_result_notification(self):
        """Tests the test result notification message."""
        self._console_client = cloud_console_client.PubSubBasedClient()
        self.mox.StubOutWithMock(utils,
                                 'get_moblab_serial_number')
        utils.get_moblab_serial_number().AndReturn(
            'PV120BB8JAC01E')
        self.mox.StubOutWithMock(utils, 'get_moblab_id')
        utils.get_moblab_id().AndReturn(
            'c8386d92-9ad1-11e6-80f5-111111111111')
        self.mox.ReplayAll()
        console_client = cloud_console_client.PubSubBasedClient()
        msg = console_client._create_test_job_offloaded_message(
                'gs://test_bucket/123-moblab')
        self.assertEquals(base64.b64encode(
            cloud_console_client.LEGACY_TEST_OFFLOAD_MESSAGE), msg['data'])
        self.assertEquals(
            cloud_console_client.CURRENT_MESSAGE_VERSION,
            msg['attributes'][cloud_console_client.LEGACY_ATTR_VERSION])
        self.assertEquals(
            '1c:dc:d1:11:01:e1',
            msg['attributes'][cloud_console_client.LEGACY_ATTR_MOBLAB_MAC]
            )
        self.assertEquals(
            'c8386d92-9ad1-11e6-80f5-111111111111',
            msg['attributes'][cloud_console_client.LEGACY_ATTR_MOBLAB_ID])
        self.assertEquals(
            'gs://test_bucket/123-moblab',
            msg['attributes'][cloud_console_client.LEGACY_ATTR_GCS_URI])
        self.mox.VerifyAll()
 
    def test_send_test_job_offloaded_message(self):
        """Tests send job offloaded notification."""
        console_client = cloud_console_client.PubSubBasedClient(
                pubsub_topic='test topic')
 
        message = {'data': 'dummy data', 'attributes' : {'key' : 'value'}}
        self.mox.StubOutWithMock(cloud_console_client.PubSubBasedClient,
                '_create_test_job_offloaded_message')
        console_client._create_test_job_offloaded_message(
                'gs://test_bucket/123-moblab').AndReturn(message)
 
        msg_ids = ['1']
        self._pubsub_client_mock.publish_notifications(
                'test topic', [message]).AndReturn(msg_ids)
 
        self.mox.ReplayAll()
        result = console_client.send_test_job_offloaded_message(
                'gs://test_bucket/123-moblab')
        self.assertTrue(result)
        self.mox.VerifyAll()
 
    def test_send_heartbeat(self):
        """Tests send heartbeat."""
        console_client = cloud_console_client.PubSubBasedClient(
                pubsub_topic='test topic')
        self.mox.StubOutWithMock(utils, 'get_moblab_id')
        utils.get_moblab_id().AndReturn(
            'c8386d92-9ad1-11e6-80f5-111111111111')
 
        message = {
                'attributes' : {
                    'ATTR_MOBLAB_ID': 'c8386d92-9ad1-11e6-80f5-111111111111',
                    'ATTR_MESSAGE_VERSION': '1',
                    'ATTR_MESSAGE_TYPE': 'MSG_MOBLAB_HEARTBEAT',
                    'ATTR_MOBLAB_MAC_ADDRESS': '8c:dc:d4:56:06:e7'}}
        msg_ids = ['1']
        self._pubsub_client_mock.publish_notifications(
                'test topic', [message]).AndReturn(msg_ids)
        self.mox.ReplayAll()
        console_client.send_heartbeat()
        self.mox.VerifyAll()
 
    def test_send_event(self):
        """Tests send heartbeat."""
        console_client = cloud_console_client.PubSubBasedClient(
                pubsub_topic='test topic')
        self.mox.StubOutWithMock(utils, 'get_moblab_id')
        utils.get_moblab_id().AndReturn(
            'c8386d92-9ad1-11e6-80f5-111111111111')
 
        message = {
                'data': '\x08\x01\x12\x0ethis is a test',
                'attributes' : {
                    'ATTR_MOBLAB_ID': 'c8386d92-9ad1-11e6-80f5-111111111111',
                    'ATTR_MESSAGE_VERSION': '1',
                    'ATTR_MESSAGE_TYPE': 'MSG_MOBLAB_REMOTE_EVENT',
                    'ATTR_MOBLAB_MAC_ADDRESS': '8c:dc:d4:56:06:e7'}}
        msg_ids = ['1']
        self._pubsub_client_mock.publish_notifications(
                'test topic', [message]).AndReturn(msg_ids)
        self.mox.ReplayAll()
        console_client.send_event(
                cpcon.RemoteEventMessage.EVENT_MOBLAB_BOOT_COMPLETE,
                'this is a test')
        self.mox.VerifyAll()
 
if __name__ == '__main__':
    unittest.main()