~ubuntu-branches/ubuntu/precise/deja-dup/precise-updates

« back to all changes in this revision

Viewing changes to tests/common/mock/duplicity

  • Committer: Package Import Robot
  • Author(s): Michael Terry
  • Date: 2012-02-23 12:42:44 UTC
  • mfrom: (1.1.41)
  • Revision ID: package-import@ubuntu.com-20120223124244-hnuzoxlc3qtcqz41
Tags: 21.90-0ubuntu1
* New upstream release
* debian/rules:
  - Enable 'make check' by default again
* debian/control:
  - Add dbus-x11 and desktop-file-utils as Build-Depends for tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 2; coding: utf-8 -*-
 
3
#
 
4
# This file is part of Déjà Dup.
 
5
# For copyright information, see AUTHORS.
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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/>.
 
19
 
 
20
# This mock duplicity reads from a given file describing:
 
21
# 1) What arguments to expect
 
22
# 2) What output to give
 
23
#
 
24
# The file location is specified by DEJA_DUP_TEST_MOCKSCRIPT.
 
25
# An example format of the file is:
 
26
 
 
27
# ARGS: full --include --exclude --etc --dry-run
 
28
# RETURN: 0
 
29
#
 
30
# First sample output message
 
31
#
 
32
# Second and final sample output message
 
33
#
 
34
# === deja-dup ===
 
35
# ARGS: full --include --exclude --etc
 
36
# RETURN: 0
 
37
#
 
38
# First sample output message
 
39
#
 
40
# Second and final sample output message
 
41
 
 
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.
 
46
 
 
47
import sys, os, shlex, getpass, time
 
48
 
 
49
if not os.path.exists(os.environ['DEJA_DUP_TEST_MOCKSCRIPT']):
 
50
  print >> logfd, "TESTFAIL: no mockscript"
 
51
  sys.exit(-1)
 
52
 
 
53
lines = []
 
54
with open(os.environ['DEJA_DUP_TEST_MOCKSCRIPT']) as f:
 
55
  lines = f.readlines()
 
56
 
 
57
# In general, don't bother trying to avoid exceptions. If we don't get expected
 
58
# input, that's a test failure too.
 
59
 
 
60
def skip_whitespace(lineno):
 
61
  while len(lines) > lineno and not lines[lineno].strip():
 
62
    lineno += 1
 
63
  return lineno
 
64
 
 
65
curline = skip_whitespace(0)
 
66
 
 
67
rv = 0
 
68
expected_args = []
 
69
delay = 0
 
70
 
 
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:':
 
76
    rv = int(tokens[1])
 
77
  elif tokens[0] == 'DELAY:':
 
78
    delay = int(tokens[1])
 
79
  curline += 1
 
80
 
 
81
# Where should we spit our messages to?
 
82
logfd = None
 
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=?"
 
88
 
 
89
if expected_args != sys.argv[1:]:
 
90
  print >> logfd, "TESTFAIL: expected\n%s\nvs\n%s" % (expected_args, sys.argv[1:])
 
91
  sys.exit(-1)
 
92
 
 
93
curline = skip_whitespace(curline)
 
94
 
 
95
while len(lines) > curline and lines[curline] != "=== deja-dup ===\n":
 
96
  print >> logfd, lines[curline],
 
97
  curline += 1
 
98
 
 
99
# Write back mockscript
 
100
if len(lines) <= curline:
 
101
  os.unlink(os.environ['DEJA_DUP_TEST_MOCKSCRIPT'])
 
102
else:
 
103
  lines = lines[curline+1:]
 
104
  with open(os.environ['DEJA_DUP_TEST_MOCKSCRIPT'], 'w') as f:
 
105
    f.writelines(lines)
 
106
 
 
107
time.sleep(delay)
 
108
 
 
109
sys.exit(rv)