Move init server to initness under util, add test

This commit is contained in:
nolash 2021-09-29 07:08:31 +02:00
parent d5392ff160
commit a5cc173143
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 53 additions and 11 deletions

View File

@ -45,14 +45,11 @@ config.dict_override(args_override, 'cli flag')
logg.debug('loaded config: {}\n'.format(config))
class StateRequestHandler(BaseHTTPRequestHandler):
state_store_dir = None
def do_GET(self):
init_path = os.path.join(self.state_store_dir, 'init')
def get_state(state_store_dir):
init_path = os.path.join(state_store_dir, 'init')
init_level = 0
registry_address = None
try:
f = open(init_path, 'r')
init_level = f.read()
@ -61,7 +58,7 @@ class StateRequestHandler(BaseHTTPRequestHandler):
except FileNotFoundError:
pass
registry_path = os.path.join(self.state_store_dir, 'registry')
registry_path = os.path.join(state_store_dir, 'registry')
try:
f = open(registry_path, 'r')
registry_address = f.read()
@ -70,17 +67,24 @@ class StateRequestHandler(BaseHTTPRequestHandler):
except FileNotFoundError:
pass
o = {
'init': init_level,
'registry': registry_address,
}
logg.debug('headers {}'.format(self.headers))
logg.debug('i {}'.format(o))
return 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.end_headers()
self.wfile.write(json.dumps(o).encode('utf-8'))
@ -91,10 +95,14 @@ def run(store, host=None, port=None):
httpd.serve_forever()
if __name__ == '__main__':
def main():
try:
os.stat(config.get('STATE_BASE_DIR'))
except FileNotFoundError:
os.makedirs(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'))
if __name__ == '__main__':
main()

View File

@ -0,0 +1,7 @@
from setuptools import setup
setup(
name='initness',
version='0.0.1a1',
packages=['initness'],
include_package_data=True,
)

View 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()