xref: /aosp_15_r20/tools/acloud/public/report_test.py (revision 800a58d989c669b8eb8a71d8df53b1ba3d411444)
1#!/usr/bin/env python
2#
3# Copyright 2016 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Tests for acloud.public.report."""
18
19import unittest
20from acloud.public import report
21
22
23class ReportTest(unittest.TestCase):
24    """Test Report class."""
25
26    def testAddData(self):
27        """test AddData."""
28        test_report = report.Report("create")
29        test_report.AddData("devices", {"instance_name": "instance_1"})
30        test_report.AddData("devices", {"instance_name": "instance_2"})
31        expected = {
32            "devices": [{
33                "instance_name": "instance_1"
34            }, {
35                "instance_name": "instance_2"
36            }]
37        }
38        self.assertEqual(test_report.data, expected)
39
40    def testAddError(self):
41        """test AddError."""
42        test_report = report.Report("create")
43        test_report.errors.append("some errors")
44        test_report.errors.append("some errors")
45        self.assertEqual(test_report.errors, ["some errors", "some errors"])
46
47    def testSetStatus(self):
48        """test SetStatus."""
49        test_report = report.Report("create")
50        test_report.SetStatus(report.Status.SUCCESS)
51        self.assertEqual(test_report.status, "SUCCESS")
52
53        test_report.SetStatus(report.Status.FAIL)
54        self.assertEqual(test_report.status, "FAIL")
55
56        test_report.SetStatus(report.Status.BOOT_FAIL)
57        self.assertEqual(test_report.status, "BOOT_FAIL")
58
59        # Test that more severe status won't get overriden.
60        test_report.SetStatus(report.Status.FAIL)
61        self.assertEqual(test_report.status, "BOOT_FAIL")
62
63    def testSetErrorType(self):
64        """test SetErrorType."""
65        error_type = "GCE_QUOTA_ERROR"
66        test_report = report.Report("create")
67        test_report.SetErrorType(error_type)
68        self.assertEqual(test_report.error_type, error_type)
69
70    def testUpdateFailure(self):
71        """test UpdateFailure."""
72        error_type = "GCE_QUOTA_ERROR"
73        error_msg = "Reach quota limit."
74        test_report = report.Report("create")
75        test_report.UpdateFailure(error_msg, error_type)
76        self.assertEqual(test_report.status, "FAIL")
77        self.assertEqual(test_report.errors, [error_msg])
78        self.assertEqual(test_report.error_type, error_type)
79
80    def testAddDevice(self):
81        """test AddDevice."""
82        test_report = report.Report("create")
83        test_report.AddDevice("instance_1", "127.0.0.1", 6520, 6444, 8443,
84                              logs=[report.LogFile("/log/path", "KERNEL_LOG")])
85        expected = {
86            "devices": [{
87                "instance_name": "instance_1",
88                "ip": "127.0.0.1:6520",
89                "adb_port": 6520,
90                "vnc_port": 6444,
91                "webrtc_port": 8443,
92                "logs": [{
93                    "path": "/log/path",
94                    "type": "KERNEL_LOG"
95                }]
96            }]
97        }
98        self.assertEqual(test_report.data, expected)
99
100        # Write report with "device_serial"
101        test_report = report.Report("create")
102        device_serial = "emulator-test"
103        update_data = {"screen_command": "screen console"}
104        test_report.AddDevice(
105            "instance_1", "127.0.0.1", 6520, 6444, device_serial=device_serial,
106            update_data=update_data)
107        expected = {
108            "devices": [{
109                "instance_name": "instance_1",
110                "ip": "127.0.0.1:6520",
111                "adb_port": 6520,
112                "vnc_port": 6444,
113                "device_serial": device_serial,
114                "screen_command": "screen console"
115            }]
116        }
117        self.assertEqual(test_report.data, expected)
118
119    def testAddDeviceBootFailure(self):
120        """test AddDeviceBootFailure."""
121        test_report = report.Report("create")
122        device_serial = "emulator-test"
123        test_report.AddDeviceBootFailure(
124            "instance_1", "127.0.0.1", 6520, 6444, "some errors",
125            device_serial, logs=[report.LogFile("/log/path", "TEXT", "txt")])
126        expected = {
127            "devices_failing_boot": [{
128                "instance_name": "instance_1",
129                "ip": "127.0.0.1:6520",
130                "adb_port": 6520,
131                "vnc_port": 6444,
132                "device_serial": device_serial,
133                "logs": [{
134                    "path": "/log/path",
135                    "type": "TEXT",
136                    "name": "txt"
137                }]
138            }]
139        }
140        self.assertEqual(test_report.data, expected)
141        self.assertEqual(test_report.errors, ["some errors"])
142
143
144if __name__ == "__main__":
145    unittest.main()
146