|
17 | 17 | import unittest
|
18 | 18 |
|
19 | 19 | from google.cloud import monitoring_v3
|
| 20 | +import google.auth |
20 | 21 |
|
21 | 22 | from opencensus.common import utils
|
22 | 23 | from opencensus.common.version import __version__
|
@@ -115,6 +116,32 @@ def test_constructor_param(self):
|
115 | 116 | default_monitoring_labels=default_labels))
|
116 | 117 | self.assertEqual(exporter.options.project_id, project_id)
|
117 | 118 |
|
| 119 | + def test_null_options(self): |
| 120 | + # Check that we don't suppress auth errors |
| 121 | + auth_error = google.auth.exceptions.DefaultCredentialsError |
| 122 | + mock_auth_error = mock.Mock() |
| 123 | + mock_auth_error.side_effect = auth_error |
| 124 | + with mock.patch('opencensus.ext.stackdriver.stats_exporter' |
| 125 | + '.google.auth.default', mock_auth_error): |
| 126 | + with self.assertRaises(auth_error): |
| 127 | + stackdriver.new_stats_exporter() |
| 128 | + |
| 129 | + # Check that we get the default credentials' project ID |
| 130 | + mock_auth_ok = mock.Mock() |
| 131 | + mock_auth_ok.return_value = (None, 123) |
| 132 | + with mock.patch('opencensus.ext.stackdriver.stats_exporter' |
| 133 | + '.google.auth.default', mock_auth_ok): |
| 134 | + sdse = stackdriver.new_stats_exporter() |
| 135 | + self.assertEqual(sdse.options.project_id, 123) |
| 136 | + |
| 137 | + # Check that we raise if auth works but the project is empty |
| 138 | + mock_auth_no_project = mock.Mock() |
| 139 | + mock_auth_no_project.return_value = (None, '') |
| 140 | + with mock.patch('opencensus.ext.stackdriver.stats_exporter' |
| 141 | + '.google.auth.default', mock_auth_no_project): |
| 142 | + with self.assertRaises(ValueError): |
| 143 | + stackdriver.new_stats_exporter() |
| 144 | + |
118 | 145 | def test_blank_project(self):
|
119 | 146 | self.assertRaises(ValueError, stackdriver.new_stats_exporter,
|
120 | 147 | stackdriver.Options(project_id=""))
|
|
0 commit comments