Skip to content

Commit

Permalink
Do not break repo files with multiple line values on yumpkg (bsc#1135…
Browse files Browse the repository at this point in the history
…360)
  • Loading branch information
meaksh committed Jun 5, 2019
1 parent aa9df9a commit b99e55a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
16 changes: 12 additions & 4 deletions salt/modules/yumpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2763,7 +2763,12 @@ def del_repo(repo, basedir=None, **kwargs): # pylint: disable=W0613
del filerepos[stanza]['comments']
content += '\n[{0}]'.format(stanza)
for line in filerepos[stanza]:
content += '\n{0}={1}'.format(line, filerepos[stanza][line])
# A whitespace is needed at the begining of the new line in order
# to avoid breaking multiple line values allowed on repo files.
value = filerepos[stanza][line]
if isinstance(value, six.string_types) and '\n' in value:
value = '\n '.join(value.split('\n'))
content += '\n{0}={1}'.format(line, value)
content += '\n{0}\n'.format(comments)

with salt.utils.files.fopen(repofile, 'w') as fileout:
Expand Down Expand Up @@ -2898,11 +2903,14 @@ def mod_repo(repo, basedir=None, **kwargs):
)
content += '[{0}]\n'.format(stanza)
for line in six.iterkeys(filerepos[stanza]):
# A whitespace is needed at the begining of the new line in order
# to avoid breaking multiple line values allowed on repo files.
value = filerepos[stanza][line]
if isinstance(value, six.string_types) and '\n' in value:
value = '\n '.join(value.split('\n'))
content += '{0}={1}\n'.format(
line,
filerepos[stanza][line]
if not isinstance(filerepos[stanza][line], bool)
else _bool_to_str(filerepos[stanza][line])
value if not isinstance(value, bool) else _bool_to_str(value)
)
content += comments + '\n'

Expand Down
48 changes: 48 additions & 0 deletions tests/integration/modules/test_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,54 @@ def test_mod_del_repo(self):
if repo is not None:
self.run_function('pkg.del_repo', [repo])

def test_mod_del_repo_multiline_values(self):
'''
test modifying and deleting a software repository defined with multiline values
'''
os_grain = self.run_function('grains.item', ['os'])['os']
repo = None
try:
if os_grain in ['CentOS', 'RedHat', 'SUSE']:
my_baseurl = 'http://my.fake.repo/foo/bar/\n http://my.fake.repo.alt/foo/bar/'
expected_get_repo_baseurl = 'http://my.fake.repo/foo/bar/\nhttp://my.fake.repo.alt/foo/bar/'
major_release = int(
self.run_function(
'grains.item',
['osmajorrelease']
)['osmajorrelease']
)
repo = 'fakerepo'
name = 'Fake repo for RHEL/CentOS/SUSE'
baseurl = my_baseurl
gpgkey = 'https://my.fake.repo/foo/bar/MY-GPG-KEY.pub'
failovermethod = 'priority'
gpgcheck = 1
enabled = 1
ret = self.run_function(
'pkg.mod_repo',
[repo],
name=name,
baseurl=baseurl,
gpgkey=gpgkey,
gpgcheck=gpgcheck,
enabled=enabled,
failovermethod=failovermethod,
)
# return data from pkg.mod_repo contains the file modified at
# the top level, so use next(iter(ret)) to get that key
self.assertNotEqual(ret, {})
repo_info = ret[next(iter(ret))]
self.assertIn(repo, repo_info)
self.assertEqual(repo_info[repo]['baseurl'], my_baseurl)
ret = self.run_function('pkg.get_repo', [repo])
self.assertEqual(ret['baseurl'], expected_get_repo_baseurl)
self.run_function('pkg.mod_repo', [repo])
ret = self.run_function('pkg.get_repo', [repo])
self.assertEqual(ret['baseurl'], expected_get_repo_baseurl)
finally:
if repo is not None:
self.run_function('pkg.del_repo', [repo])

@requires_salt_modules('pkg.owner')
def test_owner(self):
'''
Expand Down

0 comments on commit b99e55a

Please sign in to comment.