test_lxc.py 3.2 KB

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