test_localfs.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests the localfs tokens interface.
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import os
  7. import salt.exceptions
  8. import salt.tokens.localfs
  9. import salt.utils.files
  10. from tests.support.helpers import with_tempdir
  11. from tests.support.mock import patch
  12. from tests.support.unit import TestCase
  13. class CalledWith(object):
  14. def __init__(self, func, called_with=None):
  15. self.func = func
  16. if called_with is None:
  17. self.called_with = []
  18. else:
  19. self.called_with = called_with
  20. def __call__(self, *args, **kwargs):
  21. self.called_with.append((args, kwargs))
  22. return self.func(*args, **kwargs)
  23. class WriteTokenTest(TestCase):
  24. @with_tempdir()
  25. def test_write_token(self, tmpdir):
  26. """
  27. Validate tokens put in place with an atomic move
  28. """
  29. opts = {"token_dir": tmpdir}
  30. fopen = CalledWith(salt.utils.files.fopen)
  31. rename = CalledWith(os.rename)
  32. with patch("salt.utils.files.fopen", fopen), patch("os.rename", rename):
  33. tdata = salt.tokens.localfs.mk_token(opts, {})
  34. assert "token" in tdata
  35. t_path = os.path.join(tmpdir, tdata["token"])
  36. temp_t_path = "{}.tmp".format(t_path)
  37. assert len(fopen.called_with) == 1, len(fopen.called_with)
  38. assert fopen.called_with == [((temp_t_path, "w+b"), {})], fopen.called_with
  39. assert len(rename.called_with) == 1, len(rename.called_with)
  40. assert rename.called_with == [((temp_t_path, t_path), {})], rename.called_with
  41. class TestLocalFS(TestCase):
  42. def setUp(self):
  43. # Default expected data
  44. self.expected_data = {"this": "is", "some": "token data"}
  45. @with_tempdir()
  46. def test_get_token_should_return_token_if_exists(self, tempdir):
  47. opts = {"token_dir": tempdir}
  48. tok = salt.tokens.localfs.mk_token(opts=opts, tdata=self.expected_data,)[
  49. "token"
  50. ]
  51. actual_data = salt.tokens.localfs.get_token(opts=opts, tok=tok)
  52. self.assertDictEqual(self.expected_data, actual_data)
  53. @with_tempdir()
  54. def test_get_token_should_raise_SaltDeserializationError_if_token_file_is_empty(
  55. self, tempdir
  56. ):
  57. opts = {"token_dir": tempdir}
  58. tok = salt.tokens.localfs.mk_token(opts=opts, tdata=self.expected_data,)[
  59. "token"
  60. ]
  61. with salt.utils.files.fopen(os.path.join(tempdir, tok), "w") as f:
  62. f.truncate()
  63. with self.assertRaises(salt.exceptions.SaltDeserializationError) as e:
  64. salt.tokens.localfs.get_token(opts=opts, tok=tok)
  65. @with_tempdir()
  66. def test_get_token_should_raise_SaltDeserializationError_if_token_file_is_malformed(
  67. self, tempdir
  68. ):
  69. opts = {"token_dir": tempdir}
  70. tok = salt.tokens.localfs.mk_token(opts=opts, tdata=self.expected_data,)[
  71. "token"
  72. ]
  73. with salt.utils.files.fopen(os.path.join(tempdir, tok), "w") as f:
  74. f.truncate()
  75. f.write("this is not valid msgpack data")
  76. with self.assertRaises(salt.exceptions.SaltDeserializationError) as e:
  77. salt.tokens.localfs.get_token(opts=opts, tok=tok)