test_csvpillar.py 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. # -*- coding: utf-8 -*-
  2. '''test for pillar csvpillar.py'''
  3. # Import python libs
  4. from __future__ import absolute_import, print_function, unicode_literals
  5. # Import Salt Testing libs
  6. from tests.support.unit import TestCase, skipIf
  7. from tests.support.mock import patch, mock_open, NO_MOCK, NO_MOCK_REASON
  8. # Import Salt Libs
  9. import salt.pillar.csvpillar as csvpillar
  10. @skipIf(NO_MOCK, NO_MOCK_REASON)
  11. class CSVPillarTestCase(TestCase):
  12. def test_001_load_utf8_csv(self):
  13. fake_csv = "id,foo,bar\r\nminion1,foo1,bar1"
  14. fake_dict = {'id': 'minion1', 'foo': 'foo1', 'bar': 'bar1'}
  15. fopen_mock = mock_open(fake_csv)
  16. with patch('salt.utils.files.fopen', fopen_mock):
  17. result = csvpillar.ext_pillar(mid='minion1', pillar=None,
  18. path="/fake/path/file.csv", idkey="id", namespace=None)
  19. self.assertDictEqual(fake_dict, result)
  20. def test_002_load_utf8_csv_namespc(self):
  21. fake_csv = "id,foo,bar\r\nminion1,foo1,bar1"
  22. fake_dict = {'baz': {'id': 'minion1', 'foo': 'foo1', 'bar': 'bar1'}}
  23. fopen_mock = mock_open(fake_csv)
  24. with patch('salt.utils.files.fopen', fopen_mock):
  25. result = csvpillar.ext_pillar(mid='minion1', pillar=None,
  26. path="/fake/path/file.csv", idkey="id", namespace='baz')
  27. self.assertDictEqual(fake_dict, result)