Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

made MessageBus a class

Change-Id: Ib499fc9e82d326eb31769a8ec7371f8f788239bc
  • Loading branch information...
commit 16e4126286fb659475255ac7f54296430b4402d7 1 parent 3f3d12e
@d d authored
View
8 lib/cloud_controller/api/app.rb
@@ -63,10 +63,10 @@ def after_update(app, changes)
end
def send_droplet_updated_message(app)
- json = Yajl::Encoder.encode(:droplet => app.guid,
- :cc_partition => config[:cc_partition])
- MessageBus.publish("droplet.updated", json)
- nil
+ MessageBus.instance.publish("droplet.updated", Yajl::Encoder.encode(
+ :droplet => app.guid,
+ :cc_partition => config[:cc_partition]
+ ))
end
def self.translate_validation_exception(e, attributes)
View
3  lib/cloud_controller/app_stager.rb
@@ -22,7 +22,8 @@ def stage_app(app)
LegacyStaging.with_upload_handle(app.guid) do |handle|
client_error = nil
results = EM.schedule_sync do |promise|
- client = VCAP::Stager::Client::EmAware.new(MessageBus.nats.client, queue)
+ client = VCAP::Stager::Client::EmAware.new(MessageBus.instance.nats.client, queue)
+
request = staging_request(app)
logger.debug "staging #{app.guid} request: #{request}"
deferrable = client.stage(request, staging_timeout)
View
17 lib/cloud_controller/config.rb
@@ -143,19 +143,20 @@ def self.merge_defaults(config)
end
def self.configure(config)
- # TODO: this introduces 2 config styles. CC takes config
- # via per instance constructor. Remove that in favor of this
- # method as there will be more along these lines.
- VCAP::CloudController::MessageBus.configure(config)
+ mbus = VCAP::CloudController::MessageBus.new(config)
+ VCAP::CloudController::MessageBus.instance = mbus
+
VCAP::CloudController::AccountCapacity.configure(config)
VCAP::CloudController::ResourcePool.configure(config)
VCAP::CloudController::AppPackage.configure(config)
VCAP::CloudController::AppStager.configure(config)
VCAP::CloudController::LegacyStaging.configure(config)
- VCAP::CloudController::DeaPool.configure(config)
- VCAP::CloudController::DeaClient.configure(config)
- VCAP::CloudController::HealthManagerClient.configure
- VCAP::CloudController::LegacyBulk.configure(config)
+
+ VCAP::CloudController::DeaPool.configure(config, mbus)
+ VCAP::CloudController::DeaClient.configure(config, mbus)
+ VCAP::CloudController::HealthManagerClient.configure(mbus)
+
+ VCAP::CloudController::LegacyBulk.configure(config, mbus)
VCAP::CloudController::Models::QuotaDefinition.configure(config)
end
View
2  lib/cloud_controller/dea/dea_client.rb
@@ -24,7 +24,7 @@ class << self
attr_reader :config, :message_bus, :dea_pool
- def configure(config, message_bus = MessageBus, dea_pool = DeaPool)
+ def configure(config, message_bus, dea_pool = DeaPool)
@config = config
@message_bus = message_bus
@dea_pool = dea_pool
View
2  lib/cloud_controller/dea/dea_pool.rb
@@ -9,7 +9,7 @@ module DeaPool
class << self
attr_reader :config, :message_bus
- def configure(config, message_bus = MessageBus)
+ def configure(config, message_bus)
@config = config
@message_bus = message_bus
@deas = {}
View
2  lib/cloud_controller/health_manager_client.rb
@@ -5,7 +5,7 @@ module HealthManagerClient
class << self
attr_reader :message_bus
- def configure(message_bus = MessageBus)
+ def configure(message_bus)
@message_bus = message_bus
end
View
7 lib/cloud_controller/legacy_api/legacy_bulk.rb
@@ -37,13 +37,13 @@ class LegacyBulk < RestController::Base
allow_unauthenticated_access
class << self
- attr_reader :message_bus, :config
+ attr_reader :config, :message_bus
- def configure(config)
- @message_bus = config.fetch(:message_bus, MessageBus)
+ def configure(config, message_bus)
@config = config[:bulk_api].merge(
:cc_partition => config.fetch(:cc_partition),
)
+ @message_bus = message_bus
end
def register_subscription
@@ -62,7 +62,6 @@ def credentials
config[:auth_password],
]
end
-
end
def initialize(*)
View
51 lib/cloud_controller/message_bus.rb
@@ -4,19 +4,24 @@
require "vcap/component"
require "cloud_controller/json_patch"
-module VCAP::CloudController::MessageBus
+class VCAP::CloudController::MessageBus
class << self
- attr_reader :config
- attr_reader :nats
+ attr_reader :instance
- def configure(config)
- @config = config
- @nats = config[:nats] || NATS
+ def instance=(instance)
+ raise ArgumentError, "instance must not be nil" unless instance
+ @instance = instance
end
end
- def self.register_components
- # hook up with NATS
+ attr_reader :config, :nats
+
+ def initialize(config)
+ @config = config
+ @nats = config[:nats] || NATS
+ end
+
+ def register_components
# TODO: put useful metrics in varz
# TODO: subscribe to the two DEA channels
EM.schedule do
@@ -32,7 +37,7 @@ def self.register_components
end
end
- def self.register_routes
+ def register_routes
EM.schedule do
# TODO: blacklist api2 in legacy CC
# TODO: Yajl should probably also be injected
@@ -42,7 +47,9 @@ def self.register_routes
:uris => [config[:external_domain]],
:tags => {:component => "CloudController" },
})
+
nats.publish("router.register", router_register_message)
+
# Broadcast when a router restarts
nats.subscribe("router.start") do
nats.publish("router.register", router_register_message)
@@ -59,7 +66,7 @@ def self.register_routes
# @yield [payload, inbox] callback invoked when a message is posted on the subject
# @yieldparam [String] payload the message posted on the channel
# @yieldparam [optional, String] inbox an optional "reply to" subject, nil if not requested
- def self.subscribe(subject, opts = {}, &blk)
+ def subscribe(subject, opts = {}, &blk)
subscribe_on_reactor(subject, opts) do |payload, inbox|
EM.defer do
begin
@@ -74,21 +81,13 @@ def self.subscribe(subject, opts = {}, &blk)
end
end
- def self.subscribe_on_reactor(subject, opts = {}, &blk)
- EM.schedule do
- nats.subscribe(subject, opts) do |msg, inbox|
- process_message(msg, inbox, &blk)
- end
- end
- end
-
- def self.publish(subject, message = nil)
+ def publish(subject, message = nil)
EM.schedule do
nats.publish(subject, message)
end
end
- def self.request(subject, data = nil, opts = {})
+ def request(subject, data = nil, opts = {})
opts ||= {}
expected = opts[:expected] || 1
timeout = opts[:timeout] || -1
@@ -115,14 +114,22 @@ def self.request(subject, data = nil, opts = {})
private
- def self.process_message(msg, inbox, &blk)
+ def subscribe_on_reactor(subject, opts = {}, &blk)
+ EM.schedule do
+ nats.subscribe(subject, opts) do |msg, inbox|
+ process_message(msg, inbox, &blk)
+ end
+ end
+ end
+
+ def process_message(msg, inbox, &blk)
payload = Yajl::Parser.parse(msg, :symbolize_keys => true)
blk.yield(payload, inbox)
rescue => e
logger.error "exception processing: '#{msg}' '#{e}'"
end
- def self.logger
+ def logger
@logger ||= Steno.logger("cc.mbus")
end
end
View
4 lib/cloud_controller/runner.rb
@@ -116,8 +116,8 @@ def run!
# TODO: we really should put these bootstrapping into a place other
# than Rack::Builder
use Rack::CommonLogger
- VCAP::CloudController::MessageBus.register_components
- VCAP::CloudController::MessageBus.register_routes
+ VCAP::CloudController::MessageBus.instance.register_components
+ VCAP::CloudController::MessageBus.instance.register_routes
VCAP::CloudController::DeaPool.register_subscriptions
VCAP::CloudController::LegacyBulk.register_subscription
VCAP::CloudController.health_manager_respondent = VCAP::CloudController::HealthManagerRespondent.new(config)
View
89 spec/api/app_spec.rb
@@ -108,7 +108,7 @@ module VCAP::CloudController
app_obj.save
app_obj.needs_staging?.should be_false
- MessageBus.should_not_receive(:publish).with("droplet.updated", anything)
+ MessageBus.instance.should_not_receive(:publish).with("droplet.updated", anything)
req = Yajl::Encoder.encode(:instances => app_obj.instances + 1)
put "/v2/apps/#{app_obj.guid}", req, json_headers(admin_headers)
@@ -126,7 +126,7 @@ module VCAP::CloudController
"cc_partition" => config[:cc_partition],
}
- MessageBus.should_not_receive(:publish)
+ MessageBus.instance.should_not_receive(:publish)
req = Yajl::Encoder.encode(:instances => app_obj.instances + 1)
put "/v2/apps/#{app_obj.guid}", req, json_headers(admin_headers)
@@ -138,17 +138,18 @@ module VCAP::CloudController
app_obj.state = "STARTED"
app_obj.save
app_obj.needs_staging?.should be_true
+
AppStager.should_receive(:stage_app) do |app|
app.update(:droplet_hash => "def")
end
- MessageBus.should_receive(:publish).with(
+
+ MessageBus.instance.should_receive(:publish).with(
"droplet.updated",
- json_match(
- hash_including(
- "droplet" => app_obj.guid,
- ),
- ),
+ json_match(hash_including(
+ "droplet" => app_obj.guid,
+ )),
)
+
req = Yajl::Encoder.encode(:instances => app_obj.instances + 1)
put "/v2/apps/#{app_obj.guid}", req, json_headers(admin_headers)
last_response.status.should == 201
@@ -175,9 +176,10 @@ module VCAP::CloudController
"cc_partition" => config[:cc_partition],
}
- MessageBus.should_receive(:publish).
- with("droplet.updated",
- json_match(hash_including(expected)))
+ MessageBus.instance.should_receive(:publish).with(
+ "droplet.updated",
+ json_match(hash_including(expected))
+ )
put "/v2/apps/#{app_obj.guid}", req, json_headers(admin_headers)
last_response.status.should == 201
@@ -194,9 +196,10 @@ module VCAP::CloudController
"cc_partition" => config[:cc_partition],
}
- MessageBus.should_receive(:publish).
- with("droplet.updated",
- json_match(hash_including(expected)))
+ MessageBus.instance.should_receive(:publish).with(
+ "droplet.updated",
+ json_match(hash_including(expected))
+ )
put "/v2/apps/#{app_obj.guid}", req, json_headers(admin_headers)
last_response.status.should == 201
@@ -225,9 +228,10 @@ module VCAP::CloudController
"cc_partition" => config[:cc_partition],
}
- MessageBus.should_receive(:publish).
- with("droplet.updated",
- json_match(hash_including(expected)))
+ MessageBus.instance.should_receive(:publish).with(
+ "droplet.updated",
+ json_match(hash_including(expected))
+ )
put "/v2/apps/#{app_obj.guid}", req, json_headers(admin_headers)
last_response.status.should == 201
@@ -301,25 +305,20 @@ module VCAP::CloudController
:space => space,
)
- nats = double("mock nats")
- config_override(:nats => nats)
- nats.should_receive(:publish).with(
+ MessageBus.instance.should_receive(:publish).with(
"dea.update",
json_match(
hash_including("uris" => ["app.jesse.cloud"]),
),
)
- EM.run do
- put(
- @app_url,
- App::UpdateMessage.new(
- :route_guids => [route.guid],
- ).encode(),
- @headers_for_user,
- )
- EM.next_tick { EM.stop }
- end
+ put(
+ @app_url,
+ App::UpdateMessage.new(
+ :route_guids => [route.guid],
+ ).encode(),
+ @headers_for_user,
+ )
last_response.status.should == 201
end
@@ -341,28 +340,20 @@ module VCAP::CloudController
r["metadata"]["guid"]
}.sort.should == [bar_route.guid, route.guid].sort
- nats = double("mock nats")
- config_override(:nats => nats)
- # inject mock nats
- MessageBus.configure(config)
-
- nats.should_receive(:publish).with(
+ MessageBus.instance.should_receive(:publish).with(
"dea.update",
- json_match(
- hash_including("uris" => ["foo.jesse.cloud"]),
- ),
+ json_match(hash_including(
+ "uris" => ["foo.jesse.cloud"],
+ )),
)
- EM.run do
- put(
- @app_url,
- App::UpdateMessage.new(
- :route_guids => [route.guid],
- ).encode,
- @headers_for_user,
- )
- EM.stop
- end
+ put(
+ @app_url,
+ App::UpdateMessage.new(
+ :route_guids => [route.guid],
+ ).encode,
+ @headers_for_user,
+ )
last_response.status.should == 201
end
View
131 spec/api/legacy_apps_spec.rb
@@ -24,8 +24,10 @@ module VCAP::CloudController
before do
@apps = []
7.times do
- @apps << Models::App.make(:space => user.default_space,
- :environment_json => {})
+ @apps << Models::App.make(
+ :space => user.default_space,
+ :environment_json => {}
+ )
end
3.times do
@@ -670,10 +672,10 @@ module VCAP::CloudController
it "sends a dea.update message when adding a URL to a running app" do
app = Models::App.make(
- :name => app_name,
- :space => user.default_space,
- :state => "STARTED",
- )
+ :name => app_name,
+ :space => user.default_space,
+ :state => "STARTED",
+ )
app.update(
# I hate that implicit save on package_hash=,
@@ -683,34 +685,30 @@ module VCAP::CloudController
:package_state => "STAGED",
)
- nats = double("mock nats")
- nats.should_receive(:publish).with(
+ MessageBus.instance.should_receive(:publish).with(
"dea.update",
- json_match(
- hash_including("uris" => [uri1, uri2]),
- ),
+ json_match(hash_including(
+ "uris" => [uri1, uri2]
+ )),
)
- config_override(:nats => nats)
- EM.run do
- put(
- "/apps/#{app_name}",
- Yajl::Encoder.encode(
- :uris => [uri1, uri2],
- ),
- headers_for(user),
- )
- last_response.status.should == 200
- EM.next_tick { EM.stop }
- end
+ put(
+ "/apps/#{app_name}",
+ Yajl::Encoder.encode(
+ :uris => [uri1, uri2],
+ ),
+ headers_for(user),
+ )
+ last_response.status.should == 200
end
it "sends a dea.update message when removing a URL to a running app" do
app = Models::App.make(
- :name => app_name,
- :space => user.default_space,
- :state => "STARTED",
- )
+ :name => app_name,
+ :space => user.default_space,
+ :state => "STARTED",
+ )
+
app.update(
# I hate that implicit save on package_hash=,
# but I'll bare with it while nothing breaks...
@@ -719,38 +717,30 @@ module VCAP::CloudController
:package_state => "STAGED",
)
- black_hole = double
- black_hole.stub(:publish)
- config_override(:nats => black_hole)
+ put(
+ "/apps/#{app_name}",
+ Yajl::Encoder.encode(
+ :uris => [uri1, uri2],
+ ),
+ headers_for(user),
+ )
+ last_response.status.should == 200
- nats = double("mock nats")
- nats.should_receive(:publish).with(
+ MessageBus.instance.should_receive(:publish).with(
"dea.update",
- json_match(
- hash_including("uris" => [uri2]),
- ),
+ json_match(hash_including(
+ "uris" => [uri2]
+ )),
)
- EM.run do
- put(
- "/apps/#{app_name}",
- Yajl::Encoder.encode(
- :uris => [uri1, uri2],
- ),
- headers_for(user),
- )
- last_response.status.should == 200
- config_override(:nats => nats)
- put(
- "/apps/#{app_name}",
- Yajl::Encoder.encode(
- :uris => [uri2],
- ),
- headers_for(user),
- )
- last_response.status.should == 200
- EM.next_tick { EM.stop }
- end
+ put(
+ "/apps/#{app_name}",
+ Yajl::Encoder.encode(
+ :uris => [uri2],
+ ),
+ headers_for(user),
+ )
+ last_response.status.should == 200
end
it "does not send a dea.update message when adding a URL to a stopped app" do
@@ -759,20 +749,17 @@ module VCAP::CloudController
:space => user.default_space,
:state => "STOPPED",
)
- nats = double("mock nats")
- nats.should_not_receive(:publish)
- config_override(:nats => nats)
- EM.run do
- put(
- "/apps/#{app_name}",
- Yajl::Encoder.encode(
- :uris => [uri1, uri2],
- ),
- headers_for(user),
- )
- last_response.status.should == 200
- EM.next_tick { EM.stop }
- end
+
+ MessageBus.instance.should_not_receive(:publish)
+
+ put(
+ "/apps/#{app_name}",
+ Yajl::Encoder.encode(
+ :uris => [uri1, uri2],
+ ),
+ headers_for(user),
+ )
+ last_response.status.should == 200
end
it "does not send a dea.update message when removing a URL to a stopped app" do
@@ -781,9 +768,9 @@ module VCAP::CloudController
:space => user.default_space,
:state => "STOPPED",
)
- nats = double("mock nats")
- nats.should_not_receive(:publish)
- config_override(:nats => nats)
+
+ MessageBus.instance.should_not_receive(:publish)
+
EM.run do
put(
"/apps/#{app_name}",
View
31 spec/api/legacy_bulk_spec.rb
@@ -8,28 +8,28 @@
module VCAP::CloudController
module VCAP::CloudController
describe LegacyBulk do
- before :each do
+ let(:mbus) { MockMessageBus.new({}) }
+
+ before do
@bulk_user = "bulk_user"
@bulk_password = "bulk_password"
end
describe ".register_subscription" do
it "should be able to discover credentials through NATS" do
- message_bus = double("mock message bus")
- config_override(
- :message_bus => message_bus,
- :cc_partition => "ng",
- )
- LegacyBulk.configure(config)
- message_bus.should_receive(:subscribe).
- with("cloudcontroller.bulk.credentials.ng").
- and_yield("xxx", "inbox")
- message_bus.should_receive(:publish).with("inbox", anything) do |_, msg|
+ LegacyBulk.configure(config, mbus)
+
+ mbus.should_receive(:subscribe)
+ .with("cloudcontroller.bulk.credentials.ng")
+ .and_yield("xxx", "inbox")
+
+ mbus.should_receive(:publish).with("inbox", anything) do |_, msg|
Yajl::Parser.parse(msg).should == {
"user" => @bulk_user,
"password" => @bulk_password,
}
end
+
LegacyBulk.register_subscription
end
end
@@ -39,11 +39,10 @@ module VCAP::CloudController
reset_database
@framework = Models::Framework.make
10.times do
- Models::App.make(
- :framework => @framework,
- )
+ Models::App.make(:framework => @framework)
end
end
+
it "requires authentication" do
get "/bulk/apps"
last_response.status.should == 401
@@ -54,7 +53,7 @@ module VCAP::CloudController
end
describe "with authentication" do
- before :each do
+ before do
authorize @bulk_user, @bulk_password
end
@@ -92,7 +91,6 @@ module VCAP::CloudController
}
end
-
it "respects the batch_size parameter" do
[3,5].each { |size|
get "/bulk/apps", {
@@ -144,7 +142,6 @@ module VCAP::CloudController
}
decoded_response["results"].size.should == 0
end
-
end
end
View
71 spec/api/route_spec.rb
@@ -267,36 +267,28 @@ module VCAP::CloudController
r["metadata"]["guid"]
}.should eq [@foo_app.guid]
- nats = double("mock nats")
- config_override(:nats => nats)
- # inject mock nats
- MessageBus.configure(config)
-
- nats.should_receive(:publish).with(
+ MessageBus.instance.should_receive(:publish).with(
"dea.update",
- json_match(
- hash_including(
- "uris" => ["foo.jesse.cloud"],
- "droplet" => @bar_app.guid,
- ),
- ),
+ json_match(hash_including(
+ "uris" => ["foo.jesse.cloud"],
+ "droplet" => @bar_app.guid,
+ )),
+ )
+
+ put(
+ "/v2/routes/#{@route.guid}",
+ Route::UpdateMessage.new(
+ :app_guids => [@foo_app.guid, @bar_app.guid],
+ ).encode,
+ @headers_for_user,
)
- EM.run do
- put(
- "/v2/routes/#{@route.guid}",
- Route::UpdateMessage.new(
- :app_guids => [@foo_app.guid, @bar_app.guid],
- ).encode,
- @headers_for_user,
- )
- EM.next_tick { EM.stop }
- end
last_response.status.should == 201
end
it "sends a dea.update message after removing an app" do
@foo_app.add_route(@route)
@bar_app.add_route(@route)
+
get "/v2/routes/#{@route.guid}/apps", {}, @headers_for_user
last_response.status.should == 200
decoded_response["total_results"].should eq(2)
@@ -304,30 +296,21 @@ module VCAP::CloudController
r["metadata"]["guid"]
}.sort.should eq [@foo_app.guid, @bar_app.guid].sort
- nats = double("mock nats")
- config_override(:nats => nats)
- # inject mock nats
- MessageBus.configure(config)
-
- nats.should_receive(:publish).with(
+ MessageBus.instance.should_receive(:publish).with(
"dea.update",
- json_match(
- hash_including(
- "uris" => [],
- "droplet" => @foo_app.guid,
- ),
- ),
+ json_match(hash_including(
+ "uris" => [],
+ "droplet" => @foo_app.guid,
+ )),
+ )
+
+ put(
+ "/v2/routes/#{@route.guid}",
+ Route::UpdateMessage.new(
+ :app_guids => [@bar_app.guid],
+ ).encode,
+ @headers_for_user,
)
- EM.run do
- put(
- "/v2/routes/#{@route.guid}",
- Route::UpdateMessage.new(
- :app_guids => [@bar_app.guid],
- ).encode,
- @headers_for_user,
- )
- EM.next_tick { EM.stop }
- end
last_response.status.should == 201
end
end
View
4 spec/dea/dea_client_spec.rb
@@ -104,9 +104,7 @@ module VCAP::CloudController
end
it "sends a dea start message that includes cc_partition" do
- config_override(
- :cc_partition => "ngFTW",
- )
+ config_override(:cc_partition => "ngFTW")
DeaClient.configure(config, @message_bus, @dea_pool)
@app.instances = 1
View
4 spec/health_manager_respondent_spec.rb
@@ -11,9 +11,7 @@ module VCAP::CloudController
:instances => 2,
).save
@mbus = double("mock nats")
- @dea_client = double("mock dea client",
- :message_bus => @mbus,
- )
+ @dea_client = double("mock dea client", :message_bus => @mbus)
@mbus.should_receive(:subscribe).with(
"cloudcontrollers.hm.requests.ng",
View
95 spec/message_bus_spec.rb
@@ -5,61 +5,45 @@
module VCAP::CloudController
describe VCAP::CloudController::MessageBus do
let(:nats) { double(:nats) }
+ let!(:bus) { MessageBus.new(:nats => nats) }
- let(:msg_json) do
- Yajl::Encoder.encode(:foo => "bar")
- end
+ let(:msg) { {:foo => "bar"} }
+ let(:msg_json) { Yajl::Encoder.encode(msg) }
- before do
- MessageBus.configure(:nats => nats)
- end
+ describe "#subscribe" do
+ before { nats.should_receive(:subscribe).and_yield(msg_json, nil) }
- shared_examples "subscription" do |receive_in_reactor|
- (desc, method) = if receive_in_reactor
- ["on the reactor thread", :subscribe_on_reactor]
- else
- ["on a thread", :subscribe]
- end
+ it "should receive nats messages" do
+ was_on_reactor_thread = false
+ received_msg = nil
- it "should receive nats messages #{desc}" do
- received_msg = false
- nats.should_receive(:subscribe).and_yield(msg_json, nil)
with_em_and_thread(:auto_stop => false) do
- MessageBus.send(method, "some_subject") do |msg|
- EM.reactor_thread?.should == receive_in_reactor
- msg[:foo].should == "bar"
- received_msg = true
+ bus.subscribe("some_subject") do |msg|
+ was_on_reactor_thread = EM.reactor_thread?
+ received_msg = msg
EM.next_tick { EM.stop }
end
end
- received_msg.should == true
- end
- end
- describe "subscribe_on_reactor" do
- include_examples "subscription", true
+ was_on_reactor_thread.should be_false
+ received_msg.should == msg
+ end
it "should not leak exceptions into the defer block" do
- nats.should_receive(:subscribe).and_yield(msg_json, nil)
-
logger = mock(:logger)
logger.should_receive(:error)
- MessageBus.should_receive(:logger).and_return(logger)
+ bus.should_receive(:logger).and_return(logger)
with_em_and_thread(:auto_stop => false) do
EventMachine::Timer.new(0.1) { EM.stop }
- MessageBus.subscribe("foo") do
+ bus.subscribe("foo") do
raise "boom"
end
end
end
end
- describe "subscribe" do
- include_examples "subscription", false
- end
-
- describe "publish" do
+ describe "#publish" do
it "should publish to nats on the reactor thread" do
published = false
@@ -69,21 +53,22 @@ module VCAP::CloudController
end
with_em_and_thread do
- MessageBus.publish("another_subject", "abc")
+ bus.publish("another_subject", "abc")
end
published.should == true
end
end
- describe "request" do
+ describe "#request" do
it "should use default expected value when not specified" do
- nats.should_receive(:request).once.with("subject", "abc",
- :max => 1)
- .and_yield(msg_json)
+ nats.should_receive(:request)
+ .once
+ .with("subject", "abc", :max => 1)
+ .and_yield(msg_json)
with_em_and_thread do
- response = MessageBus.request("subject", "abc")
+ response = bus.request("subject", "abc")
response.should be_an_instance_of Array
response.size.should == 1
response.should == [msg_json]
@@ -91,12 +76,14 @@ module VCAP::CloudController
end
it "should use the specified expected value" do
- nats.should_receive(:request).once.with("subject", "abc",
- :max => 2)
- .and_yield(msg_json).and_yield(msg_json)
+ nats.should_receive(:request)
+ .once
+ .with("subject", "abc", :max => 2)
+ .and_yield(msg_json)
+ .and_yield(msg_json)
with_em_and_thread do
- response = MessageBus.request("subject", "abc", :expected => 2)
+ response = bus.request("subject", "abc", :expected => 2)
response.should be_an_instance_of Array
response.size.should == 2
response.should == [msg_json, msg_json]
@@ -106,20 +93,22 @@ module VCAP::CloudController
it "should not subscribe to nats when specified expected value is zero" do
# We don't expect nats to receive anything.
with_em_and_thread do
- response = MessageBus.request("subject", "abc", :expected => 0)
+ response = bus.request("subject", "abc", :expected => 0)
response.should be_an_instance_of Array
response.should == []
end
end
it "should not register timeout with nats when none is specified" do
- nats.should_receive(:request).once.
- with("subject", "abc", :max => 1).and_yield(msg_json)
+ nats.should_receive(:request)
+ .once
+ .with("subject", "abc", :max => 1)
+ .and_yield(msg_json)
nats.should_not_receive(:timeout)
with_em_and_thread do
- response = MessageBus.request("subject", "abc")
+ response = bus.request("subject", "abc")
response.should be_an_instance_of Array
response.size.should == 1
response.should == [msg_json]
@@ -133,7 +122,7 @@ module VCAP::CloudController
nats.should_not_receive(:timeout)
with_em_and_thread do
- response = MessageBus.request("subject", "abc")
+ response = bus.request("subject", "abc")
response.should be_an_instance_of Array
response.size.should == 1
response.should == [msg_json]
@@ -151,14 +140,14 @@ module VCAP::CloudController
and_yield
with_em_and_thread do
- response = MessageBus.request("subject", "abc", :timeout => 0.1)
+ response = bus.request("subject", "abc", :timeout => 0.1)
response.should be_an_instance_of Array
response.size.should == 0
end
end
end
- describe "process_message" do
+ describe "#process_message" do
let(:msg) { Yajl::Encoder.encode({:a => 1, :b => 2}) }
it "should pssed the parsed message and inbox to the block" do
@@ -170,7 +159,7 @@ module VCAP::CloudController
inbox_received = inbox
end
- MessageBus.send(:process_message, msg, "myinbox", &block)
+ bus.send(:process_message, msg, "myinbox", &block)
msg_received.should == {:a => 1, :b => 2}
inbox_received.should == "myinbox"
end
@@ -178,13 +167,13 @@ module VCAP::CloudController
it "should catch and log an error on an exception" do
logger = mock(:logger)
logger.should_receive(:error)
- MessageBus.should_receive(:logger).and_return(logger)
+ bus.should_receive(:logger).and_return(logger)
block = proc do |msg, inbox|
raise "boom"
end
- MessageBus.send(:process_message, msg, "myinbox", &block)
+ bus.send(:process_message, msg, "myinbox", &block)
end
end
end
View
1  spec/models/spec_helper.rb
@@ -8,6 +8,7 @@
RSpec.configure do |rspec_config|
rspec_config.include VCAP::CloudController::ModelSpecHelper
+
rspec_config.before :each do
# Yuck
# We need to stub out this because it's in an after_destroy_commit hook
View
129 spec/spec_helper.rb
@@ -14,6 +14,8 @@
require "rspec_let_monkey_patch"
require "mock_redis"
+Dir.glob(File.join(File.dirname(__FILE__), "support/**/*.rb")).each { |f| require f }
+
module VCAP::CloudController
class SpecEnvironment
def initialize
@@ -80,48 +82,71 @@ def reset_database
def config_override(hash)
@config_override ||= {}
@config_override.update(hash)
+
+ @config = nil
+ config
end
def config
- config_file = File.expand_path("../../config/cloud_controller.yml", __FILE__)
- c = VCAP::CloudController::Config.from_file(config_file)
- c = c.merge(
- :nginx => { :use_nginx => true },
- :resource_pool => {
- :resource_directory_key => "spec-cc-resources",
- :fog_connection => {
- :provider => "AWS",
- :aws_access_key_id => "fake_aws_key_id",
- :aws_secret_access_key => "fake_secret_access_key",
- }
- },
- :packages => {
- :app_package_directory_key => "cc-packages",
- :fog_connection => {
- :provider => "AWS",
- :aws_access_key_id => "fake_aws_key_id",
- :aws_secret_access_key => "fake_secret_access_key",
- }
- },
- :droplets => {
- :droplet_directory_key => "cc-droplets",
- :fog_connection => {
- :provider => "AWS",
- :aws_access_key_id => "fake_aws_key_id",
- :aws_secret_access_key => "fake_secret_access_key",
+ @config ||= begin
+ config_file = File.expand_path("../../config/cloud_controller.yml", __FILE__)
+ config_hash = VCAP::CloudController::Config.from_file(config_file)
+
+ config_hash.merge!(
+ :nginx => { :use_nginx => true },
+ :resource_pool => {
+ :resource_directory_key => "spec-cc-resources",
+ :fog_connection => {
+ :provider => "AWS",
+ :aws_access_key_id => "fake_aws_key_id",
+ :aws_secret_access_key => "fake_secret_access_key",
+ }
+ },
+ :packages => {
+ :app_package_directory_key => "cc-packages",
+ :fog_connection => {
+ :provider => "AWS",
+ :aws_access_key_id => "fake_aws_key_id",
+ :aws_secret_access_key => "fake_secret_access_key",
+ }
+ },
+ :droplets => {
+ :droplet_directory_key => "cc-droplets",
+ :fog_connection => {
+ :provider => "AWS",
+ :aws_access_key_id => "fake_aws_key_id",
+ :aws_secret_access_key => "fake_secret_access_key",
+ }
}
- }
- )
+ )
- c = c.merge(@config_override || {})
+ config_hash.merge!(@config_override || {})
- unless (c[:resource_pool][:fog_connection][:provider].downcase == "local" ||
- c[:packages][:fog_connection][:provider].downcase == "local")
- Fog.mock!
+ res_pool_connection_provider = config_hash[:resource_pool][:fog_connection][:provider].downcase
+ packages_connection_provider = config_hash[:packages][:fog_connection][:provider].downcase
+ Fog.mock! unless (res_pool_connection_provider == "local" || packages_connection_provider == "local")
+
+ configure_components(config_hash)
+ config_hash
end
+ end
- VCAP::CloudController::Config.configure(c)
- c
+ def configure_components(config)
+ mbus = MockMessageBus.new(config)
+ VCAP::CloudController::MessageBus.instance = mbus
+
+ VCAP::CloudController::AccountCapacity.configure(config)
+ VCAP::CloudController::ResourcePool.configure(config)
+ VCAP::CloudController::AppPackage.configure(config)
+ VCAP::CloudController::AppStager.configure(config)
+ VCAP::CloudController::LegacyStaging.configure(config)
+
+ VCAP::CloudController::DeaPool.configure(config, mbus)
+ VCAP::CloudController::DeaClient.configure(config, mbus)
+ VCAP::CloudController::HealthManagerClient.configure(mbus)
+
+ VCAP::CloudController::LegacyBulk.configure(config, mbus)
+ VCAP::CloudController::Models::QuotaDefinition.configure(config)
end
def configure
@@ -220,7 +245,7 @@ def with_em_and_thread(opts = {}, &blk)
num_dirs = 3
num_unique_allowed_files_per_dir = 7
file_duplication_factor = 2
- max_file_size = 1098 # this is arbitrary
+ @max_file_size = 1098 # this is arbitrary
@total_allowed_files =
num_dirs * num_unique_allowed_files_per_dir * file_duplication_factor
@@ -228,20 +253,6 @@ def with_em_and_thread(opts = {}, &blk)
@dummy_descriptor = { "sha1" => Digest::SHA1.hexdigest("abc"), "size" => 1}
@tmpdir = Dir.mktmpdir
- cfg = {
- :resource_pool => {
- :maximum_size => max_file_size,
- :resource_directory_key => "spec-cc-resources",
- :fog_connection => {
- :provider => "AWS",
- :aws_access_key_id => "fake_aws_key_id",
- :aws_secret_access_key => "fake_secret_access_key",
- }
- }
- }
- VCAP::CloudController::ResourcePool.configure(cfg)
- Fog.mock!
-
@descriptors = []
num_dirs.times do
dirname = SecureRandom.uuid
@@ -264,12 +275,27 @@ def with_em_and_thread(opts = {}, &blk)
end
File.open("#{path}-not-allowed", "w") do |f|
- f.write "A" * max_file_size
+ f.write "A" * @max_file_size
end
end
end
end
+ before do
+ VCAP::CloudController::ResourcePool.configure(
+ :resource_pool => {
+ :maximum_size => @max_file_size,
+ :resource_directory_key => "spec-cc-resources",
+ :fog_connection => {
+ :provider => "AWS",
+ :aws_access_key_id => "fake_aws_key_id",
+ :aws_secret_access_key => "fake_secret_access_key",
+ }
+ }
+ )
+ Fog.mock!
+ end
+
after(:all) do
FileUtils.rm_rf(@tmpdir)
end
@@ -293,8 +319,9 @@ def self.new(*args)
rspec_config.include Rack::Test::Methods
rspec_config.include VCAP::CloudController::SpecHelper
- rspec_config.before(:each) do |example|
+ rspec_config.before(:each) do
VCAP::CloudController::SecurityContext.clear
+ configure
end
end
View
20 spec/support/mock_message_bus.rb
@@ -0,0 +1,20 @@
+class MockMessageBus
+ attr_reader :config, :nats
+
+ def initialize(config)
+ @config = config
+ @nats = config[:nats] || MockNATS
+ end
+
+ def subscribe(*args)
+ end
+
+ def register_components
+ end
+
+ def register_routes
+ end
+
+ def publish(*args)
+ end
+end
View
9 spec/support/mock_nats.rb
@@ -0,0 +1,9 @@
+class MockNATS
+ class << self
+ attr_accessor :client
+
+ def start(*args)
+ self.client = new
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.