Various changes to the provider to ensure commands are successful,
as well as improvements to the tests.
This commit is contained in:
parent
00130ef6c6
commit
8bf03681f3
2 changed files with 22 additions and 10 deletions
|
@ -4,8 +4,8 @@ Puppet::Type.type(:database_user).provide(:mysql) do
|
||||||
|
|
||||||
defaultfor :kernel => 'Linux'
|
defaultfor :kernel => 'Linux'
|
||||||
|
|
||||||
optional_commands :mysql => 'mysql'
|
commands :mysql => 'mysql'
|
||||||
optional_commands :mysqladmin => 'mysqladmin'
|
commands :mysqladmin => 'mysqladmin'
|
||||||
|
|
||||||
def self.instances
|
def self.instances
|
||||||
users = mysql([defaults_file, "mysql", '-BNe' "select concat(User, '@',Host) as User from mysql.user"].compact).split("\n")
|
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
|
end
|
||||||
|
|
||||||
def create
|
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
|
end
|
||||||
|
|
||||||
def destroy
|
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
|
end
|
||||||
|
|
||||||
def password_hash
|
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
|
end
|
||||||
|
|
||||||
def password_hash=(string)
|
def password_hash=(string)
|
||||||
mysql([defaults_file, "mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] ].compact)
|
mysql([defaults_file, "mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] ].compact)
|
||||||
|
|
||||||
|
password_hash == string ? (return true) : (return false)
|
||||||
end
|
end
|
||||||
|
|
||||||
def exists?
|
def exists?
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
require 'pry'
|
|
||||||
|
|
||||||
provider_class = Puppet::Type.type(:database_user).provider(:mysql)
|
provider_class = Puppet::Type.type(:database_user).provider(:mysql)
|
||||||
|
|
||||||
|
@ -48,27 +47,31 @@ usvn_user@localhost
|
||||||
describe 'create' do
|
describe 'create' do
|
||||||
it 'makes a user' do
|
it 'makes a user' do
|
||||||
subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "create user 'joe'@'localhost' identified by PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'"])
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'destroy' do
|
describe 'destroy' do
|
||||||
it 'removes a user if present' do
|
it 'removes a user if present' do
|
||||||
subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "drop user 'joe'@'localhost'"])
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'password_hash' do
|
describe 'password_hash' do
|
||||||
it 'returns a 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')
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'password_hash=' do
|
describe 'password_hash=' do
|
||||||
it 'changes the 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')
|
@provider.password_hash=('*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue