Source

vasm / src / tests / test_fstab.py

#!/usr/bin/env python

#    This file is part of VASM.
#
#    VASM is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#
#    VASM 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.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with VASM.  If not, see <http://www.gnu.org/licenses/>.

__author__ = "Moises Henriquez"
__author_email__ = "moc.liamg@xnl.E0M"[::-1]

import unittest
import os
import sys
vasmdir = os.path.join(os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))), 'vasm')
if not vasmdir in sys.path: sys.path.append(vasmdir)
import unittest
from backend import fstab
from shutil import copy2

class FstabTest(unittest.TestCase):
    def setUp(self):
        # create a copy of /etc/fstab so we can play with it
        self.datapath = '/tmp/test_fstab'
        copy2('/etc/fstab', self.datapath)
        self.datamodel = fstab.FstabModel(self.datapath)
        self.fake_entry = fstab.FstabEntry()
        self.fake_entry.device = "/dev/sdz1"
        self.fake_entry.mountpoint = "fake_mount_point"
        self.fake_entry.filesystem = "ext2"
        self.fake_entry.options = "defaults 1 0"
        return

    def test_check_for_entry(self):
        # Check if an entry exists
        # We have not yet added fake_entry, so this should return False
        self.assertTrue(self.datamodel.hasEntry(self.fake_entry.device) is False)

    def test_add_entry(self):
        # Check if we can add an entry
        self.datamodel.addEntry(self.fake_entry)
        # now check if we have the entry there
        self.assertTrue(self.datamodel.hasEntry(self.fake_entry.device))

    def test_remove_entry(self):
        # Check if we can remove an entry
        self.datamodel.addEntry(self.fake_entry)
        self.assertTrue(self.datamodel.hasEntry(self.fake_entry.device))
        self.datamodel.removeEntry(self.fake_entry.device)
        self.assertTrue(self.datamodel.hasEntry(self.fake_entry.device) is False)

    def test_invalid_entry_argument(self):
        # Make sure it raises an exception if we give it a string
        self.assertRaises(AssertionError,
                          self.datamodel.addEntry, 'foo')

    def test_incomplete_entry_attributes(self):
        # make sure we raise an exception when the attributes of the entry are not complete
        entry = fstab.FstabEntry() # empty entry
        self.assertRaises(AssertionError,
                          self.datamodel.addEntry, entry)
        
    def tearDown(self):
        """ Remove the file when we're done """
        return os.remove(self.datapath)


if __name__ == "__main__":
    assert os.geteuid() == 0, "You must be root to run tests on /etc/fstab """
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.