aboutsummaryrefslogtreecommitdiff
path: root/cros_utils/buildbot_utils_unittest.py
blob: c615c95ffec62701862966d97a797a4f369456ec (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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2018 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.

"""Unittest for buildbot_utils.py."""

from __future__ import print_function

import time

import unittest
from unittest.mock import patch

from cros_utils import buildbot_utils
from cros_utils import command_executer


class TrybotTest(unittest.TestCase):
  """Test for CommandExecuter class."""

  tryjob_out = (
      '[{"buildbucket_id": "8952721143823688176", "build_config": '
      '"cave-llvm-toolchain-tryjob", "url": '
      # pylint: disable=line-too-long
      '"http://cros-goldeneye/chromeos/healthmonitoring/buildDetails?buildbucketId=8952721143823688176"}]'
  )

  GSUTILS_LS = '\n'.join([
      'gs://chromeos-image-archive/{0}/R78-12421.0.0/',
      'gs://chromeos-image-archive/{0}/R78-12422.0.0/',
      'gs://chromeos-image-archive/{0}/R78-12423.0.0/',
  ])

  GSUTILS_LS_RECIPE = '\n'.join([
      'gs://chromeos-image-archive/{0}/R83-12995.0.0-30031-8885075268947031/',
      'gs://chromeos-image-archive/{0}/R83-13003.0.0-30196-8884755532184725/',
      'gs://chromeos-image-archive/{0}/R83-13003.0.0-30218-8884712858556419/',
  ])

  buildresult_out = (
      '{"8952721143823688176": {"status": "pass", "artifacts_url":'
      '"gs://chromeos-image-archive/trybot-elm-release-tryjob/R67-10468.0.0-'
      'b20789"}}')

  buildbucket_id = '8952721143823688176'
  counter_1 = 10

  def testGetTrybotImage(self):
    with patch.object(buildbot_utils, 'SubmitTryjob') as mock_submit:
      with patch.object(buildbot_utils, 'PeekTrybotImage') as mock_peek:
        with patch.object(time, 'sleep', return_value=None):

          def peek(_chromeos_root, _buildbucket_id):
            self.counter_1 -= 1
            if self.counter_1 >= 0:
              return ('running', '')
            return ('pass',
                    'gs://chromeos-image-archive/trybot-elm-release-tryjob/'
                    'R67-10468.0.0-b20789')

          mock_peek.side_effect = peek
          mock_submit.return_value = self.buildbucket_id

          # sync
          buildbucket_id, image = buildbot_utils.GetTrybotImage(
              '/tmp', 'falco-release-tryjob', [])
          self.assertEqual(buildbucket_id, self.buildbucket_id)
          self.assertEqual('trybot-elm-release-tryjob/'
                           'R67-10468.0.0-b20789', image)

          # async
          buildbucket_id, image = buildbot_utils.GetTrybotImage(
              '/tmp', 'falco-release-tryjob', [], asynchronous=True)
          self.assertEqual(buildbucket_id, self.buildbucket_id)
          self.assertEqual(' ', image)

  def testSubmitTryjob(self):
    with patch.object(command_executer.CommandExecuter,
                      'RunCommandWOutput') as mocked_run:
      mocked_run.return_value = (0, self.tryjob_out, '')
      buildbucket_id = buildbot_utils.SubmitTryjob('/', 'falco-release-tryjob',
                                                   [], [])
      self.assertEqual(buildbucket_id, self.buildbucket_id)

  def testPeekTrybotImage(self):
    with patch.object(command_executer.CommandExecuter,
                      'RunCommandWOutput') as mocked_run:
      # pass
      mocked_run.return_value = (0, self.buildresult_out, '')
      status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id)
      self.assertEqual('pass', status)
      self.assertEqual(
          'gs://chromeos-image-archive/trybot-elm-release-tryjob/'
          'R67-10468.0.0-b20789', image)

      # running
      mocked_run.return_value = (1, '', '')
      status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id)
      self.assertEqual('running', status)
      self.assertEqual(None, image)

      # fail
      buildresult_fail = self.buildresult_out.replace('\"pass\"', '\"fail\"')
      mocked_run.return_value = (0, buildresult_fail, '')
      status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id)
      self.assertEqual('fail', status)
      self.assertEqual(
          'gs://chromeos-image-archive/trybot-elm-release-tryjob/'
          'R67-10468.0.0-b20789', image)

  def testParseTryjobBuildbucketId(self):
    buildbucket_id = buildbot_utils.ParseTryjobBuildbucketId(self.tryjob_out)
    self.assertEqual(buildbucket_id, self.buildbucket_id)

  def testGetLatestImageValid(self):
    with patch.object(command_executer.CommandExecuter,
                      'ChrootRunCommandWOutput') as mocked_run:
      with patch.object(buildbot_utils, 'DoesImageExist') as mocked_imageexist:
        IMAGE_DIR = 'lulu-release'
        mocked_run.return_value = (0, self.GSUTILS_LS.format(IMAGE_DIR), '')
        mocked_imageexist.return_value = True
        image = buildbot_utils.GetLatestImage('', IMAGE_DIR)
        self.assertEqual(image, '{0}/R78-12423.0.0'.format(IMAGE_DIR))

  def testGetLatestImageInvalid(self):
    with patch.object(command_executer.CommandExecuter,
                      'ChrootRunCommandWOutput') as mocked_run:
      with patch.object(buildbot_utils, 'DoesImageExist') as mocked_imageexist:
        IMAGE_DIR = 'kefka-release'
        mocked_run.return_value = (0, self.GSUTILS_LS.format(IMAGE_DIR), '')
        mocked_imageexist.return_value = False
        image = buildbot_utils.GetLatestImage('', IMAGE_DIR)
        self.assertIsNone(image)

  def testGetLatestRecipeImageValid(self):
    with patch.object(command_executer.CommandExecuter,
                      'ChrootRunCommandWOutput') as mocked_run:
      with patch.object(buildbot_utils, 'DoesImageExist') as mocked_imageexist:
        IMAGE_DIR = 'lulu-llvm-next-nightly'
        mocked_run.return_value = (0, self.GSUTILS_LS_RECIPE.format(IMAGE_DIR),
                                   '')
        mocked_imageexist.return_value = True
        image = buildbot_utils.GetLatestRecipeImage('', IMAGE_DIR)
        self.assertEqual(
            image, '{0}/R83-13003.0.0-30218-8884712858556419'.format(IMAGE_DIR))

  def testGetLatestRecipeImageInvalid(self):
    with patch.object(command_executer.CommandExecuter,
                      'ChrootRunCommandWOutput') as mocked_run:
      with patch.object(buildbot_utils, 'DoesImageExist') as mocked_imageexist:
        IMAGE_DIR = 'kefka-llvm-next-nightly'
        mocked_run.return_value = (0, self.GSUTILS_LS_RECIPE.format(IMAGE_DIR),
                                   '')
        mocked_imageexist.return_value = False
        image = buildbot_utils.GetLatestRecipeImage('', IMAGE_DIR)
        self.assertIsNone(image)

  def testGetLatestRecipeImageTwodays(self):
    with patch.object(command_executer.CommandExecuter,
                      'ChrootRunCommandWOutput') as mocked_run:
      with patch.object(buildbot_utils, 'DoesImageExist') as mocked_imageexist:
        IMAGE_DIR = 'lulu-llvm-next-nightly'
        mocked_run.return_value = (0, self.GSUTILS_LS_RECIPE.format(IMAGE_DIR),
                                   '')
        mocked_imageexist.side_effect = [False, False, True]
        image = buildbot_utils.GetLatestRecipeImage('', IMAGE_DIR)
        self.assertIsNone(image)
        mocked_imageexist.side_effect = [False, True, True]
        image = buildbot_utils.GetLatestRecipeImage('', IMAGE_DIR)
        self.assertEqual(
            image, '{0}/R83-13003.0.0-30196-8884755532184725'.format(IMAGE_DIR))


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