File: test.py

package info (click to toggle)
cedar-backup2 2.27.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,388 kB
  • sloc: python: 68,546; xml: 9,607; sh: 604; makefile: 65
file content (263 lines) | stat: -rwxr-xr-x 10,924 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# vim: set ft=python ts=3 sw=3 expandtab:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
#              C E D A R
#          S O L U T I O N S       "Software done right."
#           S O F T W A R E
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Copyright (c) 2004-2008,2010,2014 Kenneth J. Pronovici.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# Version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Copies of the GNU General Public License are available from
# the Free Software Foundation website, http://www.gnu.org/.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Author   : Kenneth J. Pronovici <pronovic@ieee.org>
# Language : Python 2 (>= 2.7)
# Project  : Cedar Backup, release 2
# Purpose  : Run all of the unit tests for the project.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

########################################################################
# Notes
########################################################################

"""
Run the CedarBackup2 unit tests.

This script runs all of the unit tests at once so we can get one big success or
failure result, rather than 20 different smaller results that we somehow have
to aggregate together to get the "big picture".  This is done by creating and
running one big unit test suite based on the suites in the individual unit test
modules.

The composite suite is always run using the TextTestRunner at verbosity level
1, which prints one dot (".") on the screen for each test run.  This output is
the same as one would get when using unittest.main() in an individual test.

Generally, I'm trying to keep all of the "special" validation logic (i.e. did
we find the right Python, did we find the right libraries, etc.) in this code
rather than in the individual unit tests so they're more focused on what to
test than how their environment should be configured.

We want to make sure the tests use the modules in the current source tree, not
any versions previously-installed elsewhere, if possible.  We don't actually
import the modules here, but we warn if the wrong ones would be found.  We also
want to make sure we are running the correct 'test' package - not one found
elsewhere on the user's path - since 'test' could be a relatively common name
for a package.

Most people will want to run the script with no arguments.  This will result in
a "reduced feature set" test suite that covers all of the available test
suites, but executes only those tests with no surprising system, kernel or
network dependencies.

If "full" is specified as one of the command-line arguments, then all of the
unit tests will be run, including those that require a specialized environment.
For instance, some tests require remote connectivity, a loopback filesystem,
etc.

Other arguments on the command line are assumed to be named tests, so for
instance passing "config" runs only the tests for config.py.  Any number of
individual tests may be listed on the command line, and unknown values will
simply be ignored.

@author: Kenneth J. Pronovici <pronovic@ieee.org>
"""

########################################################################
# Imported modules
########################################################################

import sys
import os
import logging
import unittest


##################
# main() function
##################

def main():

   """
   Main routine for program.
   @return: Integer 0 upon success, integer 1 upon failure.
   """

   # Check the Python version.  We require 2.7 or greater.
   try:
      if map(int, [sys.version_info[0], sys.version_info[1]]) < [2, 7]:
         print "Python 2 version 2.7 or greater required, sorry."
         return 1
   except:
      # sys.version_info isn't available before 2.0
      print "Python 2 version 2.7 or greater required, sorry."
      return 1

   # Check for the correct CedarBackup2 location and import utilities
   try:
      if os.path.exists(os.path.join(".", "CedarBackup2", "filesystem.py")):
         sys.path.insert(0, ".")
      elif os.path.basename(os.getcwd()) == "testcase" and os.path.exists(os.path.join("..", "CedarBackup2", "filesystem.py")):
         sys.path.insert(0, "..")
      else:
         print "WARNING: CedarBackup2 modules were not found in the expected"
         print "location.  If the import succeeds, you may be using an"
         print "unexpected version of CedarBackup2."
         print ""
      from CedarBackup2.util import nullDevice, Diagnostics
   except ImportError, e:
      print "Failed to import CedarBackup2 util module: %s" % e
      print "You must either run the unit tests from the CedarBackup2 source"
      print "tree, or properly set the PYTHONPATH enviroment variable."
      return 1

   # Import the unit test modules
   try:
      if os.path.exists(os.path.join(".", "testcase", "filesystemtests.py")):
         sys.path.insert(0, ".")
      elif os.path.basename(os.getcwd()) == "testcase" and os.path.exists(os.path.join("..", "testcase", "filesystemtests.py")):
         sys.path.insert(0, "..")
      else:
         print "WARNING: CedarBackup2 unit test modules were not found in"
         print "the expected location.  If the import succeeds, you may be"
         print "using an unexpected version of the test suite."
         print ""
      from testcase import utiltests
      from testcase import knapsacktests
      from testcase import filesystemtests
      from testcase import peertests
      from testcase import actionsutiltests
      from testcase import writersutiltests
      from testcase import cdwritertests
      from testcase import dvdwritertests
      from testcase import configtests
      from testcase import clitests
      from testcase import mysqltests
      from testcase import postgresqltests
      from testcase import subversiontests
      from testcase import mboxtests
      from testcase import encrypttests
      from testcase import amazons3tests
      from testcase import splittests
      from testcase import spantests
      from testcase import synctests
      from testcase import capacitytests
      from testcase import customizetests
   except ImportError, e:
      print "Failed to import CedarBackup2 unit test module: %s" % e
      print "You must either run the unit tests from the CedarBackup2 source"
      print "tree, or properly set the PYTHONPATH enviroment variable."
      return 1

   # Get a list of program arguments
   args = sys.argv[1:]

   # Set verbosity for the test runner
   if "verbose" in args:
      verbosity = 2   # prints each test name
      args.remove("verbose")
   else:
      verbosity = 1   # prints a . for each test

   # Set up logging, where "debug" sends all output to stderr
   if "debug" in args:
      handler = logging.StreamHandler(sys.stdout)
      handler.setLevel(logging.DEBUG)
      logger = logging.getLogger("CedarBackup2")
      logger.setLevel(logging.DEBUG)
      logger.addHandler(handler)
      args.remove("debug")
   else:
      devnull = nullDevice()
      handler = logging.FileHandler(filename=devnull)
      handler.setLevel(logging.NOTSET)
      logger = logging.getLogger("CedarBackup2")
      logger.setLevel(logging.NOTSET)
      logger.addHandler(handler)

   # Set flags in the environment to control tests
   if "full" in args:
      full = True
      os.environ["PEERTESTS_FULL"] = "Y"
      os.environ["WRITERSUTILTESTS_FULL"] = "Y"
      os.environ["ENCRYPTTESTS_FULL"] = "Y"
      os.environ["SPLITTESTS_FULL"] = "Y"
      args.remove("full") # remainder of list will be specific tests to run, if any
   else:
      full = False
      os.environ["PEERTESTS_FULL"] = "N"
      os.environ["WRITERSUTILTESTS_FULL"] = "N"
      os.environ["ENCRYPTTESTS_FULL"] = "N"
      os.environ["SPLITTESTS_FULL"] = "N"

   # Print a starting banner
   print "\n*** Running CedarBackup2 unit tests."
   if not full:
      print "*** Using reduced feature set suite with minimum system requirements."

   # Make a list of tests to run
   unittests = { }
   if args == [] or "util" in args: unittests["util"] = utiltests.suite()
   if args == [] or "knapsack" in args: unittests["knapsack"] = knapsacktests.suite()
   if args == [] or "filesystem" in args: unittests["filesystem"] = filesystemtests.suite()
   if args == [] or "peer" in args: unittests["peer"] = peertests.suite()
   if args == [] or "actionsutil" in args: unittests["actionsutil"] = actionsutiltests.suite()
   if args == [] or "writersutil" in args: unittests["writersutil"] = writersutiltests.suite()
   if args == [] or "cdwriter" in args: unittests["cdwriter"] = cdwritertests.suite()
   if args == [] or "dvdwriter" in args: unittests["dvdwriter"] = dvdwritertests.suite()
   if args == [] or "config" in args: unittests["config"] = configtests.suite()
   if args == [] or "cli" in args: unittests["cli"] = clitests.suite()
   if args == [] or "mysql" in args: unittests["mysql"] = mysqltests.suite()
   if args == [] or "postgresql" in args: unittests["postgresql"] = postgresqltests.suite()
   if args == [] or "subversion" in args: unittests["subversion"] = subversiontests.suite()
   if args == [] or "mbox" in args: unittests["mbox"] = mboxtests.suite()
   if args == [] or "split" in args: unittests["split"] = splittests.suite()
   if args == [] or "encrypt" in args: unittests["encrypt"] = encrypttests.suite()
   if args == [] or "amazons3" in args: unittests["amazons3"] = amazons3tests.suite()
   if args == [] or "span" in args: unittests["span"] = spantests.suite()
   if args == [] or "sync" in args: unittests["sync"] = synctests.suite()
   if args == [] or "capacity" in args: unittests["capacity"] = capacitytests.suite()
   if args == [] or "customize" in args: unittests["customize"] = customizetests.suite()
   if args != []: print "*** Executing specific tests: %s" % unittests.keys()

   # Print some diagnostic information
   print ""
   Diagnostics().printDiagnostics(prefix="*** ")

   # Create and run the test suite
   print ""
   suite = unittest.TestSuite(unittests.values())
   suiteResult = unittest.TextTestRunner(verbosity=verbosity).run(suite)
   print ""
   if not suiteResult.wasSuccessful():
      return 1
   else:
      return 0


########################################################################
# Module entry point
########################################################################

# Run the main routine if the module is executed rather than sourced
if __name__ == '__main__':
   result = main()
   sys.exit(result)