• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 json
18import datetime
19import os
20import sys
21import unittest
22from unittest import mock
23
24from google.cloud import ndb
25
26sys.path.append(os.path.dirname(__file__))
27# pylint: disable=wrong-import-position
28
29from datastore_entities import BuildsHistory
30from datastore_entities import Project
31from request_build import get_build_steps
32from request_build import get_project_data
33from request_build import update_build_history
34import test_utils
35
36# pylint: disable=no-member
37
38
39class TestRequestBuilds(unittest.TestCase):
40  """Unit tests for sync."""
41
42  @classmethod
43  def setUpClass(cls):
44    cls.ds_emulator = test_utils.start_datastore_emulator()
45    test_utils.wait_for_emulator_ready(cls.ds_emulator, 'datastore',
46                                       test_utils.DATASTORE_READY_INDICATOR)
47    test_utils.set_gcp_environment()
48
49  def setUp(self):
50    test_utils.reset_ds_emulator()
51
52  @mock.patch('build_lib.get_signed_url', return_value='test_url')
53  @mock.patch('datetime.datetime')
54  def test_get_build_steps(self, mocked_url, mocked_time):
55    """Test for get_build_steps."""
56    del mocked_url, mocked_time
57    datetime.datetime = test_utils.SpoofedDatetime
58    project_yaml_contents = ('language: c++\n'
59                             'sanitizers:\n'
60                             '  - address\n'
61                             'architectures:\n'
62                             '  - x86_64\n')
63    image_project = 'oss-fuzz'
64    base_images_project = 'oss-fuzz-base'
65    testcase_path = os.path.join(os.path.dirname(__file__),
66                                 'expected_build_steps.json')
67    with open(testcase_path) as testcase_file:
68      expected_build_steps = json.load(testcase_file)
69
70    with ndb.Client().context():
71      Project(name='test-project',
72              project_yaml_contents=project_yaml_contents,
73              dockerfile_contents='test line').put()
74      build_steps = get_build_steps('test-project', image_project,
75                                    base_images_project)
76    self.assertEqual(build_steps, expected_build_steps)
77
78  def test_get_build_steps_no_project(self):
79    """Test for when project isn't available in datastore."""
80    with ndb.Client().context():
81      self.assertRaises(RuntimeError, get_build_steps, 'test-project',
82                        'oss-fuzz', 'oss-fuzz-base')
83
84  def test_build_history(self):
85    """Testing build history."""
86    with ndb.Client().context():
87      BuildsHistory(id='test-project-fuzzing',
88                    build_tag='fuzzing',
89                    project='test-project',
90                    build_ids=[str(i) for i in range(1, 65)]).put()
91      update_build_history('test-project', '65', 'fuzzing')
92      expected_build_ids = [str(i) for i in range(2, 66)]
93
94      self.assertEqual(BuildsHistory.query().get().build_ids,
95                       expected_build_ids)
96
97  def test_build_history_no_existing_project(self):
98    """Testing build history when build history object is missing."""
99    with ndb.Client().context():
100      update_build_history('test-project', '1', 'fuzzing')
101      expected_build_ids = ['1']
102
103      self.assertEqual(BuildsHistory.query().get().build_ids,
104                       expected_build_ids)
105
106  def test_get_project_data(self):
107    """Testing get project data."""
108    with ndb.Client().context():
109      self.assertRaises(RuntimeError, get_project_data, 'test-project')
110
111  @classmethod
112  def tearDownClass(cls):
113    test_utils.cleanup_emulator(cls.ds_emulator)
114
115
116if __name__ == '__main__':
117  unittest.main(exit=False)
118