test_lxc.py 3.1 KB

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