|
|
@@ -30,6 +30,7 @@ import textwrap |
|
|
|
from testutil.helpers import * |
|
|
|
|
|
|
|
testurl = "http://localhost:32181/" |
|
|
|
#testurl = "http://bucket.mit.edu/nilmrun/" |
|
|
|
|
|
|
|
def setup_module(): |
|
|
|
global test_server |
|
|
@@ -49,10 +50,10 @@ class TestClient(object): |
|
|
|
|
|
|
|
def wait_kill(self, client, pid, timeout = 1): |
|
|
|
time.sleep(timeout) |
|
|
|
status = client.get("/process/status", { "pid": pid }) |
|
|
|
status = client.get("process/status", { "pid": pid }) |
|
|
|
if not status["alive"]: |
|
|
|
raise AssertionError("died before we could kill it") |
|
|
|
status = client.post("/process/remove", { "pid": pid }) |
|
|
|
status = client.post("process/remove", { "pid": pid }) |
|
|
|
if status["alive"]: |
|
|
|
raise AssertionError("didn't get killed") |
|
|
|
return status |
|
|
@@ -61,46 +62,46 @@ class TestClient(object): |
|
|
|
start = time.time() |
|
|
|
status = None |
|
|
|
while (time.time() - start) < timeout: |
|
|
|
status = client.get("/process/status", { "pid": pid }) |
|
|
|
status = client.get("process/status", { "pid": pid }) |
|
|
|
if status["alive"] == False: |
|
|
|
break |
|
|
|
else: |
|
|
|
raise AssertionError("process " + str(pid) + " didn't die in " + |
|
|
|
str(timeout) + " seconds: " + repr(status)) |
|
|
|
if remove: |
|
|
|
status = client.post("/process/remove", { "pid": pid }) |
|
|
|
status = client.post("process/remove", { "pid": pid }) |
|
|
|
return status |
|
|
|
|
|
|
|
def test_client_01_basic(self): |
|
|
|
client = HTTPClient(baseurl = testurl) |
|
|
|
version = client.get("/version") |
|
|
|
version = client.get("version") |
|
|
|
eq_(distutils.version.LooseVersion(version), |
|
|
|
distutils.version.LooseVersion(nilmrun.__version__)) |
|
|
|
|
|
|
|
in_("This is NilmRun", client.get("/")) |
|
|
|
in_("This is NilmRun", client.get("")) |
|
|
|
|
|
|
|
with assert_raises(ClientError): |
|
|
|
client.get("/favicon.ico") |
|
|
|
client.get("favicon.ico") |
|
|
|
|
|
|
|
def test_client_02_manager(self): |
|
|
|
client = HTTPClient(baseurl = testurl) |
|
|
|
|
|
|
|
eq_(client.get("/process/list"), []) |
|
|
|
eq_(client.get("process/list"), []) |
|
|
|
|
|
|
|
with assert_raises(ClientError) as e: |
|
|
|
client.get("/process/status", { "pid": 12345 }) |
|
|
|
client.get("process/status", { "pid": 12345 }) |
|
|
|
in_("No such PID", str(e.exception)) |
|
|
|
with assert_raises(ClientError): |
|
|
|
client.get("/process/remove", { "pid": 12345 }) |
|
|
|
client.get("process/remove", { "pid": 12345 }) |
|
|
|
in_("No such PID", str(e.exception)) |
|
|
|
|
|
|
|
def test_client_03_run_command(self): |
|
|
|
client = HTTPClient(baseurl = testurl, post_json = True) |
|
|
|
eq_(client.get("/process/list"), []) |
|
|
|
eq_(client.get("process/list"), []) |
|
|
|
|
|
|
|
def do(argv, kill): |
|
|
|
pid = client.post("/run/command", { "argv": argv } ) |
|
|
|
eq_(client.get("/process/list"), [pid]) |
|
|
|
pid = client.post("run/command", { "argv": argv } ) |
|
|
|
eq_(client.get("process/list"), [pid]) |
|
|
|
if kill: |
|
|
|
return self.wait_kill(client, pid) |
|
|
|
return self.wait_end(client, pid) |
|
|
@@ -132,18 +133,18 @@ class TestClient(object): |
|
|
|
nilmrun.testfilter.test(json.loads(sys.argv[1])) |
|
|
|
""") |
|
|
|
jsonargs = json.dumps(args) |
|
|
|
return client.post("/run/code", { "code": code, "args": [ jsonargs ] }) |
|
|
|
return client.post("run/code", { "code": code, "args": [ jsonargs ] }) |
|
|
|
|
|
|
|
def test_client_04_process_basic(self): |
|
|
|
client = HTTPClient(baseurl = testurl, post_json = True) |
|
|
|
|
|
|
|
# start dummy filter |
|
|
|
pid = self._run_testfilter(client, 30) |
|
|
|
eq_(client.get("/process/list"), [pid]) |
|
|
|
eq_(client.get("process/list"), [pid]) |
|
|
|
time.sleep(1) |
|
|
|
|
|
|
|
# Verify that status looks OK |
|
|
|
status = client.get("/process/status", { "pid": pid, "clear": True }) |
|
|
|
status = client.get("process/status", { "pid": pid, "clear": True }) |
|
|
|
for x in [ "pid", "alive", "exitcode", "name", "start_time", "log" ]: |
|
|
|
in_(x, status) |
|
|
|
in_("dummy 0\ndummy 1\ndummy 2\ndummy 3\n", status["log"]) |
|
|
@@ -151,7 +152,7 @@ class TestClient(object): |
|
|
|
eq_(status["exitcode"], None) |
|
|
|
|
|
|
|
# Check that the log got cleared |
|
|
|
status = client.get("/process/status", { "pid": pid }) |
|
|
|
status = client.get("process/status", { "pid": pid }) |
|
|
|
nin_("dummy 0\ndummy 1\ndummy 2\ndummy 3\n", status["log"]) |
|
|
|
|
|
|
|
# See that it ended properly |
|
|
@@ -160,11 +161,11 @@ class TestClient(object): |
|
|
|
eq_(status["exitcode"], 0) |
|
|
|
|
|
|
|
# Remove it |
|
|
|
killstatus = client.post("/process/remove", { "pid": pid }) |
|
|
|
killstatus = client.post("process/remove", { "pid": pid }) |
|
|
|
eq_(status, killstatus) |
|
|
|
eq_(client.get("/process/list"), []) |
|
|
|
eq_(client.get("process/list"), []) |
|
|
|
with assert_raises(ClientError) as e: |
|
|
|
client.post("/process/remove", { "pid": pid }) |
|
|
|
client.post("process/remove", { "pid": pid }) |
|
|
|
in_("No such PID", str(e.exception)) |
|
|
|
|
|
|
|
def test_client_05_process_terminate(self): |
|
|
@@ -173,18 +174,18 @@ class TestClient(object): |
|
|
|
# Trigger exception in filter |
|
|
|
pid = self._run_testfilter(client, -1) |
|
|
|
time.sleep(0.5) |
|
|
|
status = client.get("/process/status", { "pid": pid }) |
|
|
|
status = client.get("process/status", { "pid": pid }) |
|
|
|
eq_(status["alive"], False) |
|
|
|
eq_(status["exitcode"], 1) |
|
|
|
in_("Exception: test exception", status["log"]) |
|
|
|
client.post("/process/remove", { "pid": pid }) |
|
|
|
client.post("process/remove", { "pid": pid }) |
|
|
|
|
|
|
|
# Kill a running filter by removing it early |
|
|
|
newpid = self._run_testfilter(client, 50) |
|
|
|
ne_(newpid, pid) |
|
|
|
time.sleep(0.5) |
|
|
|
start = time.time() |
|
|
|
status = client.post("/process/remove", { "pid": newpid }) |
|
|
|
status = client.post("process/remove", { "pid": newpid }) |
|
|
|
elapsed = time.time() - start |
|
|
|
# Should have died in slightly over 1 second |
|
|
|
assert(0.5 < elapsed < 2) |
|
|
@@ -192,12 +193,12 @@ class TestClient(object): |
|
|
|
ne_(status["exitcode"], 0) |
|
|
|
|
|
|
|
# No more |
|
|
|
eq_(client.get("/process/list"), []) |
|
|
|
eq_(client.get("process/list"), []) |
|
|
|
|
|
|
|
# Try to remove a running filter that ignored SIGTERM |
|
|
|
pid = self._run_testfilter(client, 0) |
|
|
|
start = time.time() |
|
|
|
status = client.post("/process/remove", { "pid": pid }) |
|
|
|
status = client.post("process/remove", { "pid": pid }) |
|
|
|
elapsed = time.time() - start |
|
|
|
# Should have died in slightly over 2 seconds |
|
|
|
assert(1.5 < elapsed < 3) |
|
|
@@ -238,25 +239,25 @@ class TestClient(object): |
|
|
|
} |
|
|
|
] |
|
|
|
} |
|
|
|
pid = client.post("/run/code", { "code": "import nilmtools.trainola\n" + |
|
|
|
pid = client.post("run/code", { "code": "import nilmtools.trainola\n" + |
|
|
|
"nilmtools.trainola.main()", |
|
|
|
"args": [ data ] }) |
|
|
|
while True: |
|
|
|
status = client.get("/process/status", { "pid": pid, "clear": 1 }) |
|
|
|
status = client.get("process/status", { "pid": pid, "clear": 1 }) |
|
|
|
sys.stdout.write(status["log"]) |
|
|
|
sys.stdout.flush() |
|
|
|
if status["alive"] == False: |
|
|
|
break |
|
|
|
status = client.post("/process/remove", { "pid": pid }) |
|
|
|
status = client.post("process/remove", { "pid": pid }) |
|
|
|
os._exit(int(status["exitcode"])) |
|
|
|
|
|
|
|
def test_client_07_run_code(self): |
|
|
|
client = HTTPClient(baseurl = testurl, post_json = True) |
|
|
|
eq_(client.get("/process/list"), []) |
|
|
|
eq_(client.get("process/list"), []) |
|
|
|
|
|
|
|
def do(code, args, kill): |
|
|
|
pid = client.post("/run/code", { "code": code, "args": args } ) |
|
|
|
eq_(client.get("/process/list"), [pid]) |
|
|
|
pid = client.post("run/code", { "code": code, "args": args } ) |
|
|
|
eq_(client.get("process/list"), [pid]) |
|
|
|
if kill: |
|
|
|
return self.wait_kill(client, pid) |
|
|
|
return self.wait_end(client, pid) |
|
|
@@ -324,9 +325,9 @@ class TestClient(object): |
|
|
|
client = HTTPClient(baseurl = testurl, post_json = True) |
|
|
|
|
|
|
|
with assert_raises(ClientError) as e: |
|
|
|
client.post("/run/code", { "code": "asdf", "args": "qwer" }) |
|
|
|
client.post("run/code", { "code": "asdf", "args": "qwer" }) |
|
|
|
in_("must be a list", str(e.exception)) |
|
|
|
|
|
|
|
with assert_raises(ClientError) as e: |
|
|
|
client.post("/run/command", { "argv": "asdf" }) |
|
|
|
client.post("run/command", { "argv": "asdf" }) |
|
|
|
in_("must be a list", str(e.exception)) |