Skip to content

Commit 9f8ccf4

Browse files
committed
Optimize Fog mock setup: skip per-test reset for most specs
- Remove Fog::Mock.reset and bucket setup from spec_helper's before(:each) - Initialize Fog mock buckets once in before(:suite) instead - Create fog_spec_helper for specs that need clean Fog state between tests This saves ~5ms per test by avoiding unnecessary Fog mock reset and bucket recreation. Only ~24 specs that directly manipulate blobstores need the per-test reset provided by fog_spec_helper. Converted to fog_spec_helper (24 files): - Blobstore job specs (upload, delete, cleanup) - Blobstore lib specs (fog_client, retryable_client, etc.) - Controller specs that handle buildpack/staging uploads - Model specs for buildpack operations
1 parent daf0d9d commit 9f8ccf4

26 files changed

Lines changed: 54 additions & 33 deletions

spec/fog_spec_helper.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Use this helper for specs that need Fog/blobstore functionality with
2+
# a clean state between tests (upload, download, delete operations).
3+
#
4+
# This helper resets Fog mocks and recreates buckets before each test.
5+
#
6+
# For specs that don't need blobstore isolation, use spec_helper instead.
7+
8+
require 'spec_helper'
9+
10+
RSpec.configure do |config|
11+
config.before do
12+
Fog::Mock.reset
13+
14+
if Fog.mock?
15+
CloudController::DependencyLocator.instance.droplet_blobstore.ensure_bucket_exists
16+
CloudController::DependencyLocator.instance.package_blobstore.ensure_bucket_exists
17+
CloudController::DependencyLocator.instance.global_app_bits_cache.ensure_bucket_exists
18+
CloudController::DependencyLocator.instance.buildpack_blobstore.ensure_bucket_exists
19+
end
20+
end
21+
end

spec/spec_helper.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@
159159
# calling this more than once will load tasks again and 'invoke' or 'execute' calls
160160
# will call rake tasks multiple times
161161
Application.load_tasks
162+
163+
# Initialize Fog mock buckets once at suite start.
164+
# Tests that need isolated/clean Fog state should use fog_spec_helper.
165+
if Fog.mock?
166+
CloudController::DependencyLocator.instance.droplet_blobstore.ensure_bucket_exists
167+
CloudController::DependencyLocator.instance.package_blobstore.ensure_bucket_exists
168+
CloudController::DependencyLocator.instance.global_app_bits_cache.ensure_bucket_exists
169+
CloudController::DependencyLocator.instance.buildpack_blobstore.ensure_bucket_exists
170+
end
162171
end
163172

164173
rspec_config.before do
@@ -169,15 +178,6 @@
169178
TestConfig.context = example.metadata[:job_context] || :api
170179
TestConfig.reset
171180

172-
Fog::Mock.reset
173-
174-
if Fog.mock?
175-
CloudController::DependencyLocator.instance.droplet_blobstore.ensure_bucket_exists
176-
CloudController::DependencyLocator.instance.package_blobstore.ensure_bucket_exists
177-
CloudController::DependencyLocator.instance.global_app_bits_cache.ensure_bucket_exists
178-
CloudController::DependencyLocator.instance.buildpack_blobstore.ensure_bucket_exists
179-
end
180-
181181
VCAP::CloudController::SecurityContext.clear
182182
VCAP::Request.current_id = nil
183183
allow_any_instance_of(VCAP::CloudController::UaaTokenDecoder).to receive(:uaa_issuer).and_return(UAAIssuer::ISSUER)

spec/unit/controllers/runtime/buildpack_bits_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'spec_helper'
1+
require 'fog_spec_helper'
22

33
## NOTICE: Prefer request specs over controller specs as per ADR #0003 ##
44

spec/unit/controllers/runtime/buildpacks_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'spec_helper'
1+
require 'fog_spec_helper'
22

33
## NOTICE: Prefer request specs over controller specs as per ADR #0003 ##
44

spec/unit/controllers/runtime/stagings_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'spec_helper'
1+
require 'fog_spec_helper'
22

33
## NOTICE: Prefer request specs over controller specs as per ADR #0003 ##
44

spec/unit/jobs/runtime/blobstore_delete_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'spec_helper'
1+
require 'fog_spec_helper'
22

33
module VCAP::CloudController
44
module Jobs::Runtime

spec/unit/jobs/runtime/blobstore_upload_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'spec_helper'
1+
require 'fog_spec_helper'
22

33
module VCAP::CloudController
44
module Jobs::Runtime

spec/unit/jobs/runtime/buildpack_cache_cleanup_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'spec_helper'
1+
require 'fog_spec_helper'
22

33
module VCAP::CloudController
44
module Jobs::Runtime

spec/unit/jobs/v3/buildpack_cache_cleanup_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'spec_helper'
1+
require 'fog_spec_helper'
22

33
module VCAP::CloudController
44
module Jobs::V3

spec/unit/jobs/v3/buildpack_cache_delete_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'spec_helper'
1+
require 'fog_spec_helper'
22
require 'jobs/v3/buildpack_cache_delete'
33

44
module VCAP::CloudController

0 commit comments

Comments
 (0)