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.proto import model
26from webapp.src.tasks import indexing
27from webapp.src.testing import unittest_base
28
29
30class IndexingHandlerTest(unittest_base.UnitTestBase):
31    """Tests for IndexingHandler.
32
33    Attributes:
34        testbed: A Testbed instance which provides local unit testing.
35        indexing_handler: A mock IndexingHandler instance.
36    """
37
38    def setUp(self):
39        """Initializes test"""
40        super(IndexingHandlerTest, self).setUp()
41        # Mocking IndexingHandler.
42        self.indexing_handler = indexing.IndexingHandler(mock.Mock())
43        self.indexing_handler.request = mock.Mock()
44
45    def testSingleJobReindexing(self):
46        """Asserts re-indexing links job and schedule successfully."""
47
48        print("\nCreating a single schedule...")
49        schedule = self.GenerateScheduleModel()
50        schedule.put()
51
52        schedules = model.ScheduleModel.query().fetch()
53        self.assertEqual(1, len(schedules))
54
55        print("Creating a job for stored schedule...")
56        for schedule in schedules:
57            job = model.JobModel()
58            job.priority = schedule.priority
59            job.test_name = schedule.test_name
60            job.period = schedule.period
61            job.build_storage_type = schedule.build_storage_type
62            job.manifest_branch = schedule.manifest_branch
63            job.build_target = schedule.build_target
64            job.pab_account_id = schedule.device_pab_account_id
65            job.shards = schedule.shards
66            job.retry_count = schedule.retry_count
67            job.gsi_storage_type = schedule.gsi_storage_type
68            job.gsi_branch = schedule.gsi_branch
69            job.gsi_build_target = schedule.gsi_build_target
70            job.gsi_pab_account_id = schedule.gsi_pab_account_id
71            job.gsi_vendor_version = schedule.gsi_vendor_version
72            job.test_storage_type = schedule.test_storage_type
73            job.test_branch = schedule.test_branch
74            job.test_build_target = schedule.test_build_target
75            job.test_pab_account_id = schedule.test_pab_account_id
76            job.put()
77
78        jobs = model.JobModel.query().fetch()
79        self.assertEqual(1, len(jobs))
80
81        print("Seeking children jobs before re-indexing...")
82        jobs = model.JobModel.query().fetch()
83        for job in jobs:
84            parent_key = job.parent_schedule
85            self.assertIsNone(parent_key)
86
87        print("Seeking children jobs after re-indexing...")
88        self.indexing_handler.request.get = mock.MagicMock(return_value="job")
89        self.indexing_handler.post()
90        jobs = model.JobModel.query().fetch()
91        for job in jobs:
92            parent_key = job.parent_schedule
93            parent_schedule = parent_key.get()
94            self.assertEqual(
95                True,
96                ((parent_schedule.priority == job.priority) and
97                 (parent_schedule.test_name == job.test_name) and
98                 (parent_schedule.period == job.period) and
99                 (parent_schedule.build_storage_type == job.build_storage_type)
100                 and (parent_schedule.manifest_branch == job.manifest_branch)
101                 and (parent_schedule.build_target == job.build_target) and
102                 (parent_schedule.device_pab_account_id == job.pab_account_id)
103                 and (parent_schedule.shards == job.shards) and
104                 (parent_schedule.retry_count == job.retry_count) and
105                 (parent_schedule.gsi_storage_type == job.gsi_storage_type) and
106                 (parent_schedule.gsi_branch == job.gsi_branch) and
107                 (parent_schedule.gsi_build_target == job.gsi_build_target) and
108                 (parent_schedule.gsi_pab_account_id == job.gsi_pab_account_id)
109                 and
110                 (parent_schedule.test_storage_type == job.test_storage_type)
111                 and (parent_schedule.test_branch == job.test_branch) and
112                 (parent_schedule.test_build_target == job.test_build_target)
113                 and (parent_schedule.test_pab_account_id ==
114                      job.test_pab_account_id)))
115
116    def testMultiJobReindexing(self):
117        """Asserts re-indexing links job and schedule successfully."""
118        print("\nCreating four schedules...")
119        for num in xrange(4):
120            schedule = self.GenerateScheduleModel(test_name=str(num + 1))
121            schedule.put()
122            schedule.put()
123
124        schedules = model.ScheduleModel.query().fetch()
125        self.assertEqual(4, len(schedules))
126
127        print("Creating jobs as number of test_name...")
128        for schedule in schedules:
129            for _ in xrange(int(schedule.test_name)):
130                job = model.JobModel()
131                job.priority = schedule.priority
132                job.test_name = schedule.test_name
133                job.period = schedule.period
134                job.build_storage_type = schedule.build_storage_type
135                job.manifest_branch = schedule.manifest_branch
136                job.build_target = schedule.build_target
137                job.pab_account_id = schedule.device_pab_account_id
138                job.shards = schedule.shards
139                job.retry_count = schedule.retry_count
140                job.gsi_storage_type = schedule.gsi_storage_type
141                job.gsi_branch = schedule.gsi_branch
142                job.gsi_build_target = schedule.gsi_build_target
143                job.gsi_pab_account_id = schedule.gsi_pab_account_id
144                job.gsi_vendor_version = schedule.gsi_vendor_version
145                job.test_storage_type = schedule.test_storage_type
146                job.test_branch = schedule.test_branch
147                job.test_build_target = schedule.test_build_target
148                job.test_pab_account_id = schedule.test_pab_account_id
149                job.put()
150
151        jobs = model.JobModel.query().fetch()
152        self.assertEqual(10, len(jobs))
153
154        print("Seeking children jobs before re-indexing...")
155        jobs = model.JobModel.query().fetch()
156        for job in jobs:
157            parent_key = job.parent_schedule
158            self.assertIsNone(parent_key)
159
160        print("Seeking children jobs after re-indexing...")
161        self.indexing_handler.request.get = mock.MagicMock(return_value="job")
162        self.indexing_handler.post()
163        jobs = model.JobModel.query().fetch()
164        for job in jobs:
165            parent_key = job.parent_schedule
166            parent_schedule = parent_key.get()
167            self.assertEqual(
168                True,
169                ((parent_schedule.priority == job.priority) and
170                 (parent_schedule.test_name == job.test_name) and
171                 (parent_schedule.period == job.period) and
172                 (parent_schedule.build_storage_type == job.build_storage_type)
173                 and (parent_schedule.manifest_branch == job.manifest_branch)
174                 and (parent_schedule.build_target == job.build_target) and
175                 (parent_schedule.device_pab_account_id == job.pab_account_id)
176                 and (parent_schedule.shards == job.shards) and
177                 (parent_schedule.retry_count == job.retry_count) and
178                 (parent_schedule.gsi_storage_type == job.gsi_storage_type) and
179                 (parent_schedule.gsi_branch == job.gsi_branch) and
180                 (parent_schedule.gsi_build_target == job.gsi_build_target) and
181                 (parent_schedule.gsi_pab_account_id == job.gsi_pab_account_id)
182                 and
183                 (parent_schedule.test_storage_type == job.test_storage_type)
184                 and (parent_schedule.test_branch == job.test_branch) and
185                 (parent_schedule.test_build_target == job.test_build_target)
186                 and (parent_schedule.test_pab_account_id ==
187                      job.test_pab_account_id)))
188
189
190if __name__ == "__main__":
191    unittest.main()
192