blob: 93ce122b31bcfded2513e9391ffa5e508e56e33f [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
23from tests.base import BaseTestCase
Joshua Hesketh352264b2015-08-11 23:42:08 +100024from zuul.connection.gerrit import GerritConnection
Tobias Henkeldd78ef22015-08-06 14:14:22 +020025
26FIXTURE_DIR = os.path.join(os.path.dirname(__file__), 'fixtures/gerrit')
27
28
29def 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
37def 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
47class TestGerrit(BaseTestCase):
48
Joshua Hesketh352264b2015-08-11 23:42:08 +100049 @mock.patch('zuul.connection.gerrit.GerritConnection._ssh')
Tobias Henkeldd78ef22015-08-06 14:14:22 +020050 def run_query(self, files, expected_patches, _ssh_mock):
Joshua Hesketh352264b2015-08-11 23:42:08 +100051 gerrit_config = {
52 'user': 'gerrit',
53 'server': 'localhost',
54 }
55 gerrit = GerritConnection('review_gerrit', gerrit_config)
Tobias Henkeldd78ef22015-08-06 14:14:22 +020056
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)