1414#
1515################################################################################
1616"""Unit tests for Cloud Function request builds which builds projects."""
17+ import base64
18+ import json
1719import os
1820import sys
1921import unittest
22+ from unittest import mock
2023
2124from google .cloud import ndb
2225
2629import datastore_entities
2730import request_build
2831import test_utils
32+ import build_project
33+ import build_lib
2934
3035# pylint: disable=no-member
3136
@@ -43,13 +48,96 @@ def setUpClass(cls):
4348 def setUp (self ):
4449 test_utils .reset_ds_emulator ()
4550 self .maxDiff = None # pylint: disable=invalid-name
51+ # Mocks globais para evitar chamadas de API reais
52+ self .mock_get_signed_url = mock .patch (
53+ 'build_lib.get_signed_url' ,
54+ return_value = 'https://example.com/signed-url' ).start ()
55+ self .mock_get_signed_policy = mock .patch (
56+ 'build_lib.get_signed_policy_document_upload_prefix' ,
57+ return_value = mock .MagicMock ()).start ()
58+ self .mock_curl_args = mock .patch (
59+ 'build_lib.signed_policy_document_curl_args' , return_value = []).start ()
60+
61+ def tearDown (self ):
62+ mock .patch .stopall ()
4663
4764 def test_get_build_steps_no_project (self ):
4865 """Test for when project isn't available in datastore."""
4966 with ndb .Client ().context ():
5067 self .assertRaises (RuntimeError , request_build .get_build_steps ,
5168 'test-project' )
5269
70+ @mock .patch ('build_project.run_build' , return_value = {'id' : 'mock-build-id' })
71+ def test_get_build_steps_with_base_os_version (self , mock_run_build ):
72+ """Test that get_build_steps uses the base_os_version."""
73+ project_name = 'example'
74+ base_os_version = 'ubuntu-24-04'
75+
76+ project_yaml_contents = """
77+ homepage: https://my-api.example.com
78+ main_repo: https://github.com/example/my-api
79+ language: c++
80+ vendor_ccs: []
81+ fuzzing_engines:
82+ - libfuzzer
83+ sanitizers:
84+ - address
85+ base_os_version: ubuntu-24-04
86+ """
87+ dockerfile_contents = """
88+ # Copyright 2017 Google Inc.
89+ #
90+ # Licensed under the Apache License, Version 2.0 (the "License");
91+ # you may not use this file except in compliance with the License.
92+ # You may obtain a copy of the License at
93+ #
94+ # http://www.apache.org/licenses/LICENSE-2.0
95+ #
96+ # Unless required by applicable law or agreed to in writing, software
97+ # distributed under the License is distributed on an "AS IS" BASIS,
98+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
99+ # See the License for the specific language governing permissions and
100+ # limitations under the License.
101+ #
102+ ################################################################################
103+
104+ FROM gcr.io/oss-fuzz-base/base-builder
105+ RUN apt-get update && apt-get install -y make
106+
107+ # Get *your* source code here.
108+ RUN git clone https://github.com/google/oss-fuzz.git my-git-repo
109+ WORKDIR my-git-repo
110+ COPY build.sh $SRC/
111+ """
112+ with ndb .Client ().context ():
113+ datastore_entities .Project (name = project_name ,
114+ project_yaml_contents = project_yaml_contents ,
115+ dockerfile_contents = dockerfile_contents ).put ()
116+
117+ event = {'data' : base64 .b64encode (project_name .encode ('utf-8' ))}
118+
119+ with mock .patch ('google.auth.default' , return_value = (None , 'oss-fuzz' )):
120+ request_build .request_build (event , None )
121+
122+ # Check that run_build was called.
123+ self .assertTrue (mock_run_build .called )
124+
125+ # Get the build_steps from the first call to run_build.
126+ self .assertEqual (2 , mock_run_build .call_count )
127+ build_steps = mock_run_build .call_args_list [0 ][0 ][1 ]
128+
129+ # Find the 'build-check' step and assert the runner image is correct.
130+ found_build_check_step = False
131+ for inner_step in build_steps [0 ]:
132+ if isinstance (
133+ inner_step ,
134+ dict ) and inner_step .get ('id' ) and 'build-check' in inner_step ['id' ]:
135+ found_build_check_step = True
136+ expected_image = f'gcr.io/oss-fuzz-base/base-runner:{ base_os_version } '
137+ self .assertIn (expected_image , inner_step ['args' ])
138+ break
139+ self .assertTrue (found_build_check_step , 'Build check step not found.' )
140+
53141 def test_build_history (self ):
54142 """Testing build history."""
55143 with ndb .Client ().context ():
0 commit comments