Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unittests for build_api.py""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
| 11 | |
| 12 | from chromite.api import router as router_lib |
| 13 | from chromite.lib import cros_test_lib |
| 14 | from chromite.lib import osutils |
| 15 | from chromite.scripts import build_api |
| 16 | |
| 17 | |
| 18 | class BuildApiScriptTest(cros_test_lib.MockTempDirTestCase): |
| 19 | """Tests for main()""" |
| 20 | |
| 21 | def setUp(self): |
| 22 | self.find_mock = self.PatchObject(router_lib.Router, 'Route') |
| 23 | self.input_json = os.path.join(self.tempdir, 'input.json') |
| 24 | self.output_json = os.path.join(self.tempdir, 'output.json') |
| 25 | self.tee_log = os.path.join(self.tempdir, 'tee_out.txt') |
| 26 | input_json_contents = """ |
| 27 | { |
| 28 | "buildTarget": { |
| 29 | "name": "amd64-generic" |
| 30 | } |
| 31 | } |
| 32 | """ |
| 33 | osutils.WriteFile(self.input_json, input_json_contents) |
| 34 | |
| 35 | def testSmoke(self): |
| 36 | """Basic sanity check""" |
| 37 | build_api.main(['--input-json', self.input_json, |
| 38 | '--output-json', self.output_json, |
| 39 | 'chromite.api.PackageService/GetTargetVersions']) |
| 40 | |
| 41 | def testTee(self): |
| 42 | """Call build_api with tee-log set, verify log contents.""" |
| 43 | build_api.main(['--input-json', self.input_json, |
| 44 | '--output-json', self.output_json, |
| 45 | '--tee-log', self.tee_log, |
| 46 | 'chromite.api.PackageService/GetTargetVersions']) |
| 47 | contents = osutils.ReadFile(self.tee_log) |
| 48 | self.assertIn('Teeing stdout', contents) |
Michael Mortensen | a0515d9 | 2020-01-02 11:39:34 -0700 | [diff] [blame^] | 49 | |
| 50 | def testEnvTee(self): |
| 51 | """Call build_api with tee-log set, verify log contents.""" |
| 52 | os.environ['BUILD_API_TEE_LOG_FILE'] = self.tee_log |
| 53 | build_api.main(['--input-json', self.input_json, |
| 54 | '--output-json', self.output_json, |
| 55 | 'chromite.api.PackageService/GetTargetVersions']) |
| 56 | contents = osutils.ReadFile(self.tee_log) |
| 57 | self.assertIn('Teeing stdout and stderr to env path ', contents) |