1#!/usr/bin/env python
2#
3# Copyright (C) 2018 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 webapp.src import vtslab_status as Status
26from webapp.src.endpoint import schedule_info
27from webapp.src.proto import model
28from webapp.src.testing import unittest_base
29
30
31class ScheduleInfoTest(unittest_base.UnitTestBase):
32    """A class to test schedule_info endpoint API."""
33
34    def setUp(self):
35        """Initializes test"""
36        super(ScheduleInfoTest, self).setUp()
37
38    def testSetWithSimpleMessage(self):
39        """Asserts schedule_info/set API receives a simple message."""
40        # As of June 8, 2018, these are uploaded from host controller.
41        container = (
42            schedule_info.SCHEDULE_INFO_RESOURCE.combined_message_class(
43                manifest_branch=self.GetRandomString(),
44                build_storage_type=Status.STORAGE_TYPE_DICT["PAB"],
45                build_target=self.GetRandomString(),
46                require_signed_device_build=False,
47                has_bootloader_img=True,
48                has_radio_img=True,
49                test_name=self.GetRandomString(),
50                period=360,
51                priority="high",
52                device=[self.GetRandomString()],
53                required_host_equipment=[self.GetRandomString()],
54                required_device_equipment=[self.GetRandomString()],
55                device_pab_account_id=self.GetRandomString(),
56                shards=1,
57                param=[self.GetRandomString()],
58                retry_count=1,
59                gsi_storage_type=Status.STORAGE_TYPE_DICT["PAB"],
60                gsi_branch=self.GetRandomString(),
61                gsi_build_target=self.GetRandomString(),
62                gsi_pab_account_id=self.GetRandomString(),
63                gsi_vendor_version=self.GetRandomString(),
64                test_storage_type=Status.STORAGE_TYPE_DICT["PAB"],
65                test_branch=self.GetRandomString(),
66                test_build_target=self.GetRandomString(),
67                test_pab_account_id=self.GetRandomString(),
68                image_package_repo_base=self.GetRandomString(),
69                report_bucket=[self.GetRandomString()],
70                report_spreadsheet_id=[self.GetRandomString()],
71                report_persistent_url=[self.GetRandomString()],
72                report_reference_url=[self.GetRandomString()],
73            ))
74        api = schedule_info.ScheduleInfoApi()
75        response = api.set(container)
76
77        self.assertEqual(response.return_code, model.ReturnCodeMessage.SUCCESS)
78
79    def testSetWithEmptyRepeatedField(self):
80        """Asserts schedule_info/set API receives a message.
81
82        This test sets required_host_equipment to empty and sends to endpoint
83        method.
84        """
85        # As of June 8, 2018, these are uploaded from host controller.
86        container = (
87            schedule_info.SCHEDULE_INFO_RESOURCE.combined_message_class(
88                manifest_branch=self.GetRandomString(),
89                build_storage_type=Status.STORAGE_TYPE_DICT["PAB"],
90                build_target=self.GetRandomString(),
91                require_signed_device_build=False,
92                has_bootloader_img=True,
93                has_radio_img=True,
94                test_name=self.GetRandomString(),
95                period=360,
96                priority="high",
97                device=[self.GetRandomString()],
98                required_host_equipment=[self.GetRandomString()],
99                required_device_equipment=[self.GetRandomString()],
100                device_pab_account_id=self.GetRandomString(),
101                shards=1,
102                param=[self.GetRandomString()],
103                retry_count=1,
104                gsi_storage_type=Status.STORAGE_TYPE_DICT["PAB"],
105                gsi_branch=self.GetRandomString(),
106                gsi_build_target=self.GetRandomString(),
107                gsi_pab_account_id=self.GetRandomString(),
108                gsi_vendor_version=self.GetRandomString(),
109                test_storage_type=Status.STORAGE_TYPE_DICT["PAB"],
110                test_branch=self.GetRandomString(),
111                test_build_target=self.GetRandomString(),
112                test_pab_account_id=self.GetRandomString(),
113                image_package_repo_base=self.GetRandomString(),
114                report_bucket=[],
115                report_spreadsheet_id=[],
116                report_persistent_url=[],
117                report_reference_url=[],
118            ))
119        api = schedule_info.ScheduleInfoApi()
120        response = api.set(container)
121
122        self.assertEqual(response.return_code, model.ReturnCodeMessage.SUCCESS)
123
124
125if __name__ == "__main__":
126    unittest.main()
127