2
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 2; coding: utf-8 -*-
4
# This file is part of Déjà Dup.
5
# For copyright information, see AUTHORS.
7
# Déjà Dup is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
12
# Déjà Dup is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
# General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with Déjà Dup. If not, see <http://www.gnu.org/licenses/>.
20
# This mock duplicity reads from a given file describing:
21
# 1) What arguments to expect
22
# 2) What output to give
24
# The file location is specified by DEJA_DUP_TEST_MOCKSCRIPT.
25
# An example format of the file is:
27
# ARGS: full --include --exclude --etc --dry-run
30
# First sample output message
32
# Second and final sample output message
35
# ARGS: full --include --exclude --etc
38
# First sample output message
40
# Second and final sample output message
42
# Every time if things go as expected, we will wipe the first stanza from the
43
# file. If it's the last stanza left, we'll delete the file. That way,
44
# any caller can know if we got passed unexpected arguments by testing for the
45
# existence of the file.
47
import sys, os, shlex, getpass, time
49
if not os.path.exists(os.environ['DEJA_DUP_TEST_MOCKSCRIPT']):
50
print >> logfd, "TESTFAIL: no mockscript"
54
with open(os.environ['DEJA_DUP_TEST_MOCKSCRIPT']) as f:
57
# In general, don't bother trying to avoid exceptions. If we don't get expected
58
# input, that's a test failure too.
60
def skip_whitespace(lineno):
61
while len(lines) > lineno and not lines[lineno].strip():
65
curline = skip_whitespace(0)
71
while len(lines) > curline and lines[curline].strip():
72
tokens = lines[curline].split()
73
if tokens[0] == 'ARGS:':
74
expected_args = shlex.split(lines[curline])[1:]
75
elif tokens[0] == 'RETURN:':
77
elif tokens[0] == 'DELAY:':
78
delay = int(tokens[1])
81
# Where should we spit our messages to?
83
for i in xrange(len(sys.argv)):
84
split = sys.argv[i].split('=', 1)
85
if len(split) > 1 and split[0] == "--log-fd":
86
logfd = os.fdopen(int(split[1]), "w")
87
sys.argv[i] = "--log-fd=?"
89
if expected_args != sys.argv[1:]:
90
print >> logfd, "TESTFAIL: expected\n%s\nvs\n%s" % (expected_args, sys.argv[1:])
93
curline = skip_whitespace(curline)
95
while len(lines) > curline and lines[curline] != "=== deja-dup ===\n":
96
print >> logfd, lines[curline],
99
# Write back mockscript
100
if len(lines) <= curline:
101
os.unlink(os.environ['DEJA_DUP_TEST_MOCKSCRIPT'])
103
lines = lines[curline+1:]
104
with open(os.environ['DEJA_DUP_TEST_MOCKSCRIPT'], 'w') as f: