aboutsummaryrefslogtreecommitdiff
path: root/infra/build/functions/request_build_test.py
blob: 57ff3fe4caa01ec55c71c8cd530511b349a1ff0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2020 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
"""Unit tests for Cloud Function request builds which builds projects."""
import json
import datetime
import os
import unittest
from unittest import mock

from google.cloud import ndb

from datastore_entities import BuildsHistory
from datastore_entities import Project
from request_build import get_build_steps
from request_build import get_project_data
from request_build import update_build_history
import test_utils


class TestRequestBuilds(unittest.TestCase):
  """Unit tests for sync."""

  @classmethod
  def setUpClass(cls):
    cls.ds_emulator = test_utils.start_datastore_emulator()
    test_utils.wait_for_emulator_ready(cls.ds_emulator, 'datastore',
                                       test_utils.DATASTORE_READY_INDICATOR)
    test_utils.set_gcp_environment()

  def setUp(self):
    test_utils.reset_ds_emulator()

  @mock.patch('build_lib.get_signed_url', return_value='test_url')
  @mock.patch('datetime.datetime')
  def test_get_build_steps(self, mocked_url, mocked_time):
    """Test for get_build_steps."""
    del mocked_url, mocked_time
    datetime.datetime = test_utils.SpoofedDatetime
    project_yaml_contents = ('language: c++\n'
                             'sanitizers:\n'
                             '  - address\n'
                             'architectures:\n'
                             '  - x86_64\n')
    image_project = 'oss-fuzz'
    base_images_project = 'oss-fuzz-base'
    testcase_path = os.path.join(os.path.dirname(__file__),
                                 'expected_build_steps.json')
    with open(testcase_path) as testcase_file:
      expected_build_steps = json.load(testcase_file)

    with ndb.Client().context():
      Project(name='test-project',
              project_yaml_contents=project_yaml_contents,
              dockerfile_contents='test line').put()

    build_steps = get_build_steps('test-project', image_project,
                                  base_images_project)
    self.assertEqual(build_steps, expected_build_steps)

  def test_get_build_steps_no_project(self):
    """Test for when project isn't available in datastore."""
    with ndb.Client().context():
      self.assertRaises(RuntimeError, get_build_steps, 'test-project',
                        'oss-fuzz', 'oss-fuzz-base')

  def test_build_history(self):
    """Testing build history."""
    with ndb.Client().context():
      BuildsHistory(id='test-project-fuzzing',
                    build_tag_suffix='fuzzing',
                    project='test-project',
                    build_ids=[str(i) for i in range(1, 65)]).put()
      update_build_history('test-project', '65', '-fuzzing')
      expected_build_ids = [str(i) for i in range(2, 66)]

      self.assertEqual(BuildsHistory.query().get().build_ids,
                       expected_build_ids)

  def test_build_history_no_existing_project(self):
    """Testing build history when build history object is missing."""
    with ndb.Client().context():
      update_build_history('test-project', '1', 'fuzzing')
      expected_build_ids = ['1']

      self.assertEqual(BuildsHistory.query().get().build_ids,
                       expected_build_ids)

  def test_get_project_data(self):
    """Testing get project data."""
    with ndb.Client().context():
      self.assertRaises(RuntimeError, get_project_data, 'test-project')

  @classmethod
  def tearDownClass(cls):
    test_utils.cleanup_emulator(cls.ds_emulator)


if __name__ == '__main__':
  unittest.main(exit=False)