Skip to content

Commit

Permalink
Added functional test for mount/unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven V. Gonzales committed May 29, 2012
1 parent 3db89b8 commit a321874
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 5 deletions.
6 changes: 1 addition & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ this time that's the first priority.
Currently Working
-----------------

- Mounting works! (Not fully tested)
- Mounting works! (Tested and works)
- Creating a table of mounted filesystems

TODO
----

- Unmounting

Possible interface examples
Expand Down
10 changes: 10 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
import sys

TEST_DIR = os.path.abspath(os.path.dirname(__file__))
PROJECT_DIR = os.path.abspath(os.path.join(TEST_DIR, '..'))

sys.path.insert(0, PROJECT_DIR)

def fixtures_path(file_path=''):
return os.path.join(TEST_DIR, 'fixtures', file_path)
1 change: 1 addition & 0 deletions tests/fixtures/lower_dir/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello, world!
Empty file.
74 changes: 74 additions & 0 deletions tests/test_as_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
tests.test_as_root
~~~~~~~~~~~~~~~~~~
These are tests that must be run as root. They will be skipped by nose
automatically if you are not root.
These tests are meant to be as close to truly functional as possible. Take
extra caution when running these tests. If this is not a stable release please
do not run these tests in a production system.
"""
import os
from testkit import temp_directory, random_string
from tests import fixtures_path
from .utils import only_as_root
import overlay4u

def read_file_data(filepath):
f = open(filepath)
data = f.read()
f.close()
return data

@only_as_root
def test_mount():
# Get lower directory from fixtures
lower_dir = fixtures_path('lower_dir')
# Read the hello.txt file in the lower_dir
hello_orig_path = os.path.join(lower_dir, 'hello.txt')
hello_orig_data = read_file_data(hello_orig_path)
# Use a temp directory for the upper directory
with temp_directory() as temp_upper_dir:
# Use a temp directory for the mount point
with temp_directory() as temp_mount_point:
# Mount the overlay
overlay = overlay4u.mount(temp_mount_point, lower_dir, temp_upper_dir)

# Read the hello.txt file in the mount_point
hello_overlay_path = os.path.join(temp_mount_point, 'hello.txt')
hello_overlay_read_data = read_file_data(hello_overlay_path)

# Assert that the text file's contents
assert hello_orig_data == hello_overlay_read_data, ('Data '
'for hello.txt should be same in overlay and lower dir')

# Write data to the overlay hello.txt
hello_overlay_write = open(hello_overlay_path, 'w')

# Generate a random string
hello_overlay_write_data = random_string(50)

# Write that string to the file
hello_overlay_write.write(hello_overlay_write_data)

# Save the file
hello_overlay_write.close()

# Unmount the overlay
overlay.unmount()

# Check that the file doesn't exist at the mount point now (means
# successful unmount)
assert os.path.exists(hello_overlay_path) == False, ('Mount '
'point should have nothing in it')

# Check that the upper directory's hello.txt has the correct data
# from the random generated string
hello_upper_path = os.path.join(temp_upper_dir, 'hello.txt')
hello_upper_data = read_file_data(hello_upper_path)
assert hello_upper_data == hello_overlay_write_data

# Check that the original file still has the same content
current_orig_data = read_file_data(hello_orig_path)
assert hello_orig_data == current_orig_data
12 changes: 12 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
from functools import wraps
from nose.plugins.skip import SkipTest

def only_as_root(f):
@wraps(f)
def decorated_function(*args, **kwargs):
euid = os.geteuid()
if euid != 0:
raise SkipTest("Sorry. Test requires root")
return f(*args, **kwargs)
return decorated_function

0 comments on commit a321874

Please sign in to comment.