Skip to content

Commit 00130ef

Browse files
author
Ashley Penney
committed
Add initial spec tests for database_user.
1 parent e961b83 commit 00130ef

File tree

1 file changed

+97
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)