blob: 5feb94d6a20bd1a1b53ee28b73d17fa56b2b2228 [file] [log] [blame]
Michael Mortensen3e86c1e2019-11-21 15:51:54 -07001# -*- 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
8from __future__ import print_function
9
10import os
11
12from chromite.api import router as router_lib
13from chromite.lib import cros_test_lib
14from chromite.lib import osutils
15from chromite.scripts import build_api
16
17
18class 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')
Alex Kleind815ca62020-01-10 12:21:30 -070025 self.config_json = os.path.join(self.tempdir, 'config.json')
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070026 self.tee_log = os.path.join(self.tempdir, 'tee_out.txt')
Alex Kleind815ca62020-01-10 12:21:30 -070027
28 tee_config = '{"log_path": "%s"}' % self.tee_log
29
30 osutils.WriteFile(self.input_json, '{}')
31 osutils.WriteFile(self.config_json, tee_config)
32
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070033
34 def testSmoke(self):
35 """Basic sanity check"""
36 build_api.main(['--input-json', self.input_json,
37 '--output-json', self.output_json,
Alex Kleind815ca62020-01-10 12:21:30 -070038 'chromite.api.VersionService/Get'])
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070039
40 def testTee(self):
41 """Call build_api with tee-log set, verify log contents."""
42 build_api.main(['--input-json', self.input_json,
43 '--output-json', self.output_json,
Alex Kleind815ca62020-01-10 12:21:30 -070044 '--config-json', self.config_json,
45 'chromite.api.VersionService/Get'])
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070046 contents = osutils.ReadFile(self.tee_log)
47 self.assertIn('Teeing stdout', contents)
Michael Mortensena0515d92020-01-02 11:39:34 -070048
49 def testEnvTee(self):
50 """Call build_api with tee-log set, verify log contents."""
51 os.environ['BUILD_API_TEE_LOG_FILE'] = self.tee_log
52 build_api.main(['--input-json', self.input_json,
53 '--output-json', self.output_json,
Alex Kleind815ca62020-01-10 12:21:30 -070054 'chromite.api.VersionService/Get'])
Michael Mortensena0515d92020-01-02 11:39:34 -070055 contents = osutils.ReadFile(self.tee_log)
56 self.assertIn('Teeing stdout and stderr to env path ', contents)