Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: appengine/swarming/server/bot_code_test.py

Issue 2953253003: Replace custom blob gRPC API with ByteStream (Closed)
Patch Set: Rebase to latest Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2014 The LUCI Authors. All rights reserved. 2 # Copyright 2014 The LUCI Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0 3 # Use of this source code is governed under the Apache License, Version 2.0
4 # that can be found in the LICENSE file. 4 # that can be found in the LICENSE file.
5 5
6 import StringIO 6 import StringIO
7 import logging 7 import logging
8 import os 8 import os
9 import re 9 import re
10 import subprocess 10 import subprocess
(...skipping 22 matching lines...) Expand all
33 33
34 class BotManagementTest(test_case.TestCase): 34 class BotManagementTest(test_case.TestCase):
35 def setUp(self): 35 def setUp(self):
36 super(BotManagementTest, self).setUp() 36 super(BotManagementTest, self).setUp()
37 self.testbed.init_user_stub() 37 self.testbed.init_user_stub()
38 38
39 self.mock( 39 self.mock(
40 auth, 'get_current_identity', 40 auth, 'get_current_identity',
41 lambda: auth.Identity(auth.IDENTITY_USER, 'joe@localhost')) 41 lambda: auth.Identity(auth.IDENTITY_USER, 'joe@localhost'))
42 42
43 self._memcached={}
44 self._read_from_memcached = False
45
46 def mock_set_cached_bot_entry(content, version, desc, part=None):
47 key = bot_code.cached_bot_key(version, desc, part)
48 self._memcached[key] = content
49
50 def mock_get_cached_bot_entry(version, desc, part=None):
51 key = bot_code.cached_bot_key(version, desc, part)
52 if key not in self._memcached:
53 raise bot_code.MemcacheMissingException
54 self._read_from_memcached = True
55 return self._memcached[key]
56
57 self.mock(bot_code, 'set_cached_bot_entry', mock_set_cached_bot_entry)
58 self.mock(bot_code, 'get_cached_bot_entry', mock_get_cached_bot_entry)
59 self.mock(bot_code, 'MAX_MEMCACHED_SIZE_BYTES', 100000)
60
43 def test_get_bootstrap(self): 61 def test_get_bootstrap(self):
44 def get_self_config_mock(path, revision=None, store_last_good=False): 62 def get_self_config_mock(path, revision=None, store_last_good=False):
45 self.assertEqual('scripts/bootstrap.py', path) 63 self.assertEqual('scripts/bootstrap.py', path)
46 self.assertEqual(None, revision) 64 self.assertEqual(None, revision)
47 self.assertEqual(True, store_last_good) 65 self.assertEqual(True, store_last_good)
48 return None, 'foo bar' 66 return None, 'foo bar'
49 self.mock(config, 'get_self_config', get_self_config_mock) 67 self.mock(config, 'get_self_config', get_self_config_mock)
50 f = bot_code.get_bootstrap('localhost', 'token', None) 68 f = bot_code.get_bootstrap('localhost', 'token', None)
51 expected = ( 69 expected = (
52 '#!/usr/bin/env python\n' 70 '#!/usr/bin/env python\n'
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 def test_get_bot_version(self): 106 def test_get_bot_version(self):
89 actual, additionals = bot_code.get_bot_version('http://localhost') 107 actual, additionals = bot_code.get_bot_version('http://localhost')
90 self.assertTrue(re.match(r'^[0-9a-f]{64}$', actual), actual) 108 self.assertTrue(re.match(r'^[0-9a-f]{64}$', actual), actual)
91 expected = { 109 expected = {
92 'config/bot_config.py': bot_code.get_bot_config().content, 110 'config/bot_config.py': bot_code.get_bot_config().content,
93 } 111 }
94 self.assertEqual(expected, additionals) 112 self.assertEqual(expected, additionals)
95 113
96 def test_get_swarming_bot_zip(self): 114 def test_get_swarming_bot_zip(self):
97 zipped_code = bot_code.get_swarming_bot_zip('http://localhost') 115 zipped_code = bot_code.get_swarming_bot_zip('http://localhost')
116 self.assertFalse(self._read_from_memcached)
117 # Make sure that we read from memcached if we get it again
118 zipped_code_copy = bot_code.get_swarming_bot_zip('http://localhost')
119 self.assertTrue(self._read_from_memcached)
120 # Why not assertEqual? Don't want to dump ~1MB of data if this fails.
121 self.assertTrue(zipped_code == zipped_code_copy)
98 # Ensure the zip is valid and all the expected files are present. 122 # Ensure the zip is valid and all the expected files are present.
99 with zipfile.ZipFile(StringIO.StringIO(zipped_code), 'r') as zip_file: 123 with zipfile.ZipFile(StringIO.StringIO(zipped_code), 'r') as zip_file:
100 for i in bot_archive.FILES: 124 for i in bot_archive.FILES:
101 with zip_file.open(i) as f: 125 with zip_file.open(i) as f:
102 content = f.read() 126 content = f.read()
103 if os.path.basename(i) != '__init__.py': 127 if os.path.basename(i) != '__init__.py':
104 self.assertTrue(content, i) 128 self.assertTrue(content, i)
105 129
106 temp_dir = tempfile.mkdtemp(prefix='swarming') 130 temp_dir = tempfile.mkdtemp(prefix='swarming')
107 try: 131 try:
108 # Try running the bot and ensure it can import the required files. (It 132 # Try running the bot and ensure it can import the required files. (It
109 # would crash if it failed to import them). 133 # would crash if it failed to import them).
110 bot_path = os.path.join(temp_dir, 'swarming_bot.zip') 134 bot_path = os.path.join(temp_dir, 'swarming_bot.zip')
111 with open(bot_path, 'wb') as f: 135 with open(bot_path, 'wb') as f:
112 f.write(zipped_code) 136 f.write(zipped_code)
113 proc = subprocess.Popen( 137 proc = subprocess.Popen(
114 [sys.executable, bot_path, 'start_bot', '-h'], 138 [sys.executable, bot_path, 'start_bot', '-h'],
115 cwd=temp_dir, 139 cwd=temp_dir,
116 stdout=subprocess.PIPE, 140 stdout=subprocess.PIPE,
117 stderr=subprocess.STDOUT) 141 stderr=subprocess.STDOUT)
118 out = proc.communicate()[0] 142 out = proc.communicate()[0]
119 self.assertEqual(0, proc.returncode, out) 143 self.assertEqual(0, proc.returncode, out)
120 finally: 144 finally:
121 file_path.rmtree(temp_dir) 145 file_path.rmtree(temp_dir)
122 146
147
123 def test_bootstrap_token(self): 148 def test_bootstrap_token(self):
124 tok = bot_code.generate_bootstrap_token() 149 tok = bot_code.generate_bootstrap_token()
125 self.assertEqual( 150 self.assertEqual(
126 {'for': 'user:joe@localhost'}, bot_code.validate_bootstrap_token(tok)) 151 {'for': 'user:joe@localhost'}, bot_code.validate_bootstrap_token(tok))
127 152
128 153
129 if __name__ == '__main__': 154 if __name__ == '__main__':
130 fix_encoding.fix_encoding() 155 fix_encoding.fix_encoding()
131 logging.basicConfig( 156 logging.basicConfig(
132 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) 157 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
133 if '-v' in sys.argv: 158 if '-v' in sys.argv:
134 unittest.TestCase.maxDiff = None 159 unittest.TestCase.maxDiff = None
135 unittest.main() 160 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698