Commit 01abe672 authored by Łukasz Nowak's avatar Łukasz Nowak

Use Flask as slapos master simulation.

It is already requirement for slapos.core, and it is well known code.

Ue multiprocessing instead of threading, as there is no other way to kill
Flask application from code except SIGTERMing it.
parent e847cbd9
from slapos.grid import slapgrid from slapos.grid import slapgrid
import flask
import multiprocessing
import os import os
import shutil import shutil
import tempfile
import signal import signal
import unittest import slapos.slap.slap
import socket import socket
import BaseHTTPServer import tempfile
import time import unittest
import urllib2 import xml_marshaller
import errno
import threading app = flask.Flask(__name__)
class BasicMixin: class BasicMixin:
def setUp(self): def setUp(self):
...@@ -45,46 +46,23 @@ class TestBasicSlapgridCP(BasicMixin, unittest.TestCase): ...@@ -45,46 +46,23 @@ class TestBasicSlapgridCP(BasicMixin, unittest.TestCase):
os.mkdir(self.instance_root) os.mkdir(self.instance_root)
self.assertRaises(socket.error, self.grid.processComputerPartitionList) self.assertRaises(socket.error, self.grid.processComputerPartitionList)
class Server(BaseHTTPServer.HTTPServer):
__run = True
def serve_forever(self):
while self.__run:
self.handle_request()
def handle_error(self, *_):
self.__run = False
class SlapOSMasterHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
if '!killme!' in self.path:
self.send_response(200)
raise SystemExit
self.send_response(200)
return
def _run_server(host, port): def _run_server(host, port):
address = (host, port) global app
httpd = Server(address, SlapOSMasterHTTPRequestHandler) app.run(host=host, port=port, use_reloader=False, debug=True)
httpd.serve_forever()
class MasterMixin(BasicMixin): class MasterMixin(BasicMixin):
_master_port = 45678 _master_port = 45678
_master_host = '127.0.0.1' _master_host = '127.0.0.1'
def startMaster(self): def startMaster(self):
self.thread = threading.Thread(target=_run_server, args=(self._master_host, self.process = multiprocessing.Process(target=_run_server,
self._master_port)) args=(self._master_host, self._master_port))
self.thread.start() self.process.start()
self.master_url = 'http://%s:%s/' % (self._master_host, self._master_port) self.master_url = 'http://%s:%s/' % (self._master_host, self._master_port)
def stopMaster(self): def stopMaster(self):
try: self.process.terminate()
urllib2.urlopen(self.master_url+'!killme!') self.process.join()
except Exception:
pass
if self.thread is not None:
self.thread.join()
def setUp(self): def setUp(self):
# prepare master # prepare master
...@@ -105,8 +83,15 @@ class MasterMixin(BasicMixin): ...@@ -105,8 +83,15 @@ class MasterMixin(BasicMixin):
BasicMixin.tearDown(self) BasicMixin.tearDown(self)
class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
@app.route('/getComputerInformation', methods=['GET'])
def getComputerInformation():
computer_id = flask.request.args['computer_id']
slap_computer = slapos.slap.Computer(computer_id)
slap_computer._software_release_list = []
slap_computer._computer_partition_list = []
return xml_marshaller.xml_marshaller.dumps(slap_computer)
def test_nothing_to_do(self): def test_nothing_to_do(self):
os.mkdir(self.software_root) os.mkdir(self.software_root)
os.mkdir(self.instance_root) os.mkdir(self.instance_root)
self.grid.processComputerPartitionList() self.assertTrue(self.grid.processComputerPartitionList())
raise NotImplementedError
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment