summaryrefslogtreecommitdiffstats
path: root/spec/unit/rails_spec.rb
blob: fe7fd8e290d90abc6add8abd4656ae8df5365d53 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env rspec
require 'spec_helper'
require 'puppet/rails'

describe Puppet::Rails, "when initializing any connection", :if => Puppet.features.rails? do
  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 "ActiveRecord Version" do
    it "should set ActiveRecord::Base.allow_concurrency if ActiveRecord is 2.1" do
      Puppet::Util.stubs(:activerecord_version).returns(2.1)
      ActiveRecord::Base.expects(:allow_concurrency=).with(true)

      Puppet::Rails.connect
    end

    it "should not set ActiveRecord::Base.allow_concurrency if ActiveRecord is >= 2.2" do
      Puppet::Util.stubs(:activerecord_version).returns(2.2)
      ActiveRecord::Base.expects(:allow_concurrency=).never

      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", :if => Puppet.features.rails? do
  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", :if => Puppet.features.rails? do
  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(:dbconnections).returns((pool_size = 45).to_s)
    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",
      :pool => pool_size,
      :database => "testname",
      :reconnect => true
    }
  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(:dbconnections).returns((pool_size = 12).to_s)
    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",
      :port => "9999",
      :username => "testuser",
      :password => "testpassword",
      :pool => pool_size,
      :database => "testname",
      :socket => "testsocket",
      :reconnect => true
    }
  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(:dbconnections).returns((pool_size = 23).to_s)
    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",
      :port => "9999",
      :username => "testuser",
      :password => "testpassword",
      :pool => pool_size,
      :database => "testname",
      :socket => "testsocket",
      :reconnect => true
    }
  end

  it "should not provide the pool if dbconnections is 0, '0', or ''" 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(0)
    Puppet::Rails.database_arguments.should_not be_include(:pool)

    Puppet.settings.stubs(:value).with(:dbconnections).returns('0')
    Puppet::Rails.database_arguments.should_not be_include(:pool)

    Puppet.settings.stubs(:value).with(:dbconnections).returns('')
    Puppet::Rails.database_arguments.should_not be_include(:pool)
  end
end

describe Puppet::Rails, "when initializing a postgresql connection", :if => Puppet.features.rails? do
  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(:dbconnections).returns((pool_size = 200).to_s)
    Puppet.settings.stubs(:value).with(:dbname).returns("testname")
    Puppet.settings.stubs(:value).with(:dbsocket).returns("")

    Puppet::Rails.database_arguments.should == {
      :adapter => "postgresql",
      :log_level => "testlevel",
      :host => "testserver",
      :port => "9999",
      :username => "testuser",
      :password => "testpassword",
      :pool => pool_size,
      :database => "testname",
      :reconnect => true
    }
  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(:dbconnections).returns((pool_size = 122).to_s)
    Puppet.settings.stubs(:value).with(:dbname).returns("testname")
    Puppet.settings.stubs(:value).with(:dbsocket).returns("testsocket")

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

  it "should not provide the pool if dbconnections is 0, '0', or ''" 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(0)
    Puppet::Rails.database_arguments.should_not be_include(:pool)

    Puppet.settings.stubs(:value).with(:dbconnections).returns('0')
    Puppet::Rails.database_arguments.should_not be_include(:pool)

    Puppet.settings.stubs(:value).with(:dbconnections).returns('')
    Puppet::Rails.database_arguments.should_not be_include(:pool)
  end
end

describe Puppet::Rails, "when initializing an Oracle connection", :if => Puppet.features.rails? do
  it "should provide the adapter, log_level, and username, password, 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(:dbconnections).returns((pool_size = 123).to_s)
    Puppet.settings.stubs(:value).with(:dbname).returns("testname")

    Puppet::Rails.database_arguments.should == {
      :adapter => "oracle_enhanced",
      :log_level => "testlevel",
      :username => "testuser",
      :password => "testpassword",
      :pool => pool_size,
      :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("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(:dbconnections).returns((pool_size = 124).to_s)
    Puppet.settings.stubs(:value).with(:dbname).returns("testname")

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

  it "should not provide the pool if dbconnections is 0, '0', or ''" 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(0)
    Puppet::Rails.database_arguments.should_not be_include(:pool)

    Puppet.settings.stubs(:value).with(:dbconnections).returns('0')
    Puppet::Rails.database_arguments.should_not be_include(:pool)

    Puppet.settings.stubs(:value).with(:dbconnections).returns('')
    Puppet::Rails.database_arguments.should_not be_include(:pool)
  end
end