Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 2 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 6 | """Unit test for pubsub_utils.py""" |
| 7 | |
| 8 | from __future__ import print_function |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 9 | import os |
| 10 | import unittest |
| 11 | |
| 12 | import mox |
| 13 | |
Mike Frysinger | 393b23d | 2020-02-10 22:52:43 -0500 | [diff] [blame] | 14 | # TODO(crbug.com/1050892): The unittests rely on apiclient in chromite. |
| 15 | import chromite # pylint: disable=unused-import |
| 16 | |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 17 | from apiclient import discovery |
| 18 | from oauth2client.client import ApplicationDefaultCredentialsError |
| 19 | from oauth2client.client import GoogleCredentials |
| 20 | from googleapiclient.errors import UnknownApiNameOrVersion |
| 21 | |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 22 | import pubsub_utils |
| 23 | |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 24 | _TEST_CLOUD_SERVICE_ACCOUNT_FILE = '/tmp/test-credential' |
| 25 | |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 26 | |
| 27 | class MockedPubSub(object): |
| 28 | """A mocked PubSub handle.""" |
| 29 | def __init__(self, test, topic, msg, retry, ret_val=None, |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 30 | raise_except=False): |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 31 | self.test = test |
| 32 | self.topic = topic |
| 33 | self.msg = msg |
| 34 | self.retry = retry |
| 35 | self.ret_val = ret_val |
| 36 | self.raise_except = raise_except |
| 37 | |
| 38 | def projects(self): |
| 39 | """Mocked PubSub projects.""" |
| 40 | return self |
| 41 | |
| 42 | def topics(self): |
| 43 | """Mocked PubSub topics.""" |
| 44 | return self |
| 45 | |
| 46 | def publish(self, topic, body): |
| 47 | """Mocked PubSub publish method. |
| 48 | |
| 49 | @param topic: PubSub topic string. |
| 50 | @param body: PubSub notification body. |
| 51 | """ |
| 52 | self.test.assertEquals(self.topic, topic) |
| 53 | self.test.assertEquals(self.msg, body['messages'][0]) |
| 54 | return self |
| 55 | |
| 56 | def execute(self, num_retries): |
| 57 | """Mocked PubSub execute method. |
| 58 | |
| 59 | @param num_retries: Number of retries. |
| 60 | """ |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 61 | self.test.assertEquals(self.retry, num_retries) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 62 | if self.raise_except: |
| 63 | raise Exception() |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 64 | return self.ret_val |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def _create_sample_message(): |
| 68 | """Creates a sample pubsub message.""" |
| 69 | msg_payload = {'data': 'sample data'} |
| 70 | msg_attributes = {} |
| 71 | msg_attributes['var'] = 'value' |
| 72 | msg_payload['attributes'] = msg_attributes |
| 73 | |
| 74 | return msg_payload |
| 75 | |
| 76 | |
| 77 | class PubSubTests(mox.MoxTestBase): |
| 78 | """Tests for pubsub related functios.""" |
| 79 | |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 80 | def test_pubsub_with_no_service_account(self): |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 81 | """Test getting the pubsub service""" |
| 82 | self.mox.StubOutWithMock(os.path, 'isfile') |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 83 | self.mox.ReplayAll() |
Michael Tang | 56cbe94 | 2017-05-19 09:30:49 -0700 | [diff] [blame] | 84 | with self.assertRaises(pubsub_utils.PubSubException): |
| 85 | pubsub_utils.PubSubClient() |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 86 | self.mox.VerifyAll() |
| 87 | |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 88 | def test_pubsub_with_non_existing_service_account(self): |
| 89 | """Test getting the pubsub service""" |
| 90 | self.mox.StubOutWithMock(os.path, 'isfile') |
| 91 | os.path.isfile(_TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(False) |
| 92 | self.mox.ReplayAll() |
| 93 | with self.assertRaises(pubsub_utils.PubSubException): |
| 94 | pubsub_utils.PubSubClient(_TEST_CLOUD_SERVICE_ACCOUNT_FILE) |
| 95 | self.mox.VerifyAll() |
| 96 | |
Michael Tang | 56cbe94 | 2017-05-19 09:30:49 -0700 | [diff] [blame] | 97 | def test_pubsub_with_corrupted_service_account(self): |
| 98 | """Test pubsub with corrupted service account.""" |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 99 | self.mox.StubOutWithMock(os.path, 'isfile') |
| 100 | self.mox.StubOutWithMock(GoogleCredentials, 'from_stream') |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 101 | os.path.isfile(_TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 102 | GoogleCredentials.from_stream( |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 103 | _TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndRaise( |
| 104 | ApplicationDefaultCredentialsError()) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 105 | self.mox.ReplayAll() |
Michael Tang | 56cbe94 | 2017-05-19 09:30:49 -0700 | [diff] [blame] | 106 | with self.assertRaises(pubsub_utils.PubSubException): |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 107 | pubsub_utils.PubSubClient(_TEST_CLOUD_SERVICE_ACCOUNT_FILE) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 108 | self.mox.VerifyAll() |
| 109 | |
Michael Tang | 56cbe94 | 2017-05-19 09:30:49 -0700 | [diff] [blame] | 110 | def test_pubsub_with_invalid_service_account(self): |
| 111 | """Test pubsubwith invalid service account.""" |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 112 | self.mox.StubOutWithMock(os.path, 'isfile') |
| 113 | self.mox.StubOutWithMock(GoogleCredentials, 'from_stream') |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 114 | os.path.isfile(_TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 115 | credentials = self.mox.CreateMock(GoogleCredentials) |
| 116 | GoogleCredentials.from_stream( |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 117 | _TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(credentials) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 118 | credentials.create_scoped_required().AndReturn(True) |
| 119 | credentials.create_scoped(pubsub_utils.PUBSUB_SCOPES).AndReturn( |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 120 | credentials) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 121 | self.mox.StubOutWithMock(discovery, 'build') |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 122 | discovery.build( |
| 123 | pubsub_utils.PUBSUB_SERVICE_NAME, |
| 124 | pubsub_utils.PUBSUB_VERSION, |
| 125 | credentials=credentials).AndRaise(UnknownApiNameOrVersion()) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 126 | self.mox.ReplayAll() |
Michael Tang | 56cbe94 | 2017-05-19 09:30:49 -0700 | [diff] [blame] | 127 | with self.assertRaises(pubsub_utils.PubSubException): |
| 128 | msg = _create_sample_message() |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 129 | pubsub_client = pubsub_utils.PubSubClient( |
| 130 | _TEST_CLOUD_SERVICE_ACCOUNT_FILE) |
Michael Tang | 56cbe94 | 2017-05-19 09:30:49 -0700 | [diff] [blame] | 131 | pubsub_client.publish_notifications('test_topic', [msg]) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 132 | self.mox.VerifyAll() |
| 133 | |
Michael Tang | 56cbe94 | 2017-05-19 09:30:49 -0700 | [diff] [blame] | 134 | def test_publish_notifications(self): |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 135 | """Test getting the pubsub service""" |
| 136 | self.mox.StubOutWithMock(os.path, 'isfile') |
| 137 | self.mox.StubOutWithMock(GoogleCredentials, 'from_stream') |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 138 | os.path.isfile(_TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 139 | credentials = self.mox.CreateMock(GoogleCredentials) |
| 140 | GoogleCredentials.from_stream( |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 141 | _TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(credentials) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 142 | credentials.create_scoped_required().AndReturn(True) |
| 143 | credentials.create_scoped(pubsub_utils.PUBSUB_SCOPES).AndReturn( |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 144 | credentials) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 145 | self.mox.StubOutWithMock(discovery, 'build') |
Michael Tang | 56cbe94 | 2017-05-19 09:30:49 -0700 | [diff] [blame] | 146 | msg = _create_sample_message() |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 147 | discovery.build( |
| 148 | pubsub_utils.PUBSUB_SERVICE_NAME, |
| 149 | pubsub_utils.PUBSUB_VERSION, |
| 150 | credentials=credentials).AndReturn(MockedPubSub( |
| 151 | self, |
| 152 | 'test_topic', |
| 153 | msg, |
| 154 | pubsub_utils.DEFAULT_PUBSUB_NUM_RETRIES, |
| 155 | # use tuple ('123') instead of list just for easy to |
| 156 | # write the test. |
| 157 | ret_val={'messageIds': ('123')})) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 158 | |
| 159 | self.mox.ReplayAll() |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 160 | pubsub_client = pubsub_utils.PubSubClient( |
| 161 | _TEST_CLOUD_SERVICE_ACCOUNT_FILE) |
| 162 | msg_ids = pubsub_client.publish_notifications('test_topic', [msg]) |
| 163 | self.assertEquals(('123'), msg_ids) |
| 164 | |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 165 | self.mox.VerifyAll() |
| 166 | |
| 167 | |
| 168 | if __name__ == '__main__': |
| 169 | unittest.main() |