forked from openSUSE/openSUSE-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReviewBot_tests.py
165 lines (137 loc) · 7.43 KB
/
ReviewBot_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import logging
from . import OBSLocal
from osclib.comments import CommentAPI
from ReviewBot import ReviewBot
import random
COMMENT = 'short comment'
PROJECT = 'openSUSE:Factory:Staging'
class TestReviewBotComment(OBSLocal.TestCase):
def setUp(self):
super(TestReviewBotComment, self).setUp()
self.api = CommentAPI(self.apiurl)
self.wf = OBSLocal.FactoryWorkflow()
self.wf.create_user('factory-auto')
self.project = self.wf.create_project(PROJECT)
# Ensure different test runs operate in unique namespace.
self.bot = '::'.join([type(self).__name__, str(random.getrandbits(8))])
self.review_bot = ReviewBot(self.apiurl, logger=logging.getLogger(self.bot))
self.review_bot.bot_name = self.bot
self.osc_user('factory-auto')
def tearDown(self):
self.api.delete_from(project_name=PROJECT)
self.assertFalse(len(self.api.get_comments(project_name=PROJECT)))
self.osc_user('Admin')
del self.wf
def test_basic_logger(self):
self.assertFalse(self.comments_filtered(self.bot)[0])
# Initial comment.
self.review_bot.comment_handler_add()
self.review_bot.logger.info('something interesting')
self.review_bot.comment_write(project=PROJECT)
comment, _ = self.comments_filtered(self.bot)
self.assertTrue(comment['comment'].endswith('something interesting'))
# Second comment with extra line.
self.review_bot.comment_handler_add()
self.review_bot.logger.info('something interesting')
self.review_bot.logger.info('something extra')
self.review_bot.comment_write(project=PROJECT)
comment, _ = self.comments_filtered(self.bot)
self.assertTrue(comment['comment'].endswith('something extra'))
def test_workflow(self):
comment_count = len(self.api.get_comments(project_name=PROJECT))
self.assertFalse(self.comments_filtered(self.bot)[0])
# Initial comment.
info = {'state': 'seen', 'result': 'failed'}
info_extra = {'build': '1'}
info_merged = info.copy()
info_merged.update(info_extra)
self.review_bot.comment_write(state='seen', result='failed', identical=True,
info_extra=info_extra, info_extra_identical=False,
project=PROJECT, message=COMMENT)
comment, info_parsed = self.comments_filtered(self.bot)
self.assertTrue(comment['comment'].endswith(COMMENT))
self.assertEqual(info_parsed, info_merged)
# Only build change (expect no change).
info_extra = {'build': '2'}
self.review_bot.comment_write(state='seen', result='failed', identical=True,
info_extra=info_extra, info_extra_identical=False,
project=PROJECT, message=COMMENT)
comment, info_parsed = self.comments_filtered(self.bot)
self.assertTrue(comment['comment'].endswith(COMMENT))
self.assertEqual(info_parsed, info_merged)
# Build and comment (except comment replacement).
info_extra = {'build': '3'}
info_merged.update(info_extra)
self.review_bot.comment_write(state='seen', result='failed', identical=True,
info_extra=info_extra, info_extra_identical=False,
project=PROJECT, message=COMMENT + '3')
comment, info_parsed = self.comments_filtered(self.bot)
self.assertTrue(comment['comment'].endswith(COMMENT + '3'))
self.assertEqual(info_parsed, info_merged)
# Final build (except comment replacement).
info_extra = {'build': '4'}
info_merged.update(info_extra)
self.review_bot.comment_write(state='seen', result='failed', identical=True,
info_extra=info_extra, info_extra_identical=True,
project=PROJECT, message=COMMENT + '4')
comment, info_parsed = self.comments_filtered(self.bot)
self.assertTrue(comment['comment'].endswith(COMMENT + '4'))
self.assertEqual(info_parsed, info_merged)
# Final build (except comment replacement).
info = {'state': 'done', 'result': 'passed'}
info_extra = {'build': '5'}
info_merged = info.copy()
info_merged.update(info_extra)
self.review_bot.comment_write(state='done', result='passed', identical=True,
info_extra=info_extra, info_extra_identical=True,
only_replace=True,
project=PROJECT, message=COMMENT + '5')
comment, info_parsed = self.comments_filtered(self.bot)
self.assertTrue(comment['comment'].endswith(COMMENT + '5'))
self.assertEqual(info_parsed, info_merged)
# Should never be more than one new comment.
self.assertEqual(len(self.api.get_comments(project_name=PROJECT)), comment_count + 1)
def test_only_replace_none(self):
self.review_bot.comment_write(only_replace=True,
project=PROJECT, message=COMMENT)
self.assertFalse(self.comments_filtered(self.bot)[0])
def test_dryrun(self):
# dryrun = True, no comment.
self.review_bot.dryrun = True
self.review_bot.comment_write(project=PROJECT, message=COMMENT)
self.assertFalse(self.comments_filtered(self.bot)[0])
# dryrun = False, a comment.
self.review_bot.dryrun = False
self.review_bot.comment_write(project=PROJECT, message=COMMENT)
self.assertTrue(self.comments_filtered(self.bot)[0])
# dryrun = True, no replacement.
self.review_bot.dryrun = True
self.review_bot.comment_write(state='changed', project=PROJECT, message=COMMENT)
_, info = self.comments_filtered(self.bot)
self.assertEqual(info['state'], 'done')
# dryrun = False, replacement.
self.review_bot.dryrun = False
self.review_bot.comment_write(state='changed', project=PROJECT, message=COMMENT)
_, info = self.comments_filtered(self.bot)
self.assertEqual(info['state'], 'changed')
def test_bot_name_suffix(self):
suffix1 = 'suffix1'
bot_suffixed1 = '::'.join([self.bot, suffix1])
suffix2 = 'suffix2'
bot_suffixed2 = '::'.join([self.bot, suffix2])
self.review_bot.comment_write(bot_name_suffix=suffix1, project=PROJECT, message=COMMENT)
self.assertFalse(self.comments_filtered(self.bot)[0])
self.assertTrue(self.comments_filtered(bot_suffixed1)[0])
self.assertFalse(self.comments_filtered(bot_suffixed2)[0])
self.review_bot.comment_write(bot_name_suffix=suffix2, project=PROJECT, message=COMMENT)
self.assertFalse(self.comments_filtered(self.bot)[0])
self.assertTrue(self.comments_filtered(bot_suffixed1)[0])
self.assertTrue(self.comments_filtered(bot_suffixed2)[0])
self.review_bot.comment_write(bot_name_suffix=suffix1, project=PROJECT, message=COMMENT + '\nnew')
comment, _ = self.comments_filtered(bot_suffixed1)
self.assertTrue(comment['comment'].endswith(COMMENT + '\nnew'))
comment, _ = self.comments_filtered(bot_suffixed2)
self.assertTrue(comment['comment'].endswith(COMMENT))
def comments_filtered(self, bot):
comments = self.api.get_comments(project_name=PROJECT)
return self.api.comment_find(comments, bot)