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 |
| 24 | from zuul.lib.gerrit import Gerrit |
| 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 | |
| 49 | @mock.patch('zuul.lib.gerrit.Gerrit._ssh') |
| 50 | def run_query(self, files, expected_patches, _ssh_mock): |
| 51 | gerrit = Gerrit('localhost', 'user') |
| 52 | |
| 53 | calls, values = read_fixtures(files) |
| 54 | _ssh_mock.side_effect = values |
| 55 | |
| 56 | result = gerrit.simpleQuery('project:openstack-infra/zuul') |
| 57 | |
| 58 | _ssh_mock.assert_has_calls(calls) |
| 59 | self.assertEquals(len(calls), _ssh_mock.call_count, |
| 60 | '_ssh should be called %d times' % len(calls)) |
| 61 | self.assertIsNotNone(result, 'Result is not none') |
| 62 | self.assertEquals(len(result), expected_patches, |
| 63 | 'There must be %d patches.' % expected_patches) |
| 64 | |
| 65 | def test_simple_query_pagination_new(self): |
| 66 | files = ['simple_query_pagination_new_1', |
| 67 | 'simple_query_pagination_new_2'] |
| 68 | expected_patches = 5 |
| 69 | self.run_query(files, expected_patches) |
| 70 | |
| 71 | def test_simple_query_pagination_old(self): |
| 72 | files = ['simple_query_pagination_old_1', |
| 73 | 'simple_query_pagination_old_2', |
| 74 | 'simple_query_pagination_old_3'] |
| 75 | expected_patches = 5 |
| 76 | self.run_query(files, expected_patches) |