| # Copyright 2017 Google Inc. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| from __future__ import absolute_import | |
| import argparse | |
| import contextlib | |
| import cStringIO | |
| import datetime | |
| import sys | |
| import unittest | |
| import batch_runner | |
| import mock | |
| from test_util import test_common | |
| REQUIREMENT_PARAMETER = ['--batch-report-folder', '/folder'] | |
| @contextlib.contextmanager | |
| def captured_err_output(): | |
| new_err = cStringIO.StringIO() | |
| old_err = sys.stderr | |
| try: | |
| sys.stderr = new_err | |
| yield sys.stderr | |
| finally: | |
| sys.stderr = old_err | |
| class TestRunner(unittest.TestCase): | |
| def setUp(self): | |
| self.create_folder = mock.patch('utilities.util.CreateEntirePathFolder', | |
| autospec=True).start() | |
| def test_ParseArgs(self): | |
| with self.assertRaises(SystemExit) as context: | |
| with captured_err_output() as err: | |
| batch_runner._ParseArgs() | |
| self.assertEqual(context.exception.code, 2) | |
| self.assertIn('argument --batch-report-folder is required', err.getvalue()) | |
| def test_CsvBatchfileParamsArePresent(self): | |
| with self.assertRaises(test_common.ArgumentError) as context: | |
| batch_runner._ParseArgs(REQUIREMENT_PARAMETER) | |
| self.assertEqual('--batch-file or --workbook has to be specified!', | |
| context.exception.message) | |
| def test_CsvParamHas1Value(self): | |
| with self.assertRaises(test_common.ArgumentError) as context: | |
| batch_runner._ParseArgs(REQUIREMENT_PARAMETER + [ | |
| '--workbook', 'only_test_googleSheet']) | |
| self.assertEqual(''.join([ | |
| '--workbook has to have at least', ' 2 comma separated parameters: ', | |
| '"spreadsheet_key, workbook_id"']), | |
| context.exception.message) | |
| @mock.patch('download_testcases_csv.DownloadCSVFromGoogleSheet', | |
| autospec=True) | |
| @mock.patch('batch_runner.BatchReportPrefix', autospec=True) | |
| def test_CsvParamHas2Values(self, report_prefix_mock, csv_download_mock): | |
| csv_download_mock.return_value = '/tmp/batch.csv' | |
| prefix = '78.0.3904.62__Canary__26__arm_' | |
| report_prefix_mock.return_value = prefix | |
| now = datetime.datetime.now() | |
| args = REQUIREMENT_PARAMETER + ['--workbook', | |
| 'test_googleSheet,workbook_id'] | |
| with mock.patch.object(datetime, 'datetime', | |
| mock.Mock(wraps=datetime.datetime)) as patched: | |
| patched.now.return_value = now | |
| batch_runner._ParseArgs(args) | |
| csv_download_mock.assert_called_once_with( | |
| argparse.Namespace( | |
| batch_file='/tmp/batch.csv', | |
| batch_report_folder='/folder/%s_batch_%s' % ( | |
| prefix, now.isoformat()), | |
| workbook='test_googleSheet,workbook_id', | |
| no_batch_report_upload=False, | |
| device_id=None, | |
| extra_args=None, | |
| channel='Canary', | |
| ), | |
| 'test_googleSheet', | |
| set(['workbook_id']), | |
| folder='/folder/%s_batch_%s' % (prefix, now.isoformat()) | |
| ) | |
| @mock.patch('csv.reader', autoSpec=True) | |
| def test_ParseSuiteParameters(self, mock_csv): | |
| csv_data = [['col1', 'col2', 'col3'], ['1', '2', '3']] | |
| commands = [['col1', '1', 'col2', '2', 'col3', '3']] | |
| with mock.patch('__builtin__.open', mock.mock_open(read_data='')): | |
| mock_csv.return_value = csv_data | |
| self.assertEqual(commands, batch_runner.ParseSuiteParameters('file.csv')) | |
| @mock.patch('csv.reader', autoSpec=True) | |
| def test_ParseSuiteParametersBoolFlags(self, mock_csv): | |
| csv_data = [['col1', 'col2', 'col3'], | |
| ['1', '', '3'], | |
| ['1', '#DISABLED#', '3']] | |
| commands = [['col1', '1', 'col2', 'col3', '3'], | |
| ['col1', '1', 'col3', '3']] | |
| with mock.patch('__builtin__.open', mock.mock_open(read_data='')): | |
| mock_csv.return_value = csv_data | |
| self.assertEqual(commands, batch_runner.ParseSuiteParameters('file.csv')) | |
| if __name__ == '__main__': | |
| unittest.main() |