test_seed.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import shutil
  9. import uuid
  10. import pytest
  11. import salt.modules.seed as seed
  12. # Import Salt Libs
  13. import salt.utils.files
  14. import salt.utils.odict
  15. # Import Salt Testing Libs
  16. from tests.support.mixins import LoaderModuleMockMixin
  17. from tests.support.mock import MagicMock, patch
  18. from tests.support.unit import TestCase
  19. class SeedTestCase(TestCase, LoaderModuleMockMixin):
  20. """
  21. Test cases for salt.modules.seed
  22. """
  23. def setup_loader_modules(self):
  24. return {seed: {}}
  25. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  26. def test_mkconfig_odict(self):
  27. with patch.dict(seed.__opts__, {"master": "foo"}):
  28. ddd = salt.utils.odict.OrderedDict()
  29. ddd["b"] = "b"
  30. ddd["a"] = "b"
  31. data = seed.mkconfig(ddd, approve_key=False)
  32. with salt.utils.files.fopen(data["config"]) as fic:
  33. fdata = fic.read()
  34. self.assertEqual(fdata, "b: b\na: b\nmaster: foo\n")
  35. def test_prep_bootstrap(self):
  36. """
  37. Test to update and get the random script to a random place
  38. """
  39. with patch.dict(
  40. seed.__salt__,
  41. {
  42. "config.gather_bootstrap_script": MagicMock(
  43. return_value=os.path.join("BS_PATH", "BS")
  44. )
  45. },
  46. ), patch.object(uuid, "uuid4", return_value="UUID"), patch.object(
  47. os.path, "exists", return_value=True
  48. ), patch.object(
  49. os, "chmod", return_value=None
  50. ), patch.object(
  51. shutil, "copy", return_value=None
  52. ):
  53. expect = (
  54. os.path.join("MPT", "tmp", "UUID", "BS"),
  55. os.sep + os.path.join("tmp", "UUID"),
  56. )
  57. self.assertEqual(seed.prep_bootstrap("MPT"), expect)
  58. expect = (
  59. os.sep + os.path.join("MPT", "tmp", "UUID", "BS"),
  60. os.sep + os.path.join("tmp", "UUID"),
  61. )
  62. self.assertEqual(seed.prep_bootstrap(os.sep + "MPT"), expect)
  63. def test_apply_(self):
  64. """
  65. Test to seed a location (disk image, directory, or block device)
  66. with the minion config, approve the minion's key, and/or install
  67. salt-minion.
  68. """
  69. mock = MagicMock(
  70. side_effect=[
  71. False,
  72. {"type": "type", "target": "target"},
  73. {"type": "type", "target": "target"},
  74. {"type": "type", "target": "target"},
  75. ]
  76. )
  77. with patch.dict(seed.__salt__, {"file.stats": mock}):
  78. self.assertEqual(seed.apply_("path"), "path does not exist")
  79. with patch.object(seed, "_mount", return_value=False):
  80. self.assertEqual(seed.apply_("path"), "target could not be mounted")
  81. with patch.object(seed, "_mount", return_value="/mountpoint"):
  82. with patch.object(os.path, "join", return_value="A"):
  83. with patch.object(
  84. os, "makedirs", MagicMock(side_effect=OSError("f"))
  85. ):
  86. with patch.object(os.path, "isdir", return_value=False):
  87. self.assertRaises(OSError, seed.apply_, "p")
  88. with patch.object(os, "makedirs", MagicMock()):
  89. with patch.object(seed, "mkconfig", return_value="A"):
  90. with patch.object(
  91. seed, "_check_install", return_value=False
  92. ):
  93. with patch.object(
  94. seed, "_umount", return_value=None
  95. ) as umount_mock:
  96. self.assertFalse(seed.apply_("path", install=False))
  97. umount_mock.assert_called_once_with(
  98. "/mountpoint", "target", "type"
  99. )