• 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 that builds coverage reports."""
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 Project
30from build_and_run_coverage import get_build_steps
31import test_utils
32
33# pylint: disable=no-member
34
35
36class TestRequestCoverageBuilds(unittest.TestCase):
37  """Unit tests for sync."""
38
39  @classmethod
40  def setUpClass(cls):
41    cls.ds_emulator = test_utils.start_datastore_emulator()
42    test_utils.wait_for_emulator_ready(cls.ds_emulator, 'datastore',
43                                       test_utils.DATASTORE_READY_INDICATOR)
44    test_utils.set_gcp_environment()
45
46  def setUp(self):
47    test_utils.reset_ds_emulator()
48
49  @mock.patch('build_lib.get_signed_url', return_value='test_url')
50  @mock.patch('build_lib.download_corpora_steps',
51              return_value=[{
52                  'url': 'test_download'
53              }])
54  @mock.patch('datetime.datetime')
55  def test_get_coverage_build_steps(self, mocked_url, mocked_corpora_steps,
56                                    mocked_time):
57    """Test for get_build_steps."""
58    del mocked_url, mocked_corpora_steps, mocked_time
59    datetime.datetime = test_utils.SpoofedDatetime
60    project_yaml_contents = ('language: c++\n'
61                             'sanitizers:\n'
62                             '  - address\n'
63                             'architectures:\n'
64                             '  - x86_64\n')
65    dockerfile_contents = 'test line'
66    image_project = 'oss-fuzz'
67    base_images_project = 'oss-fuzz-base'
68    testcase_path = os.path.join(os.path.dirname(__file__),
69                                 'expected_coverage_build_steps.json')
70    with open(testcase_path) as testcase_file:
71      expected_coverage_build_steps = json.load(testcase_file)
72
73    with ndb.Client().context():
74      Project(name='test-project',
75              project_yaml_contents=project_yaml_contents,
76              dockerfile_contents=dockerfile_contents).put()
77
78    dockerfile_lines = dockerfile_contents.split('\n')
79    build_steps = get_build_steps('test-project', project_yaml_contents,
80                                  dockerfile_lines, image_project,
81                                  base_images_project)
82    self.assertEqual(build_steps, expected_coverage_build_steps)
83
84  @classmethod
85  def tearDownClass(cls):
86    test_utils.cleanup_emulator(cls.ds_emulator)
87
88
89if __name__ == '__main__':
90  unittest.main(exit=False)
91