Source

vasm / src / tests / test_NETWORKING.py

Full commit
#!/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/>.

""" test_NETWORKING.py
Test suite for the networking module"""
__author__ = "Moises Henriquez"
__author_email__ = "moc.liamg@xnl.E0M"[::-1]

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 NETWORKING

class NetworkingTests(unittest.TestCase):
    def setUp(self):
        self.addCleanup(self._cleanup)
        self.assertTrue(hasattr(NETWORKING, 'NetConfModel'))
        self.hostname = 'foo.host.vnet'
        self.host_file = '/tmp/hostnamefile'
        self.model = NETWORKING.NetConfModel(host_file=self.host_file)
    
    def tearDown(self):
        return self._cleanup()
    
    def _cleanup(self):
        if os.path.exists(self.host_file):
            os.remove(self.host_file)
    
    def testCreateNic(self):
        # Create a fake nic object
        mynic = NETWORKING.Nic('wlan10')
        # The config file should not exist
        self.assertTrue(not os.path.exists(mynic.inet.fpath))
        self.assertEqual(mynic.inet.config['DEVICE'], mynic.devname)
        # Disabling this nic should raise an exception because the
        # config file does not exist
        self.assertRaises(AssertionError, mynic.disable)
    
    def testHostnameWriteRead(self):
        # writing an empty hostname raise exception
        self.model.hostname = ""
        self.assertRaises(AssertionError, self.model.set_hostname)
        self.model.hostname = None
        self.assertRaises(AssertionError, self.model.set_hostname)
        # Passing an argument to set_hostname will raise TypeError
        self.assertRaises(TypeError, self.model.set_hostname, 'foo')
        
        # write the hostname
        self.model.hostname = self.hostname
        self.model.set_hostname()
        readname = self.model.get_hostname()
        return self.assertEqual(readname, self.hostname)


if __name__ == '__main__':
    assert os.geteuid() == 0, "Only root is allowed to run these tests."""
    unittest.main()