1
0

test_s3.py 3.3 KB

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