Various changes to the provider to ensure commands are successful,

as well as improvements to the tests.
This commit is contained in:
Ashley Penney 2013-07-03 15:59:17 -04:00
parent 00130ef6c6
commit 8bf03681f3
2 changed files with 22 additions and 10 deletions

View file

@ -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?

View file

@ -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