From 0f4d3ab7f9c352e361c99c4c0ad80969eafb08c3 Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Mon, 1 Jun 2020 14:44:00 +0200 Subject: [PATCH] Start unit tests for jails. --- tests/test_jail.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/test_jail.py diff --git a/tests/test_jail.py b/tests/test_jail.py new file mode 100644 index 0000000..dd45c5d --- /dev/null +++ b/tests/test_jail.py @@ -0,0 +1,28 @@ +from focker.jail import backup_file +import tempfile +import os + + +def test_backup_file(): + with tempfile.TemporaryDirectory() as d: + fname = os.path.join(d, 'dummy.conf') + with open(fname, 'w') as f: + f.write('init') + nbackups = 10 + for i in range(15): + backup_file(fname, nbackups=nbackups, chmod=0o640) + with open(fname, 'w') as f: + f.write(str(i)) + + fname = os.path.join(d, 'dummy.conf') + with open(fname, 'r') as f: + assert f.read() == '14' + + for i in range(nbackups): + fname = os.path.join(d, 'dummy.conf.%d' % i) + assert os.path.exists(fname) + with open(fname, 'r') as f: + if i < 5: + assert f.read() == str(i + 9) + else: + assert f.read() == str(i - 1)