Skip to content

Commit 8bf0368

Browse files
author
Ashley Penney
committed
Various changes to the provider to ensure commands are successful,
as well as improvements to the tests.
1 parent 00130ef commit 8bf0368

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

lib/puppet/provider/database_user/mysql.rb

+14-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
defaultfor :kernel => 'Linux'
66

7-
optional_commands :mysql => 'mysql'
8-
optional_commands :mysqladmin => 'mysqladmin'
7+
commands :mysql => 'mysql'
8+
commands :mysqladmin => 'mysqladmin'
99

1010
def self.instances
1111
users = mysql([defaults_file, "mysql", '-BNe' "select concat(User, '@',Host) as User from mysql.user"].compact).split("\n")
@@ -15,19 +15,28 @@ def self.instances
1515
end
1616

1717
def create
18-
mysql([defaults_file, "mysql", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.value(:password_hash) ]].compact)
18+
merged_name = @resource[:name].sub("@", "'@'")
19+
password_hash = @resource.value(:password_hash)
20+
mysql([defaults_file, "mysql", "-e", "create user '#{merged_name}' identified by PASSWORD '#{password_hash}'"].compact)
21+
22+
exists? ? (return true) : (return false)
1923
end
2024

2125
def destroy
22-
mysql([defaults_file, "mysql", "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") ].compact)
26+
merged_name = @resource[:name].sub("@", "'@'")
27+
mysql([defaults_file, "mysql", "-e", "drop user '#{merged_name}'"].compact)
28+
29+
exists? ? (return false) : (return true)
2330
end
2431

2532
def password_hash
26-
mysql([defaults_file, "mysql", "-NBe", "select password from mysql.user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)].compact).chomp
33+
mysql([defaults_file, "mysql", "-NBe", "select password from mysql.user where CONCAT(user, '@', host) = '#{@resource[:name]}'"].compact).chomp
2734
end
2835

2936
def password_hash=(string)
3037
mysql([defaults_file, "mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] ].compact)
38+
39+
password_hash == string ? (return true) : (return false)
3140
end
3241

3342
def exists?

spec/unit/puppet/provider/database_user/mysql_spec.rb

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'spec_helper'
2-
require 'pry'
32

43
provider_class = Puppet::Type.type(:database_user).provider(:mysql)
54

@@ -48,27 +47,31 @@
4847
describe 'create' do
4948
it 'makes a user' do
5049
subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "create user 'joe'@'localhost' identified by PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'"])
51-
@provider.create
50+
@provider.expects(:exists?).returns(true)
51+
@provider.create.should be_true
5252
end
5353
end
5454

5555
describe 'destroy' do
5656
it 'removes a user if present' do
5757
subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "drop user 'joe'@'localhost'"])
58-
@provider.destroy
58+
@provider.expects(:exists?).returns(false)
59+
@provider.destroy.should be_true
5960
end
6061
end
6162

6263
describe 'password_hash' do
6364
it 'returns a hash' do
6465
subject.expects(:mysql).with([defaults_file, 'mysql', '-NBe', "select password from mysql.user where CONCAT(user, '@', host) = 'joe@localhost'"]).returns('*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4')
65-
@provider.password_hash
66+
@provider.password_hash.should == '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'
6667
end
6768
end
6869

6970
describe 'password_hash=' do
7071
it 'changes the hash' do
71-
subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "SET PASSWORD FOR 'joe'@'localhost' = '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5'"])
72+
subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "SET PASSWORD FOR 'joe'@'localhost' = '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5'"]).returns('0')
73+
74+
@provider.expects(:password_hash).returns('*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5')
7275
@provider.password_hash=('*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5')
7376
end
7477
end

0 commit comments

Comments
 (0)