test_localfs.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.unit import TestCase
  11. from tests.support.helpers import with_tempdir
  12. from tests.support.mock import patch
  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 = {
  30. 'token_dir': tmpdir
  31. }
  32. fopen = CalledWith(salt.utils.files.fopen)
  33. rename = CalledWith(os.rename)
  34. with patch('salt.utils.files.fopen', fopen), patch('os.rename', rename):
  35. tdata = salt.tokens.localfs.mk_token(opts, {})
  36. assert 'token' in tdata
  37. t_path = os.path.join(tmpdir, tdata['token'])
  38. temp_t_path = '{}.tmp'.format(t_path)
  39. assert len(fopen.called_with) == 1, len(fopen.called_with)
  40. assert fopen.called_with == [
  41. ((temp_t_path, 'w+b'), {})
  42. ], fopen.called_with
  43. assert len(rename.called_with) == 1, len(rename.called_with)
  44. assert rename.called_with == [
  45. ((temp_t_path, t_path), {})
  46. ], rename.called_with
  47. class TestLocalFS(TestCase):
  48. def setUp(self):
  49. # Default expected data
  50. self.expected_data = {'this': 'is', 'some': 'token data'}
  51. @with_tempdir()
  52. def test_get_token_should_return_token_if_exists(self, tempdir):
  53. opts = {'token_dir': tempdir}
  54. tok = salt.tokens.localfs.mk_token(
  55. opts=opts,
  56. tdata=self.expected_data,
  57. )['token']
  58. actual_data = salt.tokens.localfs.get_token(opts=opts, tok=tok)
  59. self.assertDictEqual(self.expected_data, actual_data)
  60. @with_tempdir()
  61. def test_get_token_should_raise_SaltDeserializationError_if_token_file_is_empty(self, tempdir):
  62. opts = {'token_dir': tempdir}
  63. tok = salt.tokens.localfs.mk_token(
  64. opts=opts,
  65. tdata=self.expected_data,
  66. )['token']
  67. with salt.utils.files.fopen(os.path.join(tempdir, tok), 'w') as f:
  68. f.truncate()
  69. with self.assertRaises(salt.exceptions.SaltDeserializationError) as e:
  70. salt.tokens.localfs.get_token(opts=opts, tok=tok)
  71. @with_tempdir()
  72. def test_get_token_should_raise_SaltDeserializationError_if_token_file_is_malformed(self, tempdir):
  73. opts = {'token_dir': tempdir}
  74. tok = salt.tokens.localfs.mk_token(
  75. opts=opts,
  76. tdata=self.expected_data,
  77. )['token']
  78. with salt.utils.files.fopen(os.path.join(tempdir, tok), 'w') as f:
  79. f.truncate()
  80. f.write('this is not valid msgpack data')
  81. with self.assertRaises(salt.exceptions.SaltDeserializationError) as e:
  82. salt.tokens.localfs.get_token(opts=opts, tok=tok)