blob: d5244d8290f96f394ecbb1b4aea6b131ebac4e12 [file] [log] [blame]
Michael Tang97d188c2016-06-25 11:18:42 -07001#!/usr/bin/env python
2#
3# Copyright 2016 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import os
8import unittest
9
10import mox
11
12from apiclient import discovery
13from oauth2client.client import ApplicationDefaultCredentialsError
14from oauth2client.client import GoogleCredentials
15from googleapiclient.errors import UnknownApiNameOrVersion
16
17import common
18import pubsub_utils
19
20
21class MockedPubSub(object):
22 """A mocked PubSub handle."""
23 def __init__(self, test, topic, msg, retry, ret_val=None,
24 raise_except=False):
25 self.test = test
26 self.topic = topic
27 self.msg = msg
28 self.retry = retry
29 self.ret_val = ret_val
30 self.raise_except = raise_except
31
32 def projects(self):
33 """Mocked PubSub projects."""
34 return self
35
36 def topics(self):
37 """Mocked PubSub topics."""
38 return self
39
40 def publish(self, topic, body):
41 """Mocked PubSub publish method.
42
43 @param topic: PubSub topic string.
44 @param body: PubSub notification body.
45 """
46 self.test.assertEquals(self.topic, topic)
47 self.test.assertEquals(self.msg, body['messages'][0])
48 return self
49
50 def execute(self, num_retries):
51 """Mocked PubSub execute method.
52
53 @param num_retries: Number of retries.
54 """
55 self.test.assertEquals(self.num_retries, num_retries)
56 if self.raise_except:
57 raise Exception()
58 return self.ret
59
60
61def _create_sample_message():
62 """Creates a sample pubsub message."""
63 msg_payload = {'data': 'sample data'}
64 msg_attributes = {}
65 msg_attributes['var'] = 'value'
66 msg_payload['attributes'] = msg_attributes
67
68 return msg_payload
69
70
71class PubSubTests(mox.MoxTestBase):
72 """Tests for pubsub related functios."""
73
Michael Tang56cbe942017-05-19 09:30:49 -070074 def test_ubsub_with_no_service_account(self):
Michael Tang97d188c2016-06-25 11:18:42 -070075 """Test getting the pubsub service"""
76 self.mox.StubOutWithMock(os.path, 'isfile')
77 os.path.isfile(pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(False)
78 self.mox.ReplayAll()
Michael Tang56cbe942017-05-19 09:30:49 -070079 with self.assertRaises(pubsub_utils.PubSubException):
80 pubsub_utils.PubSubClient()
Michael Tang97d188c2016-06-25 11:18:42 -070081 self.mox.VerifyAll()
82
Michael Tang56cbe942017-05-19 09:30:49 -070083 def test_pubsub_with_corrupted_service_account(self):
84 """Test pubsub with corrupted service account."""
Michael Tang97d188c2016-06-25 11:18:42 -070085 self.mox.StubOutWithMock(os.path, 'isfile')
86 self.mox.StubOutWithMock(GoogleCredentials, 'from_stream')
87 os.path.isfile(pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True)
88 credentials = self.mox.CreateMock(GoogleCredentials)
89 GoogleCredentials.from_stream(
90 pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndRaise(
91 ApplicationDefaultCredentialsError())
92 self.mox.ReplayAll()
Michael Tang56cbe942017-05-19 09:30:49 -070093 with self.assertRaises(pubsub_utils.PubSubException):
94 pubsub_utils.PubSubClient()
Michael Tang97d188c2016-06-25 11:18:42 -070095 self.mox.VerifyAll()
96
Michael Tang56cbe942017-05-19 09:30:49 -070097 def test_pubsub_with_invalid_service_account(self):
98 """Test pubsubwith invalid service account."""
Michael Tang97d188c2016-06-25 11:18:42 -070099 self.mox.StubOutWithMock(os.path, 'isfile')
100 self.mox.StubOutWithMock(GoogleCredentials, 'from_stream')
101 os.path.isfile(pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True)
102 credentials = self.mox.CreateMock(GoogleCredentials)
103 GoogleCredentials.from_stream(
104 pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(credentials)
105 credentials.create_scoped_required().AndReturn(True)
106 credentials.create_scoped(pubsub_utils.PUBSUB_SCOPES).AndReturn(
107 credentials)
108 self.mox.StubOutWithMock(discovery, 'build')
109 discovery.build(pubsub_utils.PUBSUB_SERVICE_NAME,
110 pubsub_utils.PUBSUB_VERSION,
111 credentials=credentials).AndRaise(UnknownApiNameOrVersion())
112 self.mox.ReplayAll()
Michael Tang56cbe942017-05-19 09:30:49 -0700113 with self.assertRaises(pubsub_utils.PubSubException):
114 msg = _create_sample_message()
115 pubsub_client = pubsub_utils.PubSubClient()
116 pubsub_client.publish_notifications('test_topic', [msg])
Michael Tang97d188c2016-06-25 11:18:42 -0700117 self.mox.VerifyAll()
118
Michael Tang56cbe942017-05-19 09:30:49 -0700119 def test_publish_notifications(self):
Michael Tang97d188c2016-06-25 11:18:42 -0700120 """Test getting the pubsub service"""
121 self.mox.StubOutWithMock(os.path, 'isfile')
122 self.mox.StubOutWithMock(GoogleCredentials, 'from_stream')
123 os.path.isfile(pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True)
124 credentials = self.mox.CreateMock(GoogleCredentials)
125 GoogleCredentials.from_stream(
126 pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(credentials)
127 credentials.create_scoped_required().AndReturn(True)
128 credentials.create_scoped(pubsub_utils.PUBSUB_SCOPES).AndReturn(
129 credentials)
130 self.mox.StubOutWithMock(discovery, 'build')
Michael Tang56cbe942017-05-19 09:30:49 -0700131 msg = _create_sample_message()
Michael Tang97d188c2016-06-25 11:18:42 -0700132 discovery.build(pubsub_utils.PUBSUB_SERVICE_NAME,
133 pubsub_utils.PUBSUB_VERSION,
Michael Tang56cbe942017-05-19 09:30:49 -0700134 credentials=credentials).AndReturn(MockedPubSub(
135 self,
136 'test_topic',
137 msg,
138 pubsub_utils._PUBSUB_NUM_RETRIES,
139 # use tuple ('123') instead of list just for easy to
140 # write the test.
141 ret_val = {'messageIds', ('123')}))
Michael Tang97d188c2016-06-25 11:18:42 -0700142
143 self.mox.ReplayAll()
Michael Tang56cbe942017-05-19 09:30:49 -0700144 with self.assertRaises(pubsub_utils.PubSubException):
145 pubsub_client = pubsub_utils.PubSubClient()
146 pubsub_client.publish_notifications('test_topic', [msg])
Michael Tang97d188c2016-06-25 11:18:42 -0700147 self.mox.VerifyAll()
148
149
150if __name__ == '__main__':
151 unittest.main()