test_sysrc.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import sys
  4. import pytest
  5. from tests.support.case import ModuleCase
  6. from tests.support.unit import skipIf
  7. class SysrcModuleTest(ModuleCase):
  8. def setUp(self):
  9. super(SysrcModuleTest, self).setUp()
  10. ret = self.run_function("cmd.has_exec", ["sysrc"])
  11. if not ret:
  12. self.skipTest("sysrc not found")
  13. @skipIf(not sys.platform.startswith("freebsd"), "FreeBSD specific")
  14. def test_show(self):
  15. ret = self.run_function("sysrc.get")
  16. self.assertIsInstance(
  17. ret, dict, "sysrc.get returned wrong type, expecting dictionary"
  18. )
  19. self.assertIn(
  20. "/etc/rc.conf", ret, "sysrc.get should have an rc.conf key in it."
  21. )
  22. @skipIf(not sys.platform.startswith("freebsd"), "FreeBSD specific")
  23. @pytest.mark.destructive_test
  24. def test_set(self):
  25. ret = self.run_function("sysrc.set", ["test_var", "1"])
  26. self.assertIsInstance(
  27. ret, dict, "sysrc.get returned wrong type, expecting dictionary"
  28. )
  29. self.assertIn(
  30. "/etc/rc.conf", ret, "sysrc.set should have an rc.conf key in it."
  31. )
  32. self.assertIn(
  33. "1",
  34. ret["/etc/rc.conf"]["test_var"],
  35. "sysrc.set should return the value it set.",
  36. )
  37. ret = self.run_function("sysrc.remove", ["test_var"])
  38. self.assertEqual("test_var removed", ret)
  39. @skipIf(not sys.platform.startswith("freebsd"), "FreeBSD specific")
  40. @pytest.mark.destructive_test
  41. def test_set_bool(self):
  42. ret = self.run_function("sysrc.set", ["test_var", True])
  43. self.assertIsInstance(
  44. ret, dict, "sysrc.get returned wrong type, expecting dictionary"
  45. )
  46. self.assertIn(
  47. "/etc/rc.conf", ret, "sysrc.set should have an rc.conf key in it."
  48. )
  49. self.assertIn(
  50. "YES",
  51. ret["/etc/rc.conf"]["test_var"],
  52. "sysrc.set should return the value it set.",
  53. )
  54. ret = self.run_function("sysrc.remove", ["test_var"])
  55. self.assertEqual("test_var removed", ret)