|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -from unittest import mock |
16 |
| -import unittest |
| 15 | +import metrics_quickstart |
17 | 16 |
|
18 |
| -from opencensus.ext.stackdriver import stats_exporter |
19 |
| -from opencensus.stats import aggregation |
20 |
| -from opencensus.stats import measure |
21 |
| -from opencensus.stats import stats |
22 |
| -from opencensus.stats import view |
23 | 17 |
|
24 |
| - |
25 |
| -class TestMetricsQuickstart(unittest.TestCase): |
26 |
| - """Sanity checks for the OpenCensus metrics quickstart examples. |
27 |
| -
|
28 |
| - These tests check that a few basic features of the library work as expected |
29 |
| - in order for the demo to run. See the opencensus and |
30 |
| - opencensus-ext-stackdriver source for actual library tests. |
31 |
| - """ |
32 |
| - def test_measure_creation(self): |
33 |
| - measure.MeasureFloat( |
34 |
| - "task_latency", |
35 |
| - "The task latency in milliseconds", |
36 |
| - "ms") |
37 |
| - |
38 |
| - def test_view_creation(self): |
39 |
| - test_view = view.View( |
40 |
| - "task_latency_distribution", |
41 |
| - "The distribution of the task latencies", |
42 |
| - [], |
43 |
| - mock.Mock(), |
44 |
| - aggregation.DistributionAggregation([1.0, 2.0, 3.0])) |
45 |
| - # Check that metric descriptor conversion doesn't crash |
46 |
| - test_view.get_metric_descriptor() |
47 |
| - |
48 |
| - # Don't modify global stats |
49 |
| - @mock.patch('opencensus.ext.stackdriver.stats_exporter.stats.stats', |
50 |
| - stats._Stats()) |
51 |
| - def test_measurement_map_record(self): |
52 |
| - mock_measure = mock.Mock() |
53 |
| - mock_measure_name = mock.Mock() |
54 |
| - mock_measure.name = mock_measure_name |
55 |
| - mock_view = mock.Mock() |
56 |
| - mock_view.columns = [] |
57 |
| - mock_view.measure = mock_measure |
58 |
| - |
59 |
| - stats.stats.view_manager.register_view(mock_view) |
60 |
| - |
61 |
| - mmap = stats.stats.stats_recorder.new_measurement_map() |
62 |
| - mmap.measure_float_put(mock_measure, 1.0) |
63 |
| - mmap.record() |
64 |
| - |
65 |
| - # Reaching into the stats internals here to check that recording the |
66 |
| - # measurement map affects view data. |
67 |
| - m2vd = (stats.stats.view_manager.measure_to_view_map |
68 |
| - ._measure_to_view_data_list_map) |
69 |
| - self.assertIn(mock_measure_name, m2vd) |
70 |
| - [view_data] = m2vd[mock_measure_name] |
71 |
| - agg_data = view_data.tag_value_aggregation_data_map[tuple()] |
72 |
| - agg_data.add_sample.assert_called_once() |
73 |
| - |
74 |
| - @mock.patch('opencensus.ext.stackdriver.stats_exporter' |
75 |
| - '.monitoring_v3.MetricServiceClient') |
76 |
| - def test_new_stats_exporter(self, mock_client): |
77 |
| - transport = stats_exporter.new_stats_exporter() |
78 |
| - self.assertIsNotNone(transport) |
79 |
| - self.assertIsNotNone(transport.options) |
80 |
| - self.assertIsNotNone(transport.options.project_id) |
| 18 | +def test_quickstart_main(capsys): |
| 19 | + # Run the quickstart, making sure that it runs successfully |
| 20 | + metrics_quickstart.main() |
| 21 | + output = capsys.readouterr() |
| 22 | + assert "Fake latency recorded" in output.out |
0 commit comments