2013-07-02 23:42:44 +02:00
require 'spec_helper'
provider_class = Puppet :: Type . type ( :database_user ) . provider ( :mysql )
describe provider_class do
subject { provider_class }
let ( :root_home ) { '/root' }
let ( :defaults_file ) { '--defaults-file=/root/.my.cnf' }
let ( :newhash ) { '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5' }
let ( :raw_users ) do
<<-SQL_OUTPUT
root @ 127 . 0 . 0 . 1
root @ :: 1
@localhost
debian - sys - maint @localhost
root @localhost
usvn_user @localhost
@vagrant - ubuntu - raring - 64
SQL_OUTPUT
end
let ( :parsed_users ) { [ 'root@127.0.0.1' , 'root@::1' , 'debian-sys-maint@localhost' , 'root@localhost' , 'usvn_user@localhost' ] }
before :each do
# password hash = mypass
@resource = Puppet :: Type :: Database_user . new (
{ :password_hash = > '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4' , :name = > 'joe@localhost' }
)
@provider = provider_class . new ( @resource )
Facter . stubs ( :value ) . with ( :root_home ) . returns ( root_home )
Puppet :: Util . stubs ( :which ) . with ( " mysql " ) . returns ( " /usr/bin/mysql " )
subject . stubs ( :which ) . with ( " mysql " ) . returns ( " /usr/bin/mysql " )
subject . stubs ( :defaults_file ) . returns ( '--defaults-file=/root/.my.cnf' )
end
describe 'self.instances' do
it 'returns an array of users' do
subject . stubs ( :mysql ) . with ( [ defaults_file , 'mysql' , " -BNeselect concat(User, '@',Host) as User from mysql.user " ] ) . returns ( raw_users )
usernames = subject . instances . collect { | x | x . name }
parsed_users . should match_array ( usernames )
end
end
describe 'create' do
it 'makes a user' do
subject . expects ( :mysql ) . with ( [ defaults_file , 'mysql' , '-e' , " create user 'joe'@'localhost' identified by PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4' " ] )
2013-07-03 21:59:17 +02:00
@provider . expects ( :exists? ) . returns ( true )
@provider . create . should be_true
2013-07-02 23:42:44 +02:00
end
end
describe 'destroy' do
it 'removes a user if present' do
subject . expects ( :mysql ) . with ( [ defaults_file , 'mysql' , '-e' , " drop user 'joe'@'localhost' " ] )
2013-07-03 21:59:17 +02:00
@provider . expects ( :exists? ) . returns ( false )
@provider . destroy . should be_true
2013-07-02 23:42:44 +02:00
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' )
2013-07-03 21:59:17 +02:00
@provider . password_hash . should == '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'
2013-07-02 23:42:44 +02:00
end
end
describe 'password_hash=' do
it 'changes the hash' do
2013-07-03 21:59:17 +02:00
subject . expects ( :mysql ) . with ( [ defaults_file , 'mysql' , '-e' , " SET PASSWORD FOR 'joe'@'localhost' = '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5' " ] ) . returns ( '0' )
@provider . expects ( :password_hash ) . returns ( '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5' )
2013-07-02 23:42:44 +02:00
@provider . password_hash = ( '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF5' )
end
end
describe 'exists?' do
it 'checks if user exists' do
subject . expects ( :mysql ) . with ( [ defaults_file , 'mysql' , '-NBe' , " select '1' from mysql.user where CONCAT(user, '@', host) = 'joe@localhost' " ] ) . returns ( '1' )
@provider . exists? . should be_true
end
end
describe 'flush' do
it 'removes cached privileges' do
subject . expects ( :mysqladmin ) . with ( [ defaults_file , 'flush-privileges' ] )
@provider . flush
end
end
describe 'self.defaults_file' do
it 'sets --defaults-file' do
File . stubs ( :file? ) . with ( '#{root_home}/.my.cnf' ) . returns ( true )
@provider . defaults_file . should == '--defaults-file=/root/.my.cnf'
end
end
end