Merge "policy: Quote single quotes in yaml format"

This commit is contained in:
Zuul 2022-03-30 12:24:33 +00:00 committed by Gerrit Code Review
commit 1af71b2255
2 changed files with 35 additions and 1 deletions

View File

@ -83,9 +83,15 @@ define openstacklib::policy::base (
~> Augeas<| title == "${file_path}-${key}-${value}" |>
}
'yaml': {
# NOTE(tkajianm): Currently we use single quotes('') to quote the whole
# value, thus a single quote in value should be escaped
# by another single quote (which results in '')
# NOTE(tkajinam): Replace '' by ' first in case ' is already escaped
$value_real = regsubst(regsubst($value, '\'\'', '\'', 'G'), '\'', '\'\'', 'G')
file_line { "${file_path}-${key}" :
path => $file_path,
line => "'${key}': '${value}'",
line => "'${key}': '${value_real}'",
match => "^['\"]?${key}['\"]?\\s*:.+"
}
Openstacklib::Policy::Default<| title == $file_path |>

View File

@ -74,6 +74,34 @@ describe 'openstacklib::policy::base' do
:line => '\'context_is_admin or owner\': \'foo:bar\'',
:match => '^[\'"]?context_is_admin or owner[\'"]?\s*:.+'
) }
context 'with single-quotes in value' do
before do
params.merge!({
:value => 'foo:\'bar\''
})
end
it { should contain_file_line('/etc/nova/policy.yaml-context_is_admin or owner').with(
:path => '/etc/nova/policy.yaml',
:line => '\'context_is_admin or owner\': \'foo:\'\'bar\'\'\'',
:match => '^[\'"]?context_is_admin or owner[\'"]?\s*:.+'
) }
end
context 'with pre-formatted single-quotes in value' do
before do
params.merge!({
:value => 'foo:\'\'bar\'\''
})
end
it { should contain_file_line('/etc/nova/policy.yaml-context_is_admin or owner').with(
:path => '/etc/nova/policy.yaml',
:line => '\'context_is_admin or owner\': \'foo:\'\'bar\'\'\'',
:match => '^[\'"]?context_is_admin or owner[\'"]?\s*:.+'
) }
end
end
context 'with purge_config enabled' do