blob: 5ce1aa38e6aab568f7df09e2618c01e0f429f90a [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. Blair0a899752017-03-29 13:22:16 -070025from zuul.driver.gerrit import GerritDriver
James E. Blaire511d2f2016-12-08 15:22:26 -080026from zuul.driver.gerrit.gerritconnection import GerritConnection
Tobias Henkeldd78ef22015-08-06 14:14:22 +020027
James E. Blair2a8e0fa2017-01-24 10:18:38 -080028FIXTURE_DIR = os.path.join(tests.base.FIXTURE_DIR, 'gerrit')
Tobias Henkeldd78ef22015-08-06 14:14:22 +020029
30
31def read_fixture(file):
32 with open('%s/%s' % (FIXTURE_DIR, file), 'r') as fixturefile:
33 lines = fixturefile.readlines()
34 command = lines[0].replace('\n', '')
35 value = ''.join(lines[1:])
36 return command, value
37
38
39def read_fixtures(files):
40 calls = []
41 values = []
42 for fixture_file in files:
43 command, value = read_fixture(fixture_file)
44 calls.append(mock.call(command))
45 values.append([value, ''])
46 return calls, values
47
48
49class TestGerrit(BaseTestCase):
50
James E. Blaire511d2f2016-12-08 15:22:26 -080051 @mock.patch('zuul.driver.gerrit.gerritconnection.GerritConnection._ssh')
Tobias Henkeldd78ef22015-08-06 14:14:22 +020052 def run_query(self, files, expected_patches, _ssh_mock):
Joshua Hesketh352264b2015-08-11 23:42:08 +100053 gerrit_config = {
54 'user': 'gerrit',
55 'server': 'localhost',
56 }
James E. Blair0a899752017-03-29 13:22:16 -070057 driver = GerritDriver()
58 gerrit = GerritConnection(driver, 'review_gerrit', gerrit_config)
Tobias Henkeldd78ef22015-08-06 14:14:22 +020059
60 calls, values = read_fixtures(files)
61 _ssh_mock.side_effect = values
62
63 result = gerrit.simpleQuery('project:openstack-infra/zuul')
64
65 _ssh_mock.assert_has_calls(calls)
zhangyangyangc3e786f2017-09-13 10:47:52 +080066 self.assertEqual(len(calls), _ssh_mock.call_count,
67 '_ssh should be called %d times' % len(calls))
Tobias Henkeldd78ef22015-08-06 14:14:22 +020068 self.assertIsNotNone(result, 'Result is not none')
zhangyangyangc3e786f2017-09-13 10:47:52 +080069 self.assertEqual(len(result), expected_patches,
70 'There must be %d patches.' % expected_patches)
Tobias Henkeldd78ef22015-08-06 14:14:22 +020071
72 def test_simple_query_pagination_new(self):
73 files = ['simple_query_pagination_new_1',
74 'simple_query_pagination_new_2']
75 expected_patches = 5
76 self.run_query(files, expected_patches)
77
78 def test_simple_query_pagination_old(self):
79 files = ['simple_query_pagination_old_1',
80 'simple_query_pagination_old_2',
81 'simple_query_pagination_old_3']
82 expected_patches = 5
83 self.run_query(files, expected_patches)