def test_cron_lock_acquisition(self, subprocess_mock, tmpdir, capsys, caplog): """ Basic test for cron process lock acquisition """ server = build_real_server({'barman_home': tmpdir.strpath}) # Basic cron lock acquisition with ServerCronLock(tmpdir.strpath, server.config.name): server.cron(wals=True, retention_policies=False) out, err = capsys.readouterr() assert ("Another cron process is already running on server %s. " "Skipping to the next server\n" % server.config.name) in out # Lock acquisition for archive-wal with ServerWalArchiveLock(tmpdir.strpath, server.config.name): server.cron(wals=True, retention_policies=False) out, err = capsys.readouterr() assert ("Another archive-wal process is already running " "on server %s. Skipping to the next server" % server.config.name) in out # Lock acquisition for receive-wal with ServerWalArchiveLock(tmpdir.strpath, server.config.name): with ServerWalReceiveLock(tmpdir.strpath, server.config.name): # force the streaming_archiver to True for this test server.config.streaming_archiver = True server.cron(wals=True, retention_policies=False) assert ("Another STREAMING ARCHIVER process is running for " "server %s" % server.config.name) in caplog.text
def test_init(self, tmpdir): """ Test the init method """ # Build a basic configuration config = build_config_from_dicts( {"barman_lock_directory": tmpdir.strpath}) config.name = "main" # Acquire a lock and initialise the ProjectManager. # Expect the ProjectManager to Retrieve the # "Running process" identified by the lock lock = ServerWalReceiveLock(tmpdir.strpath, "main") with lock: pm = ProcessManager(config) # Test for the length of the process list assert len(pm.process_list) == 1 # Test for the server identifier of the process assert pm.process_list[0].server_name == "main" # Test for the task type assert pm.process_list[0].task == "receive-wal" # Read the pid from the lockfile and test id against the ProcessInfo # contained in the process_list with open(lock.filename, "r") as lockfile: pid = lockfile.read().strip() assert int(pid) == pm.process_list[0].pid # Test lock file parse error. # Skip the lock and don't raise any exception. with lock: with open(lock.filename, "w") as lockfile: lockfile.write("invalid") pm = ProcessManager(config) assert len(pm.process_list) == 0
def test_server_wal_receive_lock(self, tmpdir): """ Tests for ServerCronLock class """ lock = ServerWalReceiveLock(tmpdir.strpath, 'server_name') assert lock.filename == tmpdir.join('.server_name-receive-wal.lock') assert lock.raise_if_fail assert not lock.wait
def test_list(self, tmpdir): """ Test the list method from the ProjectManager class """ config = build_config_from_dicts( {'barman_lock_directory': tmpdir.strpath}) config.name = 'main' with ServerWalReceiveLock(tmpdir.strpath, 'main'): pm = ProcessManager(config) process = pm.list('receive-wal')[0] assert process.server_name == 'main' assert process.task == 'receive-wal' with open( os.path.join(tmpdir.strpath, '.%s-receive-wal.lock' % config.name)) as lockfile: pid = lockfile.read().strip() assert int(pid) == process.pid
def test_kill(self, kill_mock, tmpdir): """ Test the Kill method from the ProjectManager class. Mocks the os.kill used inside the the kill method """ config = build_config_from_dicts( {"barman_lock_directory": tmpdir.strpath}) config.name = "main" # Acquire a lock, simulating a running process with ServerWalReceiveLock(tmpdir.strpath, "main"): # Build a ProcessManager and retrieve the receive-wal process pm = ProcessManager(config) pi = pm.list("receive-wal")[0] # Exit at the first invocation of kill (this is a failed kill) kill_mock.side_effect = OSError(errno.EPERM, "", "") kill = pm.kill(pi) # Expect the kill result to be false assert kill is False assert kill_mock.call_count == 1 kill_mock.assert_called_with(pi.pid, 2) kill_mock.reset_mock() # Exit at the second invocation of kill (this is a successful kill) kill_mock.side_effect = [None, OSError(errno.ESRCH, "", "")] # Expect the kill result to be true kill = pm.kill(pi) assert kill assert kill_mock.call_count == 2 kill_mock.assert_has_calls( [mock.call(pi.pid, 2), mock.call(pi.pid, 0)]) kill_mock.reset_mock() # Check for the retry feature. exit at the second iteration of the # kill cycle kill_mock.side_effect = [None, None, OSError(errno.ESRCH, "", "")] kill = pm.kill(pi) assert kill assert kill_mock.call_count == 3 kill_mock.assert_has_calls([ mock.call(pi.pid, 2), mock.call(pi.pid, 0), mock.call(pi.pid, 0) ])
def test_list(self, tmpdir): """ Test the list method from the ProjectManager class """ config = build_config_from_dicts( {"barman_lock_directory": tmpdir.strpath}) config.name = "main" lockfile = tmpdir.join(".%s-receive-wal.lock" % config.name) # Listing the processes should not modify the lockfile content lockfile.write("0\n") pm = ProcessManager(config) assert pm.list("receive-wal") == [] assert "0\n" == lockfile.read() # If there is a running process it must correctly read its pid with ServerWalReceiveLock(tmpdir.strpath, config.name): pm = ProcessManager(config) process = pm.list("receive-wal")[0] assert "main" == process.server_name assert "receive-wal" == process.task assert os.getpid() == process.pid assert str(os.getpid()) == lockfile.read().strip()