From 8bf03681f33a935c23cc6fb557c0c2cba97c7e44 Mon Sep 17 00:00:00 2001 From: Ashley Penney Date: Wed, 3 Jul 2013 15:59:17 -0400 Subject: [PATCH] Various changes to the provider to ensure commands are successful, as well as improvements to the tests. --- lib/puppet/provider/database_user/mysql.rb | 19 ++++++++++++++----- .../provider/database_user/mysql_spec.rb | 13 ++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/puppet/provider/database_user/mysql.rb b/lib/puppet/provider/database_user/mysql.rb index 3eee597..a4f0942 100644 --- a/lib/puppet/provider/database_user/mysql.rb +++ b/lib/puppet/provider/database_user/mysql.rb @@ -4,8 +4,8 @@ Puppet::Type.type(:database_user).provide(:mysql) do defaultfor :kernel => 'Linux' - optional_commands :mysql => 'mysql' - optional_commands :mysqladmin => 'mysqladmin' + commands :mysql => 'mysql' + commands :mysqladmin => 'mysqladmin' def self.instances users = mysql([defaults_file, "mysql", '-BNe' "select concat(User, '@',Host) as User from mysql.user"].compact).split("\n") @@ -15,19 +15,28 @@ Puppet::Type.type(:database_user).provide(:mysql) do end def create - mysql([defaults_file, "mysql", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.value(:password_hash) ]].compact) + merged_name = @resource[:name].sub("@", "'@'") + password_hash = @resource.value(:password_hash) + mysql([defaults_file, "mysql", "-e", "create user '#{merged_name}' identified by PASSWORD '#{password_hash}'"].compact) + + exists? ? (return true) : (return false) end def destroy - mysql([defaults_file, "mysql", "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") ].compact) + merged_name = @resource[:name].sub("@", "'@'") + mysql([defaults_file, "mysql", "-e", "drop user '#{merged_name}'"].compact) + + exists? ? (return false) : (return true) end def password_hash - mysql([defaults_file, "mysql", "-NBe", "select password from mysql.user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)].compact).chomp + mysql([defaults_file, "mysql", "-NBe", "select password from mysql.user where CONCAT(user, '@', host) = '#{@resource[:name]}'"].compact).chomp end def password_hash=(string) mysql([defaults_file, "mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] ].compact) + + password_hash == string ? (return true) : (return false) end def exists? diff --git a/spec/unit/puppet/provider/database_user/mysql_spec.rb b/spec/unit/puppet/provider/database_user/mysql_spec.rb index 74b3386..d022fc4 100644 --- a/spec/unit/puppet/provider/database_user/mysql_spec.rb +++ b/spec/unit/puppet/provider/database_user/mysql_spec.rb @@ -1,5 +1,4 @@ require 'spec_helper' -require 'pry' provider_class = Puppet::Type.type(:database_user).provider(:mysql) @@ -48,27 +47,31 @@ usvn_user@localhost describe 'create' do it 'makes a user' do subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "create user 'joe'@'localhost' identified by PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'"]) - @provider.create + @provider.expects(:exists?).returns(true) + @provider.create.should be_true end end describe 'destroy' do it 'removes a user if present' do subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "drop user 'joe'@'localhost'"]) - @provider.destroy + @provider.expects(:exists?).returns(false) + @provider.destroy.should be_true end end describe 'password_hash' do it 'returns a hash' do subject.expects(:mysql).with([defaults_file, 'mysql', '-NBe', "select password from mysql.user where CONCAT(user, '@', host) = 'joe@localhost'"]).returns('*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4') - @provider.password_hash + @provider.password_hash.should == '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4' end end describe 'password_hash=' do it 'changes the hash' do - subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "SET PASSWORD FOR 'joe'@'localhost' = '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5'"]) + subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "SET PASSWORD FOR 'joe'@'localhost' = '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5'"]).returns('0') + + @provider.expects(:password_hash).returns('*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5') @provider.password_hash=('*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5') end end