test_localfs.py 3.3 KB

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