Skip to content

Commit

Permalink
tests: added test for backup functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
wsteitz committed Dec 20, 2016
1 parent 3813cbe commit 0dabba2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
45 changes: 45 additions & 0 deletions test/test_backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest
from time import sleep
from subprocess import check_call, check_output
import smtplib

from settings import *
from common import random_id
from test_mail import new_message, check_imap_received


def test_backup_mail():
# send a mail, to ensure we have something to backup
msg, subject = new_message(TEST_ADDRESS, TEST_ADDRESS)
s = smtplib.SMTP(TEST_DOMAIN, 587)
s.starttls()
s.login(TEST_ADDRESS, TEST_PASSWORD)
s.sendmail(TEST_ADDRESS, [TEST_ADDRESS], msg)
s.quit()

# trigger a backup
sleep(2)
cmd_ssh = "sshpass -p vagrant ssh vagrant@{} -p {} ".format(TEST_SERVER, TEST_PORT)
cmd_count = cmd_ssh + "ls -l /home/user-data/backup/encrypted | wc -l"
num_backup_files = int(check_output(cmd_count, shell=True))
cmd = cmd_ssh + "sudo /vagrant/management/backup.py"
check_call(cmd, shell=True)
num_backup_files_new = int(check_output(cmd_count, shell=True))
assert num_backup_files_new > num_backup_files

# delete mail
assert check_imap_received(subject)
assert not check_imap_received(subject)

# restore backup
path = "/home/user-data"
passphrase = "export PASSPHRASE=\$(sudo cat /home/user-data/backup/secret_key.txt) &&"
# extract to temp directory
restore = "sudo -E duplicity restore --force file://{0}/backup/encrypted {0}/restore &&".format(path)
# move restored backup using rsync, because it allows to overwrite files
move = "sudo rsync -av {0}/restore/* {0}/ &&".format(path)
rm = "sudo rm -rf {0}/restore/".format(path)
check_call(cmd_ssh + "\"" + passphrase + restore + move + rm + "\"", shell=True)

# check the mail is there again
assert check_imap_received(subject)
26 changes: 14 additions & 12 deletions test/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ def new_message(from_email, to_email):
msg['From'] = from_email
msg['To'] = to_email
return msg.as_string(), msg['subject']



def assert_imap_received(subject):
def check_imap_received(subject):
"""Connects with IMAP and asserts the existence of an email, then deletes it"""

sleep(3)
Expand All @@ -30,15 +31,16 @@ def assert_imap_received(subject):
m.login(TEST_ADDRESS, TEST_PASSWORD)
m.select()

# Assert the message exists
# check the message exists
typ, data = m.search(None, '(SUBJECT \"{}\")'.format(subject))
assert len(data[0].split()) == 1

# Delete it & logout
m.store(data[0].strip(), '+FLAGS', '\\Deleted')
m.expunge()
res = len(data[0].split()) == 1
if res:
m.store(data[0].strip(), '+FLAGS', '\\Deleted')
m.expunge()
m.close()
m.logout()
return res


def assert_pop3_received(subject):
Expand Down Expand Up @@ -81,7 +83,7 @@ def test_smtps():
s.login(TEST_ADDRESS, TEST_PASSWORD)
s.sendmail(TEST_ADDRESS, [TEST_ADDRESS], msg)
s.quit()
assert_imap_received(subject)
assert check_imap_received(subject)


def test_smtps_tag():
Expand All @@ -93,7 +95,7 @@ def test_smtps_tag():
s.login(TEST_ADDRESS, TEST_PASSWORD)
s.sendmail(TEST_ADDRESS, [mail_address], msg)
s.quit()
assert_imap_received(subject)
assert check_imap_received(subject)


def test_smtps_requires_auth():
Expand All @@ -117,7 +119,7 @@ def test_smtp():
s = smtplib.SMTP(TEST_DOMAIN, 25)
s.sendmail(TEST_SENDER, [TEST_ADDRESS], msg)
s.quit()
assert_imap_received(subject)
assert check_imap_received(subject)


def test_smtp_tls():
Expand All @@ -127,7 +129,7 @@ def test_smtp_tls():
s.starttls()
s.sendmail(TEST_SENDER, [TEST_ADDRESS], msg)
s.quit()
assert_imap_received(subject)
assert check_imap_received(subject)


# FIXME
Expand Down Expand Up @@ -193,7 +195,7 @@ def test_smtp_headers():


def test_pop3s():
"""Connects with POP3S and asserts the existance of an email, then deletes it"""
"""Connects with POP3S and asserts the existance of an email"""
msg, subject = new_message(TEST_ADDRESS, TEST_ADDRESS)
s = smtplib.SMTP(TEST_DOMAIN, 587)
s.starttls()
Expand Down

0 comments on commit 0dabba2

Please sign in to comment.