1# Copyright 2021 Google LLC 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"""Tests for continuous_integration_module.""" 15import os 16import sys 17import unittest 18from unittest import mock 19 20import continuous_integration 21 22# pylint: disable=wrong-import-position,import-error 23sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 24 25import repo_manager 26 27# pylint: disable=no-self-use 28 29 30class FixGitRepoForDiffTest(unittest.TestCase): 31 """Tests for fix_git_repo_for_diff.""" 32 33 @mock.patch('utils.execute') 34 def test_fix_git_repo_for_diff(self, mock_execute): 35 """Tests that fix_git_repo_for_diff works as intended.""" 36 repo_dir = '/dir' 37 repo_manager_obj = repo_manager.RepoManager(repo_dir) 38 continuous_integration.fix_git_repo_for_diff(repo_manager_obj) 39 expected_command = [ 40 'git', 'symbolic-ref', 'refs/remotes/origin/HEAD', 41 'refs/remotes/origin/master' 42 ] 43 44 mock_execute.assert_called_with(expected_command, location=repo_dir) 45 46 47class GetBuildCommand(unittest.TestCase): 48 """Tests for get_build_command.""" 49 50 def test_build_command(self): 51 """Tests that get_build_command works as intended.""" 52 self.assertEqual(continuous_integration.get_build_command(), 'compile') 53 54 55class GetReplaceRepoAndBuildCommand(unittest.TestCase): 56 """Tests for get_replace_repo_and_build_command.""" 57 58 def test_get_replace_repo_and_build_command(self): 59 """Tests that get_replace_repo_and_build_command works as intended.""" 60 host_repo_path = '/path/on/host/to/repo' 61 image_repo_path = '/src/repo' 62 command = continuous_integration.get_replace_repo_and_build_command( 63 host_repo_path, image_repo_path) 64 expected_command = ('cd / && rm -rf /src/repo/* && ' 65 'cp -r /path/on/host/to/repo /src && cd - ' 66 '&& compile') 67 self.assertEqual(command, expected_command) 68 69 70class BuildExternalProjetDockerImage(unittest.TestCase): 71 """Tests for build_external_project_docker_image.""" 72 73 @mock.patch('helper.docker_build') 74 def test_build_external_project_docker_image(self, mock_docker_build): 75 """Tests that build_external_project_docker_image works as intended.""" 76 build_integration_path = '.clusterfuzzlite' 77 project_src = '/path/to/project/src' 78 continuous_integration.build_external_project_docker_image( 79 project_src, build_integration_path) 80 81 mock_docker_build.assert_called_with([ 82 '-t', 'external-project', '-f', 83 os.path.join('.clusterfuzzlite', 'Dockerfile'), project_src 84 ]) 85 86 87# TODO(metzman): Write tests for the rest of continuous_integration.py. 88