Source

vasm / src / tests / test_dateset.py


#!/usr/bin/env python
# -*- encoding: utf-8 -*-

#    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/>.


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 dateset

class ClockTest(unittest.TestCase):
    """ Test the functionality to set the hardware clock """
    def setUp(self):
        self.clock = dateset.HWClock()
        super(ClockTest, self).setUp()
        self.dummyvalue = "localtime"

    def test_setClock(self):
        # This will except if something goes wrong
        self.clock.set_value(self.dummyvalue)
        gotval = self.clock.get_value()
        return self.assertEqual(gotval, self.dummyvalue)

    def test_invalid_values(self):
        self.assertRaises(AssertionError,
                          self.clock.set_value, "foo")
        self.assertRaises(AssertionError,
                          self.clock.set_value, "")
        

class TimeZoneTest(unittest.TestCase):
    """ Test timezone setting and getting """
    def setUp(self):
        self.zone = dateset.TimeZone()
        super(TimeZoneTest, self).setUp()
        self.zonesbase = "/usr/share/zoneinfo/"
        self.tz = "US/Eastern"

    def test_setEmtyZone(self):
        self.assertRaises(AssertionError,
                          self.zone.set_zone, "")
        return self.assertRaises(AssertionError,
                                 self.zone.set_zone, None)

    def test_setzone(self):
        ret = self.zone.set_zone(self.tz)
        return self.assertTrue(ret)

    def test_getzone(self):
        # set the zone.
        self.assertTrue(self.zone.set_zone(self.tz))
        zone = self.zone.get_zone()
        return self.assertEqual(zone, self.tz)

    def test_verify_with_filesystem(self):
        # we must set it again
        self.assertTrue(self.zone.set_zone(self.tz))
        rlink = os.readlink("/etc/localtime-copied-from")
        return self.assertEqual(rlink, os.path.join(self.zonesbase, self.tz))


class DateTimeTest(unittest.TestCase):
    """ Test the functionality to set and get the system time """

    def setUp(self):
        super(DateTimeTest, self).setUp()
        self.date_time = dateset.DateTime()

        # save current time for restoring it later
        self.now = self.date_time._date_time
        self.date = self.now.year, self.now.month, self.now.day
        self.time = self.now.hour, self.now.minute, self.now.second

    def tearDown(self):
        super(DateTimeTest, self).tearDown()
        dt = self.date_time
        dt.year, dt.month, dt.day = self.date
        dt.hour, dt.minute, dt.second = self.time
        self.date_time.set_date_and_time()

    def testSetDate(self):

        # test day and month < 10
        self.date_time.year += 1
        self.date_time.month = 2
        self.date_time.day = 2
        self.date_time.set_date()
        current_date = dateset.DateTime()
        self.assertEqual(current_date.year, self.date[0] + 1)
        self.assertEqual(self.date_time.year, current_date.year)
        self.assertEqual(self.date_time.month, current_date.month)
        self.assertEqual(self.date_time.day, current_date.day)

        # test day and month > 10
        self.date_time.month = 12
        self.date_time.day = 24
        self.date_time.set_date()
        current_date = dateset.DateTime()
        self.assertEqual(current_date.year, self.date[0] + 1)
        self.assertEqual(self.date_time.year, current_date.year)
        self.assertEqual(self.date_time.month, current_date.month)
        self.assertEqual(self.date_time.day, current_date.day)

    def testSetTime(self):
 
        self.date_time.hour += 1
        self.date_time.minute += 1
        self.date_time.second = 0
        self.date_time.set_time()
        current_time = dateset.DateTime()
        self.assertEqual(current_time.hour, self.time[0] + 1)
        self.assertEqual(current_time.minute, self.time[1] + 1)
        self.assertEqual(self.date_time.hour, current_time.hour)
        self.assertEqual(self.date_time.minute, current_time.minute)
        self.assertAlmostEqual(self.date_time.second, current_time.second)


if __name__ == "__main__":
    assert os.getuid() == 0, "You need to be root for running DateTime tests"
    unittest.main()