Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Do not break repo files with multiple line values on "yumpkg" #55584

16 changes: 12 additions & 4 deletions salt/modules/yumpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2733,7 +2733,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 @@ -2868,11 +2873,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 @@ -134,6 +134,54 @@ def test_mod_del_repo(self, grains):
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']:
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