Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2015 BMW Car IT GmbH |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | import os |
| 17 | |
| 18 | try: |
| 19 | from unittest import mock |
| 20 | except ImportError: |
| 21 | import mock |
| 22 | |
James E. Blair | 2a8e0fa | 2017-01-24 10:18:38 -0800 | [diff] [blame] | 23 | import tests.base |
Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 24 | from tests.base import BaseTestCase |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 25 | from zuul.driver.gerrit import GerritDriver |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 26 | from zuul.driver.gerrit.gerritconnection import GerritConnection |
Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 27 | |
James E. Blair | 2a8e0fa | 2017-01-24 10:18:38 -0800 | [diff] [blame] | 28 | FIXTURE_DIR = os.path.join(tests.base.FIXTURE_DIR, 'gerrit') |
Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def read_fixture(file): |
| 32 | with open('%s/%s' % (FIXTURE_DIR, file), 'r') as fixturefile: |
| 33 | lines = fixturefile.readlines() |
| 34 | command = lines[0].replace('\n', '') |
| 35 | value = ''.join(lines[1:]) |
| 36 | return command, value |
| 37 | |
| 38 | |
| 39 | def read_fixtures(files): |
| 40 | calls = [] |
| 41 | values = [] |
| 42 | for fixture_file in files: |
| 43 | command, value = read_fixture(fixture_file) |
| 44 | calls.append(mock.call(command)) |
| 45 | values.append([value, '']) |
| 46 | return calls, values |
| 47 | |
| 48 | |
| 49 | class TestGerrit(BaseTestCase): |
| 50 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 51 | @mock.patch('zuul.driver.gerrit.gerritconnection.GerritConnection._ssh') |
Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 52 | def run_query(self, files, expected_patches, _ssh_mock): |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 53 | gerrit_config = { |
| 54 | 'user': 'gerrit', |
| 55 | 'server': 'localhost', |
| 56 | } |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 57 | driver = GerritDriver() |
| 58 | gerrit = GerritConnection(driver, 'review_gerrit', gerrit_config) |
Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 59 | |
| 60 | calls, values = read_fixtures(files) |
| 61 | _ssh_mock.side_effect = values |
| 62 | |
| 63 | result = gerrit.simpleQuery('project:openstack-infra/zuul') |
| 64 | |
| 65 | _ssh_mock.assert_has_calls(calls) |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 66 | self.assertEqual(len(calls), _ssh_mock.call_count, |
| 67 | '_ssh should be called %d times' % len(calls)) |
Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 68 | self.assertIsNotNone(result, 'Result is not none') |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 69 | self.assertEqual(len(result), expected_patches, |
| 70 | 'There must be %d patches.' % expected_patches) |
Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 71 | |
| 72 | def test_simple_query_pagination_new(self): |
| 73 | files = ['simple_query_pagination_new_1', |
| 74 | 'simple_query_pagination_new_2'] |
| 75 | expected_patches = 5 |
| 76 | self.run_query(files, expected_patches) |
| 77 | |
| 78 | def test_simple_query_pagination_old(self): |
| 79 | files = ['simple_query_pagination_old_1', |
| 80 | 'simple_query_pagination_old_2', |
| 81 | 'simple_query_pagination_old_3'] |
| 82 | expected_patches = 5 |
| 83 | self.run_query(files, expected_patches) |