/
spec_helper.rb
198 lines (167 loc) · 5.14 KB
/
spec_helper.rb
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
# Copyright (c) 2009-2012 VMware, Inc.
$:.unshift(File.expand_path("../../lib", __FILE__))
require "rubygems"
require "bundler"
require "bundler/setup"
require "machinist/sequel"
require "rack/test"
require "steno"
require "cloud_controller"
require "rspec_let_monkey_patch"
module VCAP::CloudController
class SpecEnvironment
def initialize
FileUtils.mkdir_p artifacts_dir
File.unlink(log_filename) if File.exists?(log_filename)
Steno.init(Steno::Config.new(:default_log_level => "debug",
:sinks => [Steno::Sink::IO.for_file(log_filename)]))
VCAP::CloudController::DB.apply_migrations(db)
end
def spec_dir
File.expand_path("..", __FILE__)
end
def artifacts_dir
File.join(spec_dir, "artifacts")
end
def artifact_filename(name)
File.join(artifacts_dir, name)
end
def log_filename
artifact_filename("spec.log")
end
def reset_database
db.execute("PRAGMA foreign_keys = OFF")
db.tables.each do |table|
db.drop_table(table)
end
db.execute("PRAGMA foreign_keys = ON")
VCAP::CloudController::DB.apply_migrations(db)
end
def db
@db ||= VCAP::CloudController::DB.connect(
db_logger, :database => "sqlite:///", :log_level => "debug2")
end
def db_logger
return @db_logger if @db_logger
@db_logger = Steno.logger("cc.db")
if ENV["DB_LOG_LEVEL"]
level = ENV["DB_LOG_LEVEL"].downcase.to_sym
@db_logger.level = level if Steno::Logger::LEVELS.include? level
end
@db_logger
end
end
end
$spec_env = VCAP::CloudController::SpecEnvironment.new
module VCAP::CloudController::SpecHelper
def reset_database
$spec_env.reset_database
VCAP::CloudController::Models::QuotaDefinition.populate_from_config(config)
end
# Note that this method is mixed into each example, and so the instance
# variable we created here gets cleared automatically after each example
def config_override(hash)
@config_override ||= {}
@config_override.update(hash)
end
def config
config_file = File.expand_path("../../config/cloud_controller.yml", __FILE__)
c = VCAP::CloudController::Config.from_file(config_file)
c[:nginx][:use_nginx] = true
c = c.merge(@config_override || {})
VCAP::CloudController::Config.configure(c)
c
end
def configure
config
end
def create_zip(zip_name, file_count, file_size=1024)
total_size = file_count * file_size
files = []
file_count.times do |i|
tf = Tempfile.new("ziptest_#{i}")
files << tf
tf.write("A" * file_size)
tf.close
end
child = POSIX::Spawn::Child.new("zip", zip_name, *files.map(&:path))
child.status.exitstatus.should == 0
total_size
end
def with_em_and_thread(opts = {}, &blk)
auto_stop = opts.has_key?(:auto_stop) ? opts[:auto_stop] : true
Thread.abort_on_exception = true
EM.run do
EM.reactor_thread?.should == true
Thread.new do
EM.reactor_thread?.should == false
blk.call
EM.reactor_thread?.should == false
if auto_stop
EM.next_tick { EM.stop }
end
end
EM.reactor_thread?.should == true
end
end
RSpec::Matchers.define :be_recent do |expected|
match do |actual|
actual.should be_within(2).of(Time.now)
end
end
# @param [Hash] expecteds key-value pairs of messages and responses
# @return [#==]
RSpec::Matchers.define(:respond_with) do |expecteds|
match do |actual|
expecteds.all? do |message, matcher|
if matcher.respond_to?(:matches?)
matcher.matches?(actual.public_send(message))
else
matcher == actual.public_send(message)
end
end
end
end
RSpec::Matchers.define :json_match do |matcher|
# RSpect matcher?
if matcher.respond_to?(:matches?)
match do |json|
actual = Yajl::Parser.parse(json)
matcher.matches?(actual)
end
# regular values or RSpec Mocks argument matchers
else
match do |json|
actual = Yajl::Parser.parse(json)
matcher == actual
end
end
end
shared_examples "a vcap rest error response" do |description_match|
let(:decoded_response) { Yajl::Parser.parse(last_response.body) }
it "should contain a numeric code" do
decoded_response["code"].should_not be_nil
decoded_response["code"].should be_a_kind_of(Fixnum)
end
it "should contain a description" do
decoded_response["description"].should_not be_nil
decoded_response["description"].should be_a_kind_of(String)
end
if description_match
it "should contain a description that matches #{description_match}" do
decoded_response["description"].should match /#{description_match}/
end
end
end
end
RSpec.configure do |rspec_config|
rspec_config.include VCAP::CloudController
rspec_config.include Rack::Test::Methods
rspec_config.include VCAP::CloudController::SpecHelper
rspec_config.before(:each) do |example|
VCAP::CloudController::SecurityContext.clear
end
end
require "cloud_controller/models"
require "blueprints"
require "models/spec_helper.rb"