-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathbuild_project_test.py
More file actions
156 lines (138 loc) · 6.16 KB
/
build_project_test.py
File metadata and controls
156 lines (138 loc) · 6.16 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Copyright 2021 Google LLC
#
# 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 build_project."""
import json
import os
import sys
import unittest
from unittest import mock
from pyfakefs import fake_filesystem_unittest
FUNCTIONS_DIR = os.path.dirname(__file__)
sys.path.append(FUNCTIONS_DIR)
# pylint: disable=wrong-import-position
import build_project
import test_utils
# pylint: disable=no-member
class TestRequestCoverageBuilds(fake_filesystem_unittest.TestCase):
"""Unit tests for sync."""
def setUp(self):
self.maxDiff = None # pylint: disable=invalid-name
self.setUpPyfakefs()
@mock.patch('build_lib.get_signed_url', return_value='test_url')
@mock.patch('build_project.get_datetime_now',
return_value=test_utils.FAKE_DATETIME)
@mock.patch('build_lib.get_unique_build_step_image_id',
return_value='UNIQUE_ID')
def test_get_build_steps(self, mock_url, mock_get_datetime_now, mock_get_id):
"""Test for get_build_steps."""
del mock_url, mock_get_datetime_now
project_yaml_contents = (
'language: c++\n'
'sanitizers:\n'
' - address\n'
' - memory\n'
' - undefined\n'
'architectures:\n'
' - x86_64\n'
' - i386\n'
' - aarch64\n'
'main_repo: https://github.com/google/oss-fuzz.git\n')
self.fs.create_dir(test_utils.PROJECT_DIR)
test_utils.create_project_data(test_utils.PROJECT, project_yaml_contents)
expected_build_steps_file_path = test_utils.get_test_data_file_path(
'expected_build_steps.json')
self.fs.add_real_file(expected_build_steps_file_path)
with open(expected_build_steps_file_path) as expected_build_steps_file:
expected_build_steps = json.load(expected_build_steps_file)
config = build_project.Config(upload=True)
project_yaml, dockerfile = build_project.get_project_data(
test_utils.PROJECT)
build_steps, _ = build_project.get_build_steps(test_utils.PROJECT,
project_yaml, dockerfile,
config)
self.assertEqual(build_steps, expected_build_steps)
@mock.patch('build_lib.get_signed_url', return_value='test_url')
@mock.patch('build_project.get_datetime_now',
return_value=test_utils.FAKE_DATETIME)
@mock.patch('build_lib.get_unique_build_step_image_id',
return_value='UNIQUE_ID')
def test_get_centipede_build_steps(self, mock_url, mock_get_datetime_now,
mock_get_id):
"""Test for get_build_steps of centipede."""
del mock_url, mock_get_datetime_now
# The none sanitizer should be added automatically when other sanitizers are
# specified by the users.
project_yaml_contents = (
'language: c++\n'
'fuzzing_engines:\n'
' - centipede\n'
'sanitizers:\n'
' - address\n'
'architectures:\n'
' - x86_64\n'
'main_repo: https://github.com/google/centipede.git\n')
self.fs.create_dir(test_utils.PROJECT_DIR)
test_utils.create_project_data(test_utils.PROJECT, project_yaml_contents)
expected_build_steps_file_path = test_utils.get_test_data_file_path(
'expected_centipede_build_steps.json')
self.fs.add_real_file(expected_build_steps_file_path)
with open(expected_build_steps_file_path) as expected_build_steps_file:
expected_build_steps = json.load(expected_build_steps_file)
config = build_project.Config(upload=True)
project_yaml, dockerfile = build_project.get_project_data(
test_utils.PROJECT)
build_steps, _ = build_project.get_build_steps(test_utils.PROJECT,
project_yaml, dockerfile,
config)
self.assertEqual(build_steps, expected_build_steps)
@mock.patch('build_lib.get_signed_url', return_value='test_url')
@mock.patch('build_project.get_datetime_now',
return_value=test_utils.FAKE_DATETIME)
@mock.patch('build_lib.get_unique_build_step_image_id',
return_value='UNIQUE_ID')
def test_get_build_steps_ubuntu_24_04(self, mock_url, mock_get_datetime_now,
mock_get_id):
"""Test for get_build_steps with ubuntu-24-04."""
del mock_url, mock_get_datetime_now
project_yaml_contents = (
'language: c++\n'
'base_os_version: ubuntu-24-04\n'
'sanitizers:\n'
' - address\n'
' - memory\n'
' - undefined\n'
'architectures:\n'
' - x86_64\n'
' - i386\n'
' - aarch64\n'
'main_repo: https://github.com/google/oss-fuzz.git\n')
self.fs.create_dir(test_utils.PROJECT_DIR)
test_utils.create_project_data(test_utils.PROJECT, project_yaml_contents)
expected_build_steps_file_path = test_utils.get_test_data_file_path(
'expected_build_steps_ubuntu_24_04.json')
self.fs.add_real_file(expected_build_steps_file_path)
with open(expected_build_steps_file_path) as expected_build_steps_file:
expected_build_steps = json.load(expected_build_steps_file)
config = build_project.Config(upload=True)
project_yaml, dockerfile = build_project.get_project_data(
test_utils.PROJECT)
config.base_image_tag = project_yaml.get('base_os_version')
build_steps, _ = build_project.get_build_steps(test_utils.PROJECT,
project_yaml, dockerfile,
config)
self.assertEqual(build_steps, expected_build_steps)
if __name__ == '__main__':
unittest.main(exit=False)