File: recipe_test.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (181 lines) | stat: -rwxr-xr-x 7,277 bytes parent folder | download | duplicates (3)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env vpython3
# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Tests for recipe.py"""

import json
import os
import pathlib
import shutil
import tempfile
import unittest
from unittest import mock

import recipe


class LegacyRunnerTests(unittest.TestCase):

  class AsyncMock(mock.MagicMock):

    def __init__(self, *args, **kwargs):
      super().__init__(*args, **kwargs)
      self.returncode = 0

    async def wait(self):
      pass

  def setUp(self):
    self.tmp_dir = pathlib.Path(tempfile.mkdtemp())
    self.tmp_dir.joinpath('recipes').touch()
    self.build_dir = self.tmp_dir.joinpath('some', 'build', 'dir')
    self.addCleanup(shutil.rmtree, self.tmp_dir)

    self.subp_mock = self.AsyncMock()

    patch_tempdir = mock.patch('tempfile.TemporaryDirectory')
    self.mock_tempdir = patch_tempdir.start()
    self.mock_tempdir.return_value.__enter__.return_value = self.tmp_dir
    self.addCleanup(patch_tempdir.stop)

    patch_input = mock.patch('builtins.input')
    self.mock_input = patch_input.start()
    self.addCleanup(patch_input.stop)

    patch_terminal_size = mock.patch('os.get_terminal_size')
    mock_terminal_size = patch_terminal_size.start()
    mock_terminal_size.return_value = (128, 1)
    self.addCleanup(patch_terminal_size.stop)

  def testProps(self):
    runner = recipe.LegacyRunner(self.tmp_dir, {}, 'some-project',
                                 'some-bucket', 'some-builder', [], False,
                                 False, False, self.build_dir)
    self.assertEqual(
        runner._input_props['$recipe_engine/buildbucket']['build']['builder']
        ['builder'], 'some-builder')

  def testRun(self):
    runner = recipe.LegacyRunner(self.tmp_dir, {}, 'some-project',
                                 'some-bucket', 'some-builder', [], False,
                                 False, False, self.build_dir)
    self.subp_mock.returncode = 123
    with mock.patch('asyncio.create_subprocess_exec',
                    return_value=self.subp_mock):
      exit_code, _ = runner.run_recipe()
      self.assertEqual(exit_code, 123)

  def testJson(self):
    runner = recipe.LegacyRunner(self.tmp_dir, {}, 'some-project',
                                 'some-bucket', 'some-builder', [], False,
                                 False, False, self.build_dir)
    with mock.patch('asyncio.create_subprocess_exec',
                    return_value=self.subp_mock):
      # Passing run.
      self.subp_mock.returncode = 0
      with open(self.tmp_dir.joinpath('out.json'), 'w') as f:
        json.dump({}, f)
      _, error_msg = runner.run_recipe()
      self.assertIsNone(error_msg)

      # Missing json file
      self.subp_mock.returncode = 1
      rc, error_msg = runner.run_recipe()
      self.assertEqual(rc, 1)
      self.assertIsNone(error_msg)

      # Broken json
      with open(self.tmp_dir.joinpath('out.json'), 'w') as f:
        f.write('this-is-not-json')
      rc, error_msg = runner.run_recipe()
      self.assertEqual(rc, 1)
      self.assertIsNone(error_msg)

      # Actual json. It'll get printed to the terminal, so all that run_recipe()
      # returns is a generic failure message.
      with open(self.tmp_dir.joinpath('out.json'), 'w') as f:
        json.dump({'failure': {'humanReason': 'it exploded'}}, f)
      rc, error_msg = runner.run_recipe()
      self.assertEqual(rc, 1)
      self.assertIsNone(error_msg)

  def testReruns(self):
    runner = recipe.LegacyRunner(self.tmp_dir, {}, 'some-project',
                                 'some-bucket', 'some-builder', [], False,
                                 False, False, self.build_dir)
    with mock.patch('asyncio.create_subprocess_exec',
                    return_value=self.subp_mock):
      # Input "n" to the first re-run prompt.
      self.mock_input.return_value = 'n'
      with open(self.tmp_dir.joinpath('rerun_props.json'), 'w') as f:
        json.dump([['y', {'some-new-prop': 'some-val'}], ['n', {}]], f)
      _, error_msg = runner.run_recipe()
      self.assertEqual(error_msg, 'User-aborted due to warning')

      # Input "y" to too many re-runs.
      self.mock_input.return_value = 'y'
      with open(self.tmp_dir.joinpath('rerun_props.json'), 'w') as f:
        json.dump([['y', {'some-new-prop': 'some-val'}], ['n', {}]], f)
      _, error_msg = runner.run_recipe()
      self.assertEqual(error_msg, 'Exceeded too many recipe re-runs')

      # Re-running once and succeeding. Need to manage two different tmp dirs,
      # one for each recipe invocations.
      first_tmp_dir = self.tmp_dir
      second_tmp_dir = pathlib.Path(tempfile.mkdtemp())
      self.addCleanup(shutil.rmtree, second_tmp_dir)
      self.mock_input.return_value = 'y'
      with open(first_tmp_dir.joinpath('rerun_props.json'), 'w') as f:
        json.dump([['y', {'some-new-prop': 'some-val'}], ['n', {}]], f)
      self.mock_tempdir.side_effect = [first_tmp_dir, second_tmp_dir]
      _, error_msg = runner.run_recipe()
      self.assertIsNone(error_msg)


  def testRerunsWithForce(self):
    runner = recipe.LegacyRunner(self.tmp_dir, {}, 'some-project',
                                 'some-bucket', 'some-builder', [], False,
                                 False, True, self.build_dir)
    with mock.patch('asyncio.create_subprocess_exec',
                    return_value=self.subp_mock):
      # Re-running once and succeeding. Need to manage two different tmp dirs,
      # one for each recipe invocations. input() shouldn't be called since we
      # pass --force.
      first_tmp_dir = self.tmp_dir
      second_tmp_dir = pathlib.Path(tempfile.mkdtemp())
      self.addCleanup(shutil.rmtree, second_tmp_dir)
      with open(first_tmp_dir.joinpath('rerun_props.json'), 'w') as f:
        json.dump([['y', {'some-new-prop': 'some-val'}], ['n', {}]], f)
      self.mock_tempdir.side_effect = [first_tmp_dir, second_tmp_dir]
      _, error_msg = runner.run_recipe()
      self.assertIsNone(error_msg)
      self.mock_input.assert_not_called()

  def testRerunsWithOverwrite(self):
    runner = recipe.LegacyRunner(self.tmp_dir, {},
                                 'some-project',
                                 'some-bucket',
                                 'some-builder', [],
                                 False,
                                 False,
                                 False,
                                 self.build_dir,
                                 skip_coverage=True)
    with mock.patch('asyncio.create_subprocess_exec',
                    return_value=self.subp_mock):
      self.mock_input.return_value = 'n'
      with open(self.tmp_dir.joinpath('rerun_props.json'), 'w') as f:
        json.dump([['y', {'some-new-prop': 'some-val'}], ['n', {}]], f)
      runner.run_recipe()

      # The first run of the recipe should have coverage-related fields off
      # due to skip_coverage=True.
      stdin_write = self.subp_mock.mock_calls[0]
      input_props = json.loads(stdin_write.args[0])
      self.assertTrue(input_props['rerun_options']['bypass_branch_check'])
      self.assertTrue(input_props['rerun_options']['skip_instrumentation'])


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