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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
acaa36cb
Commit
acaa36cb
authored
Dec 19, 2016
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added a first bunch of tests
parent
9ca8ea6c
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
309 additions
and
12 deletions
+309
-12
app/models/namespace.rb
app/models/namespace.rb
+1
-2
app/services/update_build_minutes_service.rb
app/services/update_build_minutes_service.rb
+5
-6
spec/factories/namespace_metrics.rb
spec/factories/namespace_metrics.rb
+5
-0
spec/factories/namespaces.rb
spec/factories/namespaces.rb
+7
-2
spec/factories/project_metrics.rb
spec/factories/project_metrics.rb
+5
-0
spec/models/namespace_metrics_spec.rb
spec/models/namespace_metrics_spec.rb
+7
-0
spec/models/namespace_spec.rb
spec/models/namespace_spec.rb
+112
-0
spec/models/project_metrics_spec.rb
spec/models/project_metrics_spec.rb
+7
-0
spec/models/project_spec.rb
spec/models/project_spec.rb
+61
-2
spec/services/update_build_minutes_service_spec.rb
spec/services/update_build_minutes_service_spec.rb
+58
-0
spec/workers/clean_shared_runner_minutes_worker_spec.rb
spec/workers/clean_shared_runner_minutes_worker_spec.rb
+41
-0
No files found.
app/models/namespace.rb
View file @
acaa36cb
...
@@ -174,8 +174,7 @@ class Namespace < ActiveRecord::Base
...
@@ -174,8 +174,7 @@ class Namespace < ActiveRecord::Base
end
end
def
shared_runners_enabled?
def
shared_runners_enabled?
projects
.
where
(
shared_runners_enabled:
true
).
any?
||
projects
.
where
(
shared_runners_enabled:
true
).
any?
current_application_settings
.
shared_runners_enabled?
end
end
def
shared_runners_minutes_limit
def
shared_runners_minutes_limit
...
...
app/services/update_build_minutes_service.rb
View file @
acaa36cb
class
UpdateBuildMinutesService
<
BaseService
class
UpdateBuildMinutesService
def
execute
(
build
)
def
execute
(
build
)
return
unless
build
.
runner
return
unless
build
.
runner
.
try
(
:shared?
)
return
unless
build
.
runner
.
shared?
return
unless
build
.
project
.
try
(
:shared_runners_minutes_limit_enabled?
)
return
unless
build
.
finished?
return
unless
build
.
duration
return
unless
build
.
duration
return
unless
build
.
project
return
unless
build
.
project
.
shared_runners_minutes_limit_enabled?
project
.
find_or_create_project_metrics
.
project
.
find_or_create_project_metrics
.
update_all
(
'shared_runners_minutes = shared_runners_minutes + ?'
,
build
.
duration
)
update_all
(
'shared_runners_minutes = shared_runners_minutes + ?'
,
build
.
duration
)
...
...
spec/factories/namespace_metrics.rb
0 → 100644
View file @
acaa36cb
FactoryGirl
.
define
do
factory
:namespace_metrics
do
namespace
factory: :namespace
end
end
spec/factories/namespaces.rb
View file @
acaa36cb
...
@@ -4,11 +4,16 @@ FactoryGirl.define do
...
@@ -4,11 +4,16 @@ FactoryGirl.define do
path
{
name
.
downcase
.
gsub
(
/\s/
,
'_'
)
}
path
{
name
.
downcase
.
gsub
(
/\s/
,
'_'
)
}
owner
owner
trait
:with_limit
do
trait
:with_
build_minutes_
limit
do
shared_runners_minutes_limit
500
shared_runners_minutes_limit
500
end
end
trait
:with_used_limit
do
trait
:with_not_used_build_minutes_limit
do
namespace_metrics
factory: :namespace_metrics
,
shared_runners_minutes:
300
shared_runners_minutes_limit
500
end
trait
:with_used_build_minutes_limit
do
namespace_metrics
factory: :namespace_metrics
,
shared_runners_minutes:
1000
namespace_metrics
factory: :namespace_metrics
,
shared_runners_minutes:
1000
shared_runners_minutes_limit
500
shared_runners_minutes_limit
500
end
end
...
...
spec/factories/project_metrics.rb
0 → 100644
View file @
acaa36cb
FactoryGirl
.
define
do
factory
:project_metrics
do
project
factory: :empty_project
end
end
spec/models/namespace_metrics_spec.rb
0 → 100644
View file @
acaa36cb
require
'spec_helper'
describe
NamespaceMetrics
,
models:
true
do
it
{
is_expected
.
to
belong_to
(
:namespace
)
}
it
{
is_expected
.
to
validate_presence_of
(
:namespace
)
}
end
spec/models/namespace_spec.rb
View file @
acaa36cb
...
@@ -4,6 +4,7 @@ describe Namespace, models: true do
...
@@ -4,6 +4,7 @@ describe Namespace, models: true do
let!
(
:namespace
)
{
create
(
:namespace
)
}
let!
(
:namespace
)
{
create
(
:namespace
)
}
it
{
is_expected
.
to
have_many
:projects
}
it
{
is_expected
.
to
have_many
:projects
}
it
{
is_expected
.
to
have_one
(
:namespace_metrics
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
validate_presence_of
(
:name
)
}
it
{
is_expected
.
to
validate_presence_of
(
:name
)
}
it
{
is_expected
.
to
validate_uniqueness_of
(
:name
)
}
it
{
is_expected
.
to
validate_uniqueness_of
(
:name
)
}
...
@@ -17,6 +18,9 @@ describe Namespace, models: true do
...
@@ -17,6 +18,9 @@ describe Namespace, models: true do
it
{
is_expected
.
to
validate_presence_of
(
:owner
)
}
it
{
is_expected
.
to
validate_presence_of
(
:owner
)
}
it
{
is_expected
.
to
delegate_method
(
:shared_runner_minutes
).
to
(
:namespace_metrics
)
}
it
{
is_expected
.
to
delegate_method
(
:shared_runner_last_reset
).
to
(
:namespace_metrics
)
}
describe
"Mass assignment"
do
describe
"Mass assignment"
do
end
end
...
@@ -144,4 +148,112 @@ describe Namespace, models: true do
...
@@ -144,4 +148,112 @@ describe Namespace, models: true do
it
{
expect
(
group
.
full_path
).
to
eq
(
group
.
path
)
}
it
{
expect
(
group
.
full_path
).
to
eq
(
group
.
path
)
}
it
{
expect
(
nested_group
.
full_path
).
to
eq
(
"
#{
group
.
path
}
/
#{
nested_group
.
path
}
"
)
}
it
{
expect
(
nested_group
.
full_path
).
to
eq
(
"
#{
group
.
path
}
/
#{
nested_group
.
path
}
"
)
}
end
end
describe
'#shared_runners_enabled?'
do
subject
{
namespace
.
shared_runners_enabled?
}
context
'without projects'
do
it
{
is_expected
.
to
be_falsey
}
end
context
'with project'
do
context
'and disabled shared runners'
do
let!
(
:project
)
{
create
(
:empty_project
,
namespace:
namespace
)
}
it
{
is_expected
.
to
be_falsey
}
end
context
'and enabled shared runners'
do
let!
(
:project
)
do
create
(
:empty_project
,
namespace:
namespace
,
shared_runners_enabled:
true
)
end
it
{
is_expected
.
to
be_truthy
}
end
end
end
describe
'#shared_runners_minutes_limit'
do
subject
{
namespace
.
shared_runners_minutes_limit
}
context
'when no limit defined'
do
it
{
is_expected
.
to
be_nil
}
end
context
'when application settings limit is set'
do
before
do
stub_application_setting
(
shared_runners_minutes:
1000
)
end
it
'returns global limit'
do
is_expected
.
to
eq
(
1000
)
end
context
'when namespace limit is set'
do
before
do
namespace
.
shared_runners_minutes_limit
=
500
end
it
'returns namespace limit'
do
is_expected
.
to
eq
(
500
)
end
end
end
end
describe
'#shared_runners_minutes_limit_enabled?'
do
subject
{
namespace
.
shared_runners_minutes_limit_enabled?
}
context
'when no limit defined'
do
it
{
is_expected
.
to
be_falsey
}
end
context
'when limit is defined'
do
before
do
namespace
.
shared_runners_minutes_limit
=
500
end
it
{
is_expected
.
to
be_truthy
}
end
end
describe
'#shared_runners_minutes_used?'
do
subject
{
namespace
.
shared_runners_minutes_used?
}
context
'with project'
do
let!
(
:project
)
do
create
(
:empty_project
,
namespace:
namespace
,
shared_runners_enabled:
true
)
end
context
'when limit is defined'
do
context
'when limit is used'
do
let
(
:namespace
)
{
create
(
:namespace
,
:with_used_build_minutes_limit
)
}
it
{
is_expected
.
to
be_truthy
}
end
context
'when limit not yet used'
do
let
(
:namespace
)
{
create
(
:namespace
,
:with_not_used_build_minutes_limit
)
}
it
{
is_expected
.
to
be_falsey
}
end
context
'when minutes are not yet set'
do
it
{
is_expected
.
to
be_falsey
}
end
end
context
'without limit'
do
it
{
is_expected
.
to
be_falsey
}
end
end
context
'without project'
do
it
{
is_expected
.
to
be_falsey
}
end
end
end
end
spec/models/project_metrics_spec.rb
0 → 100644
View file @
acaa36cb
require
'spec_helper'
describe
ProjectMetrics
,
models:
true
do
it
{
is_expected
.
to
belong_to
(
:project
)
}
it
{
is_expected
.
to
validate_presence_of
(
:project
)
}
end
spec/models/project_spec.rb
View file @
acaa36cb
...
@@ -5,6 +5,7 @@ describe Project, models: true do
...
@@ -5,6 +5,7 @@ describe Project, models: true do
it
{
is_expected
.
to
belong_to
(
:group
)
}
it
{
is_expected
.
to
belong_to
(
:group
)
}
it
{
is_expected
.
to
belong_to
(
:namespace
)
}
it
{
is_expected
.
to
belong_to
(
:namespace
)
}
it
{
is_expected
.
to
belong_to
(
:creator
).
class_name
(
'User'
)
}
it
{
is_expected
.
to
belong_to
(
:creator
).
class_name
(
'User'
)
}
it
{
is_expected
.
to
have_one
(
:project_metrics
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_many
(
:users
)
}
it
{
is_expected
.
to
have_many
(
:users
)
}
it
{
is_expected
.
to
have_many
(
:services
)
}
it
{
is_expected
.
to
have_many
(
:services
)
}
it
{
is_expected
.
to
have_many
(
:events
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_many
(
:events
).
dependent
(
:destroy
)
}
...
@@ -72,6 +73,13 @@ describe Project, models: true do
...
@@ -72,6 +73,13 @@ describe Project, models: true do
it
{
is_expected
.
to
have_many
(
:forks
).
through
(
:forked_project_links
)
}
it
{
is_expected
.
to
have_many
(
:forks
).
through
(
:forked_project_links
)
}
it
{
is_expected
.
to
have_many
(
:approver_groups
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_many
(
:approver_groups
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
delegate_method
(
:shared_runner_minutes
).
to
(
:project_metrics
)
}
it
{
is_expected
.
to
delegate_method
(
:shared_runner_last_reset
).
to
(
:project_metrics
)
}
it
{
is_expected
.
to
delegate_method
(
:shared_runners_minutes_limit
).
to
(
:namespace
)
}
it
{
is_expected
.
to
delegate_method
(
:shared_runners_minutes_limit_enabled?
).
to
(
:namespace
)
}
it
{
is_expected
.
to
delegate_method
(
:shared_runners_minutes_used?
).
to
(
:namespace
)
}
context
'after initialized'
do
context
'after initialized'
do
it
"has a project_feature"
do
it
"has a project_feature"
do
project
=
FactoryGirl
.
build
(
:project
)
project
=
FactoryGirl
.
build
(
:project
)
...
@@ -1087,15 +1095,30 @@ describe Project, models: true do
...
@@ -1087,15 +1095,30 @@ describe Project, models: true do
context
'for shared runners enabled'
do
context
'for shared runners enabled'
do
let
(
:shared_runners_enabled
)
{
true
}
let
(
:shared_runners_enabled
)
{
true
}
it
'has a shared runner'
do
before
do
shared_runner
shared_runner
end
it
'has a shared runner'
do
expect
(
project
.
any_runners?
).
to
be_truthy
expect
(
project
.
any_runners?
).
to
be_truthy
end
end
it
'checks the presence of shared runner'
do
it
'checks the presence of shared runner'
do
shared_runner
expect
(
project
.
any_runners?
{
|
runner
|
runner
==
shared_runner
}).
to
be_truthy
expect
(
project
.
any_runners?
{
|
runner
|
runner
==
shared_runner
}).
to
be_truthy
end
end
context
'with used build minutes'
do
let
(
:namespace
)
{
create
(
:namespace
,
:with_used_build_minutes_limit
)
}
let
(
:project
)
do
create
(
:empty_project
,
namespace:
namespace
,
shared_runners_enabled:
shared_runners_enabled
)
end
it
'does not have a shared runner'
do
expect
(
project
.
any_runners?
).
to
be_falsey
end
end
end
end
end
end
...
@@ -2048,6 +2071,42 @@ describe Project, models: true do
...
@@ -2048,6 +2071,42 @@ describe Project, models: true do
end
end
end
end
describe
'#shared_runners_minutes_limit_enabled?'
do
subject
{
project
.
shared_runners_minutes_limit_enabled?
}
context
'with limit enabled'
do
let
(
:namespace
)
{
create
(
:namespace
,
:with_build_minutes_limit
)
}
context
'for public project'
do
let
(
:project
)
{
create
(
:empty_project
,
:public
,
namespace:
namespace
)
}
it
{
is_expected
.
to
be_truthy
}
end
context
'for internal project'
do
let
(
:project
)
{
create
(
:empty_project
,
:internal
,
namespace:
namespace
)
}
it
{
is_expected
.
to
be_falsey
}
end
context
'for private project'
do
let
(
:project
)
{
create
(
:empty_project
,
:private
,
namespace:
namespace
)
}
it
{
is_expected
.
to
be_falsey
}
end
end
context
'with limit not set'
do
let
(
:namespace
)
{
create
(
:namespace
)
}
context
'for public project'
do
let
(
:project
)
{
create
(
:empty_project
,
:public
,
namespace:
namespace
)
}
it
{
is_expected
.
to
be_falsey
}
end
end
end
def
enable_lfs
def
enable_lfs
allow
(
Gitlab
.
config
.
lfs
).
to
receive
(
:enabled
).
and_return
(
true
)
allow
(
Gitlab
.
config
.
lfs
).
to
receive
(
:enabled
).
and_return
(
true
)
end
end
...
...
spec/services/update_build_minutes_service_spec.rb
0 → 100644
View file @
acaa36cb
require
'spec_helper'
describe
UpdateBuildMinutesService
,
services:
true
do
context
'#perform'
do
let
(
:namespace
)
{
create
(
:namespace
)
}
let
(
:project
)
{
create
(
:empty_project
,
namespace:
namespace
)
}
let
(
:pipeline
)
{
create
(
:ci_pipeline
)
}
let
(
:build
)
do
create
(
:ci_build
,
:success
,
runner:
runner
,
pipeline:
pipeline
,
started_at:
2
.
hour
.
ago
,
finished_at:
1
.
hour
.
ago
)
end
subject
{
described_class
.
new
.
execute
(
build
)
}
context
'with shared runner'
do
let
(
:runner
)
{
create
(
:ci_runner
,
:shared
)
}
it
"creates a metrics and sets duration"
do
subject
#expect(project.reload.project_metrics.shared_runners_minutes).to
# eq(build.duration)
expect
(
namespace
.
reload
.
namespace_metrics
.
shared_runners_minutes
).
to
eq
(
build
.
duration
)
end
context
'when metrics are created'
do
before
do
project
.
create_project_metrics
(
shared_runners_minutes:
100
)
namespace
.
create_namespace_metrics
(
shared_runners_minutes:
100
)
end
it
"updates metrics and adds duration"
do
subject
expect
(
project
.
project_metrics
.
shared_runners_minutes
).
to
eq
(
100
+
build
.
duration
)
expect
(
namespace
.
namespace_metrics
.
shared_runners_minutes
).
to
eq
(
100
+
build
.
duration
)
end
end
end
context
'for specific runner'
do
let
(
:runner
)
{
create
(
:ci_runner
)
}
it
"does not create metrics"
do
subject
expect
(
project
.
project_metrics
).
to
be_nil
expect
(
namespace
.
namespace_metrics
).
to
be_nil
end
end
end
end
spec/workers/clean_shared_runner_minutes_worker_spec.rb
0 → 100644
View file @
acaa36cb
require
'spec_helper'
describe
ClearSharedRunnerMinutesWorker
do
let
(
:worker
)
{
described_class
.
new
}
describe
'#perform'
do
subject
{
worker
.
perform
}
context
'when project metrics are defined'
do
let!
(
:project_metrics
)
{
create
(
:project_metrics
,
shared_runners_minutes:
100
)
}
it
'clears counters'
do
subject
expect
(
project_metrics
.
reload
.
shared_runners_minutes
).
to
be_zero
end
it
'resets timer'
do
subject
expect
(
project_metrics
.
reload
.
shared_runners_minutes_last_reset
).
to
be_like_time
(
Time
.
now
)
end
end
context
'when project metrics are defined'
do
let!
(
:namespace_metrics
)
{
create
(
:namespace_metrics
,
shared_runners_minutes:
100
)
}
it
'clears counters'
do
subject
expect
(
namespace_metrics
.
reload
.
shared_runners_minutes
).
to
be_zero
end
it
'resets timer'
do
subject
expect
(
namespace_metrics
.
reload
.
shared_runners_minutes_last_reset
).
to
be_like_time
(
Time
.
now
)
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