Skip to content

Commit

Permalink
Restructure runner spec
Browse files Browse the repository at this point in the history
  • Loading branch information
svkrieger committed Oct 5, 2023
1 parent 8a7a1f1 commit d3e6922
Showing 1 changed file with 52 additions and 41 deletions.
93 changes: 52 additions & 41 deletions spec/unit/lib/cloud_controller/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module VCAP::CloudController
let(:valid_config_file_path) { File.join(Paths::CONFIG, 'cloud_controller.yml') }
let(:config_file) { File.new(valid_config_file_path) }
let(:periodic_updater) { instance_double(VCAP::CloudController::Metrics::PeriodicUpdater) }
let(:request_metrics) { instance_double(VCAP::CloudController::Metrics::RequestMetrics) }
let(:routing_api_client) { instance_double(VCAP::CloudController::RoutingApi::Client, router_group_guid: '') }

let(:argv) { [] }
Expand All @@ -14,6 +15,7 @@ module VCAP::CloudController
allow(CloudController::DependencyLocator.instance).to receive(:routing_api_client).and_return(routing_api_client)
allow(EM).to receive(:run).and_yield
allow(VCAP::CloudController::Metrics::PeriodicUpdater).to receive(:new).and_return(periodic_updater)
allow(VCAP::CloudController::Metrics::RequestMetrics).to receive(:new).and_return(request_metrics)
allow(periodic_updater).to receive(:setup_updates)
allow(VCAP::PidFile).to receive(:new) { double(:pidfile, unlink_at_exit: nil) }
allow_any_instance_of(VCAP::CloudController::ThinRunner).to receive(:start!)
Expand All @@ -24,9 +26,45 @@ module VCAP::CloudController
end

describe '#run!' do
let(:runner) { Runner.new(argv + ['-c', config_file.path]) }

it 'creates a pidfile' do
expect(VCAP::PidFile).to receive(:new).with('/tmp/cloud_controller.pid')
subject.run!
runner.run!
end

it 'starts the web server' do
server = double(:server)
allow(server).to receive(:start!)
expect(VCAP::CloudController::ThinRunner).to receive(:new).and_return(server)

runner.run!
expect(server).to have_received(:start!).once
end
end

describe '#initialize' do
before do
allow_any_instance_of(Runner).to receive(:deprecation_warning)
end

describe 'web server selection' do
context 'when thin is specifed' do
it 'chooses ThinRunner as the web server' do
expect(subject.instance_variable_get(:@server)).to be_an_instance_of(ThinRunner)
end
end

context 'when puma is specified' do
before do
TestConfig.override(webserver: 'puma')
allow(Config).to receive(:load_from_file).and_return(TestConfig.config_instance)
end

it 'chooses puma as the web server' do
expect(subject.instance_variable_get(:@server)).to be_an_instance_of(PumaRunner)
end
end
end

it 'registers a log counter with the component' do
Expand All @@ -37,24 +75,24 @@ module VCAP::CloudController
expect(steno_config.sinks).to include log_counter
end

subject.run!
subject
end

it 'sets up database' do
expect(DB).to receive(:load_models)
subject.run!
subject
end

it 'configures components' do
expect_any_instance_of(Config).to receive(:configure_components)
subject.run!
subject
end

it 'sets up loggregator emitter' do
loggregator_emitter = double(:loggregator_emitter)
expect(LoggregatorEmitter::Emitter).to receive(:new).and_return(loggregator_emitter)
expect(VCAP::AppLogEmitter).to receive(:emitter=).with(loggregator_emitter)
subject.run!
subject
end

context 'when fluent is configured' do
Expand All @@ -74,7 +112,7 @@ module VCAP::CloudController
it 'sets up fluent emitter' do
expect(::Fluent::Logger::FluentLogger).to receive(:new).and_return(fluent_logger)
expect(VCAP::AppLogEmitter).to receive(:fluent_emitter=).with(instance_of(VCAP::FluentEmitter))
subject.run!
subject
end
end

Expand All @@ -83,14 +121,14 @@ module VCAP::CloudController
allow(RackAppBuilder).to receive(:new).and_return(builder)
request_logs = double(:request_logs)
allow(VCAP::CloudController::Logs::RequestLogs).to receive(:new).and_return(request_logs)
expect(builder).to receive(:build).with(anything, instance_of(VCAP::CloudController::Metrics::RequestMetrics),
expect(builder).to receive(:build).with(anything, request_metrics,
request_logs)
subject.run!
subject
end

it 'sets a local ip in the host system' do
expect_any_instance_of(VCAP::HostSystem).to receive(:local_ip).and_return('some_local_ip')
subject.run!
subject
end

it 'sets up logging before creating a logger' do
Expand All @@ -110,7 +148,7 @@ module VCAP::CloudController
logger
end

subject.run!
subject

expect(logging_configuration_time).to be < logger_creation_time
end
Expand All @@ -120,17 +158,15 @@ module VCAP::CloudController
allow(StenoConfigurer).to receive(:new).and_return(steno_configurer)
allow(steno_configurer).to receive(:configure)

subject.run!
subject.run!
subject

expect(steno_configurer).to have_received(:configure).once
end

it 'sets up telemetry logging once' do
allow(TelemetryLogger).to receive(:init)

subject.run!
subject.run!
subject

expect(TelemetryLogger).to have_received(:init).once
end
Expand All @@ -148,7 +184,7 @@ module VCAP::CloudController
it 'sets up telemetry logging with nil logger' do
allow(TelemetryLogger).to receive(:init)

subject.run!
subject

expect(TelemetryLogger).not_to have_received(:init)
end
Expand All @@ -165,38 +201,13 @@ module VCAP::CloudController
expect(CloudController::DependencyLocator.instance).to receive(:global_app_bits_cache).and_return(resource_blobstore)
expect(CloudController::DependencyLocator.instance).to receive(:buildpack_blobstore).and_return(buildpack_blobstore)

subject.run!
subject

expect(droplet_blobstore).to have_received(:ensure_bucket_exists)
expect(package_blobstore).to have_received(:ensure_bucket_exists)
expect(resource_blobstore).to have_received(:ensure_bucket_exists)
expect(buildpack_blobstore).to have_received(:ensure_bucket_exists)
end
end

describe '#initialize' do
before do
allow_any_instance_of(Runner).to receive(:deprecation_warning)
end

describe 'web server selection' do
context 'when thin is specifed' do
it 'chooses ThinRunner as the web server' do
expect(subject.instance_variable_get(:@server)).to be_an_instance_of(ThinRunner)
end
end

context 'when puma is specified' do
before do
TestConfig.override(webserver: 'puma')
allow(Config).to receive(:load_from_file).and_return(TestConfig.config_instance)
end

it 'chooses puma as the web server' do
expect(subject.instance_variable_get(:@server)).to be_an_instance_of(PumaRunner)
end
end
end

describe 'argument parsing' do
subject { Runner.new(argv_options) }
Expand Down

0 comments on commit d3e6922

Please sign in to comment.