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.endpoint import build_info 26from webapp.src.proto import model 27from webapp.src.testing import unittest_base 28 29 30class BuildInfoTest(unittest_base.UnitTestBase): 31 """A class to test build_info endpoint API.""" 32 33 def setUp(self): 34 """Initializes test""" 35 super(BuildInfoTest, self).setUp() 36 37 def testSetNewBuildModel(self): 38 """Asserts build_info/set API receives a new build.""" 39 builds = model.BuildModel.query().fetch() 40 self.assertEqual(len(builds), 0) 41 container = ( 42 build_info.BUILD_INFO_RESOURCE.combined_message_class( 43 manifest_branch=self.GetRandomString(), 44 build_id=self.GetRandomString(), 45 build_target=self.GetRandomString(), 46 build_type=self.GetRandomString(), 47 artifact_type=self.GetRandomString(), 48 )) 49 api = build_info.BuildInfoApi() 50 response = api.set(container) 51 52 self.assertEqual(response.return_code, model.ReturnCodeMessage.SUCCESS) 53 builds = model.BuildModel.query().fetch() 54 self.assertEqual(len(builds), 1) 55 56 def testSetDuplicatedBuildModel(self): 57 """Asserts build_info/set API receives a duplicated build.""" 58 manifest_branch = self.GetRandomString() 59 build_id = self.GetRandomString() 60 build_target = self.GetRandomString() 61 build_type = self.GetRandomString() 62 artifact_type = self.GetRandomString() 63 64 builds = model.BuildModel.query().fetch() 65 self.assertEqual(len(builds), 0) 66 container = ( 67 build_info.BUILD_INFO_RESOURCE.combined_message_class( 68 manifest_branch=manifest_branch, 69 build_id=build_id, 70 build_target=build_target, 71 build_type=build_type, 72 artifact_type=artifact_type, 73 )) 74 api = build_info.BuildInfoApi() 75 response = api.set(container) 76 77 self.assertEqual(response.return_code, model.ReturnCodeMessage.SUCCESS) 78 builds = model.BuildModel.query().fetch() 79 self.assertEqual(len(builds), 1) 80 81 container = ( 82 build_info.BUILD_INFO_RESOURCE.combined_message_class( 83 manifest_branch=manifest_branch, 84 build_id=build_id, 85 build_target=build_target, 86 build_type=build_type, 87 artifact_type=artifact_type, 88 )) 89 api = build_info.BuildInfoApi() 90 response = api.set(container) 91 self.assertEqual(response.return_code, model.ReturnCodeMessage.SUCCESS) 92 builds = model.BuildModel.query().fetch() 93 self.assertEqual(len(builds), 1) 94 95 def testUpdateSignedBuildModel(self): 96 """Asserts build_info/set API receives a duplicated build.""" 97 manifest_branch = self.GetRandomString() 98 build_id = self.GetRandomString() 99 build_target = self.GetRandomString() 100 build_type = self.GetRandomString() 101 artifact_type = self.GetRandomString() 102 103 builds = model.BuildModel.query().fetch() 104 self.assertEqual(len(builds), 0) 105 container = ( 106 build_info.BUILD_INFO_RESOURCE.combined_message_class( 107 manifest_branch=manifest_branch, 108 build_id=build_id, 109 build_target=build_target, 110 build_type=build_type, 111 artifact_type=artifact_type, 112 signed=False, 113 )) 114 api = build_info.BuildInfoApi() 115 response = api.set(container) 116 117 self.assertEqual(response.return_code, model.ReturnCodeMessage.SUCCESS) 118 builds = model.BuildModel.query().fetch() 119 self.assertEqual(len(builds), 1) 120 121 container = ( 122 build_info.BUILD_INFO_RESOURCE.combined_message_class( 123 manifest_branch=manifest_branch, 124 build_id=build_id, 125 build_target=build_target, 126 build_type=build_type, 127 artifact_type=artifact_type, 128 signed=True 129 )) 130 api = build_info.BuildInfoApi() 131 response = api.set(container) 132 self.assertEqual(response.return_code, model.ReturnCodeMessage.SUCCESS) 133 builds = model.BuildModel.query().fetch() 134 self.assertEqual(len(builds), 1) 135 self.assertEqual(builds[0].signed, True) 136 137 138if __name__ == "__main__": 139 unittest.main() 140