summaryrefslogtreecommitdiffstats
path: root/spec/unit/rails_spec.rb
blob: eaa9680999adec9ef9f8e63662ee784b0682798d (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/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
    Puppet.settings.stubs(:use)
    @logger = mock 'logger'
    @logger.stub_everything
    Logger.stubs(:new).returns(@logger)

    ActiveRecord::Base.stubs(:logger).returns(@logger)
    ActiveRecord::Base.stubs(:connected?).returns(false)
  end

  it "should use settings" do
    Puppet.settings.expects(:use).with(:main, :rails, :master)

    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).returns({})

    Puppet::Rails.connect
  end

  describe "on ActiveRecord 2.1.x" do
    confine("ActiveRecord 2.1.x") { ::ActiveRecord::VERSION::MAJOR == 2 and ::ActiveRecord::VERSION::MINOR <= 1 }

    it "should set ActiveRecord::Base.allow_concurrency" do
      ActiveRecord::Base.expects(:allow_concurrency=).with(true)

      Puppet::Rails.connect
    end
  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).returns({})
    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 database 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",
      :database  => "testlocation"
    }
  end
end

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

  it "should provide the adapter, log_level, and host, port, username, password, database, and reconnect 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(:dbport).returns("")
    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.settings.stubs(:value).with(:dbconnections).returns(1)

    Puppet::Rails.database_arguments.should == {
      :adapter => "mysql",
      :log_level => "testlevel",
      :host => "testserver",
      :username => "testuser",
      :password => "testpassword",
      :database => "testname",
      :reconnect => true,
      :pool => 1
    }
  end

  it "should provide the adapter, log_level, and host, port, username, password, database, socket, connections, and reconnect 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(:dbport).returns("9999")
    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.settings.stubs(:value).with(:dbconnections).returns(1)

    Puppet::Rails.database_arguments.should == {
      :adapter => "mysql",
      :log_level => "testlevel",
      :host => "testserver",
      :port => "9999",
      :username => "testuser",
      :password => "testpassword",
      :database => "testname",
      :socket => "testsocket",
      :reconnect => true,
      :pool => 1
    }
  end

  it "should provide the adapter, log_level, and host, port, username, password, database, socket, and connections 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(:dbport).returns("9999")
    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.settings.stubs(:value).with(:dbconnections).returns(1)

    Puppet::Rails.database_arguments.should == {
      :adapter => "mysql",
      :log_level => "testlevel",
      :host => "testserver",
      :port => "9999",
      :username => "testuser",
      :password => "testpassword",
      :database => "testname",
      :socket => "testsocket",
      :reconnect => true,
      :pool => 1
    }
  end
end

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

  it "should provide the adapter, log_level, and host, port, username, password, connections, and database arguments" do
    Puppet.settings.stubs(:value).with(:dbadapter).returns("postgresql")
    Puppet.settings.stubs(:value).with(:rails_loglevel).returns("testlevel")
    Puppet.settings.stubs(:value).with(:dbserver).returns("testserver")
    Puppet.settings.stubs(:value).with(:dbport).returns("9999")
    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.settings.stubs(:value).with(:dbconnections).returns(1)

    Puppet::Rails.database_arguments.should == {
      :adapter => "postgresql",
      :log_level => "testlevel",
      :host => "testserver",
      :port => "9999",
      :username => "testuser",
      :password => "testpassword",
      :database => "testname",
      :reconnect => true,
      :pool => 1
    }
  end

  it "should provide the adapter, log_level, and host, port, username, password, database, connections, and socket arguments" do
    Puppet.settings.stubs(:value).with(:dbadapter).returns("postgresql")
    Puppet.settings.stubs(:value).with(:rails_loglevel).returns("testlevel")
    Puppet.settings.stubs(:value).with(:dbserver).returns("testserver")
    Puppet.settings.stubs(:value).with(:dbport).returns("9999")
    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.settings.stubs(:value).with(:dbconnections).returns(1)

    Puppet::Rails.database_arguments.should == {
      :adapter => "postgresql",
      :log_level => "testlevel",
      :host => "testserver",
      :port => "9999",
      :username => "testuser",
      :password => "testpassword",
      :database => "testname",
      :socket => "testsocket",
      :pool => 1,
      :reconnect => true
    }
  end
end

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

  it "should provide the adapter, log_level, and username, password, dbconnections, and database arguments" do
    Puppet.settings.stubs(:value).with(:dbadapter).returns("oracle_enhanced")
    Puppet.settings.stubs(:value).with(:rails_loglevel).returns("testlevel")
    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(:dbconnections).returns(1)

    Puppet::Rails.database_arguments.should == {
      :adapter => "oracle_enhanced",
      :log_level => "testlevel",
      :username => "testuser",
      :password => "testpassword",
      :database => "testname",
      :pool => 1
    }
  end

  it "should provide the adapter, log_level, and host, username, password, database, pool, and socket arguments" do
    Puppet.settings.stubs(:value).with(:dbadapter).returns("oracle_enhanced")
    Puppet.settings.stubs(:value).with(:rails_loglevel).returns("testlevel")
    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(:dbconnections).returns(1)

    Puppet::Rails.database_arguments.should == {
      :adapter => "oracle_enhanced",
      :log_level => "testlevel",
      :username => "testuser",
      :password => "testpassword",
      :database => "testname",
      :pool => 1
    }
  end
end