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 expected = { 85 "devices": [{ 86 "instance_name": "instance_1", 87 "ip": "127.0.0.1:6520", 88 "adb_port": 6520, 89 "vnc_port": 6444, 90 "webrtc_port": 8443 91 }] 92 } 93 self.assertEqual(test_report.data, expected) 94 95 # Write report with "device_serial" 96 test_report = report.Report("create") 97 device_serial = "emulator-test" 98 test_report.AddDevice("instance_1", "127.0.0.1", 6520, 6444, 99 device_serial=device_serial) 100 expected = { 101 "devices": [{ 102 "instance_name": "instance_1", 103 "ip": "127.0.0.1:6520", 104 "adb_port": 6520, 105 "vnc_port": 6444, 106 "device_serial": device_serial 107 }] 108 } 109 self.assertEqual(test_report.data, expected) 110 111 def testAddDeviceBootFailure(self): 112 """test AddDeviceBootFailure.""" 113 test_report = report.Report("create") 114 device_serial = "emulator-test" 115 test_report.AddDeviceBootFailure("instance_1", "127.0.0.1", 6520, 6444, 116 "some errors", device_serial) 117 expected = { 118 "devices_failing_boot": [{ 119 "instance_name": "instance_1", 120 "ip": "127.0.0.1:6520", 121 "adb_port": 6520, 122 "vnc_port": 6444, 123 "device_serial": device_serial 124 }] 125 } 126 self.assertEqual(test_report.data, expected) 127 self.assertEqual(test_report.errors, ["some errors"]) 128 129 130if __name__ == "__main__": 131 unittest.main() 132