• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import sys
17import unittest
18
19from unittest import mock
20
21# pylint: disable=import-error, no-name-in-module, wrong-import-position
22sys.modules["asuite"] = mock.MagicMock()
23sys.modules["asuite.metrics"] = mock.MagicMock()
24from asuite.metrics import metrics_utils
25from acloud.internal.lib import driver_test_lib
26from acloud.metrics import metrics
27
28
29class MetricsTest(driver_test_lib.BaseDriverTest):
30    """Test metrics methods."""
31    def testLogUsage(self):
32        """Test LogUsage."""
33        self.Patch(metrics_utils, "print_data_collection_notice")
34        self.Patch(metrics_utils, "send_start_event")
35        argv = ["create", "--local-instance"]
36        self.assertTrue(metrics.LogUsage(argv))
37
38        # Test arguments with "--no-metrics"
39        argv = ["create", "--no-metrics"]
40        self.assertFalse(metrics.LogUsage(argv))
41
42        # Don't collect metrics for "delete" command.
43        argv = ["delete", "--all"]
44        self.assertFalse(metrics.LogUsage(argv))
45
46    def testLogExitEvent(self):
47        """Test LogExitEvent."""
48        exit_code = 0
49        self.Patch(metrics_utils, "send_exit_event")
50        metrics.LogExitEvent(exit_code)
51        metrics_utils.send_exit_event.assert_called_once_with(
52            exit_code, stacktrace="", logs="")
53
54
55if __name__ == "__main__":
56    unittest.main()
57