[tests] Purge tests and start with a clean implementation of subprocess

Seems like subprocess and friends (Popen, communicate) are not so easy
to mock cleanly. Therefore, start from scratch and carefully write test
by test, until (at least) the old test coverage has been restored.
This commit is contained in:
Tobi-wan Kenobi 2017-03-04 11:25:52 +01:00
parent 1c6122fc3f
commit 6dbe440cb5
23 changed files with 61 additions and 1240 deletions

View file

@ -1,18 +1,27 @@
# pylint: disable=C0103,C0111
import mock
import unittest
import mocks
from bumblebee.util import *
class TestUtil(unittest.TestCase):
def setUp(self):
self.popen = mocks.MockPopen("bumblebee.util")
self.some_command_with_args = "sample-command -a -b -c"
self.some_utf8 = "some string".encode("utf-8")
def tearDown(self):
self.popen.cleanup()
def test_bytefmt(self):
value = 10
display = 10
units = [ "B", "KiB", "MiB", "GiB" ]
for unit in units:
self.assertEquals(bytefmt(value), "{:.2f}{}".format(display, unit))
value *= 1024
self.assertEquals(bytefmt(value), "{:.2f}GiB".format(display*1024))
self.assertEquals(bytefmt(10), "10.00B")
self.assertEquals(bytefmt(15*1024), "15.00KiB")
self.assertEquals(bytefmt(20*1024*1024), "20.00MiB")
self.assertEquals(bytefmt(22*1024*1024*1024), "22.00GiB")
self.assertEquals(bytefmt(35*1024*1024*1024*1024), "35840.00GiB")
def test_durationfmt(self):
self.assertEquals(durationfmt(00), "00:00")
@ -22,4 +31,20 @@ class TestUtil(unittest.TestCase):
self.assertEquals(durationfmt(3600), "01:00:00")
self.assertEquals(durationfmt(7265), "02:01:05")
def test_execute(self):
execute(self.some_command_with_args)
self.assertTrue(self.popen.mock.popen.called)
self.popen.mock.popen.assert_call(self.some_command_with_args)
self.assertTrue(self.popen.mock.communicate.called)
def test_execute_utf8(self):
self.popen.mock.communicate.return_value = [ self.some_utf8, None ]
self.test_execute()
def test_execute_error(self):
self.popen.mock.returncode = 1
with self.assertRaises(RuntimeError):
execute(self.some_command_with_args)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4