summaryrefslogtreecommitdiffstats
path: root/spec/unit/rails.rb
blob: f0ba2b9425c26c350ca1e3f928dde5c336aaac6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../spec_helper'
require 'puppet/rails'

describe Puppet::Rails, "when initializing any connection" do
    confine "Cannot test without ActiveRecord" => Puppet.features.rails?

    before do
        @logger = stub 'logger', :level= => nil
        @logger.stub_everything
        Logger.stubs(:new).returns(@logger)

        ActiveRecord::Base.stubs(:logger).returns(@logger)
    end

    it "should use settings" do
        Puppet.settings.expects(:use).with(:main, :rails, :puppetmasterd)
        
        Puppet::Rails.connect
    end
    
    it "should set up a logger with the appropriate Rails log file" do
        logger = mock 'logger'
        Logger.expects(:new).with(Puppet[:railslog]).returns(logger)
        ActiveRecord::Base.expects(:logger=).with(logger)
        
        Puppet::Rails.connect
    end
    
    it "should set the log level to whatever the value is in the settings" do
        Puppet.settings.stubs(:use)
        Puppet.settings.stubs(:value).with(:rails_loglevel).returns("debug")
        Puppet.settings.stubs(:value).with(:railslog).returns("/my/file")
        logger = mock 'logger'
        Logger.stubs(:new).returns(logger)
        ActiveRecord::Base.stubs(:logger).returns(logger)
        logger.expects(:level=).with(Logger::DEBUG)

        ActiveRecord::Base.stubs(:allow_concurrency=)
        ActiveRecord::Base.stubs(:verify_active_connections!)
        ActiveRecord::Base.stubs(:establish_connection)
        Puppet::Rails.stubs(:database_arguments)
        
        Puppet::Rails.connect
    end
    
    it "should set ActiveRecord::Base.allow_concurrency" do
        ActiveRecord::Base.expects(:allow_concurrency=).with(true)
        
        Puppet::Rails.connect
    end
    
    it "should call ActiveRecord::Base.verify_active_connections!" do
        ActiveRecord::Base.expects(:verify_active_connections!)
        
        Puppet::Rails.connect
    end
    
    it "should call ActiveRecord::Base.establish_connection with database_arguments" do
        Puppet::Rails.expects(:database_arguments)
        ActiveRecord::Base.expects(:establish_connection)
        
        Puppet::Rails.connect
    end
end

describe Puppet::Rails, "when initializing a sqlite3 connection" do
    confine "Cannot test without ActiveRecord" => Puppet.features.rails?

    it "should provide the adapter, log_level, and dbfile arguments" do
        Puppet.settings.expects(:value).with(:dbadapter).returns("sqlite3")
        Puppet.settings.expects(:value).with(:rails_loglevel).returns("testlevel")
        Puppet.settings.expects(:value).with(:dblocation).returns("testlocation")
        
        Puppet::Rails.database_arguments.should == {
            :adapter => "sqlite3",
            :log_level => "testlevel",
            :dbfile => "testlocation"
        }
    end
end

describe Puppet::Rails, "when initializing a mysql or postgresql connection" do
    confine "Cannot test without ActiveRecord" => Puppet.features.rails?

    it "should provide the adapter, log_level, and host, username, password, and database arguments" do
        Puppet.settings.stubs(:value).with(:dbadapter).returns("mysql")
        Puppet.settings.stubs(:value).with(:rails_loglevel).returns("testlevel")
        Puppet.settings.stubs(:value).with(:dbserver).returns("testserver")
        Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
        Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
        Puppet.settings.stubs(:value).with(:dbname).returns("testname")
        Puppet.settings.stubs(:value).with(:dbsocket).returns("")
        
        Puppet::Rails.database_arguments.should == {
            :adapter => "mysql",
            :log_level => "testlevel",
            :host => "testserver",
            :username => "testuser",
            :password => "testpassword",
            :database => "testname"
        }
    end
    
    it "should provide the adapter, log_level, and host, username, password, database, and socket arguments" do
        Puppet.settings.stubs(:value).with(:dbadapter).returns("mysql")
        Puppet.settings.stubs(:value).with(:rails_loglevel).returns("testlevel")
        Puppet.settings.stubs(:value).with(:dbserver).returns("testserver")
        Puppet.settings.stubs(:value).with(:dbuser).returns("testuser")
        Puppet.settings.stubs(:value).with(:dbpassword).returns("testpassword")
        Puppet.settings.stubs(:value).with(:dbname).returns("testname")
        Puppet.settings.stubs(:value).with(:dbsocket).returns("testsocket")
        
        Puppet::Rails.database_arguments.should == {
            :adapter => "mysql",
            :log_level => "testlevel",
            :host => "testserver",
            :username => "testuser",
            :password => "testpassword",
            :database => "testname",
            :socket => "testsocket"
        }
    end
end