• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# Copyright 2018 The Chromium OS Authors. All rights reserved.
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8"""Unittest for buildbot_utils.py."""
9
10from __future__ import print_function
11
12import time
13
14import unittest
15from unittest.mock import patch
16
17from cros_utils import buildbot_utils
18from cros_utils import command_executer
19
20
21class TrybotTest(unittest.TestCase):
22  """Test for CommandExecuter class."""
23
24  old_tryjob_out = (
25      'Verifying patches...\n'
26      'Submitting tryjob...\n'
27      'Successfully sent PUT request to [buildbucket_bucket:master.chromiumos.t'
28      'ryserver] with [config:success-build] [buildbucket_id:895272114382368817'
29      '6].\n'
30      'Tryjob submitted!\n'
31      'To view your tryjobs, visit:\n'
32      '  http://cros-goldeneye/chromeos/healthmonitoring/buildDetails?buildbuck'
33      'etId=8952721143823688176\n'
34      '  https://uberchromegw.corp.google.com/i/chromiumos.tryserver/waterfall?'
35      'committer=laszio@chromium.org&builder=etc\n')
36  tryjob_out = (
37      '[{"buildbucket_id": "8952721143823688176", "build_config": '
38      '"cave-llvm-toolchain-tryjob", "url": '
39      # pylint: disable=line-too-long
40      '"http://cros-goldeneye/chromeos/healthmonitoring/buildDetails?buildbucketId=8952721143823688176"}]'
41  )
42
43  GSUTILS_LS = '\n'.join([
44      'gs://chromeos-image-archive/{0}/R78-12421.0.0/',
45      'gs://chromeos-image-archive/{0}/R78-12422.0.0/',
46      'gs://chromeos-image-archive/{0}/R78-12423.0.0/',
47      # "R79-12384.0.0" image should be blacklisted.
48      # TODO(crbug.com/992242): Remove the filter by 2019-09-05.
49      'gs://chromeos-image-archive/{0}/R79-12384.0.0/',
50  ])
51
52  buildresult_out = (
53      '{"8952721143823688176": {"status": "pass", "artifacts_url":'
54      '"gs://chromeos-image-archive/trybot-elm-release-tryjob/R67-10468.0.0-'
55      'b20789"}}')
56
57  buildbucket_id = '8952721143823688176'
58  counter_1 = 10
59
60  def testGetTrybotImage(self):
61    with patch.object(buildbot_utils, 'SubmitTryjob') as mock_submit:
62      with patch.object(buildbot_utils, 'PeekTrybotImage') as mock_peek:
63        with patch.object(time, 'sleep', return_value=None):
64
65          def peek(_chromeos_root, _buildbucket_id):
66            self.counter_1 -= 1
67            if self.counter_1 >= 0:
68              return ('running', '')
69            return ('pass',
70                    'gs://chromeos-image-archive/trybot-elm-release-tryjob/'
71                    'R67-10468.0.0-b20789')
72
73          mock_peek.side_effect = peek
74          mock_submit.return_value = self.buildbucket_id
75
76          # sync
77          buildbucket_id, image = buildbot_utils.GetTrybotImage(
78              '/tmp', 'falco-release-tryjob', [])
79          self.assertEqual(buildbucket_id, self.buildbucket_id)
80          self.assertEqual('trybot-elm-release-tryjob/'
81                           'R67-10468.0.0-b20789', image)
82
83          # async
84          buildbucket_id, image = buildbot_utils.GetTrybotImage(
85              '/tmp', 'falco-release-tryjob', [], asynchronous=True)
86          self.assertEqual(buildbucket_id, self.buildbucket_id)
87          self.assertEqual(' ', image)
88
89  def testSubmitTryjob(self):
90    with patch.object(command_executer.CommandExecuter,
91                      'RunCommandWOutput') as mocked_run:
92      mocked_run.return_value = (0, self.tryjob_out, '')
93      buildbucket_id = buildbot_utils.SubmitTryjob('/', 'falco-release-tryjob',
94                                                   [], [])
95      self.assertEqual(buildbucket_id, self.buildbucket_id)
96
97  def testPeekTrybotImage(self):
98    with patch.object(command_executer.CommandExecuter,
99                      'RunCommandWOutput') as mocked_run:
100      # pass
101      mocked_run.return_value = (0, self.buildresult_out, '')
102      status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id)
103      self.assertEqual('pass', status)
104      self.assertEqual(
105          'gs://chromeos-image-archive/trybot-elm-release-tryjob/'
106          'R67-10468.0.0-b20789', image)
107
108      # running
109      mocked_run.return_value = (1, '', '')
110      status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id)
111      self.assertEqual('running', status)
112      self.assertEqual(None, image)
113
114      # fail
115      buildresult_fail = self.buildresult_out.replace('\"pass\"', '\"fail\"')
116      mocked_run.return_value = (0, buildresult_fail, '')
117      status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id)
118      self.assertEqual('fail', status)
119      self.assertEqual(
120          'gs://chromeos-image-archive/trybot-elm-release-tryjob/'
121          'R67-10468.0.0-b20789', image)
122
123  def testParseTryjobBuildbucketId(self):
124    buildbucket_id = buildbot_utils.ParseTryjobBuildbucketId(self.tryjob_out)
125    self.assertEqual(buildbucket_id, self.buildbucket_id)
126
127  def testGetLatestImageValid(self):
128    with patch.object(command_executer.CommandExecuter,
129                      'ChrootRunCommandWOutput') as mocked_run:
130      with patch.object(buildbot_utils, 'DoesImageExist') as mocked_imageexist:
131        IMAGE_DIR = 'lulu-release'
132        mocked_run.return_value = (0, self.GSUTILS_LS.format(IMAGE_DIR), '')
133        mocked_imageexist.return_value = True
134        image = buildbot_utils.GetLatestImage('', IMAGE_DIR)
135        self.assertEqual(image, '{0}/R78-12423.0.0'.format(IMAGE_DIR))
136
137  def testGetLatestImageInvalid(self):
138    with patch.object(command_executer.CommandExecuter,
139                      'ChrootRunCommandWOutput') as mocked_run:
140      with patch.object(buildbot_utils, 'DoesImageExist') as mocked_imageexist:
141        IMAGE_DIR = 'kefka-release'
142        mocked_run.return_value = (0, self.GSUTILS_LS.format(IMAGE_DIR), '')
143        mocked_imageexist.return_value = False
144        image = buildbot_utils.GetLatestImage('', IMAGE_DIR)
145        self.assertIsNone(image)
146
147
148if __name__ == '__main__':
149  unittest.main()
150