Files
puppet-openstacklib/spec/defines/openstacklib_policy_base_spec.rb
Takashi Kajinami f451dbed3a Simplify policies definition
Currently the openstacklib::policy::policies parameter requires
the following format.

openstacklib::policy { 'foo':
  policies => {
    'title' => {
      'key'   => 'policy-key',
      'value' => 'policy-value'
    }
  },
  ...
}

However the top level key ('title') is used to determine resource
names and does not affect actual configuration. This is quite redundant
and sometimes confusing to users.

This allows using title strings to define policy keys. With this change
users can use a policies value like the following example. This is
similar to the existing config classes interface.

openstacklib::policy { 'foo':
  policies => {
    'policy-key' => {
      'value' => 'policy-value'
    }
  },
  ...
}

Change-Id: I7f8caa2b3e5cab852f64c5fdbb3452485d8aabab
2023-12-27 01:50:53 +09:00

171 lines
5.0 KiB
Ruby

require 'spec_helper'
describe 'openstacklib::policy::base' do
shared_examples 'openstacklib::policy::base' do
let :title do
'context_is_admin or owner'
end
context 'with policy.json' do
let :params do
{
:file_path => '/etc/nova/policy.json',
:value => 'foo:bar',
:file_mode => '0644',
:file_user => 'foo',
:file_group => 'bar',
:file_format => 'json',
}
end
it { should contain_openstacklib__policy__default('/etc/nova/policy.json').with(
:file_mode => '0644',
:file_user => 'foo',
:file_group => 'bar',
:file_format => 'json',
:purge_config => false,
)}
it { should contain_augeas('/etc/nova/policy.json-context_is_admin or owner').with(
:lens => 'Json.lns',
:incl => '/etc/nova/policy.json',
:changes => 'set dict/entry[*][.="context_is_admin or owner"]/string "foo:bar"',
)}
it { should contain_augeas('/etc/nova/policy.json-context_is_admin or owner-add').with(
:lens => 'Json.lns',
:incl => '/etc/nova/policy.json',
:changes => [
'set dict/entry[last()+1] "context_is_admin or owner"',
'set dict/entry[last()]/string "foo:bar"'
],
:onlyif => 'match dict/entry[*][.="context_is_admin or owner"] size == 0'
)}
end
context 'with policy.yaml' do
let :params do
{
:file_path => '/etc/nova/policy.yaml',
:value => 'foo:bar',
:file_mode => '0644',
:file_user => 'foo',
:file_group => 'bar',
:file_format => 'yaml',
}
end
it { should contain_openstacklib__policy__default('/etc/nova/policy.yaml').with(
:file_mode => '0644',
:file_user => 'foo',
:file_group => 'bar',
:file_format => 'yaml',
:purge_config => false,
)}
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*:.+'
) }
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
let :params do
{
:file_path => '/etc/nova/policy.yaml',
:value => 'foo:bar',
:file_mode => '0644',
:file_user => 'foo',
:file_group => 'bar',
:file_format => 'yaml',
:purge_config => true,
}
end
it { should contain_openstacklib__policy__default('/etc/nova/policy.yaml').with(
:file_mode => '0644',
:file_user => 'foo',
:file_group => 'bar',
:file_format => 'yaml',
:purge_config => true,
)}
end
context 'with json file_path and yaml file format' do
let :params do
{
:file_path => '/etc/nova/policy.json',
:value => 'foo:bar',
:file_mode => '0644',
:file_user => 'foo',
:file_group => 'bar',
:file_format => 'yaml',
}
end
it { should raise_error(Puppet::Error) }
end
context 'with key overridden' do
let :params do
{
:file_path => '/etc/nova/policy.yaml',
:key => 'context_is_admin',
:value => 'foo:bar',
:file_mode => '0644',
:file_user => 'foo',
:file_group => 'bar',
:file_format => 'yaml',
}
end
it { should contain_file_line('/etc/nova/policy.yaml-context_is_admin').with(
:path => '/etc/nova/policy.yaml',
:line => '\'context_is_admin\': \'foo:bar\'',
:match => '^[\'"]?context_is_admin(?!:)[\'"]?\s*:.+'
) }
end
end
on_supported_os({
:supported_os => OSDefaults.get_supported_os
}).each do |os,facts|
context "on #{os}" do
let (:facts) do
facts.merge!(OSDefaults.get_facts())
end
it_behaves_like 'openstacklib::policy::base'
end
end
end