Skip to content

Commit

Permalink
Merge pull request #52747 from que5o/fix-csvpillar-read
Browse files Browse the repository at this point in the history
Fix csvpillar.py read file as binary
  • Loading branch information
waynew authored Apr 30, 2019
2 parents 657d4be + a0f83c2 commit 9f1b804
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion salt/pillar/csvpillar.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def ext_pillar(
:param list fieldnames: (Optional) if the first row of the CSV is not
column names they may be specified here instead.
'''
with salt.utils.files.fopen(path, 'rb') as f:
with salt.utils.files.fopen(path, 'r') as f:
sheet = csv.DictReader(f, fieldnames,
restkey=restkey, restval=restval, dialect=dialect)

Expand Down
33 changes: 33 additions & 0 deletions tests/unit/pillar/test_csvpillar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
'''test for pillar csvpillar.py'''

# Import python libs
from __future__ import absolute_import, print_function, unicode_literals

# Import Salt Testing libs
from tests.support.unit import TestCase, skipIf
from tests.support.mock import patch, mock_open, NO_MOCK, NO_MOCK_REASON

# Import Salt Libs
import salt.pillar.csvpillar as csvpillar


@skipIf(NO_MOCK, NO_MOCK_REASON)
class CSVPillarTestCase(TestCase):
def test_001_load_utf8_csv(self):
fake_csv = "id,foo,bar\r\nminion1,foo1,bar1"
fake_dict = {'id': 'minion1', 'foo': 'foo1', 'bar': 'bar1'}
fopen_mock = mock_open(fake_csv)
with patch('salt.utils.files.fopen', fopen_mock):
result = csvpillar.ext_pillar(mid='minion1', pillar=None,
path="/fake/path/file.csv", idkey="id", namespace=None)
self.assertDictEqual(fake_dict, result)

def test_002_load_utf8_csv_namespc(self):
fake_csv = "id,foo,bar\r\nminion1,foo1,bar1"
fake_dict = {'baz': {'id': 'minion1', 'foo': 'foo1', 'bar': 'bar1'}}
fopen_mock = mock_open(fake_csv)
with patch('salt.utils.files.fopen', fopen_mock):
result = csvpillar.ext_pillar(mid='minion1', pillar=None,
path="/fake/path/file.csv", idkey="id", namespace='baz')
self.assertDictEqual(fake_dict, result)

0 comments on commit 9f1b804

Please sign in to comment.