Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
34dbccb2
Commit
34dbccb2
authored
Jun 28, 2018
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add helper methods to stub Gitlab::ExclusiveLease
parent
e38af20c
Changes
13
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
183 additions
and
135 deletions
+183
-135
spec/lib/mattermost/session_spec.rb
spec/lib/mattermost/session_spec.rb
+8
-12
spec/models/ci/build_trace_chunk_spec.rb
spec/models/ci/build_trace_chunk_spec.rb
+3
-1
spec/models/concerns/reactive_caching_spec.rb
spec/models/concerns/reactive_caching_spec.rb
+5
-6
spec/services/clusters/applications/check_ingress_ip_address_service_spec.rb
...ers/applications/check_ingress_ip_address_service_spec.rb
+6
-11
spec/services/users/refresh_authorized_projects_service_spec.rb
...ervices/users/refresh_authorized_projects_service_spec.rb
+5
-5
spec/support/helpers/exclusive_lease_helpers.rb
spec/support/helpers/exclusive_lease_helpers.rb
+36
-0
spec/support/shared_examples/ci_trace_shared_examples.rb
spec/support/shared_examples/ci_trace_shared_examples.rb
+3
-1
spec/support/shared_examples/uploaders/object_storage_shared_examples.rb
...ared_examples/uploaders/object_storage_shared_examples.rb
+3
-5
spec/workers/project_cache_worker_spec.rb
spec/workers/project_cache_worker_spec.rb
+7
-8
spec/workers/project_migrate_hashed_storage_worker_spec.rb
spec/workers/project_migrate_hashed_storage_worker_spec.rb
+27
-33
spec/workers/propagate_service_template_worker_spec.rb
spec/workers/propagate_service_template_worker_spec.rb
+19
-19
spec/workers/repository_remove_remote_worker_spec.rb
spec/workers/repository_remove_remote_worker_spec.rb
+32
-25
spec/workers/stuck_ci_jobs_worker_spec.rb
spec/workers/stuck_ci_jobs_worker_spec.rb
+29
-9
No files found.
spec/lib/mattermost/session_spec.rb
View file @
34dbccb2
require
'spec_helper'
require
'spec_helper'
describe
Mattermost
::
Session
,
type: :request
do
describe
Mattermost
::
Session
,
type: :request
do
include
ExclusiveLeaseHelpers
let
(
:user
)
{
create
(
:user
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:gitlab_url
)
{
"http://gitlab.com"
}
let
(
:gitlab_url
)
{
"http://gitlab.com"
}
...
@@ -97,26 +99,20 @@ describe Mattermost::Session, type: :request do
...
@@ -97,26 +99,20 @@ describe Mattermost::Session, type: :request do
end
end
end
end
context
'with lease'
do
context
'exclusive lease'
do
before
do
let
(
:lease_key
)
{
'mattermost:session'
}
allow
(
subject
).
to
receive
(
:lease_try_obtain
).
and_return
(
'aldkfjsldfk'
)
end
it
'tries to obtain a lease'
do
it
'tries to obtain a lease'
do
expect
(
subject
).
to
receive
(
:lease_try_obtain
)
expect
_to_obtain_exclusive_lease
(
lease_key
,
'uuid'
)
expect
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:cancel
)
expect
_to_cancel_exclusive_lease
(
lease_key
,
'uuid'
)
# Cannot setup a session, but we should still cancel the lease
# Cannot setup a session, but we should still cancel the lease
expect
{
subject
.
with_session
}.
to
raise_error
(
Mattermost
::
NoSessionError
)
expect
{
subject
.
with_session
}.
to
raise_error
(
Mattermost
::
NoSessionError
)
end
end
end
context
'without lease'
do
it
'returns a NoSessionError error without lease'
do
before
do
stub_exclusive_lease_taken
(
lease_key
)
allow
(
subject
).
to
receive
(
:lease_try_obtain
).
and_return
(
nil
)
end
it
'returns a NoSessionError error'
do
expect
{
subject
.
with_session
}.
to
raise_error
(
Mattermost
::
NoSessionError
)
expect
{
subject
.
with_session
}.
to
raise_error
(
Mattermost
::
NoSessionError
)
end
end
end
end
...
...
spec/models/ci/build_trace_chunk_spec.rb
View file @
34dbccb2
require
'spec_helper'
require
'spec_helper'
describe
Ci
::
BuildTraceChunk
,
:clean_gitlab_redis_shared_state
do
describe
Ci
::
BuildTraceChunk
,
:clean_gitlab_redis_shared_state
do
include
ExclusiveLeaseHelpers
set
(
:build
)
{
create
(
:ci_build
,
:running
)
}
set
(
:build
)
{
create
(
:ci_build
,
:running
)
}
let
(
:chunk_index
)
{
0
}
let
(
:chunk_index
)
{
0
}
let
(
:data_store
)
{
:redis
}
let
(
:data_store
)
{
:redis
}
...
@@ -322,7 +324,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
...
@@ -322,7 +324,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
describe
'ExclusiveLock'
do
describe
'ExclusiveLock'
do
before
do
before
do
allow_any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
)
{
nil
}
stub_exclusive_lease_taken
stub_const
(
'Ci::BuildTraceChunk::WRITE_LOCK_RETRY'
,
1
)
stub_const
(
'Ci::BuildTraceChunk::WRITE_LOCK_RETRY'
,
1
)
end
end
...
...
spec/models/concerns/reactive_caching_spec.rb
View file @
34dbccb2
require
'spec_helper'
require
'spec_helper'
describe
ReactiveCaching
,
:use_clean_rails_memory_store_caching
do
describe
ReactiveCaching
,
:use_clean_rails_memory_store_caching
do
include
ExclusiveLeaseHelpers
include
ReactiveCachingHelpers
include
ReactiveCachingHelpers
class
CacheTest
class
CacheTest
...
@@ -106,8 +107,8 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
...
@@ -106,8 +107,8 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
end
end
it
'takes and releases the lease'
do
it
'takes and releases the lease'
do
expect_
any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
).
and_return
(
"000000"
)
expect_
to_obtain_exclusive_lease
(
cache_key
,
'uuid'
)
expect
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:cancel
).
with
(
cache_key
,
"000000"
)
expect
_to_cancel_exclusive_lease
(
cache_key
,
'uuid'
)
go!
go!
end
end
...
@@ -153,11 +154,9 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
...
@@ -153,11 +154,9 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
end
end
context
'when the lease is already taken'
do
context
'when the lease is already taken'
do
before
do
expect_any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
).
and_return
(
nil
)
end
it
'skips the calculation'
do
it
'skips the calculation'
do
stub_exclusive_lease_taken
(
cache_key
)
expect
(
instance
).
to
receive
(
:calculate_reactive_cache
).
never
expect
(
instance
).
to
receive
(
:calculate_reactive_cache
).
never
go!
go!
...
...
spec/services/clusters/applications/check_ingress_ip_address_service_spec.rb
View file @
34dbccb2
require
'spec_helper'
require
'spec_helper'
describe
Clusters
::
Applications
::
CheckIngressIpAddressService
do
describe
Clusters
::
Applications
::
CheckIngressIpAddressService
do
include
ExclusiveLeaseHelpers
let
(
:application
)
{
create
(
:clusters_applications_ingress
,
:installed
)
}
let
(
:application
)
{
create
(
:clusters_applications_ingress
,
:installed
)
}
let
(
:service
)
{
described_class
.
new
(
application
)
}
let
(
:service
)
{
described_class
.
new
(
application
)
}
let
(
:kubeclient
)
{
double
(
::
Kubeclient
::
Client
,
get_service:
kube_service
)
}
let
(
:kubeclient
)
{
double
(
::
Kubeclient
::
Client
,
get_service:
kube_service
)
}
let
(
:ingress
)
{
[{
ip:
'111.222.111.222'
}]
}
let
(
:ingress
)
{
[{
ip:
'111.222.111.222'
}]
}
let
(
:
exclusive_lease
)
{
instance_double
(
Gitlab
::
ExclusiveLease
,
try_obtain:
true
)
}
let
(
:
lease_key
)
{
"check_ingress_ip_address_service:
#{
application
.
id
}
"
}
let
(
:kube_service
)
do
let
(
:kube_service
)
do
::
Kubeclient
::
Resource
.
new
(
::
Kubeclient
::
Resource
.
new
(
...
@@ -22,11 +24,8 @@ describe Clusters::Applications::CheckIngressIpAddressService do
...
@@ -22,11 +24,8 @@ describe Clusters::Applications::CheckIngressIpAddressService do
subject
{
service
.
execute
}
subject
{
service
.
execute
}
before
do
before
do
stub_exclusive_lease
(
lease_key
,
timeout:
15
.
seconds
.
to_i
)
allow
(
application
.
cluster
).
to
receive
(
:kubeclient
).
and_return
(
kubeclient
)
allow
(
application
.
cluster
).
to
receive
(
:kubeclient
).
and_return
(
kubeclient
)
allow
(
Gitlab
::
ExclusiveLease
)
.
to
receive
(
:new
)
.
with
(
"check_ingress_ip_address_service:
#{
application
.
id
}
"
,
timeout:
15
.
seconds
.
to_i
)
.
and_return
(
exclusive_lease
)
end
end
describe
'#execute'
do
describe
'#execute'
do
...
@@ -47,13 +46,9 @@ describe Clusters::Applications::CheckIngressIpAddressService do
...
@@ -47,13 +46,9 @@ describe Clusters::Applications::CheckIngressIpAddressService do
end
end
context
'when the exclusive lease cannot be obtained'
do
context
'when the exclusive lease cannot be obtained'
do
before
do
allow
(
exclusive_lease
)
.
to
receive
(
:try_obtain
)
.
and_return
(
false
)
end
it
'does not call kubeclient'
do
it
'does not call kubeclient'
do
stub_exclusive_lease_taken
(
lease_key
,
timeout:
15
.
seconds
.
to_i
)
subject
subject
expect
(
kubeclient
).
not_to
have_received
(
:get_service
)
expect
(
kubeclient
).
not_to
have_received
(
:get_service
)
...
...
spec/services/users/refresh_authorized_projects_service_spec.rb
View file @
34dbccb2
require
'spec_helper'
require
'spec_helper'
describe
Users
::
RefreshAuthorizedProjectsService
do
describe
Users
::
RefreshAuthorizedProjectsService
do
include
ExclusiveLeaseHelpers
# We're using let! here so that any expectations for the service class are not
# We're using let! here so that any expectations for the service class are not
# triggered twice.
# triggered twice.
let!
(
:project
)
{
create
(
:project
)
}
let!
(
:project
)
{
create
(
:project
)
}
...
@@ -10,12 +12,10 @@ describe Users::RefreshAuthorizedProjectsService do
...
@@ -10,12 +12,10 @@ describe Users::RefreshAuthorizedProjectsService do
describe
'#execute'
,
:clean_gitlab_redis_shared_state
do
describe
'#execute'
,
:clean_gitlab_redis_shared_state
do
it
'refreshes the authorizations using a lease'
do
it
'refreshes the authorizations using a lease'
do
expect_any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
)
lease_key
=
"refresh_authorized_projects:
#{
user
.
id
}
"
.
and_return
(
'foo'
)
expect
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:cancel
)
.
with
(
an_instance_of
(
String
),
'foo'
)
expect_to_obtain_exclusive_lease
(
lease_key
,
'uuid'
)
expect_to_cancel_exclusive_lease
(
lease_key
,
'uuid'
)
expect
(
service
).
to
receive
(
:execute_without_lease
)
expect
(
service
).
to
receive
(
:execute_without_lease
)
service
.
execute
service
.
execute
...
...
spec/support/helpers/exclusive_lease_helpers.rb
0 → 100644
View file @
34dbccb2
module
ExclusiveLeaseHelpers
def
stub_exclusive_lease
(
key
=
nil
,
uuid
=
'uuid'
,
renew:
false
,
timeout:
nil
)
key
||=
instance_of
(
String
)
timeout
||=
instance_of
(
Integer
)
lease
=
instance_double
(
Gitlab
::
ExclusiveLease
,
try_obtain:
uuid
,
exists?:
true
,
renew:
renew
)
allow
(
Gitlab
::
ExclusiveLease
)
.
to
receive
(
:new
)
.
with
(
key
,
timeout:
timeout
)
.
and_return
(
lease
)
lease
end
def
stub_exclusive_lease_taken
(
key
=
nil
,
timeout:
nil
)
stub_exclusive_lease
(
key
,
nil
,
timeout:
timeout
)
end
def
expect_to_obtain_exclusive_lease
(
key
,
uuid
=
'uuid'
,
timeout:
nil
)
lease
=
stub_exclusive_lease
(
key
,
uuid
,
timeout:
timeout
)
expect
(
lease
).
to
receive
(
:try_obtain
)
end
def
expect_to_cancel_exclusive_lease
(
key
,
uuid
)
expect
(
Gitlab
::
ExclusiveLease
)
.
to
receive
(
:cancel
)
.
with
(
key
,
uuid
)
end
end
spec/support/shared_examples/ci_trace_shared_examples.rb
View file @
34dbccb2
...
@@ -247,8 +247,10 @@ shared_examples_for 'common trace features' do
...
@@ -247,8 +247,10 @@ shared_examples_for 'common trace features' do
end
end
context
'when another process has already been archiving'
,
:clean_gitlab_redis_shared_state
do
context
'when another process has already been archiving'
,
:clean_gitlab_redis_shared_state
do
include
ExclusiveLeaseHelpers
before
do
before
do
Gitlab
::
ExclusiveLease
.
new
(
"trace:archive:
#{
trace
.
job
.
id
}
"
,
timeout:
1
.
hour
).
try_obtain
stub_exclusive_lease_taken
(
"trace:archive:
#{
trace
.
job
.
id
}
"
,
timeout:
1
.
hour
)
end
end
it
'blocks concurrent archiving'
do
it
'blocks concurrent archiving'
do
...
...
spec/support/shared_examples/uploaders/object_storage_shared_examples.rb
View file @
34dbccb2
...
@@ -76,8 +76,10 @@ shared_examples "migrates" do |to_store:, from_store: nil|
...
@@ -76,8 +76,10 @@ shared_examples "migrates" do |to_store:, from_store: nil|
end
end
context
'when migrate! is occupied by another process'
do
context
'when migrate! is occupied by another process'
do
include
ExclusiveLeaseHelpers
before
do
before
do
@uuid
=
Gitlab
::
ExclusiveLease
.
new
(
subject
.
exclusive_lease_key
,
timeout:
1
.
hour
.
to_i
).
try_obtain
stub_exclusive_lease_taken
(
subject
.
exclusive_lease_key
,
timeout:
1
.
hour
.
to_i
)
end
end
it
'does not execute migrate!'
do
it
'does not execute migrate!'
do
...
@@ -91,10 +93,6 @@ shared_examples "migrates" do |to_store:, from_store: nil|
...
@@ -91,10 +93,6 @@ shared_examples "migrates" do |to_store:, from_store: nil|
expect
{
subject
.
use_file
}.
to
raise_error
(
ObjectStorage
::
ExclusiveLeaseTaken
)
expect
{
subject
.
use_file
}.
to
raise_error
(
ObjectStorage
::
ExclusiveLeaseTaken
)
end
end
after
do
Gitlab
::
ExclusiveLease
.
cancel
(
subject
.
exclusive_lease_key
,
@uuid
)
end
end
end
context
'migration is unsuccessful'
do
context
'migration is unsuccessful'
do
...
...
spec/workers/project_cache_worker_spec.rb
View file @
34dbccb2
require
'spec_helper'
require
'spec_helper'
describe
ProjectCacheWorker
do
describe
ProjectCacheWorker
do
include
ExclusiveLeaseHelpers
let
(
:worker
)
{
described_class
.
new
}
let
(
:worker
)
{
described_class
.
new
}
let
(
:project
)
{
create
(
:project
,
:repository
)
}
let
(
:project
)
{
create
(
:project
,
:repository
)
}
let
(
:statistics
)
{
project
.
statistics
}
let
(
:statistics
)
{
project
.
statistics
}
let
(
:lease_key
)
{
"project_cache_worker:
#{
project
.
id
}
:update_statistics"
}
let
(
:lease_timeout
)
{
ProjectCacheWorker
::
LEASE_TIMEOUT
}
describe
'#perform'
do
describe
'#perform'
do
before
do
before
do
allow_any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
)
stub_exclusive_lease
(
lease_key
,
timeout:
lease_timeout
)
.
and_return
(
true
)
end
end
context
'with a non-existing project'
do
context
'with a non-existing project'
do
...
@@ -63,9 +66,7 @@ describe ProjectCacheWorker do
...
@@ -63,9 +66,7 @@ describe ProjectCacheWorker do
describe
'#update_statistics'
do
describe
'#update_statistics'
do
context
'when a lease could not be obtained'
do
context
'when a lease could not be obtained'
do
it
'does not update the repository size'
do
it
'does not update the repository size'
do
allow
(
worker
).
to
receive
(
:try_obtain_lease_for
)
stub_exclusive_lease_taken
(
lease_key
,
timeout:
lease_timeout
)
.
with
(
project
.
id
,
:update_statistics
)
.
and_return
(
false
)
expect
(
statistics
).
not_to
receive
(
:refresh!
)
expect
(
statistics
).
not_to
receive
(
:refresh!
)
...
@@ -75,9 +76,7 @@ describe ProjectCacheWorker do
...
@@ -75,9 +76,7 @@ describe ProjectCacheWorker do
context
'when a lease could be obtained'
do
context
'when a lease could be obtained'
do
it
'updates the project statistics'
do
it
'updates the project statistics'
do
allow
(
worker
).
to
receive
(
:try_obtain_lease_for
)
stub_exclusive_lease
(
lease_key
,
timeout:
lease_timeout
)
.
with
(
project
.
id
,
:update_statistics
)
.
and_return
(
true
)
expect
(
statistics
).
to
receive
(
:refresh!
)
expect
(
statistics
).
to
receive
(
:refresh!
)
.
with
(
only:
%i(repository_size)
)
.
with
(
only:
%i(repository_size)
)
...
...
spec/workers/project_migrate_hashed_storage_worker_spec.rb
View file @
34dbccb2
require
'spec_helper'
require
'spec_helper'
describe
ProjectMigrateHashedStorageWorker
,
:clean_gitlab_redis_shared_state
do
describe
ProjectMigrateHashedStorageWorker
,
:clean_gitlab_redis_shared_state
do
include
ExclusiveLeaseHelpers
describe
'#perform'
do
describe
'#perform'
do
let
(
:project
)
{
create
(
:project
,
:empty_repo
)
}
let
(
:project
)
{
create
(
:project
,
:empty_repo
)
}
let
(
:pending_delete_project
)
{
create
(
:project
,
:empty_repo
,
pending_delete:
true
)
}
let
(
:lease_key
)
{
"project_migrate_hashed_storage_worker:
#{
project
.
id
}
"
}
let
(
:lease_timeout
)
{
ProjectMigrateHashedStorageWorker
::
LEASE_TIMEOUT
}
context
'when have exclusive lease'
do
before
do
lease
=
subject
.
lease_for
(
project
.
id
)
allow
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:new
).
and_return
(
lease
)
allow
(
lease
).
to
receive
(
:try_obtain
).
and_return
(
true
)
end
it
'skips when project no longer exists'
do
it
'skips when project no longer exists'
do
nonexistent_id
=
999999999999
expect
(
::
Projects
::
HashedStorageMigrationService
).
not_to
receive
(
:new
)
expect
(
::
Projects
::
HashedStorageMigrationService
).
not_to
receive
(
:new
)
subject
.
perform
(
nonexistent_id
)
subject
.
perform
(
-
1
)
end
end
it
'skips when project is pending delete'
do
it
'skips when project is pending delete'
do
pending_delete_project
=
create
(
:project
,
:empty_repo
,
pending_delete:
true
)
expect
(
::
Projects
::
HashedStorageMigrationService
).
not_to
receive
(
:new
)
expect
(
::
Projects
::
HashedStorageMigrationService
).
not_to
receive
(
:new
)
subject
.
perform
(
pending_delete_project
.
id
)
subject
.
perform
(
pending_delete_project
.
id
)
end
end
it
'delegates removal to service class'
do
it
'delegates removal to service class when have exclusive lease'
do
service
=
double
(
'service'
)
stub_exclusive_lease
(
lease_key
,
'uuid'
,
timeout:
lease_timeout
)
expect
(
::
Projects
::
HashedStorageMigrationService
).
to
receive
(
:new
).
with
(
project
,
subject
.
logger
).
and_return
(
service
)
expect
(
service
).
to
receive
(
:execute
)
subject
.
perform
(
project
.
id
)
migration_service
=
spy
end
end
context
'when dont have exclusive lease'
do
allow
(
::
Projects
::
HashedStorageMigrationService
)
before
do
.
to
receive
(
:new
).
with
(
project
,
subject
.
logger
)
lease
=
subject
.
lease_for
(
project
.
id
)
.
and_return
(
migration_service
)
allow
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:new
).
and_return
(
lease
)
subject
.
perform
(
project
.
id
)
allow
(
lease
).
to
receive
(
:try_obtain
).
and_return
(
false
)
expect
(
migration_service
).
to
have_received
(
:execute
)
end
end
it
'skips when dont have lease'
do
it
'skips when dont have lease when dont have exclusive lease'
do
stub_exclusive_lease_taken
(
lease_key
,
timeout:
lease_timeout
)
expect
(
::
Projects
::
HashedStorageMigrationService
).
not_to
receive
(
:new
)
expect
(
::
Projects
::
HashedStorageMigrationService
).
not_to
receive
(
:new
)
subject
.
perform
(
project
.
id
)
subject
.
perform
(
project
.
id
)
end
end
end
end
end
end
end
spec/workers/propagate_service_template_worker_spec.rb
View file @
34dbccb2
require
'spec_helper'
require
'spec_helper'
describe
PropagateServiceTemplateWorker
do
describe
PropagateServiceTemplateWorker
do
let!
(
:service_template
)
do
include
ExclusiveLeaseHelpers
PushoverService
.
create
(
describe
'#perform'
do
it
'calls the propagate service with the template'
do
template
=
PushoverService
.
create
(
template:
true
,
template:
true
,
active:
true
,
active:
true
,
properties:
{
properties:
{
...
@@ -12,18 +15,15 @@ describe PropagateServiceTemplateWorker do
...
@@ -12,18 +15,15 @@ describe PropagateServiceTemplateWorker do
user_key:
'asdf'
,
user_key:
'asdf'
,
api_key:
'123456789'
api_key:
'123456789'
})
})
end
before
do
stub_exclusive_lease
(
"propagate_service_template_worker:
#{
template
.
id
}
"
,
allow_any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
)
timeout:
PropagateServiceTemplateWorker
::
LEASE_TIMEOUT
)
.
and_return
(
true
)
end
describe
'#perform'
do
expect
(
Projects
::
PropagateServiceTemplate
)
it
'calls the propagate service with the template'
do
.
to
receive
(
:propagate
)
expect
(
Projects
::
PropagateServiceTemplate
).
to
receive
(
:propagate
).
with
(
service_
template
)
.
with
(
template
)
subject
.
perform
(
service_
template
.
id
)
subject
.
perform
(
template
.
id
)
end
end
end
end
end
end
spec/workers/repository_remove_remote_worker_spec.rb
View file @
34dbccb2
require
'rails_helper'
require
'rails_helper'
describe
RepositoryRemoveRemoteWorker
do
describe
RepositoryRemoveRemoteWorker
do
subject
(
:worker
)
{
described_class
.
new
}
include
ExclusiveLeaseHelpers
describe
'#perform'
do
describe
'#perform'
do
let
(
:remote_name
)
{
'joe'
}
let!
(
:project
)
{
create
(
:project
,
:repository
)
}
let!
(
:project
)
{
create
(
:project
,
:repository
)
}
let
(
:remote_name
)
{
'joe'
}
let
(
:lease_key
)
{
"remove_remote_
#{
project
.
id
}
_
#{
remote_name
}
"
}
let
(
:lease_timeout
)
{
RepositoryRemoveRemoteWorker
::
LEASE_TIMEOUT
}
context
'when it cannot obtain lease'
do
it
'returns nil when project does not exist'
do
it
'logs error'
do
expect
(
subject
.
perform
(
-
1
,
'remote_name'
)).
to
be_nil
allow_any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
)
{
nil
}
expect_any_instance_of
(
Repository
).
not_to
receive
(
:remove_remote
)
expect
(
worker
).
to
receive
(
:log_error
).
with
(
'Cannot obtain an exclusive lease. There must be another instance already in execution.'
)
worker
.
perform
(
project
.
id
,
remote_name
)
end
end
end
context
'when
it gets the lease
'
do
context
'when
project exists
'
do
before
do
before
do
allow_any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
).
and_return
(
true
)
allow
(
Project
)
.
to
receive
(
:find_by
)
.
with
(
id:
project
.
id
)
.
and_return
(
project
)
end
end
context
'when project does not exist'
do
it
'does not remove remote when cannot obtain lease'
do
it
'returns nil'
do
stub_exclusive_lease_taken
(
lease_key
,
timeout:
lease_timeout
)
expect
(
worker
.
perform
(
-
1
,
'remote_name'
)).
to
be_nil
end
expect
(
project
.
repository
)
.
not_to
receive
(
:remove_remote
)
expect
(
subject
)
.
to
receive
(
:log_error
)
.
with
(
'Cannot obtain an exclusive lease. There must be another instance already in execution.'
)
subject
.
perform
(
project
.
id
,
remote_name
)
end
end
context
'when project exists
'
do
it
'removes remote from repository when obtain a lease
'
do
it
'removes remote from repository'
do
stub_exclusive_lease
(
lease_key
,
timeout:
lease_timeout
)
masterrev
=
project
.
repository
.
find_branch
(
'master'
).
dereferenced_target
masterrev
=
project
.
repository
.
find_branch
(
'master'
).
dereferenced_target
create_remote_branch
(
remote_name
,
'remote_branch'
,
masterrev
)
create_remote_branch
(
remote_name
,
'remote_branch'
,
masterrev
)
expect_any_instance_of
(
Repository
).
to
receive
(
:remove_remote
).
with
(
remote_name
).
and_call_original
expect
(
project
.
repository
)
.
to
receive
(
:remove_remote
)
.
with
(
remote_name
)
.
and_call_original
worker
.
perform
(
project
.
id
,
remote_name
)
subject
.
perform
(
project
.
id
,
remote_name
)
end
end
end
end
end
end
end
...
@@ -47,6 +53,7 @@ describe RepositoryRemoveRemoteWorker do
...
@@ -47,6 +53,7 @@ describe RepositoryRemoveRemoteWorker do
rugged
=
Gitlab
::
GitalyClient
::
StorageSettings
.
allow_disk_access
do
rugged
=
Gitlab
::
GitalyClient
::
StorageSettings
.
allow_disk_access
do
project
.
repository
.
rugged
project
.
repository
.
rugged
end
end
rugged
.
references
.
create
(
"refs/remotes/
#{
remote_name
}
/
#{
branch_name
}
"
,
target
.
id
)
rugged
.
references
.
create
(
"refs/remotes/
#{
remote_name
}
/
#{
branch_name
}
"
,
target
.
id
)
end
end
end
end
spec/workers/stuck_ci_jobs_worker_spec.rb
View file @
34dbccb2
require
'spec_helper'
require
'spec_helper'
describe
StuckCiJobsWorker
do
describe
StuckCiJobsWorker
do
include
ExclusiveLeaseHelpers
let!
(
:runner
)
{
create
:ci_runner
}
let!
(
:runner
)
{
create
:ci_runner
}
let!
(
:job
)
{
create
:ci_build
,
runner:
runner
}
let!
(
:job
)
{
create
:ci_build
,
runner:
runner
}
let
(
:worker
)
{
described_class
.
new
}
let
(
:trace_lease_key
)
{
"trace:archive:
#{
job
.
id
}
"
}
let
(
:exclusive_lease_uuid
)
{
SecureRandom
.
uuid
}
let
(
:trace_lease_uuid
)
{
SecureRandom
.
uuid
}
let
(
:worker_lease_key
)
{
StuckCiJobsWorker
::
EXCLUSIVE_LEASE_KEY
}
let
(
:worker_lease_uuid
)
{
SecureRandom
.
uuid
}
subject
(
:worker
)
{
described_class
.
new
}
before
do
before
do
stub_exclusive_lease
(
worker_lease_key
,
worker_lease_uuid
)
stub_exclusive_lease
(
trace_lease_key
,
trace_lease_uuid
)
job
.
update!
(
status:
status
,
updated_at:
updated_at
)
job
.
update!
(
status:
status
,
updated_at:
updated_at
)
allow_any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
).
and_return
(
exclusive_lease_uuid
)
end
end
shared_examples
'job is dropped'
do
shared_examples
'job is dropped'
do
...
@@ -44,16 +51,19 @@ describe StuckCiJobsWorker do
...
@@ -44,16 +51,19 @@ describe StuckCiJobsWorker do
context
'when job was not updated for more than 1 day ago'
do
context
'when job was not updated for more than 1 day ago'
do
let
(
:updated_at
)
{
2
.
days
.
ago
}
let
(
:updated_at
)
{
2
.
days
.
ago
}
it_behaves_like
'job is dropped'
it_behaves_like
'job is dropped'
end
end
context
'when job was updated in less than 1 day ago'
do
context
'when job was updated in less than 1 day ago'
do
let
(
:updated_at
)
{
6
.
hours
.
ago
}
let
(
:updated_at
)
{
6
.
hours
.
ago
}
it_behaves_like
'job is unchanged'
it_behaves_like
'job is unchanged'
end
end
context
'when job was not updated for more than 1 hour ago'
do
context
'when job was not updated for more than 1 hour ago'
do
let
(
:updated_at
)
{
2
.
hours
.
ago
}
let
(
:updated_at
)
{
2
.
hours
.
ago
}
it_behaves_like
'job is unchanged'
it_behaves_like
'job is unchanged'
end
end
end
end
...
@@ -65,11 +75,14 @@ describe StuckCiJobsWorker do
...
@@ -65,11 +75,14 @@ describe StuckCiJobsWorker do
context
'when job was not updated for more than 1 hour ago'
do
context
'when job was not updated for more than 1 hour ago'
do
let
(
:updated_at
)
{
2
.
hours
.
ago
}
let
(
:updated_at
)
{
2
.
hours
.
ago
}
it_behaves_like
'job is dropped'
it_behaves_like
'job is dropped'
end
end
context
'when job was updated in less than 1 hour ago'
do
context
'when job was updated in less than 1
hour ago'
do
let
(
:updated_at
)
{
30
.
minutes
.
ago
}
let
(
:updated_at
)
{
30
.
minutes
.
ago
}
it_behaves_like
'job is unchanged'
it_behaves_like
'job is unchanged'
end
end
end
end
...
@@ -80,11 +93,13 @@ describe StuckCiJobsWorker do
...
@@ -80,11 +93,13 @@ describe StuckCiJobsWorker do
context
'when job was not updated for more than 1 hour ago'
do
context
'when job was not updated for more than 1 hour ago'
do
let
(
:updated_at
)
{
2
.
hours
.
ago
}
let
(
:updated_at
)
{
2
.
hours
.
ago
}
it_behaves_like
'job is dropped'
it_behaves_like
'job is dropped'
end
end
context
'when job was updated in less than 1 hour ago'
do
context
'when job was updated in less than 1 hour ago'
do
let
(
:updated_at
)
{
30
.
minutes
.
ago
}
let
(
:updated_at
)
{
30
.
minutes
.
ago
}
it_behaves_like
'job is unchanged'
it_behaves_like
'job is unchanged'
end
end
end
end
...
@@ -93,6 +108,7 @@ describe StuckCiJobsWorker do
...
@@ -93,6 +108,7 @@ describe StuckCiJobsWorker do
context
"when job is
#{
status
}
"
do
context
"when job is
#{
status
}
"
do
let
(
:status
)
{
status
}
let
(
:status
)
{
status
}
let
(
:updated_at
)
{
2
.
days
.
ago
}
let
(
:updated_at
)
{
2
.
days
.
ago
}
it_behaves_like
'job is unchanged'
it_behaves_like
'job is unchanged'
end
end
end
end
...
@@ -119,23 +135,27 @@ describe StuckCiJobsWorker do
...
@@ -119,23 +135,27 @@ describe StuckCiJobsWorker do
it
'is guard by exclusive lease when executed concurrently'
do
it
'is guard by exclusive lease when executed concurrently'
do
expect
(
worker
).
to
receive
(
:drop
).
at_least
(
:once
).
and_call_original
expect
(
worker
).
to
receive
(
:drop
).
at_least
(
:once
).
and_call_original
expect
(
worker2
).
not_to
receive
(
:drop
)
expect
(
worker2
).
not_to
receive
(
:drop
)
worker
.
perform
worker
.
perform
allow_any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
).
and_return
(
false
)
stub_exclusive_lease_taken
(
worker_lease_key
)
worker2
.
perform
worker2
.
perform
end
end
it
'can be executed in sequence'
do
it
'can be executed in sequence'
do
expect
(
worker
).
to
receive
(
:drop
).
at_least
(
:once
).
and_call_original
expect
(
worker
).
to
receive
(
:drop
).
at_least
(
:once
).
and_call_original
expect
(
worker2
).
to
receive
(
:drop
).
at_least
(
:once
).
and_call_original
expect
(
worker2
).
to
receive
(
:drop
).
at_least
(
:once
).
and_call_original
worker
.
perform
worker
.
perform
worker2
.
perform
worker2
.
perform
end
end
it
'cancels exclusive lease after worker perform'
do
it
'cancels exclusive leases after worker perform'
do
worker
.
perform
expect_to_cancel_exclusive_lease
(
trace_lease_key
,
trace_lease_uuid
)
expect_to_cancel_exclusive_lease
(
worker_lease_key
,
worker_lease_uuid
)
expect
(
Gitlab
::
ExclusiveLease
.
new
(
described_class
::
EXCLUSIVE_LEASE_KEY
,
timeout:
1
.
hour
))
worker
.
perform
.
not_to
be_exists
end
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment