| 1 |
# vim: set ft=python ts=4 sw=4 expandtab: |
| 2 |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 3 |
# |
| 4 |
# C E D A R |
| 5 |
# S O L U T I O N S "Software done right." |
| 6 |
# S O F T W A R E |
| 7 |
# |
| 8 |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 9 |
# |
| 10 |
# Copyright (c) 2007,2010,2015 Kenneth J. Pronovici. |
| 11 |
# All rights reserved. |
| 12 |
# |
| 13 |
# This program is free software; you can redistribute it and/or |
| 14 |
# modify it under the terms of the GNU General Public License, |
| 15 |
# Version 2, as published by the Free Software Foundation. |
| 16 |
# |
| 17 |
# This program is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 20 |
# |
| 21 |
# Copies of the GNU General Public License are available from |
| 22 |
# the Free Software Foundation website, http://www.gnu.org/. |
| 23 |
# |
| 24 |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 25 |
# |
| 26 |
# Author : Kenneth J. Pronovici <[email protected]> |
| 27 |
# Language : Python 3 |
| 28 |
# Project : Cedar Backup, release 3 |
| 29 |
# Purpose : Tests action utility functionality. |
| 30 |
# |
| 31 |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 32 |
|
| 33 |
######################################################################## |
| 34 |
# Module documentation |
| 35 |
######################################################################## |
| 36 |
|
| 37 |
""" |
| 38 |
Unit tests for CedarBackup3/actions/util.py. |
| 39 |
|
| 40 |
Code Coverage |
| 41 |
============= |
| 42 |
|
| 43 |
This module contains individual tests for the public functions and classes |
| 44 |
implemented in actions/util.py. |
| 45 |
|
| 46 |
Naming Conventions |
| 47 |
================== |
| 48 |
|
| 49 |
I prefer to avoid large unit tests which validate more than one piece of |
| 50 |
functionality, and I prefer to avoid using overly descriptive (read: long) |
| 51 |
test names, as well. Instead, I use lots of very small tests that each |
| 52 |
validate one specific thing. These small tests are then named with an index |
| 53 |
number, yielding something like ``testAddDir_001`` or ``testValidate_010``. |
| 54 |
Each method has a docstring describing what it's supposed to accomplish. I |
| 55 |
feel that this makes it easier to judge how important a given failure is, |
| 56 |
and also makes it somewhat easier to diagnose and fix individual problems. |
| 57 |
|
| 58 |
Full vs. Reduced Tests |
| 59 |
====================== |
| 60 |
|
| 61 |
All of the tests in this module are considered safe to be run in an average |
| 62 |
build environment. There is a no need to use a ACTIONSUTILTESTS_FULL |
| 63 |
environment variable to provide a "reduced feature set" test suite as for |
| 64 |
some of the other test modules. |
| 65 |
|
| 66 |
@author Kenneth J. Pronovici <[email protected]> |
| 67 |
""" |
| 68 |
|
| 69 |
|
| 70 |
######################################################################## |
| 71 |
# Import modules and do runtime validations |
| 72 |
######################################################################## |
| 73 |
|
| 74 |
import os |
| 75 |
import tempfile |
| 76 |
import unittest |
| 77 |
|
| 78 |
from CedarBackup3.actions.util import findDailyDirs, writeIndicatorFile |
| 79 |
from CedarBackup3.extend.encrypt import ENCRYPT_INDICATOR |
| 80 |
from CedarBackup3.testutil import buildPath, configureLogging, extractTar, findResources, removedir |
| 81 |
|
| 82 |
####################################################################### |
| 83 |
# Module-wide configuration and constants |
| 84 |
####################################################################### |
| 85 |
|
| 86 |
DATA_DIRS = [ |
| 87 |
"./data", |
| 88 |
"./tests/data", |
| 89 |
] |
| 90 |
RESOURCES = [ |
| 91 |
"tree1.tar.gz", |
| 92 |
"tree8.tar.gz", |
| 93 |
"tree15.tar.gz", |
| 94 |
"tree17.tar.gz", |
| 95 |
"tree18.tar.gz", |
| 96 |
"tree19.tar.gz", |
| 97 |
"tree20.tar.gz", |
| 98 |
] |
| 99 |
|
| 100 |
INVALID_PATH = "bogus" # This path name should never exist |
| 101 |
|
| 102 |
|
| 103 |
####################################################################### |
| 104 |
# Test Case Classes |
| 105 |
####################################################################### |
| 106 |
|
| 107 |
###################### |
| 108 |
# TestFunctions class |
| 109 |
###################### |
| 110 |
|
| 111 |
|
| 112 |
class TestFunctions(unittest.TestCase): |
| 113 |
"""Tests for the various public functions.""" |
| 114 |
|
| 115 |
################ |
| 116 |
# Setup methods |
| 117 |
################ |
| 118 |
|
| 119 |
@classmethod |
| 120 |
def setUpClass(cls): |
| 121 |
configureLogging() |
| 122 |
|
| 123 |
def setUp(self): |
| 124 |
try: |
| 125 |
self.tmpdir = tempfile.mkdtemp() |
| 126 |
self.resources = findResources(RESOURCES, DATA_DIRS) |
| 127 |
except Exception as e: |
| 128 |
self.fail(e) |
| 129 |
|
| 130 |
def tearDown(self): |
| 131 |
try: |
| 132 |
removedir(self.tmpdir) |
| 133 |
except: |
| 134 |
pass |
| 135 |
|
| 136 |
################## |
| 137 |
# Utility methods |
| 138 |
################## |
| 139 |
|
| 140 |
def extractTar(self, tarname): |
| 141 |
"""Extracts a tarfile with a particular name.""" |
| 142 |
extractTar(self.tmpdir, self.resources["%s.tar.gz" % tarname]) |
| 143 |
|
| 144 |
def buildPath(self, components): |
| 145 |
"""Builds a complete search path from a list of components.""" |
| 146 |
components.insert(0, self.tmpdir) |
| 147 |
return buildPath(components) |
| 148 |
|
| 149 |
####################### |
| 150 |
# Test findDailyDirs() |
| 151 |
####################### |
| 152 |
|
| 153 |
def testFindDailyDirs_001(self): |
| 154 |
""" |
| 155 |
Test with a nonexistent staging directory. |
| 156 |
""" |
| 157 |
stagingDir = self.buildPath([INVALID_PATH]) |
| 158 |
self.assertRaises(ValueError, findDailyDirs, stagingDir, ENCRYPT_INDICATOR) |
| 159 |
|
| 160 |
def testFindDailyDirs_002(self): |
| 161 |
""" |
| 162 |
Test with an empty staging directory. |
| 163 |
""" |
| 164 |
self.extractTar("tree8") |
| 165 |
stagingDir = self.buildPath(["tree8", "dir001"]) |
| 166 |
dailyDirs = findDailyDirs(stagingDir, ENCRYPT_INDICATOR) |
| 167 |
self.assertEqual([], dailyDirs) |
| 168 |
|
| 169 |
def testFindDailyDirs_003(self): |
| 170 |
""" |
| 171 |
Test with a staging directory containing only files. |
| 172 |
""" |
| 173 |
self.extractTar("tree1") |
| 174 |
stagingDir = self.buildPath(["tree1"]) |
| 175 |
dailyDirs = findDailyDirs(stagingDir, ENCRYPT_INDICATOR) |
| 176 |
self.assertEqual([], dailyDirs) |
| 177 |
|
| 178 |
def testFindDailyDirs_004(self): |
| 179 |
""" |
| 180 |
Test with a staging directory containing only links. |
| 181 |
""" |
| 182 |
self.extractTar("tree15") |
| 183 |
stagingDir = self.buildPath(["tree15", "dir001"]) |
| 184 |
dailyDirs = findDailyDirs(stagingDir, ENCRYPT_INDICATOR) |
| 185 |
self.assertEqual([], dailyDirs) |
| 186 |
|
| 187 |
def testFindDailyDirs_005(self): |
| 188 |
""" |
| 189 |
Test with a valid staging directory, where the daily directories do NOT |
| 190 |
contain the encrypt indicator. |
| 191 |
""" |
| 192 |
self.extractTar("tree17") |
| 193 |
stagingDir = self.buildPath(["tree17"]) |
| 194 |
dailyDirs = findDailyDirs(stagingDir, ENCRYPT_INDICATOR) |
| 195 |
self.assertEqual(6, len(dailyDirs)) |
| 196 |
self.assertTrue(self.buildPath(["tree17", "2006", "12", "29"]) in dailyDirs) |
| 197 |
self.assertTrue(self.buildPath(["tree17", "2006", "12", "30"]) in dailyDirs) |
| 198 |
self.assertTrue(self.buildPath(["tree17", "2006", "12", "31"]) in dailyDirs) |
| 199 |
self.assertTrue(self.buildPath(["tree17", "2007", "01", "01"]) in dailyDirs) |
| 200 |
self.assertTrue(self.buildPath(["tree17", "2007", "01", "02"]) in dailyDirs) |
| 201 |
self.assertTrue(self.buildPath(["tree17", "2007", "01", "03"]) in dailyDirs) |
| 202 |
|
| 203 |
def testFindDailyDirs_006(self): |
| 204 |
""" |
| 205 |
Test with a valid staging directory, where the daily directories DO |
| 206 |
contain the encrypt indicator. |
| 207 |
""" |
| 208 |
self.extractTar("tree18") |
| 209 |
stagingDir = self.buildPath(["tree18"]) |
| 210 |
dailyDirs = findDailyDirs(stagingDir, ENCRYPT_INDICATOR) |
| 211 |
self.assertEqual([], dailyDirs) |
| 212 |
|
| 213 |
def testFindDailyDirs_007(self): |
| 214 |
""" |
| 215 |
Test with a valid staging directory, where some daily directories contain |
| 216 |
the encrypt indicator and others do not. |
| 217 |
""" |
| 218 |
self.extractTar("tree19") |
| 219 |
stagingDir = self.buildPath(["tree19"]) |
| 220 |
dailyDirs = findDailyDirs(stagingDir, ENCRYPT_INDICATOR) |
| 221 |
self.assertEqual(3, len(dailyDirs)) |
| 222 |
self.assertTrue(self.buildPath(["tree19", "2006", "12", "30"]) in dailyDirs) |
| 223 |
self.assertTrue(self.buildPath(["tree19", "2007", "01", "01"]) in dailyDirs) |
| 224 |
self.assertTrue(self.buildPath(["tree19", "2007", "01", "03"]) in dailyDirs) |
| 225 |
|
| 226 |
def testFindDailyDirs_008(self): |
| 227 |
""" |
| 228 |
Test for case where directories other than daily directories contain the |
| 229 |
encrypt indicator (the indicator should be ignored). |
| 230 |
""" |
| 231 |
self.extractTar("tree20") |
| 232 |
stagingDir = self.buildPath(["tree20"]) |
| 233 |
dailyDirs = findDailyDirs(stagingDir, ENCRYPT_INDICATOR) |
| 234 |
self.assertEqual(6, len(dailyDirs)) |
| 235 |
self.assertTrue(self.buildPath(["tree20", "2006", "12", "29"]) in dailyDirs) |
| 236 |
self.assertTrue(self.buildPath(["tree20", "2006", "12", "30"]) in dailyDirs) |
| 237 |
self.assertTrue(self.buildPath(["tree20", "2006", "12", "31"]) in dailyDirs) |
| 238 |
self.assertTrue(self.buildPath(["tree20", "2007", "01", "01"]) in dailyDirs) |
| 239 |
self.assertTrue(self.buildPath(["tree20", "2007", "01", "02"]) in dailyDirs) |
| 240 |
self.assertTrue(self.buildPath(["tree20", "2007", "01", "03"]) in dailyDirs) |
| 241 |
|
| 242 |
############################ |
| 243 |
# Test writeIndicatorFile() |
| 244 |
############################ |
| 245 |
|
| 246 |
def testWriteIndicatorFile_001(self): |
| 247 |
""" |
| 248 |
Test with a nonexistent staging directory. |
| 249 |
""" |
| 250 |
stagingDir = self.buildPath([INVALID_PATH]) |
| 251 |
self.assertRaises(IOError, writeIndicatorFile, stagingDir, ENCRYPT_INDICATOR, None, None) |
| 252 |
|
| 253 |
def testWriteIndicatorFile_002(self): |
| 254 |
""" |
| 255 |
Test with a valid staging directory. |
| 256 |
""" |
| 257 |
self.extractTar("tree8") |
| 258 |
stagingDir = self.buildPath(["tree8", "dir001"]) |
| 259 |
writeIndicatorFile(stagingDir, ENCRYPT_INDICATOR, None, None) |
| 260 |
self.assertTrue(os.path.exists(self.buildPath(["tree8", "dir001", ENCRYPT_INDICATOR]))) |