File: rails_spec.rb

package info (click to toggle)
puppet 3.7.2-4+deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 18,912 kB
  • ctags: 13,168
  • sloc: ruby: 210,410; sh: 2,050; xml: 1,554; lisp: 300; makefile: 142; python: 108; sql: 103; yacc: 72
file content (244 lines) | stat: -rwxr-xr-x 8,198 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/env ruby
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[:rails_loglevel] = "debug"
    Puppet[:railslog] = "/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(:clear_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.clear_active_connections!" do
    ActiveRecord::Base.expects(:clear_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[:dbadapter] = "sqlite3"
    Puppet[:rails_loglevel] = "testlevel"
    Puppet[:dblocation] = File.expand_path("testlocation")

    Puppet::Rails.database_arguments.should == {
      :adapter   => "sqlite3",
      :log_level => "testlevel",
      :database  => File.expand_path("testlocation")
    }
  end
end

['mysql','mysql2','postgresql'].each do |dbadapter|
  describe Puppet::Rails, "when initializing a #{dbadapter} connection", :if => Puppet.features.rails? do
    it "should provide the adapter, log_level, and host, port, username, password, database, and reconnect arguments" do
      Puppet[:dbadapter] = dbadapter
      Puppet[:rails_loglevel] = "testlevel"
      Puppet[:dbserver] = "testserver"
      Puppet[:dbport] = ""
      Puppet[:dbuser] = "testuser"
      Puppet[:dbpassword] = "testpassword"
      Puppet[:dbconnections] = (pool_size = 45).to_s
      Puppet[:dbname] = "testname"
      Puppet[:dbsocket] = ""

      Puppet::Rails.database_arguments.should == {
        :adapter => dbadapter,
        :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[:dbadapter] = dbadapter
      Puppet[:rails_loglevel] = "testlevel"
      Puppet[:dbserver] = "testserver"
      Puppet[:dbport] = "9999"
      Puppet[:dbuser] = "testuser"
      Puppet[:dbpassword] = "testpassword"
      Puppet[:dbconnections] = (pool_size = 12).to_s
      Puppet[:dbname] = "testname"
      Puppet[:dbsocket] = "testsocket"

      Puppet::Rails.database_arguments.should == {
        :adapter => dbadapter,
        :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[:dbadapter] = dbadapter
      Puppet[:rails_loglevel] = "testlevel"
      Puppet[:dbserver] = "testserver"
      Puppet[:dbport] = "9999"
      Puppet[:dbuser] = "testuser"
      Puppet[:dbpassword] = "testpassword"
      Puppet[:dbconnections] = (pool_size = 23).to_s
      Puppet[:dbname] = "testname"
      Puppet[:dbsocket] = "testsocket"

      Puppet::Rails.database_arguments.should == {
        :adapter => dbadapter,
        :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[:dbadapter] = dbadapter
      Puppet[:rails_loglevel] = "testlevel"
      Puppet[:dbserver] = "testserver"
      Puppet[:dbport] = "9999"
      Puppet[:dbuser] = "testuser"
      Puppet[:dbpassword] = "testpassword"
      Puppet[:dbname] = "testname"
      Puppet[:dbsocket] = "testsocket"

      Puppet[:dbconnections] = 0
      Puppet::Rails.database_arguments.should_not be_include(:pool)

      Puppet[:dbconnections] = '0'
      Puppet::Rails.database_arguments.should_not be_include(:pool)

      Puppet[:dbconnections] = ''
      Puppet::Rails.database_arguments.should_not be_include(:pool)
    end
  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[:dbadapter] = "oracle_enhanced"
    Puppet[:rails_loglevel] = "testlevel"
    Puppet[:dbuser] = "testuser"
    Puppet[:dbpassword] = "testpassword"
    Puppet[:dbconnections] = (pool_size = 123).to_s
    Puppet[:dbname] = "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[:dbadapter] = "oracle_enhanced"
    Puppet[:rails_loglevel] = "testlevel"
    Puppet[:dbuser] = "testuser"
    Puppet[:dbpassword] = "testpassword"
    Puppet[:dbconnections] = (pool_size = 124).to_s
    Puppet[:dbname] = "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[:dbadapter] = "oracle_enhanced"
    Puppet[:rails_loglevel] = "testlevel"
    Puppet[:dbserver] = "testserver"
    Puppet[:dbport] = "9999"
    Puppet[:dbuser] = "testuser"
    Puppet[:dbpassword] = "testpassword"
    Puppet[:dbname] = "testname"
    Puppet[:dbsocket] = "testsocket"

    Puppet[:dbconnections] = 0
    Puppet::Rails.database_arguments.should_not be_include(:pool)

    Puppet[:dbconnections] = '0'
    Puppet::Rails.database_arguments.should_not be_include(:pool)

    Puppet[:dbconnections] = ''
    Puppet::Rails.database_arguments.should_not be_include(:pool)
  end
end