test_lxc.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Test the lxc module
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing libs
  8. from tests.support.case import ModuleCase
  9. from tests.support.unit import skipIf
  10. # Import 3rd-party libs
  11. import pytest
  12. from salt.ext import six
  13. @skipIf(True,
  14. 'Needs rewrite to be more distro agnostic. Also, the tearDown '
  15. 'function destroys ALL containers on the box, which is BAD.')
  16. @pytest.mark.skip_if_not_root
  17. @pytest.mark.skip_if_binaries_missing('lxc-start', message='LXC is not installed or minimal version not met')
  18. class LXCModuleTest(ModuleCase):
  19. '''
  20. Test the lxc module
  21. '''
  22. prefix = '_salttesting'
  23. def setUp(self):
  24. os = self.run_function('grains.item',
  25. ['os', 'oscodename', 'osarch'])
  26. p = {'download':
  27. {'dist': os['os'].lower(),
  28. 'arch': os['osarch'].lower(),
  29. 'template': 'download',
  30. 'release': os['oscodename'].lower()},
  31. 'sshd': {'template': 'sshd'}}
  32. self.run_function('grains.setval', ['lxc.profile', p])
  33. def tearDown(self):
  34. '''
  35. Clean up any LXCs created.
  36. '''
  37. r = self.run_function('lxc.list')
  38. for k, v in six.iteritems(r):
  39. for x in v:
  40. if x.startswith(self.prefix):
  41. self.run_function('lxc.destroy', [x])
  42. def test_create_destroy(self):
  43. '''
  44. Test basic create/destroy of an LXC.
  45. '''
  46. r = self.run_function('lxc.create', [self.prefix],
  47. template='sshd')
  48. self.assertEqual(r, {'state': {'new': 'stopped', 'old': None},
  49. 'result': True})
  50. self.assertTrue(self.run_function('lxc.exists', [self.prefix]))
  51. r = self.run_function('lxc.destroy', [self.prefix])
  52. self.assertEqual(r, {'state': None, 'change': True})
  53. self.assertFalse(self.run_function('lxc.exists', [self.prefix]))
  54. def test_init(self):
  55. '''
  56. Test basic init functionality.
  57. '''
  58. r = self.run_function('lxc.init', [self.prefix],
  59. profile='sshd', seed=False)
  60. self.assertTrue(r.get('created', False))
  61. self.assertTrue(self.run_function('lxc.exists', [self.prefix]))
  62. def test_macvlan(self):
  63. '''
  64. Regression test for macvlan nic profile.
  65. '''
  66. p = {"macvlan": {"eth0": {
  67. "macvlan.mode": "bridge",
  68. "link": "eth0",
  69. "type": "macvlan"}}}
  70. self.run_function('grains.setval', ['lxc.nic', p])
  71. self.run_function('lxc.init', [self.prefix],
  72. profile='sshd', nic='macvlan',
  73. seed=False, start=False)
  74. f = '/var/lib/lxc/{0}/config'.format(self.prefix)
  75. conf = self.run_function('lxc.read_conf', [f])
  76. # Due to a segfault in lxc-destroy caused by invalid configs,
  77. # truncate the config.
  78. self.run_function('cmd.run', ['truncate -s 0 {0}'.format(f)])
  79. self.assertEqual(conf.get('lxc.network.type'), 'macvlan')