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