Commit e847cbd9 authored by Łukasz Nowak's avatar Łukasz Nowak

Working skeleton of simple SlapOS master simulator.

Thanks to SOSS it will be possible to feed slapgrid* with various tasks, which
will allow to test different scenarios.
parent 72ca37ec
...@@ -2,15 +2,22 @@ from slapos.grid import slapgrid ...@@ -2,15 +2,22 @@ from slapos.grid import slapgrid
import os import os
import shutil import shutil
import tempfile import tempfile
import signal
import unittest import unittest
import socket import socket
import BaseHTTPServer
import time
import urllib2
import errno
import threading
class BasicMixin: class BasicMixin:
def setUp(self): def setUp(self):
self._tempdir = tempfile.mkdtemp() self._tempdir = tempfile.mkdtemp()
self.software_root = os.path.join(self._tempdir, 'software') self.software_root = os.path.join(self._tempdir, 'software')
self.instance_root = os.path.join(self._tempdir, 'instance') self.instance_root = os.path.join(self._tempdir, 'instance')
self.master_url = 'http://127.0.0.1:0/' if getattr(self, 'master_url', None) is None:
self.master_url = 'http://127.0.0.1:0/'
self.computer_id = 'computer' self.computer_id = 'computer'
self.supervisord_socket = os.path.join(self._tempdir, 'supervisord.sock') self.supervisord_socket = os.path.join(self._tempdir, 'supervisord.sock')
self.supervisord_configuration_path = os.path.join(self._tempdir, self.supervisord_configuration_path = os.path.join(self._tempdir,
...@@ -23,7 +30,7 @@ class BasicMixin: ...@@ -23,7 +30,7 @@ class BasicMixin:
self.buildout) self.buildout)
def tearDown(self): def tearDown(self):
shutil.rmtree(self._tempdir) shutil.rmtree(self._tempdir, True)
class TestBasicSlapgridCP(BasicMixin, unittest.TestCase): class TestBasicSlapgridCP(BasicMixin, unittest.TestCase):
def test_no_software_root(self): def test_no_software_root(self):
...@@ -38,23 +45,63 @@ class TestBasicSlapgridCP(BasicMixin, unittest.TestCase): ...@@ -38,23 +45,63 @@ 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):
address = (host, port)
httpd = Server(address, SlapOSMasterHTTPRequestHandler)
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._master_dir = tempfile.mkdtemp() self.thread = threading.Thread(target=_run_server, args=(self._master_host,
self._master_port))
self.thread.start()
self.master_url = 'http://%s:%s/' % (self._master_host, self._master_port)
def stopMaster(self): def stopMaster(self):
shutil.rmtree(self._master_dir) try:
urllib2.urlopen(self.master_url+'!killme!')
except Exception:
pass
if self.thread is not None:
self.thread.join()
def setUp(self): def setUp(self):
BasicMixin.setUp(self)
self.startMaster()
self.master_url = 'http://%s:%s' % (self._master_host, self._master_port)
# prepare master # prepare master
self.startMaster()
BasicMixin.setUp(self)
def tearDown(self): def tearDown(self):
self.stopMaster() self.stopMaster()
# XXX: Hardcoded pid, as it is not configurable in slapos
svc = os.path.join(self.instance_root, 'var', 'run', 'supervisord.pid')
if os.path.exists(svc):
try:
pid = int(open(svc).read().strip())
except ValueError:
pass
else:
os.kill(pid, signal.SIGTERM)
BasicMixin.tearDown(self) BasicMixin.tearDown(self)
class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
...@@ -62,3 +109,4 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): ...@@ -62,3 +109,4 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
os.mkdir(self.software_root) os.mkdir(self.software_root)
os.mkdir(self.instance_root) os.mkdir(self.instance_root)
self.grid.processComputerPartitionList() 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