Move init server to initness under util, add test
This commit is contained in:
		
							parent
							
								
									d5392ff160
								
							
						
					
					
						commit
						a5cc173143
					
				| @ -45,14 +45,11 @@ config.dict_override(args_override, 'cli flag') | |||||||
| logg.debug('loaded config: {}\n'.format(config)) | logg.debug('loaded config: {}\n'.format(config)) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class StateRequestHandler(BaseHTTPRequestHandler): | def get_state(state_store_dir): | ||||||
| 
 |         init_path = os.path.join(state_store_dir, 'init') | ||||||
|     state_store_dir = None |  | ||||||
| 
 |  | ||||||
|     def do_GET(self): |  | ||||||
|         init_path = os.path.join(self.state_store_dir, 'init') |  | ||||||
|         init_level = 0 |         init_level = 0 | ||||||
|         registry_address = None |         registry_address = None | ||||||
|  | 
 | ||||||
|         try: |         try: | ||||||
|             f = open(init_path, 'r') |             f = open(init_path, 'r') | ||||||
|             init_level = f.read() |             init_level = f.read() | ||||||
| @ -61,7 +58,7 @@ class StateRequestHandler(BaseHTTPRequestHandler): | |||||||
|         except FileNotFoundError: |         except FileNotFoundError: | ||||||
|             pass |             pass | ||||||
| 
 | 
 | ||||||
|         registry_path = os.path.join(self.state_store_dir, 'registry') |         registry_path = os.path.join(state_store_dir, 'registry') | ||||||
|         try: |         try: | ||||||
|             f = open(registry_path, 'r') |             f = open(registry_path, 'r') | ||||||
|             registry_address = f.read() |             registry_address = f.read() | ||||||
| @ -70,17 +67,24 @@ class StateRequestHandler(BaseHTTPRequestHandler): | |||||||
|         except FileNotFoundError: |         except FileNotFoundError: | ||||||
|             pass |             pass | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|         o = { |         o = { | ||||||
|             'init': init_level, |             'init': init_level, | ||||||
|             'registry': registry_address, |             'registry': registry_address, | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|         logg.debug('headers {}'.format(self.headers)) |         return o | ||||||
|         logg.debug('i {}'.format(o)) |  | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | class StateRequestHandler(BaseHTTPRequestHandler): | ||||||
|  | 
 | ||||||
|  |     state_store_dir = None | ||||||
|  | 
 | ||||||
|  |     def do_GET(self): | ||||||
|  |         | ||||||
|  |         o = get_state(self.state_store_dir) | ||||||
|         self.send_response(200, 'yarr') |         self.send_response(200, 'yarr') | ||||||
|         self.end_headers() |         self.end_headers() | ||||||
|  | 
 | ||||||
|         self.wfile.write(json.dumps(o).encode('utf-8')) |         self.wfile.write(json.dumps(o).encode('utf-8')) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -91,10 +95,14 @@ def run(store, host=None, port=None): | |||||||
|     httpd.serve_forever() |     httpd.serve_forever() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | def main(): | ||||||
|     try: |     try: | ||||||
|         os.stat(config.get('STATE_BASE_DIR')) |         os.stat(config.get('STATE_BASE_DIR')) | ||||||
|     except FileNotFoundError: |     except FileNotFoundError: | ||||||
|         os.makedirs(config.get('STATE_BASE_DIR')) |         os.makedirs(config.get('STATE_BASE_DIR')) | ||||||
|     store = StateRequestHandler.state_store_dir=config.get('STATE_BASE_DIR') |     store = StateRequestHandler.state_store_dir=config.get('STATE_BASE_DIR') | ||||||
|     run(store, host=config.get('HTTPD_HOST'), port=config.get('HTTPD_PORT')) |     run(store, host=config.get('HTTPD_HOST'), port=config.get('HTTPD_PORT')) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | if __name__ == '__main__': | ||||||
|  |     main() | ||||||
							
								
								
									
										7
									
								
								apps/util/initness/setup.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								apps/util/initness/setup.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | |||||||
|  | from setuptools import setup | ||||||
|  | setup( | ||||||
|  |         name='initness', | ||||||
|  |         version='0.0.1a1', | ||||||
|  |         packages=['initness'], | ||||||
|  |         include_package_data=True, | ||||||
|  |         ) | ||||||
							
								
								
									
										27
									
								
								apps/util/initness/tests/test_basic.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								apps/util/initness/tests/test_basic.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | |||||||
|  | # standard imports | ||||||
|  | import unittest | ||||||
|  | import tempfile | ||||||
|  | import os | ||||||
|  | 
 | ||||||
|  | # local imports | ||||||
|  | from initness.runnable.server import get_state | ||||||
|  | 
 | ||||||
|  | class TestInitness(unittest.TestCase): | ||||||
|  | 
 | ||||||
|  |     def setUp(self): | ||||||
|  |         self.dir = tempfile.mkdtemp() | ||||||
|  |         f = open(os.path.join(self.dir, 'init'), 'w') | ||||||
|  |         f.write('42') | ||||||
|  |         f.close() | ||||||
|  |         f = open(os.path.join(self.dir, 'registry'), 'w') | ||||||
|  |         f.write('0xdeadbeef') | ||||||
|  |         f.close() | ||||||
|  | 
 | ||||||
|  |     def test_state(self): | ||||||
|  |         o = get_state(self.dir) | ||||||
|  |         self.assertEqual(o['init'], '42') | ||||||
|  |         self.assertEqual(o['registry'], '0xdeadbeef') | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | if __name__ == '__main__': | ||||||
|  |     unittest.main() | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user