| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # coding=utf-8 | 2 # coding=utf-8 |
| 3 # Copyright 2015 The Swarming Authors. All rights reserved. | 3 # Copyright 2015 The Swarming Authors. All rights reserved. |
| 4 # Use of this source code is governed by the Apache v2.0 license that can be | 4 # Use of this source code is governed by the Apache v2.0 license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 import datetime | 7 import datetime |
| 8 import json | 8 import json |
| 9 import logging | 9 import logging |
| 10 import os | 10 import os |
| 11 import random | 11 import random |
| 12 import sys | 12 import sys |
| 13 import unittest | 13 import unittest |
| 14 | 14 |
| 15 import test_env_handlers | 15 import test_env_handlers |
| 16 from test_support import test_case | 16 from test_support import test_case |
| 17 | 17 |
| 18 from protorpc.remote import protojson | 18 from protorpc.remote import protojson |
| 19 import webapp2 | 19 import webapp2 |
| 20 import webtest | 20 import webtest |
| 21 | 21 |
| 22 from components import auth | 22 from components import auth |
| 23 from components import ereporter2 | 23 from components import ereporter2 |
| 24 from components import utils | 24 from components import utils |
| 25 | 25 |
| 26 import handlers_api | |
| 27 import handlers_bot | 26 import handlers_bot |
| 28 import handlers_endpoints | 27 import handlers_endpoints |
| 29 import swarming_rpcs | 28 import swarming_rpcs |
| 30 | 29 |
| 31 from server import acl | 30 from server import acl |
| 32 from server import bot_management | 31 from server import bot_management |
| 33 from server import config | 32 from server import config |
| 34 | 33 |
| 35 | 34 |
| 36 def message_to_dict(rpc_message): | 35 def message_to_dict(rpc_message): |
| 37 return json.loads(protojson.encode_message(rpc_message)) | 36 return json.loads(protojson.encode_message(rpc_message)) |
| 38 | 37 |
| 39 | 38 |
| 40 class BaseTest(test_env_handlers.AppTestBase, test_case.EndpointsTestCase): | 39 class BaseTest(test_env_handlers.AppTestBase, test_case.EndpointsTestCase): |
| 41 | 40 |
| 42 DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%f' | 41 DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%f' |
| 43 DATETIME_NO_MICRO = '%Y-%m-%dT%H:%M:%S' | 42 DATETIME_NO_MICRO = '%Y-%m-%dT%H:%M:%S' |
| 44 | 43 |
| 45 def setUp(self): | 44 def setUp(self): |
| 46 test_case.EndpointsTestCase.setUp(self) | 45 test_case.EndpointsTestCase.setUp(self) |
| 47 super(BaseTest, self).setUp() | 46 super(BaseTest, self).setUp() |
| 48 self.mock(auth, 'is_group_member', lambda *_args, **_kwargs: True) | 47 self.mock(auth, 'is_group_member', lambda *_args, **_kwargs: True) |
| 49 # handlers_bot is necessary to create fake tasks. | 48 # handlers_bot is necessary to create fake tasks. |
| 50 # TODO(maruel): Get rid of handlers_api.get_routes() here. The API should be | |
| 51 # self-sufficient. | |
| 52 routes = handlers_bot.get_routes() + handlers_api.get_routes() | |
| 53 self.app = webtest.TestApp( | 49 self.app = webtest.TestApp( |
| 54 webapp2.WSGIApplication(routes, debug=True), | 50 webapp2.WSGIApplication(handlers_bot.get_routes(), debug=True), |
| 55 extra_environ={ | 51 extra_environ={ |
| 56 'REMOTE_ADDR': self.source_ip, | 52 'REMOTE_ADDR': self.source_ip, |
| 57 'SERVER_SOFTWARE': os.environ['SERVER_SOFTWARE'], | 53 'SERVER_SOFTWARE': os.environ['SERVER_SOFTWARE'], |
| 58 }) | 54 }) |
| 59 self.mock( | 55 self.mock( |
| 60 ereporter2, 'log_request', | 56 ereporter2, 'log_request', |
| 61 lambda *args, **kwargs: self.fail('%s, %s' % (args, kwargs))) | 57 lambda *args, **kwargs: self.fail('%s, %s' % (args, kwargs))) |
| 62 # Client API test cases run by default as user. | 58 # Client API test cases run by default as user. |
| 63 self.set_as_user() | 59 self.set_as_user() |
| 64 | 60 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 79 content = f.read().decode('utf-8') | 75 content = f.read().decode('utf-8') |
| 80 | 76 |
| 81 expected = { | 77 expected = { |
| 82 u'content': content, | 78 u'content': content, |
| 83 } | 79 } |
| 84 self.assertEqual(expected, self.call_api('get_' + name).json) | 80 self.assertEqual(expected, self.call_api('get_' + name).json) |
| 85 | 81 |
| 86 expected = { | 82 expected = { |
| 87 u'version': u'0', | 83 u'version': u'0', |
| 88 u'when': u'2010-01-02T03:04:05', | 84 u'when': u'2010-01-02T03:04:05', |
| 89 u'who': u'anonymous:anonymous', | 85 u'who': u'user:user@example.com', |
| 90 } | 86 } |
| 91 response = self.call_api('put_' + name, {'content': u'hi ☀!'}) | 87 response = self.call_api('put_' + name, {'content': u'hi ☀!'}) |
| 92 self.assertEqual(expected, response.json) | 88 self.assertEqual(expected, response.json) |
| 93 | 89 |
| 94 expected = { | 90 expected = { |
| 95 u'content': u'hi \u2600!', | 91 u'content': u'hi \u2600!', |
| 96 u'version': u'0', | 92 u'version': u'0', |
| 97 u'when': u'2010-01-02T03:04:05', | 93 u'when': u'2010-01-02T03:04:05', |
| 98 u'who': u'anonymous:anonymous', | 94 u'who': u'user:user@example.com', |
| 99 } | 95 } |
| 100 self.assertEqual(expected, self.call_api('get_' + name).json) | 96 self.assertEqual(expected, self.call_api('get_' + name).json) |
| 101 | 97 |
| 102 self.mock_now(now, 60) | 98 self.mock_now(now, 60) |
| 103 expected = { | 99 expected = { |
| 104 u'version': u'1', | 100 u'version': u'1', |
| 105 u'when': u'2010-01-02T03:05:05', | 101 u'when': u'2010-01-02T03:05:05', |
| 106 u'who': u'anonymous:anonymous', | 102 u'who': u'user:user@example.com', |
| 107 } | 103 } |
| 108 response = self.call_api('put_' + name, {'content': u'hi ♕!'}) | 104 response = self.call_api('put_' + name, {'content': u'hi ♕!'}) |
| 109 self.assertEqual(expected, response.json) | 105 self.assertEqual(expected, response.json) |
| 110 | 106 |
| 111 expected = { | 107 expected = { |
| 112 u'content': u'hi ♕!', | 108 u'content': u'hi ♕!', |
| 113 u'version': u'1', | 109 u'version': u'1', |
| 114 u'when': u'2010-01-02T03:05:05', | 110 u'when': u'2010-01-02T03:05:05', |
| 115 u'who': u'anonymous:anonymous', | 111 u'who': u'user:user@example.com', |
| 116 } | 112 } |
| 117 self.assertEqual(expected, self.call_api('get_' + name).json) | 113 self.assertEqual(expected, self.call_api('get_' + name).json) |
| 118 | 114 |
| 119 expected = { | 115 expected = { |
| 120 u'content': u'hi ☀!', | 116 u'content': u'hi ☀!', |
| 121 u'version': u'0', | 117 u'version': u'0', |
| 122 u'when': u'2010-01-02T03:04:05', | 118 u'when': u'2010-01-02T03:04:05', |
| 123 u'who': u'anonymous:anonymous', | 119 u'who': u'user:user@example.com', |
| 124 } | 120 } |
| 125 response = self.call_api('get_' + name, {'version': '0'}) | 121 response = self.call_api('get_' + name, {'version': '0'}) |
| 126 self.assertEqual(expected, response.json) | 122 self.assertEqual(expected, response.json) |
| 127 | 123 |
| 128 def test_bootstrap(self): | 124 def test_bootstrap(self): |
| 129 self._test_file('bootstrap') | 125 self._test_file('bootstrap') |
| 130 | 126 |
| 131 def test_bot_config(self): | 127 def test_bot_config(self): |
| 132 self._test_file('bot_config') | 128 self._test_file('bot_config') |
| 133 | 129 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 152 ], | 148 ], |
| 153 env=[ | 149 env=[ |
| 154 swarming_rpcs.StringPair(key='PATH', value='/'), | 150 swarming_rpcs.StringPair(key='PATH', value='/'), |
| 155 ], | 151 ], |
| 156 execution_timeout_secs=30, | 152 execution_timeout_secs=30, |
| 157 io_timeout_secs=30), | 153 io_timeout_secs=30), |
| 158 tags=['foo:bar'], | 154 tags=['foo:bar'], |
| 159 user='joe@localhost') | 155 user='joe@localhost') |
| 160 expected = { | 156 expected = { |
| 161 u'request': { | 157 u'request': { |
| 162 u'authenticated': u'anonymous:anonymous', | 158 u'authenticated': u'user:user@example.com', |
| 163 u'created_ts': str_now, | 159 u'created_ts': str_now, |
| 164 u'expiration_secs': u'30', | 160 u'expiration_secs': u'30', |
| 165 u'name': u'job1', | 161 u'name': u'job1', |
| 166 u'priority': u'200', | 162 u'priority': u'200', |
| 167 u'properties': { | 163 u'properties': { |
| 168 u'command': [u'rm', u'-rf', u'/'], | 164 u'command': [u'rm', u'-rf', u'/'], |
| 169 u'dimensions': [ | 165 u'dimensions': [ |
| 170 {u'key': u'a', u'value': u'b'}, | 166 {u'key': u'a', u'value': u'b'}, |
| 171 ], | 167 ], |
| 172 u'env': [ | 168 u'env': [ |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 execution_timeout_secs=30, | 207 execution_timeout_secs=30, |
| 212 inputs_ref=swarming_rpcs.FilesRef( | 208 inputs_ref=swarming_rpcs.FilesRef( |
| 213 isolated='1'*40, | 209 isolated='1'*40, |
| 214 isolatedserver='http://localhost:1', | 210 isolatedserver='http://localhost:1', |
| 215 namespace='default-gzip'), | 211 namespace='default-gzip'), |
| 216 io_timeout_secs=30), | 212 io_timeout_secs=30), |
| 217 tags=['foo:bar'], | 213 tags=['foo:bar'], |
| 218 user='joe@localhost') | 214 user='joe@localhost') |
| 219 expected = { | 215 expected = { |
| 220 u'request': { | 216 u'request': { |
| 221 u'authenticated': u'anonymous:anonymous', | 217 u'authenticated': u'user:user@example.com', |
| 222 u'created_ts': str_now, | 218 u'created_ts': str_now, |
| 223 u'expiration_secs': u'30', | 219 u'expiration_secs': u'30', |
| 224 u'name': u'job1', | 220 u'name': u'job1', |
| 225 u'priority': u'200', | 221 u'priority': u'200', |
| 226 u'properties': { | 222 u'properties': { |
| 227 u'dimensions': [ | 223 u'dimensions': [ |
| 228 {u'key': u'a', u'value': u'b'}, | 224 {u'key': u'a', u'value': u'b'}, |
| 229 {u'key': u'foo', u'value': u'bar'}, | 225 {u'key': u'foo', u'value': u'bar'}, |
| 230 ], | 226 ], |
| 231 u'env': [ | 227 u'env': [ |
| (...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 758 self.assertEqual(expected, json_version) | 754 self.assertEqual(expected, json_version) |
| 759 | 755 |
| 760 | 756 |
| 761 if __name__ == '__main__': | 757 if __name__ == '__main__': |
| 762 if '-v' in sys.argv: | 758 if '-v' in sys.argv: |
| 763 unittest.TestCase.maxDiff = None | 759 unittest.TestCase.maxDiff = None |
| 764 logging.basicConfig(level=logging.DEBUG) | 760 logging.basicConfig(level=logging.DEBUG) |
| 765 else: | 761 else: |
| 766 logging.basicConfig(level=logging.CRITICAL) | 762 logging.basicConfig(level=logging.CRITICAL) |
| 767 unittest.main() | 763 unittest.main() |
| OLD | NEW |