test_s3.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import logging
  5. # Import Salt Testing libs
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.unit import TestCase
  8. from tests.support.mock import patch, mock_open, MagicMock
  9. # Import Salt Libs
  10. import salt.pillar.s3 as s3_pillar
  11. from salt.ext.six.moves import range
  12. log = logging.getLogger(__name__)
  13. class S3PillarTestCase(TestCase, LoaderModuleMockMixin):
  14. '''
  15. TestCase for salt.pillar.s3
  16. '''
  17. def setup_loader_modules(self):
  18. s3_pillar_globals = {
  19. '__utils__': {}
  20. }
  21. return {s3_pillar: s3_pillar_globals}
  22. def test_refresh_buckets_cache_file(self):
  23. '''
  24. Test pagination with refresh_buckets_cache_file
  25. '''
  26. key = 'XXXXXXXXXXXXXXXXXXXXX'
  27. keyid = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  28. bucket = 'dummy_bucket'
  29. service_url = 's3.amazonaws.com'
  30. cache_file = 'dummy_file'
  31. s3_creds = s3_pillar.S3Credentials(key, keyid, bucket, service_url)
  32. mock_return_first = [{'Name': 'pillar-bucket'},
  33. {'Prefix': 'test'},
  34. {'KeyCount': '10'},
  35. {'MaxKeys': '10'},
  36. {'NextContinuationToken': 'XXXXX'},
  37. {'IsTruncated': 'true'}]
  38. mock_return_second = [{'Name': 'pillar-bucket'},
  39. {'Prefix': 'test'},
  40. {'KeyCount': '10'},
  41. {'MaxKeys': '10'},
  42. {'IsTruncated': 'true'}]
  43. first_range_end = 999
  44. second_range_end = 1200
  45. for i in range(0, first_range_end):
  46. key_name = '{0}/init.sls'.format(i)
  47. tmp = {'Key': key_name,
  48. 'LastModified': '2019-12-18T15:54:39.000Z',
  49. 'ETag': '"fba0a053704e8b357c94be90b44bb640"',
  50. 'Size': '5 ',
  51. 'StorageClass': 'STANDARD'}
  52. mock_return_first.append(tmp)
  53. for i in range(first_range_end, second_range_end):
  54. key_name = '{0}/init.sls'.format(i)
  55. tmp = {'Key': key_name,
  56. 'LastModified': '2019-12-18T15:54:39.000Z',
  57. 'ETag': '"fba0a053704e8b357c94be90b44bb640"',
  58. 'Size': '5 ',
  59. 'StorageClass': 'STANDARD'}
  60. mock_return_second.append(tmp)
  61. _expected = {'base': {'dummy_bucket': []}}
  62. for i in range(0, second_range_end):
  63. key_name = '{0}/init.sls'.format(i)
  64. tmp = {'Key': key_name,
  65. 'LastModified': '2019-12-18T15:54:39.000Z',
  66. 'ETag': '"fba0a053704e8b357c94be90b44bb640"',
  67. 'Size': '5 ',
  68. 'StorageClass': 'STANDARD'}
  69. _expected['base']['dummy_bucket'].append(tmp)
  70. mock_s3_query = MagicMock(side_effect=[mock_return_first, mock_return_second])
  71. with patch.dict(s3_pillar.__utils__, {'s3.query': mock_s3_query}):
  72. with patch('salt.utils.files.fopen', mock_open(read_data=b'')):
  73. ret = s3_pillar._refresh_buckets_cache_file(s3_creds,
  74. cache_file,
  75. False,
  76. 'base',
  77. '')
  78. self.assertEqual(ret, _expected)