Skip to content

Commit

Permalink
Update allowed_hosts conditional statement
Browse files Browse the repository at this point in the history
In the origin heat::db::mysql, if the value of $allowed_hosts
contains or equals to $host, then puppet will complain duplicate
declaration error. This patch is aim to update the allowed_hosts
conditonal statement in heat::db::mysql.

There are two cases to pass $allowed_hosts to $real_allowed_hosts:

   - If $allowed_hosts is array,then remove $host from $allowed_hosts;
   - elsif $allowed_hosts is string and not equivalent to $host;

At last, if $real_allowed_hosts is not undef, then run
heat::db::mysql::host_access

Add heat_db_mysql_spec for related test.

Fix bug 1206444

Change-Id: Iac6a32fb614c9ad19f9eaa3aaa883cb3bf9aa2ef
  • Loading branch information
Xingchao Yu committed Aug 5, 2013
1 parent 3419e99 commit e9e261b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
13 changes: 10 additions & 3 deletions manifests/db/mysql.pp
Expand Up @@ -36,7 +36,7 @@

Class['mysql::server'] -> Class['heat::db::mysql']
Class['heat::db::mysql'] -> Exec<| title == 'heat-manage db_sync' |>
Database[$dbname] ~> Exec<| title == 'heat-manage db_sync' |>
Mysql::Db[$dbname] ~> Exec<| title == 'heat-manage db_sync' |>

mysql::db { $dbname:
user => $user,
Expand All @@ -46,8 +46,15 @@
require => Class['mysql::config'],
}

if $allowed_hosts {
heat::db::mysql::host_access { $allowed_hosts:
# Check allowed_hosts to avoid duplicate resource declarations
if is_array($allowed_hosts) and delete($allowed_hosts,$host) != [] {
$real_allowed_hosts = delete($allowed_hosts,$host)
} elsif is_string($allowed_hosts) and ($allowed_hosts != $host) {
$real_allowed_hosts = $allowed_hosts
}

if $real_allowed_hosts {
heat::db::mysql::host_access { $real_allowed_hosts:
user => $user,
password => $password,
database => $dbname,
Expand Down
88 changes: 88 additions & 0 deletions spec/classes/heat_db_mysql_spec.rb
@@ -0,0 +1,88 @@
require 'spec_helper'

describe 'heat::db::mysql' do
let :facts do
{ :osfamily => 'RedHat' }
end

let :pre_condition do
'include mysql::server'
end

let :params do
{ :password => 's3cr3t',
:dbname => 'heat',
:user => 'heat',
:host => 'localhost',
:charset => 'latin1'
}
end

shared_examples_for 'heat mysql database' do

context 'when omiting the required parameter password' do
before { params.delete(:password) }
it { expect { should raise_error(Puppet::Error) } }
end

it 'creates a mysql database' do
should contain_mysql__db( params[:dbname] ).with(
:user => params[:user],
:password => params[:password],
:host => params[:host],
:charset => params[:charset],
:require => 'Class[Mysql::Config]'
)
end
end

describe "overriding allowed_hosts param to array" do
let :params do
{
:password => 'heatpass',
:allowed_hosts => ['localhost','%']
}
end

it {should_not contain_heat__db__mysql__host_access("localhost").with(
:user => 'heat',
:password => 'heatpass',
:database => 'heat'
)}
it {should contain_heat__db__mysql__host_access("%").with(
:user => 'heat',
:password => 'heatpass',
:database => 'heat'
)}
end

describe "overriding allowed_hosts param to string" do
let :params do
{
:password => 'heatpass2',
:allowed_hosts => '192.168.1.1'
}
end

it {should contain_heat__db__mysql__host_access("192.168.1.1").with(
:user => 'heat',
:password => 'heatpass2',
:database => 'heat'
)}
end

describe "overriding allowed_hosts param equals to host param " do
let :params do
{
:password => 'heatpass2',
:allowed_hosts => 'localhost'
}
end

it {should_not contain_heat__db__mysql__host_access("localhost").with(
:user => 'heat',
:password => 'heatpass2',
:database => 'heat'
)}
end
end

0 comments on commit e9e261b

Please sign in to comment.