Allow arbitrary section name for *_id_setter

... so that we can set the parameters in different sections.
(eg. [database] db_flavor_ref instead of [compute] db_flavor_ref )

Implementation to inject parameters to tempset.conf is replaced by
the tempest_config type and the ini_setting provider, so that we can
use the consistent implementation to modify tempest.conf .

This change also fixes "flapping" of database/db_flavor_ref. Now
the parameter is removed by the initial tempest_config resource then
set by the tempest_glance_id_setter, when db_flavor_name is used.

Closes-Bug: #1978848
Change-Id: Ife2c563213b9b4118d3925192a2ff45253bcee2b
This commit is contained in:
Takashi Kajinami 2022-03-07 16:24:23 +09:00
parent 7b20e48cec
commit affbcb0cc6
10 changed files with 119 additions and 195 deletions

View File

@ -7,22 +7,29 @@ Puppet::Type.type(:tempest_flavor_id_setter).provide(
@credentials = Puppet::Provider::Openstack::CredentialsV3.new @credentials = Puppet::Provider::Openstack::CredentialsV3.new
def exists?
lines.find do |line|
should_line.chomp == line.chomp
end
end
def file_path def file_path
resource[:tempest_conf_path] resource[:tempest_conf_path]
end end
def exists?
conf = Puppet::Type::Tempest_config
.new(:title => resource[:name], :path => file_path)
entry = Puppet::Type.type(:tempest_config).provider(:ini_setting).new(conf)
entry.exists?
end
def create def create
handle_create_with_match conf = Puppet::Type::Tempest_config
.new(:title => resource[:name], :value => get_flavor_id, :path => file_path)
entry = Puppet::Type.type(:tempest_config).provider(:ini_setting).new(conf)
entry.create
end end
def destroy def destroy
handle_create_with_match conf = Puppet::Type::Tempest_config
.new(:title => resource[:name], :path => file_path)
entry = Puppet::Type.type(:tempest_config).provider(:ini_setting).new(conf)
entry.destroy
end end
def get_flavor_id def get_flavor_id
@ -40,49 +47,4 @@ Puppet::Type.type(:tempest_flavor_id_setter).provide(
end end
@flavor_id @flavor_id
end end
def should_line
"#{resource[:name]} = #{get_flavor_id}"
end
def match
/^\s*#{resource[:name]}\s*=\s*/
end
def handle_create_with_match()
regex = match
match_count = lines.select { |l| regex.match(l) }.count
file = lines
case match_count
when 1
File.open(file_path, 'w') do |fh|
lines.each do |l|
fh.puts(regex.match(l) ? "#{should_line}" : l)
end
end
when 0
block_pos = lines.find_index { |l| /^\[compute\]/ =~ l }
if block_pos.nil?
file += ["[compute]\n", "#{should_line}\n"]
else
file.insert(block_pos+1, "#{should_line}\n")
end
File.write(file_path, file.join)
else # cannot be negative.
raise Puppet::Error, "More than one line in file \
'#{file_path}' matches pattern '#{regex}'"
end
end
private
def lines
# If this type is ever used with very large files, we should
# write this in a different way, using a temp
# file; for now assuming that this type is only used on
# small-ish config files that can fit into memory without
# too much trouble.
@lines ||= File.readlines(file_path)
end
end end

View File

@ -7,22 +7,29 @@ Puppet::Type.type(:tempest_glance_id_setter).provide(
@credentials = Puppet::Provider::Openstack::CredentialsV3.new @credentials = Puppet::Provider::Openstack::CredentialsV3.new
def exists?
lines.find do |line|
should_line.chomp == line.chomp
end
end
def file_path def file_path
resource[:tempest_conf_path] resource[:tempest_conf_path]
end end
def exists?
conf = Puppet::Type::Tempest_config
.new(:title => resource[:name], :path => file_path)
entry = Puppet::Type.type(:tempest_config).provider(:ini_setting).new(conf)
entry.exists?
end
def create def create
handle_create_with_match conf = Puppet::Type::Tempest_config
.new(:title => resource[:name], :value => get_image_id, :path => file_path)
entry = Puppet::Type.type(:tempest_config).provider(:ini_setting).new(conf)
entry.create
end end
def destroy def destroy
handle_create_with_match conf = Puppet::Type::Tempest_config
.new(:title => resource[:name], :path => file_path)
entry = Puppet::Type.type(:tempest_config).provider(:ini_setting).new(conf)
entry.destroy
end end
def get_image_id def get_image_id
@ -40,49 +47,4 @@ Puppet::Type.type(:tempest_glance_id_setter).provide(
end end
@image_id @image_id
end end
def should_line
"#{resource[:name]} = #{get_image_id}"
end
def match
/^\s*#{resource[:name]}\s*=\s*/
end
def handle_create_with_match()
regex = match
match_count = lines.select { |l| regex.match(l) }.count
file = lines
case match_count
when 1
File.open(file_path, 'w') do |fh|
lines.each do |l|
fh.puts(regex.match(l) ? "#{should_line}" : l)
end
end
when 0
block_pos = lines.find_index { |l| /^\[compute\]/ =~ l }
if block_pos.nil?
file += ["[compute]\n", "#{should_line}\n"]
else
file.insert(block_pos+1, "#{should_line}\n")
end
File.write(file_path, file.join)
else # cannot be negative.
raise Puppet::Error, "More than one line in file \
'#{file_path}' matches pattern '#{regex}'"
end
end
private
def lines
# If this type is ever used with very large files, we should
# write this in a different way, using a temp
# file; for now assuming that this type is only used on
# small-ish config files that can fit into memory without
# too much trouble.
@lines ||= File.readlines(file_path)
end
end end

View File

@ -7,22 +7,29 @@ Puppet::Type.type(:tempest_neutron_net_id_setter).provide(
@credentials = Puppet::Provider::Openstack::CredentialsV3.new @credentials = Puppet::Provider::Openstack::CredentialsV3.new
def exists?
lines.find do |line|
should_line.chomp == line.chomp
end
end
def file_path def file_path
resource[:tempest_conf_path] resource[:tempest_conf_path]
end end
def exists?
conf = Puppet::Type::Tempest_config
.new(:title => resource[:name], :path => file_path)
entry = Puppet::Type.type(:tempest_config).provider(:ini_setting).new(conf)
entry.exists?
end
def create def create
handle_create_with_match conf = Puppet::Type::Tempest_config
.new(:title => resource[:name], :value => get_network_id, :path => file_path)
entry = Puppet::Type.type(:tempest_config).provider(:ini_setting).new(conf)
entry.create
end end
def destroy def destroy
handle_create_with_match conf = Puppet::Type::Tempest_config
.new(:title => resource[:name], :path => file_path)
entry = Puppet::Type.type(:tempest_config).provider(:ini_setting).new(conf)
entry.destroy
end end
def get_network_id def get_network_id
@ -40,48 +47,4 @@ Puppet::Type.type(:tempest_neutron_net_id_setter).provide(
end end
@network_id @network_id
end end
def should_line
"#{resource[:name]} = #{get_network_id}"
end
def match
/^\s*#{resource[:name]}\s*=\s*/
end
def handle_create_with_match()
regex = match
match_count = lines.select { |l| regex.match(l) }.count
file = lines
case match_count
when 1
File.open(file_path, 'w') do |fh|
lines.each do |l|
fh.puts(regex.match(l) ? should_line : l)
end
end
when 0
block_pos = lines.find_index { |l| /^\[network\]/ =~ l }
if block_pos.nil?
file += ["[network]\n", "#{should_line}\n"]
else
file.insert(block_pos+1, "#{should_line}\n")
end
File.write(file_path, file.join)
else # cannot be negative.
raise Puppet::Error, "More than one line in file \
'#{file_path}' matches pattern '#{regex}'"
end
end
private
def lines
# If this type is ever used with very large files, we should
# write this in a different way, using a temp
# file; for now assuming that this type is only used on
# small-ish config files that can fit into memory without
# too much trouble.
@lines ||= File.readlines(file_path)
end
end end

View File

@ -1,6 +1,6 @@
Puppet::Type.newtype(:tempest_flavor_id_setter) do Puppet::Type.newtype(:tempest_flavor_id_setter) do
# #
# tempest_flavor_id_setter { 'flavor_id': # tempest_flavor_id_setter { 'compute/flavor_id':
# tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', # tempest_conf_path => '/var/lib/tempest/etc/tempest.conf',
# flavor_name => $name, # flavor_name => $name,
# } # }
@ -10,6 +10,14 @@ Puppet::Type.newtype(:tempest_flavor_id_setter) do
newparam(:name, :namevar => true) do newparam(:name, :namevar => true) do
desc 'name of the setting to update' desc 'name of the setting to update'
munge do |value|
if value.include? '/'
value
else
# This is to keep the backword compatibility
"compute/#{value}"
end
end
end end
newparam(:tempest_conf_path) do newparam(:tempest_conf_path) do

View File

@ -1,6 +1,6 @@
Puppet::Type.newtype(:tempest_glance_id_setter) do Puppet::Type.newtype(:tempest_glance_id_setter) do
# #
# tempest_glance_id_setter { 'image_id': # tempest_glance_id_setter { 'compute/image_id':
# tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', # tempest_conf_path => '/var/lib/tempest/etc/tempest.conf',
# image_name => $name, # image_name => $name,
# } # }
@ -10,6 +10,14 @@ Puppet::Type.newtype(:tempest_glance_id_setter) do
newparam(:name, :namevar => true) do newparam(:name, :namevar => true) do
desc 'name of the setting to update' desc 'name of the setting to update'
munge do |value|
if value.include? '/'
value
else
# This is to keep the backword compatibility
"compute/#{value}"
end
end
end end
newparam(:tempest_conf_path) do newparam(:tempest_conf_path) do

View File

@ -4,6 +4,14 @@ Puppet::Type.newtype(:tempest_neutron_net_id_setter) do
newparam(:name, :namevar => true) do newparam(:name, :namevar => true) do
desc 'The name of the setting to update.' desc 'The name of the setting to update.'
munge do |value|
if value.include? '/'
value
else
# This is to keep the backword compatibility
"network/#{value}"
end
end
end end
newparam(:tempest_conf_path) do newparam(:tempest_conf_path) do

View File

@ -218,7 +218,7 @@
# Defaults to $::os_service_default # Defaults to $::os_service_default
# [*db_flavor_ref*] # [*db_flavor_ref*]
# Valid primary flavor to use in Trove tests. # Valid primary flavor to use in Trove tests.
# Defaults to $::os_service_default # Defaults to undef
# [*db_flavor_name*] # [*db_flavor_name*]
# Defaults to undef # Defaults to undef
# [*baremetal_driver*] # [*baremetal_driver*]
@ -334,7 +334,7 @@ class tempest(
# Sahara config # Sahara config
$sahara_plugins = undef, $sahara_plugins = undef,
# Trove config # Trove config
$db_flavor_ref = $::os_service_default, $db_flavor_ref = undef,
$db_flavor_name = undef, $db_flavor_name = undef,
# Service configuration # Service configuration
$cinder_available = true, $cinder_available = true,
@ -770,38 +770,38 @@ class tempest(
} }
if ! $flavor_ref and $flavor_name { if ! $flavor_ref and $flavor_name {
tempest_flavor_id_setter { 'flavor_ref': tempest_flavor_id_setter { 'compute/flavor_ref':
ensure => present, ensure => present,
tempest_conf_path => $tempest_conf, tempest_conf_path => $tempest_conf,
flavor_name => $flavor_name, flavor_name => $flavor_name,
} }
Tempest_config<||> -> Tempest_flavor_id_setter['flavor_ref'] Tempest_config<||> -> Tempest_flavor_id_setter['compute/flavor_ref']
Keystone_user_role<||> -> Tempest_flavor_id_setter['flavor_ref'] Keystone_user_role<||> -> Tempest_flavor_id_setter['compute/flavor_ref']
} elsif ($flavor_name and $flavor_ref) { } elsif ($flavor_name and $flavor_ref) {
fail('flavor_ref and flavor_name are both set: please set only one of them') fail('flavor_ref and flavor_name are both set: please set only one of them')
} }
if ! $flavor_ref_alt and $flavor_name_alt { if ! $flavor_ref_alt and $flavor_name_alt {
tempest_flavor_id_setter { 'flavor_ref_alt': tempest_flavor_id_setter { 'compute/flavor_ref_alt':
ensure => present, ensure => present,
tempest_conf_path => $tempest_conf, tempest_conf_path => $tempest_conf,
flavor_name => $flavor_name_alt, flavor_name => $flavor_name_alt,
} }
Tempest_config<||> -> Tempest_flavor_id_setter['flavor_ref_alt'] Tempest_config<||> -> Tempest_flavor_id_setter['compute/flavor_ref_alt']
Keystone_user_role<||> -> Tempest_flavor_id_setter['flavor_ref_alt'] Keystone_user_role<||> -> Tempest_flavor_id_setter['compute/flavor_ref_alt']
} elsif ($flavor_name_alt and $flavor_ref_alt) { } elsif ($flavor_name_alt and $flavor_ref_alt) {
fail('flavor_ref_alt and flavor_name_alt are both set: please set only one of them') fail('flavor_ref_alt and flavor_name_alt are both set: please set only one of them')
} }
if is_service_default($db_flavor_ref) and $db_flavor_name { if ! $db_flavor_ref and $db_flavor_name {
tempest_flavor_id_setter { 'db_flavor_ref': tempest_flavor_id_setter { 'database/db_flavor_ref':
ensure => present, ensure => present,
tempest_conf_path => $tempest_conf, tempest_conf_path => $tempest_conf,
flavor_name => $db_flavor_name, flavor_name => $db_flavor_name,
} }
Tempest_config<||> -> Tempest_flavor_id_setter['db_flavor_ref'] Tempest_config<||> -> Tempest_flavor_id_setter['database/db_flavor_ref']
Keystone_user_role<||> -> Tempest_flavor_id_setter['db_flavor_ref'] Keystone_user_role<||> -> Tempest_flavor_id_setter['database/db_flavor_ref']
} elsif ($db_flavor_name and ! is_service_default($db_flavor_ref)) { } elsif ($db_flavor_name and $db_flavor_ref) {
fail('db_flavor_ref and db_flavor_name are both set: please set only one of them') fail('db_flavor_ref and db_flavor_name are both set: please set only one of them')
} }
@ -809,24 +809,24 @@ class tempest(
if ! $image_ref and $image_name { if ! $image_ref and $image_name {
# If the image id was not provided, look it up via the image name # If the image id was not provided, look it up via the image name
# and set the value in the conf file. # and set the value in the conf file.
tempest_glance_id_setter { 'image_ref': tempest_glance_id_setter { 'compute/image_ref':
ensure => present, ensure => present,
tempest_conf_path => $tempest_conf, tempest_conf_path => $tempest_conf,
image_name => $image_name, image_name => $image_name,
} }
Tempest_config<||> -> Tempest_glance_id_setter['image_ref'] Tempest_config<||> -> Tempest_glance_id_setter['compute/image_ref']
Keystone_user_role<||> -> Tempest_glance_id_setter['image_ref'] Keystone_user_role<||> -> Tempest_glance_id_setter['compute/image_ref']
} elsif ($image_name and $image_ref) or (! $image_name and ! $image_ref) { } elsif ($image_name and $image_ref) or (! $image_name and ! $image_ref) {
fail('A value for either image_name or image_ref must be provided.') fail('A value for either image_name or image_ref must be provided.')
} }
if ! $image_ref_alt and $image_name_alt { if ! $image_ref_alt and $image_name_alt {
tempest_glance_id_setter { 'image_ref_alt': tempest_glance_id_setter { 'compute/image_ref_alt':
ensure => present, ensure => present,
tempest_conf_path => $tempest_conf, tempest_conf_path => $tempest_conf,
image_name => $image_name_alt, image_name => $image_name_alt,
} }
Tempest_config<||> -> Tempest_glance_id_setter['image_ref_alt'] Tempest_config<||> -> Tempest_glance_id_setter['compute/image_ref_alt']
Keystone_user_role<||> -> Tempest_glance_id_setter['image_ref_alt'] Keystone_user_role<||> -> Tempest_glance_id_setter['compute/image_ref_alt']
} elsif ($image_name_alt and $image_ref_alt) or (! $image_name_alt and ! $image_ref_alt) { } elsif ($image_name_alt and $image_ref_alt) or (! $image_name_alt and ! $image_ref_alt) {
fail('A value for either image_name_alt or image_ref_alt must \ fail('A value for either image_name_alt or image_ref_alt must \
be provided.') be provided.')
@ -835,13 +835,13 @@ be provided.')
if $neutron_available and $configure_networks { if $neutron_available and $configure_networks {
if ! $public_network_id and $public_network_name { if ! $public_network_id and $public_network_name {
tempest_neutron_net_id_setter { 'public_network_id': tempest_neutron_net_id_setter { 'network/public_network_id':
ensure => present, ensure => present,
tempest_conf_path => $tempest_conf, tempest_conf_path => $tempest_conf,
network_name => $public_network_name, network_name => $public_network_name,
} }
Tempest_config<||> -> Tempest_neutron_net_id_setter['public_network_id'] Tempest_config<||> -> Tempest_neutron_net_id_setter['network/public_network_id']
Keystone_user_role<||> -> Tempest_neutron_net_id_setter['public_network_id'] Keystone_user_role<||> -> Tempest_neutron_net_id_setter['network/public_network_id']
} elsif ($public_network_name and $public_network_id) or (! $public_network_name and ! $public_network_id) { } elsif ($public_network_name and $public_network_id) or (! $public_network_name and ! $public_network_id) {
fail('A value for either public_network_id or public_network_name \ fail('A value for either public_network_id or public_network_name \
must be provided.') must be provided.')

View File

@ -0,0 +1,11 @@
---
features:
- |
The id setter resources type now accepts a resource name in
``<section>/<parameter name>`` format, to use arbitrary section name.
fixes:
- |
Fixed the wrong section(``[DEFAULT]`` instead of ``[database]``) used by
automated resolution of image id value using the ``db_flavor_name``
parameter.

View File

@ -42,7 +42,7 @@ describe 'tempest' do
end end
it 'uses a resource to configure image_ref from image_name' do it 'uses a resource to configure image_ref from image_name' do
is_expected.to contain_tempest_glance_id_setter('image_ref').with_image_name('cirros') is_expected.to contain_tempest_glance_id_setter('compute/image_ref').with_image_name('cirros')
end end
end end
@ -77,7 +77,7 @@ describe 'tempest' do
end end
it 'uses a resource to configure public_network_id from public_network_name' do it 'uses a resource to configure public_network_id from public_network_name' do
is_expected.to contain_tempest_neutron_net_id_setter('public_network_id').with_network_name('public') is_expected.to contain_tempest_neutron_net_id_setter('network/public_network_id').with_network_name('public')
end end
end end
@ -242,7 +242,7 @@ describe 'tempest' do
is_expected.to contain_tempest_config('network/public_router_id').with(:value => '') is_expected.to contain_tempest_config('network/public_router_id').with(:value => '')
is_expected.to contain_tempest_config('dashboard/login_url').with(:value => nil) is_expected.to contain_tempest_config('dashboard/login_url').with(:value => nil)
is_expected.to contain_tempest_config('dashboard/dashboard_url').with(:value => nil) is_expected.to contain_tempest_config('dashboard/dashboard_url').with(:value => nil)
is_expected.to contain_tempest_config('database/db_flavor_ref').with_value('<SERVICE DEFAULT>') is_expected.to contain_tempest_config('database/db_flavor_ref').with(:value => nil)
is_expected.to contain_tempest_config('service_available/cinder').with(:value => true) is_expected.to contain_tempest_config('service_available/cinder').with(:value => true)
is_expected.to contain_tempest_config('volume-feature-enabled/backup').with(:value => false) is_expected.to contain_tempest_config('volume-feature-enabled/backup').with(:value => false)
is_expected.to contain_tempest_config('service_available/glance').with(:value => true) is_expected.to contain_tempest_config('service_available/glance').with(:value => true)
@ -299,13 +299,13 @@ describe 'tempest' do
end end
it 'set glance id' do it 'set glance id' do
is_expected.to contain_tempest_glance_id_setter('image_ref').with( is_expected.to contain_tempest_glance_id_setter('compute/image_ref').with(
:ensure => 'present', :ensure => 'present',
:tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', :tempest_conf_path => '/var/lib/tempest/etc/tempest.conf',
:image_name => 'image name', :image_name => 'image name',
) )
is_expected.to contain_tempest_glance_id_setter('image_ref_alt').with( is_expected.to contain_tempest_glance_id_setter('compute/image_ref_alt').with(
:ensure => 'present', :ensure => 'present',
:tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', :tempest_conf_path => '/var/lib/tempest/etc/tempest.conf',
:image_name => 'image name alt', :image_name => 'image name alt',
@ -313,7 +313,7 @@ describe 'tempest' do
end end
it 'neutron net id' do it 'neutron net id' do
is_expected.to contain_tempest_neutron_net_id_setter('public_network_id').with( is_expected.to contain_tempest_neutron_net_id_setter('network/public_network_id').with(
:ensure => 'present', :ensure => 'present',
:tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', :tempest_conf_path => '/var/lib/tempest/etc/tempest.conf',
:network_name => 'network name', :network_name => 'network name',
@ -393,17 +393,17 @@ describe 'tempest' do
end end
it "sets flavor id using setter" do it "sets flavor id using setter" do
is_expected.to contain_tempest_flavor_id_setter('flavor_ref').with( is_expected.to contain_tempest_flavor_id_setter('compute/flavor_ref').with(
:ensure => 'present', :ensure => 'present',
:tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', :tempest_conf_path => '/var/lib/tempest/etc/tempest.conf',
:flavor_name => 'm1.tiny', :flavor_name => 'm1.tiny',
) )
is_expected.to contain_tempest_flavor_id_setter('flavor_ref_alt').with( is_expected.to contain_tempest_flavor_id_setter('compute/flavor_ref_alt').with(
:ensure => 'present', :ensure => 'present',
:tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', :tempest_conf_path => '/var/lib/tempest/etc/tempest.conf',
:flavor_name => 'm1.nano', :flavor_name => 'm1.nano',
) )
is_expected.to contain_tempest_flavor_id_setter('db_flavor_ref').with( is_expected.to contain_tempest_flavor_id_setter('database/db_flavor_ref').with(
:ensure => 'present', :ensure => 'present',
:tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', :tempest_conf_path => '/var/lib/tempest/etc/tempest.conf',
:flavor_name => 'm1.micro', :flavor_name => 'm1.micro',

View File

@ -62,11 +62,11 @@ EOS
validate_file(<<-EOS validate_file(<<-EOS
# This is a comment # This is a comment
[compute] [compute]
image_ref = abcdef
; This is also a comment ; This is also a comment
foo=foovalue foo=foovalue
bar = barvalue bar = barvalue
master = true master = true
image_ref=abcdef
[network] [network]
foo= foovalue2 foo= foovalue2
[blah] [blah]
@ -90,8 +90,8 @@ foo=foovalue
bar = barvalue bar = barvalue
master = true master = true
[network] [network]
public_network_id = abcdef
foo= foovalue2 foo= foovalue2
public_network_id=abcdef
[blah] [blah]
EOS EOS
@ -115,6 +115,7 @@ EOS
validate_file(<<-EOS validate_file(<<-EOS
# This is a comment # This is a comment
[compute] [compute]
image_ref=abcdef image_ref=abcdef
EOS EOS
@ -131,6 +132,7 @@ EOS
validate_file(<<-EOS validate_file(<<-EOS
# This is a comment # This is a comment
[network] [network]
public_network_id=abcdef public_network_id=abcdef
EOS EOS