Skip to content

Commit

Permalink
CP-32998 - Use cgclassify to move tapdisk into pre configured vm.slic…
Browse files Browse the repository at this point in the history
…e and vm-blktap.slice

Signed-off-by: ben.sims@citrix.com <ben.sims@citrix.com>
  • Loading branch information
BenSimsCitrix committed Jul 24, 2020
1 parent d65dc85 commit 0e0d2fb
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
20 changes: 18 additions & 2 deletions drivers/blktap2.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,22 @@ def launch_from_arg(cls, arg):
arg = cls.Arg.parse(arg)
return cls.launch(arg.path, arg.type, False)

@classmethod
def cgclassify(cls, pid):

# We dont provide any <controllers>:<path>
# so cgclassify uses /etc/cgrules.conf which
# we have configured in the spec file.
cmd = ["cgclassify", str(pid)]
try:
util.pread2(cmd)
except util.CommandException as e:
util.logException(e)

@classmethod
def spawn(cls):
return TapCtl.spawn()

@classmethod
def launch_on_tap(cls, blktap, path, _type, options):

Expand All @@ -775,8 +791,8 @@ def launch_on_tap(cls, blktap, path, _type, options):
minor = blktap.minor

try:
pid = TapCtl.spawn()

pid = cls.spawn()
cls.cgclassify(pid)
try:
TapCtl.attach(pid, minor)

Expand Down
59 changes: 59 additions & 0 deletions tests/test_blktap2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,65 @@
import testlib
import util

class BogusException(Exception):
pass

class TestTapdisk(unittest.TestCase):

#
# There is a bug in python mocking that prevents @Classmethods being mocked
# hence no usual decorator mocks and the monkey patching.
# https://bugs.python.org/issue23078
#

@mock.patch('blktap2.util.pread2', autospec=True)
def test_cgclassify_normal_call(self, mock_pread2):
blktap2.Tapdisk.cgclassify(123)
mock_pread2.assert_called_with(['cgclassify', '123'])

@mock.patch('blktap2.util.pread2', autospec=True)
@mock.patch('blktap2.util.logException', autospec=True)
def test_cgclassify_exception_swallow(self, mock_log, mock_pread2):
mock_pread2.side_effect = util.CommandException(999)
blktap2.Tapdisk.cgclassify(123)
mock_pread2.assert_called_with(['cgclassify', '123'])
self.assertEquals(mock_log.call_count, 1)

def test_cgclassify_called_by_launch_on_tap(self):
blktap = mock.MagicMock()
blktap.minor = 2

# Record old functions
spawn_old = blktap2.Tapdisk.spawn
cgclassify_old = blktap2.Tapdisk.cgclassify
find_by_path_old = blktap2.Tapdisk.find_by_path

# Begin monkey patching.
blktap2.Tapdisk.spawn = mock.MagicMock()
blktap2.Tapdisk.spawn.return_value = 123

# Raise an exception just so we dont have to bother mocking out the
# rest of the function.
blktap2.Tapdisk.cgclassify = mock.MagicMock()
blktap2.Tapdisk.cgclassify.side_effect = BogusException

blktap2.Tapdisk.find_by_path = mock.MagicMock()
blktap2.Tapdisk.find_by_path.return_value = None

with self.assertRaises(BogusException) as cf:
tap = blktap2.Tapdisk.launch_on_tap(blktap,
"not used",
"not used",
"not used")

blktap2.Tapdisk.cgclassify.assert_called_with(123)

# Restor old functions.
blktap2.Tapdisk.spawn = spawn_old
blktap2.Tapdisk.cgclassify = cgclassify_old
blktap2.Tapdisk.find_by_path = find_by_path_old


class TestVDI(unittest.TestCase):
# This can't use autospec as vdi is created in __init__
# See https://docs.python.org/3/library/unittest.mock.html#autospeccing
Expand Down

0 comments on commit 0e0d2fb

Please sign in to comment.