blob: 2179c4a3aa7422b96712addafe9b10447c9e1bc0 [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
18
Mike Frysinger898265b2020-02-10 23:49:12 -050019assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
20
21
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070022class BuildApiScriptTest(cros_test_lib.MockTempDirTestCase):
23 """Tests for main()"""
24
25 def setUp(self):
26 self.find_mock = self.PatchObject(router_lib.Router, 'Route')
27 self.input_json = os.path.join(self.tempdir, 'input.json')
28 self.output_json = os.path.join(self.tempdir, 'output.json')
Alex Kleind815ca62020-01-10 12:21:30 -070029 self.config_json = os.path.join(self.tempdir, 'config.json')
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070030 self.tee_log = os.path.join(self.tempdir, 'tee_out.txt')
Alex Kleind815ca62020-01-10 12:21:30 -070031
32 tee_config = '{"log_path": "%s"}' % self.tee_log
33
34 osutils.WriteFile(self.input_json, '{}')
35 osutils.WriteFile(self.config_json, tee_config)
36
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070037
38 def testSmoke(self):
39 """Basic sanity check"""
40 build_api.main(['--input-json', self.input_json,
41 '--output-json', self.output_json,
Alex Kleind815ca62020-01-10 12:21:30 -070042 'chromite.api.VersionService/Get'])
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070043
44 def testTee(self):
45 """Call build_api with tee-log set, verify log contents."""
46 build_api.main(['--input-json', self.input_json,
47 '--output-json', self.output_json,
Alex Kleind815ca62020-01-10 12:21:30 -070048 '--config-json', self.config_json,
49 'chromite.api.VersionService/Get'])
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070050 contents = osutils.ReadFile(self.tee_log)
51 self.assertIn('Teeing stdout', contents)
Michael Mortensena0515d92020-01-02 11:39:34 -070052
53 def testEnvTee(self):
54 """Call build_api with tee-log set, verify log contents."""
55 os.environ['BUILD_API_TEE_LOG_FILE'] = self.tee_log
56 build_api.main(['--input-json', self.input_json,
57 '--output-json', self.output_json,
Alex Kleind815ca62020-01-10 12:21:30 -070058 'chromite.api.VersionService/Get'])
Michael Mortensena0515d92020-01-02 11:39:34 -070059 contents = osutils.ReadFile(self.tee_log)
60 self.assertIn('Teeing stdout and stderr to env path ', contents)