Skip to content

Commit 0dfa6a4

Browse files
committed
Merge pull request #208 from apenney/database_user
Database user refactor/tests
2 parents 731036e + 8bf0368 commit 0dfa6a4

File tree

3 files changed

+117
-8
lines changed

3 files changed

+117
-8
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_grant/mysql_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
File.stubs(:file?).with("#{root_home}/.my.cnf").returns(true)
1818
end
1919

20-
it 'should query privilegess from the database' do
20+
it 'should query privileges from the database' do
2121
provider_class.expects(:mysql) .with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'describe user']).returns <<-EOT
2222
Field Type Null Key Default Extra
2323
Host char(60) NO PRI
@@ -40,15 +40,15 @@
4040
provider_class.db_privs.should == [ 'Select_priv', 'Insert_priv', 'Update_priv' ]
4141
end
4242

43-
it 'should query set priviliges' do
43+
it 'should query set privileges' do
4444
provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', "select * from mysql.user where user='user' and host='host'"]).returns <<-EOT
4545
Host User Password Select_priv Insert_priv Update_priv
4646
host user Y N Y
4747
EOT
4848
@provider.privileges.should == [ 'Select_priv', 'Update_priv' ]
4949
end
5050

51-
it 'should recognize when all priviliges are set' do
51+
it 'should recognize when all privileges are set' do
5252
provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', "select * from mysql.user where user='user' and host='host'"]).returns <<-EOT
5353
Host User Password Select_priv Insert_priv Update_priv
5454
host user Y Y Y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
require 'spec_helper'
2+
3+
provider_class = Puppet::Type.type(:database_user).provider(:mysql)
4+
5+
describe provider_class do
6+
subject { provider_class }
7+
8+
let(:root_home) { '/root' }
9+
let(:defaults_file) { '--defaults-file=/root/.my.cnf' }
10+
let(:newhash) { '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5' }
11+
12+
let(:raw_users) do
13+
<<-SQL_OUTPUT
14+
15+
root@::1
16+
@localhost
17+
debian-sys-maint@localhost
18+
root@localhost
19+
usvn_user@localhost
20+
@vagrant-ubuntu-raring-64
21+
SQL_OUTPUT
22+
end
23+
24+
let(:parsed_users) { ['[email protected]', 'root@::1', 'debian-sys-maint@localhost', 'root@localhost', 'usvn_user@localhost'] }
25+
26+
before :each do
27+
# password hash = mypass
28+
@resource = Puppet::Type::Database_user.new(
29+
{ :password_hash => '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4', :name => 'joe@localhost' }
30+
)
31+
@provider = provider_class.new(@resource)
32+
Facter.stubs(:value).with(:root_home).returns(root_home)
33+
Puppet::Util.stubs(:which).with("mysql").returns("/usr/bin/mysql")
34+
subject.stubs(:which).with("mysql").returns("/usr/bin/mysql")
35+
subject.stubs(:defaults_file).returns('--defaults-file=/root/.my.cnf')
36+
end
37+
38+
describe 'self.instances' do
39+
it 'returns an array of users' do
40+
subject.stubs(:mysql).with([defaults_file, 'mysql', "-BNeselect concat(User, '@',Host) as User from mysql.user"]).returns(raw_users)
41+
42+
usernames = subject.instances.collect {|x| x.name }
43+
parsed_users.should match_array(usernames)
44+
end
45+
end
46+
47+
describe 'create' do
48+
it 'makes a user' do
49+
subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "create user 'joe'@'localhost' identified by PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'"])
50+
@provider.expects(:exists?).returns(true)
51+
@provider.create.should be_true
52+
end
53+
end
54+
55+
describe 'destroy' do
56+
it 'removes a user if present' do
57+
subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "drop user 'joe'@'localhost'"])
58+
@provider.expects(:exists?).returns(false)
59+
@provider.destroy.should be_true
60+
end
61+
end
62+
63+
describe 'password_hash' do
64+
it 'returns a hash' do
65+
subject.expects(:mysql).with([defaults_file, 'mysql', '-NBe', "select password from mysql.user where CONCAT(user, '@', host) = 'joe@localhost'"]).returns('*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4')
66+
@provider.password_hash.should == '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'
67+
end
68+
end
69+
70+
describe 'password_hash=' do
71+
it 'changes the hash' do
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')
75+
@provider.password_hash=('*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5')
76+
end
77+
end
78+
79+
describe 'exists?' do
80+
it 'checks if user exists' do
81+
subject.expects(:mysql).with([defaults_file, 'mysql', '-NBe', "select '1' from mysql.user where CONCAT(user, '@', host) = 'joe@localhost'"]).returns('1')
82+
@provider.exists?.should be_true
83+
end
84+
end
85+
86+
describe 'flush' do
87+
it 'removes cached privileges' do
88+
subject.expects(:mysqladmin).with([defaults_file, 'flush-privileges'])
89+
@provider.flush
90+
end
91+
end
92+
93+
describe 'self.defaults_file' do
94+
it 'sets --defaults-file' do
95+
File.stubs(:file?).with('#{root_home}/.my.cnf').returns(true)
96+
@provider.defaults_file.should == '--defaults-file=/root/.my.cnf'
97+
end
98+
end
99+
100+
end

0 commit comments

Comments
 (0)