|
| 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