Skip to content

Commit ec7e40e

Browse files
committed
Merge pull request #211 from apenney/specs
Add basic specs for database provider.
2 parents d365978 + 4d811e4 commit ec7e40e

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
require 'spec_helper'
2+
3+
provider_class = Puppet::Type.type(:database).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+
11+
let(:raw_databases) do
12+
<<-SQL_OUTPUT
13+
information_schema
14+
mydb
15+
mysql
16+
performance_schema
17+
test
18+
SQL_OUTPUT
19+
end
20+
21+
let(:parsed_databases) { ['information_schema', 'mydb', 'mysql', 'performance_schema', 'test'] }
22+
23+
before :each do
24+
@resource = Puppet::Type::Database.new(
25+
{ :charset => 'utf8', :name => 'new_database' }
26+
)
27+
@provider = provider_class.new(@resource)
28+
Facter.stubs(:value).with(:root_home).returns(root_home)
29+
Puppet::Util.stubs(:which).with("mysql").returns("/usr/bin/mysql")
30+
subject.stubs(:which).with("mysql").returns("/usr/bin/mysql")
31+
subject.stubs(:defaults_file).returns('--defaults-file=/root/.my.cnf')
32+
end
33+
34+
describe 'self.instances' do
35+
it 'returns an array of databases' do
36+
subject.stubs(:mysql).with([defaults_file, "-NBe", "show databases"]).returns(raw_databases)
37+
38+
databases = subject.instances.collect {|x| x.name }
39+
parsed_databases.should match_array(databases)
40+
end
41+
end
42+
43+
describe 'create' do
44+
it 'makes a user' do
45+
subject.expects(:mysql).with([defaults_file, '-NBe', "create database `#{@resource[:name]}` character set #{@resource[:charset]}"])
46+
@provider.create
47+
end
48+
end
49+
50+
describe 'destroy' do
51+
it 'removes a user if present' do
52+
subject.expects(:mysqladmin).with([defaults_file, '-f', 'drop', "#{@resource[:name]}"])
53+
@provider.destroy
54+
end
55+
end
56+
57+
describe 'charset' do
58+
it 'returns a charset' do
59+
subject.expects(:mysql).with([defaults_file, '-NBe', "show create database `#{@resource[:name]}`"]).returns('mydbCREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET utf8 */')
60+
@provider.charset.should == 'utf8'
61+
end
62+
end
63+
64+
describe 'charset=' do
65+
it 'changes the charset' do
66+
subject.expects(:mysql).with([defaults_file, '-NBe', "alter database `#{@resource[:name]}` CHARACTER SET blah"]).returns('0')
67+
68+
@provider.charset=('blah')
69+
end
70+
end
71+
72+
describe 'exists?' do
73+
it 'checks if user exists' do
74+
subject.expects(:mysql).with([defaults_file, '-NBe', "show databases"]).returns('information_schema\nmydb\nmysql\nperformance_schema\ntest')
75+
@provider.exists?
76+
end
77+
end
78+
79+
describe 'self.defaults_file' do
80+
it 'sets --defaults-file' do
81+
File.stubs(:file?).with('#{root_home}/.my.cnf').returns(true)
82+
@provider.defaults_file.should == '--defaults-file=/root/.my.cnf'
83+
end
84+
end
85+
86+
end

0 commit comments

Comments
 (0)