forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_sink_util_test.py
193 lines (167 loc) · 5.96 KB
/
result_sink_util_test.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import base64
import json
import mock
import requests
import unittest
import result_sink_util
SINK_ADDRESS = 'sink/address'
SINK_POST_URL = 'http://%s/prpc/luci.resultsink.v1.Sink/ReportTestResults' % SINK_ADDRESS
AUTH_TOKEN = 'some_sink_token'
LUCI_CONTEXT_FILE_DATA = """
{
"result_sink": {
"address": "%s",
"auth_token": "%s"
}
}
""" % (SINK_ADDRESS, AUTH_TOKEN)
HEADERS = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'ResultSink %s' % AUTH_TOKEN
}
class UnitTest(unittest.TestCase):
def test_compose_test_result(self):
"""Tests compose_test_result function."""
# Test a test result without log_path.
test_result = result_sink_util._compose_test_result(
'TestCase/testSomething', 'PASS', True)
expected = {
'testId': 'TestCase/testSomething',
'status': 'PASS',
'expected': True,
'tags': [],
}
self.assertEqual(test_result, expected)
short_log = 'Some logs.'
# Tests a test result with log_path.
test_result = result_sink_util._compose_test_result(
'TestCase/testSomething', 'PASS', True, short_log)
expected = {
'testId': 'TestCase/testSomething',
'status': 'PASS',
'expected': True,
'summaryHtml': '<text-artifact artifact-id="Test Log" />',
'artifacts': {
'Test Log': {
'contents': base64.b64encode(short_log)
},
},
'tags': [],
}
self.assertEqual(test_result, expected)
def test_long_test_log(self):
"""Tests long test log is reported as expected."""
len_32_str = 'This is a string in length of 32'
self.assertEqual(len(len_32_str), 32)
len_4128_str = (4 * 32 + 1) * len_32_str
self.assertEqual(len(len_4128_str), 4128)
expected = {
'testId': 'TestCase/testSomething',
'status': 'PASS',
'expected': True,
'summaryHtml': '<text-artifact artifact-id="Test Log" />',
'artifacts': {
'Test Log': {
'contents': base64.b64encode(len_4128_str)
},
},
'tags': [],
}
test_result = result_sink_util._compose_test_result(
'TestCase/testSomething', 'PASS', True, len_4128_str)
self.assertEqual(test_result, expected)
def test_compose_test_result_assertions(self):
"""Tests invalid status is rejected"""
with self.assertRaises(AssertionError):
test_result = result_sink_util._compose_test_result(
'TestCase/testSomething', 'SOME_INVALID_STATUS', True)
with self.assertRaises(AssertionError):
test_result = result_sink_util._compose_test_result(
'TestCase/testSomething', 'PASS', True, tags=('a', 'b'))
with self.assertRaises(AssertionError):
test_result = result_sink_util._compose_test_result(
'TestCase/testSomething',
'PASS',
True,
tags=[('a', 'b', 'c'), ('d', 'e')])
with self.assertRaises(AssertionError):
test_result = result_sink_util._compose_test_result(
'TestCase/testSomething', 'PASS', True, tags=[('a', 'b'), ('c', 3)])
def test_composed_with_tags(self):
"""Tests tags is in correct format."""
expected = {
'testId': 'TestCase/testSomething',
'status': 'SKIP',
'expected': True,
'tags': [{
'key': 'disabled_test',
'value': 'true',
}]
}
test_result = result_sink_util._compose_test_result(
'TestCase/testSomething',
'SKIP',
True,
tags=[('disabled_test', 'true')])
self.assertEqual(test_result, expected)
@mock.patch.object(requests.Session, 'post')
@mock.patch('%s.open' % 'result_sink_util',
mock.mock_open(read_data=LUCI_CONTEXT_FILE_DATA))
@mock.patch('os.environ.get', return_value='filename')
def test_post_test_result(self, mock_open_file, mock_session_post):
test_result = {
'testId': 'TestCase/testSomething',
'status': 'SKIP',
'expected': True,
'tags': [{
'key': 'disabled_test',
'value': 'true',
}]
}
client = result_sink_util.ResultSinkClient()
client._post_test_result(test_result)
mock_session_post.assert_called_with(
url=SINK_POST_URL,
headers=HEADERS,
data=json.dumps({'testResults': [test_result]}))
@mock.patch.object(requests.Session, 'close')
@mock.patch.object(requests.Session, 'post')
@mock.patch('%s.open' % 'result_sink_util',
mock.mock_open(read_data=LUCI_CONTEXT_FILE_DATA))
@mock.patch('os.environ.get', return_value='filename')
def test_close(self, mock_open_file, mock_session_post, mock_session_close):
client = result_sink_util.ResultSinkClient()
client._post_test_result({'some': 'result'})
mock_session_post.assert_called()
client.close()
mock_session_close.assert_called()
def test_post(self):
client = result_sink_util.ResultSinkClient()
client.sink = 'Make sink not None so _compose_test_result will be called'
client._post_test_result = mock.MagicMock()
client.post(
'testname',
'PASS',
True,
test_log='some_log',
tags=[('tag key', 'tag value')])
client._post_test_result.assert_called_with(
result_sink_util._compose_test_result(
'testname',
'PASS',
True,
test_log='some_log',
tags=[('tag key', 'tag value')]))
client.post('testname', 'PASS', True, test_log='some_log')
client._post_test_result.assert_called_with(
result_sink_util._compose_test_result(
'testname', 'PASS', True, test_log='some_log'))
client.post('testname', 'PASS', True)
client._post_test_result.assert_called_with(
result_sink_util._compose_test_result('testname', 'PASS', True))
if __name__ == '__main__':
unittest.main()