From 3c58b6cc1076660b90c45724109e4a7d386dfe55 Mon Sep 17 00:00:00 2001 From: Iblis Lin Date: Fri, 4 Sep 2015 18:38:16 +0800 Subject: [PATCH] [#20] Start working on testing FreeBSDUtils with pytest Test case for FreeBSDUtils.reboot --- cloudbaseinit/osutils/freebsd.py | 14 ++++++------- cloudbaseinit/tests/osutils/test_freebsd.py | 22 +++++++++++++++++++++ test-requirements.txt | 1 + 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 cloudbaseinit/tests/osutils/test_freebsd.py diff --git a/cloudbaseinit/osutils/freebsd.py b/cloudbaseinit/osutils/freebsd.py index 3ac10e1..49fe91b 100644 --- a/cloudbaseinit/osutils/freebsd.py +++ b/cloudbaseinit/osutils/freebsd.py @@ -1,15 +1,15 @@ -from cloudbaseinit.osutils import base - -from subprocess import CalledProcessError -import subprocess import datetime +import subprocess import os -import os.path + +from subprocess import CalledProcessError +from cloudbaseinit.osutils import base + class FreeBSDUtils(base.BaseOSUtils): def reboot(self): - if ( os.system('reboot') != 0 ): - raise Exception('Reboot failed') + if os.system('reboot') != 0: + raise OSError('Reboot failed') def user_exists(self, username): try: diff --git a/cloudbaseinit/tests/osutils/test_freebsd.py b/cloudbaseinit/tests/osutils/test_freebsd.py new file mode 100644 index 0000000..e34b950 --- /dev/null +++ b/cloudbaseinit/tests/osutils/test_freebsd.py @@ -0,0 +1,22 @@ +import unittest + +try: + import unittest.mock as mock +except ImportError: + import mock + +from cloudbaseinit.osutils.freebsd import FreeBSDUtils +from pytest import raises + + +class TestFreeBSDUtils(): + def setup_method(self, method): + self.bsd = FreeBSDUtils() + + def teardown_method(self, method): + del self.bsd + + @mock.patch('cloudbaseinit.osutils.freebsd.os.system', return_value=1) + def test_reboot(self, system): + with raises(OSError): + self.bsd.reboot() diff --git a/test-requirements.txt b/test-requirements.txt index 7d16825..8456cbe 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -6,3 +6,4 @@ sphinx>=1.1.2,<1.1.999 oslosphinx testtools>=0.9.32 testrepository>=0.0.18 +pytest