blob: f67e5638bc5018ac8149bf9ccc55d5a9e1372e78 [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
Mike Frysinger898265b2020-02-10 23:49:12 -050011import sys
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070012
13from chromite.api import router as router_lib
14from chromite.lib import cros_test_lib
15from chromite.lib import osutils
16from chromite.scripts import build_api
17
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070018pytestmark = cros_test_lib.pytestmark_sigterm
19
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070020
Mike Frysinger898265b2020-02-10 23:49:12 -050021assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
22
23
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070024class BuildApiScriptTest(cros_test_lib.MockTempDirTestCase):
25 """Tests for main()"""
26
27 def setUp(self):
28 self.find_mock = self.PatchObject(router_lib.Router, 'Route')
29 self.input_json = os.path.join(self.tempdir, 'input.json')
30 self.output_json = os.path.join(self.tempdir, 'output.json')
Alex Kleind815ca62020-01-10 12:21:30 -070031 self.config_json = os.path.join(self.tempdir, 'config.json')
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070032 self.tee_log = os.path.join(self.tempdir, 'tee_out.txt')
Alex Kleind815ca62020-01-10 12:21:30 -070033
34 tee_config = '{"log_path": "%s"}' % self.tee_log
35
36 osutils.WriteFile(self.input_json, '{}')
37 osutils.WriteFile(self.config_json, tee_config)
38
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070039
40 def testSmoke(self):
41 """Basic sanity check"""
42 build_api.main(['--input-json', self.input_json,
43 '--output-json', self.output_json,
Alex Kleind815ca62020-01-10 12:21:30 -070044 'chromite.api.VersionService/Get'])
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070045
46 def testTee(self):
47 """Call build_api with tee-log set, verify log contents."""
48 build_api.main(['--input-json', self.input_json,
49 '--output-json', self.output_json,
Alex Kleind815ca62020-01-10 12:21:30 -070050 '--config-json', self.config_json,
51 'chromite.api.VersionService/Get'])
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070052 contents = osutils.ReadFile(self.tee_log)
53 self.assertIn('Teeing stdout', contents)
Michael Mortensena0515d92020-01-02 11:39:34 -070054
55 def testEnvTee(self):
56 """Call build_api with tee-log set, verify log contents."""
57 os.environ['BUILD_API_TEE_LOG_FILE'] = self.tee_log
58 build_api.main(['--input-json', self.input_json,
59 '--output-json', self.output_json,
Alex Kleind815ca62020-01-10 12:21:30 -070060 'chromite.api.VersionService/Get'])
Michael Mortensena0515d92020-01-02 11:39:34 -070061 contents = osutils.ReadFile(self.tee_log)
62 self.assertIn('Teeing stdout and stderr to env path ', contents)