aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/update_packages_and_test_cq_unittest.py
blob: 58f99e8375bf77e0f3ff59541e87931f5ae961db (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2020 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unittests for running tryjobs after updating packages."""

from __future__ import print_function

import json
import unittest
import unittest.mock as mock

from test_helpers import ArgsOutputTest
from test_helpers import CreateTemporaryFile
from update_chromeos_llvm_hash import CommitContents
import update_chromeos_llvm_hash
import update_packages_and_test_cq


class UpdatePackagesAndRunTestCQTest(unittest.TestCase):
  """Unittests for CQ dry run after updating packages."""

  def testGetCQDependString(self):
    test_no_changelists = []
    test_single_changelist = [1234]
    test_multiple_changelists = [1234, 5678]

    self.assertEqual(
        update_packages_and_test_cq.GetCQDependString(test_no_changelists),
        None)

    self.assertEqual(
        update_packages_and_test_cq.GetCQDependString(test_single_changelist),
        '\nCq-Depend: chromium:1234')

    self.assertEqual(
        update_packages_and_test_cq.GetCQDependString(
            test_multiple_changelists),
        '\nCq-Depend: chromium:1234, chromium:5678')

  # Mock ExecCommandAndCaptureOutput for the gerrit command execution.
  @mock.patch.object(
      update_packages_and_test_cq,
      'ExecCommandAndCaptureOutput',
      return_value=None)
  def teststartCQDryRunNoDeps(self, mock_exec_cmd):
    chroot_path = '/abs/path/to/chroot'
    test_cl_number = 1000

    # test with no deps cls.
    extra_cls = []
    update_packages_and_test_cq.startCQDryRun(test_cl_number, extra_cls,
                                              chroot_path)

    expected_gerrit_message = [
        '%s/chromite/bin/gerrit' % chroot_path, 'label-cq',
        str(test_cl_number), '1'
    ]

    mock_exec_cmd.assert_called_once_with(expected_gerrit_message)

  # Mock ExecCommandAndCaptureOutput for the gerrit command execution.
  @mock.patch.object(
      update_packages_and_test_cq,
      'ExecCommandAndCaptureOutput',
      return_value=None)
  # test with a single deps cl.
  def teststartCQDryRunSingleDep(self, mock_exec_cmd):
    chroot_path = '/abs/path/to/chroot'
    test_cl_number = 1000

    extra_cls = [2000]
    update_packages_and_test_cq.startCQDryRun(test_cl_number, extra_cls,
                                              chroot_path)

    expected_gerrit_cmd_1 = [
        '%s/chromite/bin/gerrit' % chroot_path, 'label-cq',
        str(test_cl_number), '1'
    ]
    expected_gerrit_cmd_2 = [
        '%s/chromite/bin/gerrit' % chroot_path, 'label-cq',
        str(2000), '1'
    ]

    self.assertEqual(mock_exec_cmd.call_count, 2)
    self.assertEqual(mock_exec_cmd.call_args_list[0][0][0],
                     expected_gerrit_cmd_1)
    self.assertEqual(mock_exec_cmd.call_args_list[1][0][0],
                     expected_gerrit_cmd_2)

  # Mock ExecCommandAndCaptureOutput for the gerrit command execution.
  @mock.patch.object(
      update_packages_and_test_cq,
      'ExecCommandAndCaptureOutput',
      return_value=None)
  def teststartCQDryRunMultipleDep(self, mock_exec_cmd):
    chroot_path = '/abs/path/to/chroot'
    test_cl_number = 1000

    # test with multiple deps cls.
    extra_cls = [3000, 4000]
    update_packages_and_test_cq.startCQDryRun(test_cl_number, extra_cls,
                                              chroot_path)

    expected_gerrit_cmd_1 = [
        '%s/chromite/bin/gerrit' % chroot_path, 'label-cq',
        str(test_cl_number), '1'
    ]
    expected_gerrit_cmd_2 = [
        '%s/chromite/bin/gerrit' % chroot_path, 'label-cq',
        str(3000), '1'
    ]
    expected_gerrit_cmd_3 = [
        '%s/chromite/bin/gerrit' % chroot_path, 'label-cq',
        str(4000), '1'
    ]

    self.assertEqual(mock_exec_cmd.call_count, 3)
    self.assertEqual(mock_exec_cmd.call_args_list[0][0][0],
                     expected_gerrit_cmd_1)
    self.assertEqual(mock_exec_cmd.call_args_list[1][0][0],
                     expected_gerrit_cmd_2)
    self.assertEqual(mock_exec_cmd.call_args_list[2][0][0],
                     expected_gerrit_cmd_3)


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