1# Copyright 2020 Google Inc. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# 15################################################################################ 16"""Unit tests for Cloud Function request builds which builds projects.""" 17import os 18import sys 19import unittest 20 21from google.cloud import ndb 22 23sys.path.append(os.path.dirname(__file__)) 24# pylint: disable=wrong-import-position 25 26import datastore_entities 27import request_build 28import test_utils 29 30# pylint: disable=no-member 31 32 33class TestRequestBuilds(unittest.TestCase): 34 """Unit tests for sync.""" 35 36 @classmethod 37 def setUpClass(cls): 38 cls.ds_emulator = test_utils.start_datastore_emulator() 39 test_utils.wait_for_emulator_ready(cls.ds_emulator, 'datastore', 40 test_utils.DATASTORE_READY_INDICATOR) 41 test_utils.set_gcp_environment() 42 43 def setUp(self): 44 test_utils.reset_ds_emulator() 45 self.maxDiff = None # pylint: disable=invalid-name 46 47 def test_get_build_steps_no_project(self): 48 """Test for when project isn't available in datastore.""" 49 with ndb.Client().context(): 50 self.assertRaises(RuntimeError, request_build.get_build_steps, 51 'test-project', 'oss-fuzz', 'oss-fuzz-base') 52 53 def test_build_history(self): 54 """Testing build history.""" 55 with ndb.Client().context(): 56 datastore_entities.BuildsHistory(id='test-project-fuzzing', 57 build_tag='fuzzing', 58 project='test-project', 59 build_ids=[str(i) for i in range(1, 65) 60 ]).put() 61 request_build.update_build_history('test-project', '65', 'fuzzing') 62 expected_build_ids = [str(i) for i in range(2, 66)] 63 64 self.assertEqual(datastore_entities.BuildsHistory.query().get().build_ids, 65 expected_build_ids) 66 67 def test_build_history_no_existing_project(self): 68 """Testing build history when build history object is missing.""" 69 with ndb.Client().context(): 70 request_build.update_build_history('test-project', '1', 'fuzzing') 71 expected_build_ids = ['1'] 72 73 self.assertEqual(datastore_entities.BuildsHistory.query().get().build_ids, 74 expected_build_ids) 75 76 def test_get_project_data(self): 77 """Testing get project data.""" 78 with ndb.Client().context(): 79 self.assertRaises(RuntimeError, request_build.get_project_data, 80 'test-project') 81 82 @classmethod 83 def tearDownClass(cls): 84 test_utils.cleanup_emulator(cls.ds_emulator) 85 86 87if __name__ == '__main__': 88 unittest.main(exit=False) 89