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 | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 25 | from zuul.driver.gerrit.gerritconnection import GerritConnection |
Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 26 | |
James E. Blair | 2a8e0fa | 2017-01-24 10:18:38 -0800 | [diff] [blame^] | 27 | FIXTURE_DIR = os.path.join(tests.base.FIXTURE_DIR, 'gerrit') |
Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def read_fixture(file): |
| 31 | with open('%s/%s' % (FIXTURE_DIR, file), 'r') as fixturefile: |
| 32 | lines = fixturefile.readlines() |
| 33 | command = lines[0].replace('\n', '') |
| 34 | value = ''.join(lines[1:]) |
| 35 | return command, value |
| 36 | |
| 37 | |
| 38 | def read_fixtures(files): |
| 39 | calls = [] |
| 40 | values = [] |
| 41 | for fixture_file in files: |
| 42 | command, value = read_fixture(fixture_file) |
| 43 | calls.append(mock.call(command)) |
| 44 | values.append([value, '']) |
| 45 | return calls, values |
| 46 | |
| 47 | |
| 48 | class TestGerrit(BaseTestCase): |
| 49 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 50 | @mock.patch('zuul.driver.gerrit.gerritconnection.GerritConnection._ssh') |
Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 51 | def run_query(self, files, expected_patches, _ssh_mock): |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 52 | gerrit_config = { |
| 53 | 'user': 'gerrit', |
| 54 | 'server': 'localhost', |
| 55 | } |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 56 | gerrit = GerritConnection(None, 'review_gerrit', gerrit_config) |
Tobias Henkel | dd78ef2 | 2015-08-06 14:14:22 +0200 | [diff] [blame] | 57 | |
| 58 | calls, values = read_fixtures(files) |
| 59 | _ssh_mock.side_effect = values |
| 60 | |
| 61 | result = gerrit.simpleQuery('project:openstack-infra/zuul') |
| 62 | |
| 63 | _ssh_mock.assert_has_calls(calls) |
| 64 | self.assertEquals(len(calls), _ssh_mock.call_count, |
| 65 | '_ssh should be called %d times' % len(calls)) |
| 66 | self.assertIsNotNone(result, 'Result is not none') |
| 67 | self.assertEquals(len(result), expected_patches, |
| 68 | 'There must be %d patches.' % expected_patches) |
| 69 | |
| 70 | def test_simple_query_pagination_new(self): |
| 71 | files = ['simple_query_pagination_new_1', |
| 72 | 'simple_query_pagination_new_2'] |
| 73 | expected_patches = 5 |
| 74 | self.run_query(files, expected_patches) |
| 75 | |
| 76 | def test_simple_query_pagination_old(self): |
| 77 | files = ['simple_query_pagination_old_1', |
| 78 | 'simple_query_pagination_old_2', |
| 79 | 'simple_query_pagination_old_3'] |
| 80 | expected_patches = 5 |
| 81 | self.run_query(files, expected_patches) |