| herbertxue | 1837cc9 | 2021-10-13 20:34:36 +0800 | [diff] [blame] | 1 | # Copyright 2021 - The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | """Tests for metrics.""" |
| 15 | |
| 16 | import sys |
| 17 | import unittest |
| 18 | |
| 19 | from unittest import mock |
| 20 | |
| 21 | # pylint: disable=import-error, no-name-in-module, wrong-import-position |
| 22 | sys.modules["asuite"] = mock.MagicMock() |
| 23 | sys.modules["asuite.metrics"] = mock.MagicMock() |
| 24 | from asuite import atest_utils |
| 25 | from asuite.metrics import metrics_utils |
| 26 | from acloud.internal.lib import driver_test_lib |
| 27 | from acloud.metrics import metrics |
| 28 | |
| 29 | |
| 30 | class MetricsTest(driver_test_lib.BaseDriverTest): |
| 31 | """Test metrics methods.""" |
| 32 | def testLogUsage(self): |
| 33 | """Test LogUsage.""" |
| 34 | self.Patch(atest_utils, "print_data_collection_notice") |
| 35 | self.Patch(metrics_utils, "send_start_event") |
| 36 | argv = ["acloud", "create"] |
| 37 | self.assertTrue(metrics.LogUsage(argv)) |
| 38 | |
| 39 | # Test arguments with "--no-metrics" |
| 40 | argv = ["acloud", "create", "--no-metrics"] |
| 41 | self.assertFalse(metrics.LogUsage(argv)) |
| 42 | |
| 43 | def testLogExitEvent(self): |
| 44 | """Test LogExitEvent.""" |
| 45 | exit_code = 0 |
| 46 | self.Patch(metrics_utils, "send_exit_event") |
| 47 | metrics.LogExitEvent(exit_code) |
| 48 | metrics_utils.send_exit_event.assert_called_once_with( |
| 49 | exit_code, stacktrace="", logs="") |
| 50 | |
| 51 | |
| 52 | if __name__ == "__main__": |
| 53 | unittest.main() |