issue #339: minimal tests for lxc/lxd modules.

This commit is contained in:
David Wilson 2018-08-10 20:18:41 +01:00
parent 2c2fc73b0a
commit 6f524d3ff8
5 changed files with 70 additions and 1 deletions

View File

@ -52,7 +52,7 @@ class Stream(mitogen.parent.Stream):
super(Stream, self).construct(**kwargs)
self.container = container
if lxc_attach_path:
self.lxc_attach_path = lxc_attach_apth
self.lxc_attach_path = lxc_attach_path
def connect(self):
super(Stream, self).connect()

7
tests/data/fake_lxc.py Executable file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env python
import sys
import os
os.environ['ORIGINAL_ARGV'] = repr(sys.argv)
os.execv(sys.executable, sys.argv[sys.argv.index('--') + 1:])

7
tests/data/fake_lxc_attach.py Executable file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env python
import sys
import os
os.environ['ORIGINAL_ARGV'] = repr(sys.argv)
os.execv(sys.executable, sys.argv[sys.argv.index('--') + 1:])

29
tests/lxc_test.py Normal file
View File

@ -0,0 +1,29 @@
import os
import mitogen
import unittest2
import testlib
def has_subseq(seq, subseq):
return any(seq[x:x+len(subseq)] == subseq for x in range(0, len(seq)))
class FakeLxcAttachTest(testlib.RouterMixin, unittest2.TestCase):
def test_okay(self):
lxc_attach_path = testlib.data_path('fake_lxc_attach.py')
context = self.router.lxc(
container='container_name',
lxc_attach_path=lxc_attach_path,
)
argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
self.assertEquals(argv[0], lxc_attach_path)
self.assertTrue('--clear-env' in argv)
self.assertTrue(has_subseq(argv, ['--name', 'container_name']))
if __name__ == '__main__':
unittest2.main()

26
tests/lxd_test.py Normal file
View File

@ -0,0 +1,26 @@
import os
import mitogen
import unittest2
import testlib
class FakeLxcTest(testlib.RouterMixin, unittest2.TestCase):
def test_okay(self):
lxc_path = testlib.data_path('fake_lxc.py')
context = self.router.lxd(
container='container_name',
lxc_path=lxc_path,
)
argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
self.assertEquals(argv[0], lxc_path)
self.assertEquals(argv[1], 'exec')
self.assertEquals(argv[2], '--force-noninteractive')
self.assertEquals(argv[3], 'container_name')
if __name__ == '__main__':
unittest2.main()