1#!/usr/bin/env python2 2 3# Copyright 2018 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6"""Unittest for buildbot_utils.py.""" 7 8from __future__ import print_function 9 10from mock import patch 11 12import time 13import unittest 14 15from cros_utils import buildbot_utils 16from cros_utils import command_executer 17 18 19class TrybotTest(unittest.TestCase): 20 """Test for CommandExecuter class.""" 21 22 old_tryjob_out = ( 23 'Verifying patches...\n' 24 'Submitting tryjob...\n' 25 'Successfully sent PUT request to [buildbucket_bucket:master.chromiumos.t' 26 'ryserver] with [config:success-build] [buildbucket_id:895272114382368817' 27 '6].\n' 28 'Tryjob submitted!\n' 29 'To view your tryjobs, visit:\n' 30 ' http://cros-goldeneye/chromeos/healthmonitoring/buildDetails?buildbuck' 31 'etId=8952721143823688176\n' 32 ' https://uberchromegw.corp.google.com/i/chromiumos.tryserver/waterfall?' 33 'committer=laszio@chromium.org&builder=etc\n') 34 tryjob_out = ( 35 '[{"buildbucket_id": "8952721143823688176", "build_config": ' 36 '"cave-llvm-toolchain-tryjob", "url": ' 37 '"http://cros-goldeneye/chromeos/healthmonitoring/buildDetails?buildbucketId=8952721143823688176"}]' 38 ) 39 40 buildresult_out = ( 41 '{"8952721143823688176": {"status": "pass", "artifacts_url":' 42 '"gs://chromeos-image-archive/trybot-elm-release-tryjob/R67-10468.0.0-' 43 'b20789"}}') 44 45 buildbucket_id = '8952721143823688176' 46 counter_1 = 10 47 48 def testGetTrybotImage(self): 49 with patch.object(buildbot_utils, 'SubmitTryjob') as mock_submit: 50 with patch.object(buildbot_utils, 'PeekTrybotImage') as mock_peek: 51 with patch.object(time, 'sleep', return_value=None): 52 53 def peek(_chromeos_root, _buildbucket_id): 54 self.counter_1 -= 1 55 if self.counter_1 >= 0: 56 return ('running', '') 57 return ('pass', 58 'gs://chromeos-image-archive/trybot-elm-release-tryjob/' 59 'R67-10468.0.0-b20789') 60 61 mock_peek.side_effect = peek 62 mock_submit.return_value = self.buildbucket_id 63 64 # sync 65 buildbucket_id, image = buildbot_utils.GetTrybotImage( 66 '/tmp', 'falco-release-tryjob', []) 67 self.assertEqual(buildbucket_id, self.buildbucket_id) 68 self.assertEqual('trybot-elm-release-tryjob/' 69 'R67-10468.0.0-b20789', image) 70 71 # async 72 buildbucket_id, image = buildbot_utils.GetTrybotImage( 73 '/tmp', 'falco-release-tryjob', [], async=True) 74 self.assertEqual(buildbucket_id, self.buildbucket_id) 75 self.assertEqual(' ', image) 76 77 def testSubmitTryjob(self): 78 with patch.object(command_executer.CommandExecuter, 79 'RunCommandWOutput') as mocked_run: 80 mocked_run.return_value = (0, self.tryjob_out, '') 81 buildbucket_id = buildbot_utils.SubmitTryjob('/', 'falco-release-tryjob', 82 [], []) 83 self.assertEqual(buildbucket_id, self.buildbucket_id) 84 85 def testPeekTrybotImage(self): 86 with patch.object(command_executer.CommandExecuter, 87 'RunCommandWOutput') as mocked_run: 88 # pass 89 mocked_run.return_value = (0, self.buildresult_out, '') 90 status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id) 91 self.assertEqual('pass', status) 92 self.assertEqual( 93 'gs://chromeos-image-archive/trybot-elm-release-tryjob/' 94 'R67-10468.0.0-b20789', image) 95 96 # running 97 mocked_run.return_value = (1, '', '') 98 status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id) 99 self.assertEqual('running', status) 100 self.assertEqual(None, image) 101 102 # fail 103 buildresult_fail = self.buildresult_out.replace('\"pass\"', '\"fail\"') 104 mocked_run.return_value = (0, buildresult_fail, '') 105 status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id) 106 self.assertEqual('fail', status) 107 self.assertEqual( 108 'gs://chromeos-image-archive/trybot-elm-release-tryjob/' 109 'R67-10468.0.0-b20789', image) 110 111 def testParseTryjobBuildbucketId(self): 112 buildbucket_id = buildbot_utils.ParseTryjobBuildbucketId(self.tryjob_out) 113 self.assertEqual(buildbucket_id, self.buildbucket_id) 114 115 116if __name__ == '__main__': 117 unittest.main() 118