From 842916c0319c5327f408bde1ce525b73d8e24f50 Mon Sep 17 00:00:00 2001 From: Eric Putnam Date: Tue, 27 Jun 2017 11:25:40 -0700 Subject: [PATCH] (MODULES-4604) conditionalize name validation in mysql_grant type Users are running into an issue where \`puppet resource mysql_grant\` fails because there is a global name validation using properties that only exist in a compiled catalog and not in the naked type that is instantiated during a \`puppet resource\` run. self[:user] and self[:table] are nil in this use case so there is a failure. This commit surrounds the validation in an if statement so it is not called during \`puppet resource\`. Also, a happy path test is added and the acceptance test is removed." --- lib/puppet/type/mysql_grant.rb | 4 +++- spec/acceptance/types/mysql_grant_spec.rb | 19 ------------------- spec/unit/puppet/type/mysql_grant_spec.rb | 5 ++++- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/lib/puppet/type/mysql_grant.rb b/lib/puppet/type/mysql_grant.rb index 46c3fce7e..1d7f3eb86 100644 --- a/lib/puppet/type/mysql_grant.rb +++ b/lib/puppet/type/mysql_grant.rb @@ -34,7 +34,9 @@ def initialize(*args) fail('PROXY must be the only privilege specified.') if Array(self[:privileges]).count > 1 and Array(self[:privileges]).include?('PROXY') fail('table parameter is required.') if self[:ensure] == :present and self[:table].nil? fail('user parameter is required.') if self[:ensure] == :present and self[:user].nil? - fail('name must match user and table parameters') if self[:name] != "#{self[:user]}/#{self[:table]}" + if self[:user] and self[:table] + fail('name must match user@host/table format') if self[:name] != "#{self[:user]}/#{self[:table]}" + end end newparam(:name, :namevar => true) do diff --git a/spec/acceptance/types/mysql_grant_spec.rb b/spec/acceptance/types/mysql_grant_spec.rb index b2ca7fa63..704365ab0 100644 --- a/spec/acceptance/types/mysql_grant_spec.rb +++ b/spec/acceptance/types/mysql_grant_spec.rb @@ -109,25 +109,6 @@ class { 'mysql::server': end end - describe 'adding privileges with invalid name' do - it 'should fail' do - pp = <<-EOS - mysql_user { 'test2@tester': - ensure => present, - } - mysql_grant { 'test': - ensure => 'present', - table => 'test.*', - user => 'test2@tester', - privileges => ['SELECT', 'UPDATE'], - require => Mysql_user['test2@tester'], - } - EOS - - expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/name must match user and table parameters/) - end - end - describe 'adding option' do it 'should work without errors' do pp = <<-EOS diff --git a/spec/unit/puppet/type/mysql_grant_spec.rb b/spec/unit/puppet/type/mysql_grant_spec.rb index d2290030a..e1f3f95fb 100644 --- a/spec/unit/puppet/type/mysql_grant_spec.rb +++ b/spec/unit/puppet/type/mysql_grant_spec.rb @@ -67,9 +67,12 @@ end it 'should require the name to match the user and table' do + expect { + Puppet::Type.type(:mysql_grant).new(:name => 'foo@localhost/*.*', :privileges => ['ALL'], :table => ['*.*'], :user => 'foo@localhost') + }.to_not raise_error expect { Puppet::Type.type(:mysql_grant).new(:name => 'foo', :privileges => ['ALL'], :table => ['*.*'], :user => 'foo@localhost') - }.to raise_error /name must match user and table parameters/ + }.to raise_error /name must match user@host\/table format/ end describe 'it should munge privileges' do