blob: 999e55d53fcb1701231099c0bcae2a4c244e7cb6 [file] [log] [blame]
Tobias Henkeldd78ef22015-08-06 14:14:22 +02001#!/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.
16import os
17
18try:
19 from unittest import mock
20except ImportError:
21 import mock
22
James E. Blair2a8e0fa2017-01-24 10:18:38 -080023import tests.base
Tobias Henkeldd78ef22015-08-06 14:14:22 +020024from tests.base import BaseTestCase
James E. Blaire511d2f2016-12-08 15:22:26 -080025from zuul.driver.gerrit.gerritconnection import GerritConnection
Tobias Henkeldd78ef22015-08-06 14:14:22 +020026
James E. Blair2a8e0fa2017-01-24 10:18:38 -080027FIXTURE_DIR = os.path.join(tests.base.FIXTURE_DIR, 'gerrit')
Tobias Henkeldd78ef22015-08-06 14:14:22 +020028
29
30def 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
38def 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
48class TestGerrit(BaseTestCase):
49
James E. Blaire511d2f2016-12-08 15:22:26 -080050 @mock.patch('zuul.driver.gerrit.gerritconnection.GerritConnection._ssh')
Tobias Henkeldd78ef22015-08-06 14:14:22 +020051 def run_query(self, files, expected_patches, _ssh_mock):
Joshua Hesketh352264b2015-08-11 23:42:08 +100052 gerrit_config = {
53 'user': 'gerrit',
54 'server': 'localhost',
55 }
James E. Blaire511d2f2016-12-08 15:22:26 -080056 gerrit = GerritConnection(None, 'review_gerrit', gerrit_config)
Tobias Henkeldd78ef22015-08-06 14:14:22 +020057
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)