Skip to content

Instantly share code, notes, and snippets.

@fleaplus
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fleaplus/10946138 to your computer and use it in GitHub Desktop.
Save fleaplus/10946138 to your computer and use it in GitHub Desktop.
Test with a context manager to create directory/file structure
import unittest
import os
import tempfile
import re
import shutil
from mock import patch
from reports import diskSpaceReport
TEMP_DIR = tempfile.gettempdir() + '/nightly/units'
@patch('files.DATA_DIR', tempfile.gettempdir())
class TestReports(unittest.TestCase):
@classmethod
def setUpClass(cls):
# handle creation of master directory structure
os.makedirs(TEMP_DIR)
@classmethod
def tearDownClass(cls):
# remove all created directories and files
shutil.rmtree(tempfile.gettempdir() + '/nightly')
def test_disk_space_report(self):
"""
disk space report should by default report mills that are over 80
percent disk usage
"""
data = [{"name": 3002,
"diskSpace-Unit3002": "Filesystem Size Used Avail Use%% Mounted on\n/dev/hda3 950M 804M 147M 85% /\n/dev/hda1 9.2M 2.6M 6.2M 30% /boot\ntmpfs 61M 5.8M 55M 10% /dev/shm\n"},
{"name": 3003,
"diskSpace-Unit3003": "Filesystem Size Used Avail Use%% Mounted on\n/dev/hda3 950M 804M 147M 75% /\n/dev/hda1 9.2M 2.6M 6.2M 30% /boot\ntmpfs 61M 5.8M 55M 10% /dev/shm\n"}]
with Mills(data):
results = diskSpaceReport()
self.assertEqual(len(results), 1)
for result in results:
self.assertEqual(result['mill'], '3002')
def test_disk_space_report_custom(self):
"""
disk space report should show only mills over a certain threshold
of usage when passed show_usage_over=xx
"""
data = [{"name": 3002,
"diskSpace-Unit3002": "Filesystem Size Used Avail Use%% Mounted on\n/dev/hda3 950M 804M 147M 85% /\n/dev/hda1 9.2M 2.6M 6.2M 30% /boot\ntmpfs 61M 5.8M 55M 10% /dev/shm\n"},
{"name": 3003,
"diskSpace-Unit3003": "Filesystem Size Used Avail Use%% Mounted on\n/dev/hda3 950M 804M 147M 50% /\n/dev/hda1 9.2M 2.6M 6.2M 30% /boot\ntmpfs 61M 5.8M 55M 10% /dev/shm\n"}]
with Mills(data):
results = diskSpaceReport(show_usage_over=60)
self.assertEqual(len(results), 1)
for result in results:
self.assertEqual(result['mill'], '3002')
def test_disk_space_report_many_results(self):
"""
disk space report should return a list of all mills that are
over the threshold of disk usage
"""
data = [{"name": 3002,
"diskSpace-Unit3002": "Filesystem Size Used Avail Use%% Mounted on\n/dev/hda3 950M 804M 147M 85% /\n/dev/hda1 9.2M 2.6M 6.2M 30% /boot\ntmpfs 61M 5.8M 55M 10% /dev/shm\n"},
{"name": 3003,
"diskSpace-Unit3003": "Filesystem Size Used Avail Use%% Mounted on\n/dev/hda3 950M 804M 147M 90% /\n/dev/hda1 9.2M 2.6M 6.2M 30% /boot\ntmpfs 61M 5.8M 55M 10% /dev/shm\n"},
{"name": 3004,
"diskSpace-Unit3003": "Filesystem Size Used Avail Use%% Mounted on\n/dev/hda3 950M 804M 147M 30% /\n/dev/hda1 9.2M 2.6M 6.2M 30% /boot\ntmpfs 61M 5.8M 55M 10% /dev/shm\n"}]
with Mills(data):
results = diskSpaceReport()
self.assertEqual(len(results), 2)
for result in results:
self.assertIn(result['mill'], ['3002', '3003'])
class Mills(object):
""" context manager to create mill directory and files as needed for tests """
def __init__(self, data):
self.mill_dirs = []
for mill in data:
new_mill_dir = ''
if 'name' in mill.keys():
new_mill_dir = os.path.join(TEMP_DIR, str(mill['name']))
os.makedirs(new_mill_dir)
self.mill_dirs.append(new_mill_dir)
mill.pop("name", None) # we are done with this key
else:
continue # invalid test data
for key in mill:
filename = os.path.join(new_mill_dir, key)
f = open(filename, "w")
f.write(mill[key])
f.close()
def __enter__(self):
pass
def __exit__(self, *args):
""" remove all mill directories related to this context """
for mill in self.mill_dirs:
shutil.rmtree(mill)
if __name__ == '__main__':
unittest.main()
@fleaplus
Copy link
Author

removed chdir mumbo jumbo

@fleaplus
Copy link
Author

and now its an actual test!

@fleaplus
Copy link
Author

Added more tests to check scailability. Seems good.

@mrgenixus
Copy link

So Mills is a Mock class?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment