aboutsummaryrefslogtreecommitdiff
path: root/infra/cifuzz/continuous_integration_test.py
blob: 7c7e3eefdca12e671edf3e9a00233dca7ece0f8e (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
# 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.
"""Tests for continuous_integration_module."""
import os
import sys
import unittest
from unittest import mock

import continuous_integration

# pylint: disable=wrong-import-position,import-error
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import repo_manager

# pylint: disable=no-self-use


class FixGitRepoForDiffTest(unittest.TestCase):
  """Tests for fix_git_repo_for_diff."""

  @mock.patch('utils.execute')
  def test_fix_git_repo_for_diff(self, mock_execute):
    """Tests that fix_git_repo_for_diff works as intended."""
    repo_dir = '/dir'
    repo_manager_obj = repo_manager.RepoManager(repo_dir)
    continuous_integration.fix_git_repo_for_diff(repo_manager_obj)
    expected_command = [
        'git', 'symbolic-ref', 'refs/remotes/origin/HEAD',
        'refs/remotes/origin/master'
    ]

    mock_execute.assert_called_with(expected_command, location=repo_dir)


class GetBuildCommand(unittest.TestCase):
  """Tests for get_build_command."""

  def test_build_command(self):
    """Tests that get_build_command works as intended."""
    self.assertEqual(continuous_integration.get_build_command(), 'compile')


class GetReplaceRepoAndBuildCommand(unittest.TestCase):
  """Tests for get_replace_repo_and_build_command."""

  def test_get_replace_repo_and_build_command(self):
    """Tests that get_replace_repo_and_build_command works as intended."""
    host_repo_path = '/path/on/host/to/repo'
    image_repo_path = '/src/repo'
    command = continuous_integration.get_replace_repo_and_build_command(
        host_repo_path, image_repo_path)
    expected_command = ('cd / && rm -rf /src/repo/* && '
                        'cp -r /path/on/host/to/repo /src && cd - '
                        '&& compile')
    self.assertEqual(command, expected_command)


class BuildExternalProjetDockerImage(unittest.TestCase):
  """Tests for build_external_project_docker_image."""

  @mock.patch('helper.docker_build')
  def test_build_external_project_docker_image(self, mock_docker_build):
    """Tests that build_external_project_docker_image works as intended."""
    build_integration_path = '.clusterfuzzlite'
    project_src = '/path/to/project/src'
    continuous_integration.build_external_project_docker_image(
        project_src, build_integration_path)

    mock_docker_build.assert_called_with([
        '-t', 'external-project', '-f',
        os.path.join('.clusterfuzzlite', 'Dockerfile'), project_src
    ])


# TODO(metzman): Write tests for the rest of continuous_integration.py.