1#!/usr/bin/env python 2# 3# Copyright (C) 2017 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 18import unittest 19 20try: 21 from unittest import mock 22except ImportError: 23 import mock 24 25from host_controller.tfc import tfc_client 26from host_controller.tfc import command_attempt 27from host_controller.tfc import device_info 28from host_controller.tfc import request 29 30 31class TfcClientTest(unittest.TestCase): 32 """A test for tfc_client.TfcClient. 33 34 Attributes: 35 _client: The tfc_client.TfcClient being tested. 36 _service: The mock service that _client connects to. 37 """ 38 _DEVICE_INFOS = [device_info.DeviceInfo( 39 device_serial="ABCDEF", group_name="group0", 40 run_target="sailfish", state="Available")] 41 42 def setUp(self): 43 """Creates a TFC client connecting to a mock service.""" 44 self._service = mock.Mock() 45 self._client = tfc_client.TfcClient(self._service) 46 47 def testNewRequest(self): 48 """Tests requests.new.""" 49 req = request.Request(cluster="cluster0", 50 command_line="vts-codelab", 51 run_target="sailfish", 52 user="user0") 53 self._client.NewRequest(req) 54 self._service.assert_has_calls([ 55 mock.call.requests().new().execute()]) 56 57 def testLeaseHostTasks(self): 58 """Tests tasks.leasehosttasks.""" 59 tasks = {"tasks": [{"request_id": "2", 60 "command_line": "vts-codelab --serial ABCDEF", 61 "task_id": "1-0", 62 "device_serials": ["ABCDEF"], 63 "command_id": "1"}]} 64 self._service.tasks().leasehosttasks().execute.return_value = tasks 65 self._client.LeaseHostTasks("cluster0", ["cluster1", "cluster2"], 66 "host0", self._DEVICE_INFOS) 67 self._service.assert_has_calls([ 68 mock.call.tasks().leasehosttasks().execute()]) 69 70 def testHostEvents(self): 71 """Tests host_events.submit.""" 72 device_snapshots = [ 73 self._client.CreateDeviceSnapshot("vts-staging", "host0", 74 self._DEVICE_INFOS), 75 self._client.CreateDeviceSnapshot("vts-presubmit", "host0", 76 self._DEVICE_INFOS)] 77 self._client.SubmitHostEvents(device_snapshots) 78 self._service.assert_has_calls([ 79 mock.call.host_events().submit().execute()]) 80 81 def testCommandEvents(self): 82 """Tests command_events.submit.""" 83 cmd = command_attempt.CommandAttempt( 84 task_id="321-0", 85 attempt_id="abcd-1234", 86 hostname="host0", 87 device_serial="ABCDEF") 88 expected_event = { 89 "task_id": "321-0", 90 "attempt_id": "abcd-1234", 91 "hostname": "host0", 92 "device_serial": "ABCDEF", 93 "time": 1} 94 95 normal_event = cmd.CreateCommandEvent( 96 command_attempt.EventType.INVOCATION_STARTED, 97 event_time=1) 98 expected_event["type"] = command_attempt.EventType.INVOCATION_STARTED 99 self.assertDictEqual(expected_event, normal_event) 100 101 error_event = cmd.CreateCommandEvent( 102 command_attempt.EventType.EXECUTE_FAILED, 103 error="unit test", event_time=1.1) 104 expected_event["type"] = command_attempt.EventType.EXECUTE_FAILED 105 expected_event["data"] = {"error":"unit test"} 106 self.assertDictEqual(expected_event, error_event) 107 108 complete_event = cmd.CreateInvocationCompletedEvent( 109 summary="complete", total_test_count=2, failed_test_count=1, 110 error="unit test") 111 expected_event["type"] = command_attempt.EventType.INVOCATION_COMPLETED 112 expected_event["data"] = {"summary": "complete", "error": "unit test", 113 "total_test_count": 2, "failed_test_count": 1} 114 del expected_event["time"] 115 self.assertDictContainsSubset(expected_event, complete_event) 116 self.assertIn("time", complete_event) 117 118 self._client.SubmitCommandEvents([ 119 normal_event, error_event, complete_event]) 120 self._service.assert_has_calls([ 121 mock.call.command_events().submit().execute()]) 122 123 def testWrongParameter(self): 124 """Tests raising exception for wrong parameter name.""" 125 self.assertRaisesRegexp(KeyError, "sdk", device_info.DeviceInfo, 126 device_serial="123", sdk="25") 127 128 129if __name__ == "__main__": 130 unittest.main() 131