Test inventory backup file creation

In addition to adding test cases for creating the inventory backup, this
patch modifies the `get_inventory` function to make the cleanup action
optional. This was done to facilitate the tests, since they required
that clean up not happen in order to verify the relevant scenarios.

Change-Id: I3ec317bc9dc7d950dec726789c16b5434981c796
This commit is contained in:
Nolan Brubaker 2016-05-11 12:35:17 -04:00
parent 02f8d3d697
commit 1124a5e237

View File

@ -36,15 +36,17 @@ def cleanup():
os.remove(f_file)
def get_inventory():
def get_inventory(clean=True):
"Return the inventory mapping in a dict."
try:
inventory_string = di.main({'config': TARGET_DIR})
inventory = json.loads(inventory_string)
return inventory
finally:
# Remove the file system artifacts since we want to force fresh runs
cleanup()
if clean:
# Remove the file system artifacts since we want to force
# fresh runs
cleanup()
class TestArgParser(unittest.TestCase):
@ -516,5 +518,33 @@ class TestConfigChecks(unittest.TestCase):
os.rename(USER_CONFIG_FILE + ".tmp", USER_CONFIG_FILE)
class TestMultipleRuns(unittest.TestCase):
def test_creating_backup_file(self):
# Generate the initial inventory files
get_inventory(clean=False)
# run again to force creation of the backup files
get_inventory(clean=False)
backup_path = path.join(TARGET_DIR, 'backup_openstack_inventory.tar')
self.assertTrue(os.path.exists(backup_path))
def test_recreating_files(self):
# Deleting the files after the first run should cause the files to be
# completely remade
get_inventory()
get_inventory()
backup_path = path.join(TARGET_DIR, 'backup_openstack_inventory.tar')
self.assertFalse(os.path.exists(backup_path))
def tearDown(self):
# Clean up here since get_inventory will not do it by design in
# this test.
cleanup()
if __name__ == '__main__':
unittest.main()