Browse Source

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
tags/0.4.0
John L. Villalovos 1 year ago
parent
commit
01e1a0f979
4 changed files with 184 additions and 13 deletions
  1. 3
    0
      .stestr.conf
  2. 0
    4
      .testr.conf
  3. 10
    8
      gerritbot/bot.py
  4. 171
    1
      gerritbot/tests/unit/test_bot.py

+ 3
- 0
.stestr.conf View File

@@ -0,0 +1,3 @@
1
+[DEFAULT]
2
+test_path=${TESTS_DIR:-./gerritbot/tests/unit/}
3
+top_dir=./

+ 0
- 4
.testr.conf View File

@@ -1,4 +0,0 @@
1
-[DEFAULT]
2
-test_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
3
-test_id_option=--load-list $IDFILE
4
-test_list_option=--list

+ 10
- 8
gerritbot/bot.py View File

@@ -115,19 +115,21 @@ class GerritBot(irc.bot.SingleServerIRCBot):
115 115
         self.password = password
116 116
         self.log = logging.getLogger('gerritbot')
117 117
 
118
-    def on_nicknameinuse(self, c, e):
118
+    def on_nicknameinuse(self, connection, event):
119 119
         self.log.info('Nick previously in use, recovering.')
120
-        c.nick(c.get_nickname() + "_")
121
-        c.privmsg("nickserv", "identify %s " % self.password)
122
-        c.privmsg("nickserv", "ghost %s %s" % (self.nickname, self.password))
123
-        c.privmsg("nickserv", "release %s %s" % (self.nickname, self.password))
120
+        connection.nick(connection.get_nickname() + "_")
121
+        connection.privmsg("nickserv", "identify %s " % self.password)
122
+        connection.privmsg("nickserv", "ghost %s %s" % (self.nickname,
123
+                                                        self.password))
124
+        connection.privmsg("nickserv", "release %s %s" % (self.nickname,
125
+                                                          self.password))
124 126
         time.sleep(1)
125
-        c.nick(self.nickname)
127
+        connection.nick(self.nickname)
126 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 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 133
         self.log.info('Identified with IRC server.')
132 134
         self.joined_channels = {}
133 135
 

+ 171
- 1
gerritbot/tests/unit/test_bot.py View File

@@ -11,6 +11,8 @@
11 11
 #    License for the specific language governing permissions and limitations
12 12
 #    under the License.
13 13
 
14
+import collections
15
+
14 16
 import testtools
15 17
 import yaml
16 18
 
@@ -21,15 +23,183 @@ openstack-dev:
21 23
     events:
22 24
       - patchset-created
23 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
36
+openstack-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 44
     projects:
25 45
       - openstack/nova
26 46
       - openstack/swift
27 47
     branches:
28 48
       - master
49
+      - stable/queens
29 50
 """
30 51
 
31 52
 
32 53
 class ChannelConfigTestCase(testtools.TestCase):
33 54
     def test_missing_octothorpe(self):
34 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
+
96
+Message = collections.namedtuple('Message', ['channel', 'msg'])
97
+
98
+
99
+class 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
+
111
+class 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)

Loading…
Cancel
Save