Source

vasm / src / tests / test_services.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)
from backend import services as services

class ServicesTests(unittest.TestCase):
    def setUp(self):
        self.datamodel = services.ServiceModel()
        self.fake_service = self.datamodel.list_all_services()[0]
        self.fake_runlevel = 4

    def test_EnableService(self):
        code, output = self.datamodel.enable_service(self.fake_service,
                                                     self.fake_runlevel)
        # subprocess will return > 0 if this fails
        self.assertEqual(code, 0)
        # this return True when service is enabled, False otherwise
        # should be enabled now
        val = self.datamodel.get_service_runlevel_status(self.fake_service,
                                                         self.fake_runlevel)
        self.assertTrue(val)

    def test_DisableService(self):
        code, output = self.datamodel.disable_service(self.fake_service,
                                                      self.fake_runlevel)
        # Subprocess will return > 0 if this fails
        self.assertEqual(code, 0)
        # Check the status of this service now.  Should be disabled
        val = self.datamodel.get_service_runlevel_status(self.fake_service,
                                                         self.fake_runlevel)
        self.assertEqual(val, False)

    def test_InvalidRunlevel_enable(self):
        self.assertRaises(AssertionError,
                          self.datamodel.enable_service,
                          self.fake_service, 0)
        self.assertRaises(AssertionError,
                          self.datamodel.enable_service,
            self.fake_service, 6)

    def test_InvalidService_enable(self):
        """ Try to enable an unknown service."""
        return self.assertRaises(AssertionError,
                          self.datamodel.enable_service,
            "foo", 2)

    def test_InvalidRunlevel_disable(self):
        """ Try to disable services for an invalid runlevel """
        self.assertRaises(AssertionError,
                          self.datamodel.disable_service,
            self.fake_service, 0)
        self.assertRaises(AssertionError,
                          self.datamodel.disable_service,
            self.fake_service, 6)
        return

    def test_InvalidService_disable(self):
        """ Try to disable an invalid service """
        return self.assertRaises(AssertionError,
                                 self.datamodel.disable_service,
            "foo", 2)
        

if __name__ == '__main__':
    assert os.geteuid() == 0, "You must be root to run tests on system services"
    unittest.main()