summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn L. Villalovos <openstack.org@sodarock.com>2018-02-15 13:21:54 -0800
committerJohn L. Villalovos <openstack.org@sodarock.com>2018-02-17 11:28:38 -0800
commit01e1a0f979095debfbb6a5576646c428cc923cb7 (patch)
tree1c992d0615dd0a611e5944aafe30c05cb7267695
parent4658934d2239c32103c0e04cb32e2af4d44c16f4 (diff)
Add more unit tests including testing of Gerrit() class
* Add some additional unit tests, including the initial framework to be able to test the Gerrit() class. * Improve the readability of the code by using more descriptive variable names. * Migrate to using '.stestr.conf' instead of '.testr.conf'. This removes warning message that was being displayed. Change-Id: I596c4ccb9fe37d996de26089eefd8481a12ff13b
Notes
Notes (review): Code-Review+1: Andreas Jaeger <jaegerandi@gmail.com> Code-Review+2: Paul Belanger <pabelanger@redhat.com> Code-Review+2: Clark Boylan <cboylan@sapwetik.org> Workflow+1: Clark Boylan <cboylan@sapwetik.org> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Mon, 19 Feb 2018 18:31:14 +0000 Reviewed-on: https://review.openstack.org/545128 Project: openstack-infra/gerritbot Branch: refs/heads/master
-rw-r--r--.stestr.conf3
-rw-r--r--.testr.conf4
-rwxr-xr-xgerritbot/bot.py18
-rw-r--r--gerritbot/tests/unit/test_bot.py172
4 files changed, 184 insertions, 13 deletions
diff --git a/.stestr.conf b/.stestr.conf
new file mode 100644
index 0000000..d965d7a
--- /dev/null
+++ b/.stestr.conf
@@ -0,0 +1,3 @@
1[DEFAULT]
2test_path=${TESTS_DIR:-./gerritbot/tests/unit/}
3top_dir=./
diff --git a/.testr.conf b/.testr.conf
deleted file mode 100644
index 9cc40eb..0000000
--- a/.testr.conf
+++ /dev/null
@@ -1,4 +0,0 @@
1[DEFAULT]
2test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} OS_TEST_TIMEOUT=60 ${PYTHON:-python} -m subunit.run discover -t ./ ${TESTS_DIR:-./gerritbot/tests/unit/} $LISTOPT $IDOPTION
3test_id_option=--load-list $IDFILE
4test_list_option=--list
diff --git a/gerritbot/bot.py b/gerritbot/bot.py
index 6db4462..db3572c 100755
--- a/gerritbot/bot.py
+++ b/gerritbot/bot.py
@@ -115,19 +115,21 @@ class GerritBot(irc.bot.SingleServerIRCBot):
115 self.password = password 115 self.password = password
116 self.log = logging.getLogger('gerritbot') 116 self.log = logging.getLogger('gerritbot')
117 117
118 def on_nicknameinuse(self, c, e): 118 def on_nicknameinuse(self, connection, event):
119 self.log.info('Nick previously in use, recovering.') 119 self.log.info('Nick previously in use, recovering.')
120 c.nick(c.get_nickname() + "_") 120 connection.nick(connection.get_nickname() + "_")
121 c.privmsg("nickserv", "identify %s " % self.password) 121 connection.privmsg("nickserv", "identify %s " % self.password)
122 c.privmsg("nickserv", "ghost %s %s" % (self.nickname, self.password)) 122 connection.privmsg("nickserv", "ghost %s %s" % (self.nickname,
123 c.privmsg("nickserv", "release %s %s" % (self.nickname, self.password)) 123 self.password))
124 connection.privmsg("nickserv", "release %s %s" % (self.nickname,
125 self.password))
124 time.sleep(1) 126 time.sleep(1)
125 c.nick(self.nickname) 127 connection.nick(self.nickname)
126 self.log.info('Nick previously in use, recovered.') 128 self.log.info('Nick previously in use, recovered.')
127 129
128 def on_welcome(self, c, e): 130 def on_welcome(self, connection, event):
129 self.log.info('Identifying with IRC server.') 131 self.log.info('Identifying with IRC server.')
130 c.privmsg("nickserv", "identify %s " % self.password) 132 connection.privmsg("nickserv", "identify %s " % self.password)
131 self.log.info('Identified with IRC server.') 133 self.log.info('Identified with IRC server.')
132 self.joined_channels = {} 134 self.joined_channels = {}
133 135
diff --git a/gerritbot/tests/unit/test_bot.py b/gerritbot/tests/unit/test_bot.py
index 3426f8e..c2b67ad 100644
--- a/gerritbot/tests/unit/test_bot.py
+++ b/gerritbot/tests/unit/test_bot.py
@@ -11,6 +11,8 @@
11# License for the specific language governing permissions and limitations 11# License for the specific language governing permissions and limitations
12# under the License. 12# under the License.
13 13
14import collections
15
14import testtools 16import testtools
15import yaml 17import yaml
16 18
@@ -21,15 +23,183 @@ openstack-dev:
21 events: 23 events:
22 - patchset-created 24 - patchset-created
23 - change-merged 25 - change-merged
26 - x-vrif-minus-2
27 - x-vrif-plus-2
28 - x-crvw-minus-2
29 - x-crvw-plus-2
30 projects:
31 - openstack/nova
32 - openstack/swift
33 branches:
34 - master
35 - stable/queens
36openstack-infra:
37 events:
38 - patchset-created
39 - change-merged
40 - x-vrif-minus-2
41 - x-vrif-plus-2
42 - x-crvw-minus-2
43 - x-crvw-plus-2
24 projects: 44 projects:
25 - openstack/nova 45 - openstack/nova
26 - openstack/swift 46 - openstack/swift
27 branches: 47 branches:
28 - master 48 - master
49 - stable/queens
29""" 50"""
30 51
31 52
32class ChannelConfigTestCase(testtools.TestCase): 53class ChannelConfigTestCase(testtools.TestCase):
33 def test_missing_octothorpe(self): 54 def test_missing_octothorpe(self):
34 channel_config = bot.ChannelConfig(yaml.load(CHANNEL_CONFIG_YAML)) 55 channel_config = bot.ChannelConfig(yaml.load(CHANNEL_CONFIG_YAML))
35 self.assertEqual(['#openstack-dev'], channel_config.channels) 56 # TODO(jlvillal): Python 2 only assert. Must change to use
57 # six.assertCountEqual() for Python 2/3 compatibility
58 self.assertItemsEqual(['#openstack-dev', '#openstack-infra'],
59 channel_config.channels)
60
61 def test_branches(self):
62 channel_config = bot.ChannelConfig(yaml.load(CHANNEL_CONFIG_YAML))
63 expected_channels = {'#openstack-dev', '#openstack-infra'}
64 self.assertEqual(
65 {
66 'master': expected_channels,
67 'stable/queens': expected_channels,
68 },
69 channel_config.branches)
70
71 def test_events(self):
72 channel_config = bot.ChannelConfig(yaml.load(CHANNEL_CONFIG_YAML))
73 expected_channels = {'#openstack-dev', '#openstack-infra'}
74 self.assertEqual(
75 {
76 'change-merged': expected_channels,
77 'patchset-created': expected_channels,
78 'x-crvw-minus-2': expected_channels,
79 'x-crvw-plus-2': expected_channels,
80 'x-vrif-minus-2': expected_channels,
81 'x-vrif-plus-2': expected_channels,
82 },
83 channel_config.events)
84
85 def test_projects(self):
86 channel_config = bot.ChannelConfig(yaml.load(CHANNEL_CONFIG_YAML))
87 expected_channels = {'#openstack-dev', '#openstack-infra'}
88 self.assertEqual(
89 {
90 'openstack/nova': expected_channels,
91 'openstack/swift': expected_channels,
92 },
93 channel_config.projects)
94
95
96Message = collections.namedtuple('Message', ['channel', 'msg'])
97
98
99class IrcBotHelper(object):
100 """Dummy class to use for testing the Gerrit and GerritMQTT classes
101
102 For testing the Gerrit and GerritMQTT classes we need a dummy IrcBot.
103 """
104 def __init__(self):
105 self.messages = []
106
107 def send(self, channel, msg):
108 self.messages.append(Message(channel=channel, msg=msg))
109
110
111class GerritTestCase(testtools.TestCase):
112
113 def setUp(self):
114 super(GerritTestCase, self).setUp()
115 self.ircbot = IrcBotHelper()
116 self.channel_config = bot.ChannelConfig(yaml.load(CHANNEL_CONFIG_YAML))
117 self.channel = "#openstack-infra"
118 self.gerrit = bot.Gerrit(ircbot=self.ircbot,
119 channel_config=self.channel_config,
120 server='localhost',
121 username='username',
122 port=29418)
123
124 self.sample_data = {
125 'change': {
126 'branch': 'master',
127 'project': 'openstack/gerritbot',
128 'subject': 'More unit tests',
129 'url': 'https://review.openstack.org/123456',
130 },
131 'patchSet': {
132 'uploader': {
133 'name': 'John L. Villalovos',
134 },
135 },
136 'refUpdate': {
137 'project': 'openstack/gerritbot',
138 'refName': 'refs/tags/pike',
139 },
140 'submitter': {
141 'username': 'elmo',
142 },
143
144 }
145
146 def test_patchset_created(self):
147 self.gerrit.patchset_created(self.channel, self.sample_data)
148
149 self.assertEqual(1, len(self.ircbot.messages))
150 message = self.ircbot.messages[0]
151 self.assertEqual(self.channel, message.channel)
152 self.assertEqual(
153 'John L. Villalovos proposed openstack/gerritbot master: More '
154 'unit tests https://review.openstack.org/123456',
155 message.msg)
156
157 def test_ref_updated(self):
158 self.gerrit.ref_updated(self.channel, self.sample_data)
159
160 self.assertEqual(1, len(self.ircbot.messages))
161 message = self.ircbot.messages[0]
162 self.assertEqual(self.channel, message.channel)
163 self.assertEqual('elmo tagged project openstack/gerritbot with pike',
164 message.msg)
165
166 def test_change_merged(self):
167 self.gerrit.change_merged(self.channel, self.sample_data)
168
169 self.assertEqual(1, len(self.ircbot.messages))
170 message = self.ircbot.messages[0]
171 self.assertEqual(self.channel, message.channel)
172 self.assertEqual(
173 'Merged openstack/gerritbot master: More unit tests '
174 'https://review.openstack.org/123456',
175 message.msg)
176
177 def test_comment_added(self):
178 self.gerrit.comment_added(self.channel, self.sample_data)
179
180 self.assertEqual(1, len(self.ircbot.messages))
181 message = self.ircbot.messages[0]
182 self.assertEqual(self.channel, message.channel)
183 self.assertEqual(
184 'A comment has been added to a proposed change to '
185 'openstack/gerritbot: More unit tests '
186 'https://review.openstack.org/123456',
187 message.msg)
188
189 def test_comment_added_vrif(self):
190 self.sample_data['approvals'] = [{
191 'type': 'VRIF',
192 'value': '-2',
193 }]
194 self.gerrit.comment_added(self.channel, self.sample_data)
195
196 self.assertEqual(2, len(self.ircbot.messages))
197
198 # The test function 'test_comment_added()' verifies that index 0 is
199 # correct, so we will only check index 1
200 message = self.ircbot.messages[1]
201 self.assertEqual(self.channel, message.channel)
202 self.assertEqual(
203 'Verification of a change to openstack/gerritbot failed: '
204 'More unit tests https://review.openstack.org/123456',
205 message.msg)