1
0

test_seed.py 4.0 KB

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