1
0

test_lxc.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. @pytest.mark.windows_whitelisted
  19. class LXCModuleTest(ModuleCase):
  20. '''
  21. Test the lxc module
  22. '''
  23. prefix = '_salttesting'
  24. def setUp(self):
  25. os = self.run_function('grains.item',
  26. ['os', 'oscodename', 'osarch'])
  27. p = {'download':
  28. {'dist': os['os'].lower(),
  29. 'arch': os['osarch'].lower(),
  30. 'template': 'download',
  31. 'release': os['oscodename'].lower()},
  32. 'sshd': {'template': 'sshd'}}
  33. self.run_function('grains.setval', ['lxc.profile', p])
  34. def tearDown(self):
  35. '''
  36. Clean up any LXCs created.
  37. '''
  38. r = self.run_function('lxc.list')
  39. for k, v in six.iteritems(r):
  40. for x in v:
  41. if x.startswith(self.prefix):
  42. self.run_function('lxc.destroy', [x])
  43. def test_create_destroy(self):
  44. '''
  45. Test basic create/destroy of an LXC.
  46. '''
  47. r = self.run_function('lxc.create', [self.prefix],
  48. template='sshd')
  49. self.assertEqual(r, {'state': {'new': 'stopped', 'old': None},
  50. 'result': True})
  51. self.assertTrue(self.run_function('lxc.exists', [self.prefix]))
  52. r = self.run_function('lxc.destroy', [self.prefix])
  53. self.assertEqual(r, {'state': None, 'change': True})
  54. self.assertFalse(self.run_function('lxc.exists', [self.prefix]))
  55. def test_init(self):
  56. '''
  57. Test basic init functionality.
  58. '''
  59. r = self.run_function('lxc.init', [self.prefix],
  60. profile='sshd', seed=False)
  61. self.assertTrue(r.get('created', False))
  62. self.assertTrue(self.run_function('lxc.exists', [self.prefix]))
  63. def test_macvlan(self):
  64. '''
  65. Regression test for macvlan nic profile.
  66. '''
  67. p = {"macvlan": {"eth0": {
  68. "macvlan.mode": "bridge",
  69. "link": "eth0",
  70. "type": "macvlan"}}}
  71. self.run_function('grains.setval', ['lxc.nic', p])
  72. self.run_function('lxc.init', [self.prefix],
  73. profile='sshd', nic='macvlan',
  74. seed=False, start=False)
  75. f = '/var/lib/lxc/{0}/config'.format(self.prefix)
  76. conf = self.run_function('lxc.read_conf', [f])
  77. # Due to a segfault in lxc-destroy caused by invalid configs,
  78. # truncate the config.
  79. self.run_function('cmd.run', ['truncate -s 0 {0}'.format(f)])
  80. self.assertEqual(conf.get('lxc.network.type'), 'macvlan')