def test_hot_start(simple_config, state_file, cold_start, corrupt_state, delete_state): # Initially start up the server without a state file. Should be cold # started. assert not os.path.lexists(state_file) with SpallocServer(simple_config, cold_start) as s: job_id = s.create_job(None, owner="me") time.sleep(0.05) assert s.get_job_state(None, job_id)["state"] == JobState.ready # State should be dumped assert os.path.lexists(state_file) # Corrupt the state if required if corrupt_state: # Just leave the file empty... open(state_file, "w").close() # Delete the state if required if delete_state: # Just leave the file empty... os.remove(state_file) # Start a new server with SpallocServer(simple_config, cold_start) as s: # Should have the same state as before, if doing a hot start if cold_start or corrupt_state or delete_state: assert s.get_job_state(None, job_id)["state"] == JobState.unknown else: assert s.get_job_state(None, job_id)["state"] == JobState.ready
def test_commandline(monkeypatch, config_file, args, cold_start, mock_server): s, SpallocServer = mock_server s.is_alive.return_value = False import spalloc_server.server monkeypatch.setattr(spalloc_server.server, "SpallocServer", SpallocServer) main(args.format(config_file).split()) SpallocServer.assert_called_once_with(config_filename=config_file, cold_start=cold_start, port=22244)
def test_keyboard_interrupt(monkeypatch, config_file, mock_server): s, SpallocServer = mock_server import spalloc_server.server monkeypatch.setattr(spalloc_server.server, "SpallocServer", SpallocServer) s.is_alive.side_effect = KeyboardInterrupt main([config_file]) SpallocServer.assert_called_once_with(config_filename=config_file, cold_start=False, port=22244) s.is_alive.assert_called_once_with() s.stop_and_join.assert_called_once_with()
def test_stop_and_join_disconnects(simple_config): # Clients should be disconnected when doing stop and join with SpallocServer(simple_config) as s: with SimpleClient() as c: c.call("version") s.stop_and_join() time.sleep(0.05) assert c.sock.recv(1024) == b""
def test_no_initial_config_file(config_file, missing): # Should fail if config file is not valid/missing first time if missing: pass # Don't create a file! else: with open(config_file, "w") as f: f.write("foo=123") with pytest.raises(Exception): SpallocServer(config_file)
def test_join(simple_config): # Tests join, stop_and_join and is_alive with SpallocServer(simple_config) as s: assert s.is_alive() is True joining_thread = threading.Thread(target=s.join) joining_thread.start() # The server should still be running... time.sleep(0.05) assert joining_thread.is_alive() assert s.is_alive() is True # When server is stopped, the joining thread should be complete s.stop_and_join() assert s.is_alive() is False joining_thread.join()
def s(config_file): # A server which is created and shut down with each use. with SpallocServer(config_file) as s: yield s