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
Boxiang Sun
gitlab-ce
Commits
9266cd5e
Commit
9266cd5e
authored
Sep 26, 2018
by
Shinya Maeda
Committed by
Alessio Caiazza
Oct 02, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unit tests for Ci::Build. Fix validation on state transition
parent
4b0aa573
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
168 additions
and
5 deletions
+168
-5
app/models/ci/build.rb
app/models/ci/build.rb
+3
-5
spec/factories/ci/builds.rb
spec/factories/ci/builds.rb
+12
-0
spec/models/ci/build_spec.rb
spec/models/ci/build_spec.rb
+153
-0
No files found.
app/models/ci/build.rb
View file @
9266cd5e
...
@@ -168,12 +168,10 @@ module Ci
...
@@ -168,12 +168,10 @@ module Ci
end
end
event
:enqueue_scheduled
do
event
:enqueue_scheduled
do
transition
scheduled: :pending
do
transition
scheduled: :pending
,
if:
->
(
build
)
do
validate
do
|
build
|
build
.
scheduled_at
&&
build
.
scheduled_at
<
Time
.
now
build
.
scheduled_at
&&
build
.
scheduled_at
<
Time
.
now
end
end
end
end
end
before_transition
scheduled:
any
do
|
build
|
before_transition
scheduled:
any
do
|
build
|
build
.
scheduled_at
=
nil
build
.
scheduled_at
=
nil
...
@@ -269,7 +267,7 @@ module Ci
...
@@ -269,7 +267,7 @@ module Ci
end
end
def
action?
def
action?
%w[manual
schedul
ed]
.
include?
(
self
.
when
)
%w[manual
delay
ed]
.
include?
(
self
.
when
)
end
end
# rubocop: disable CodeReuse/ServiceClass
# rubocop: disable CodeReuse/ServiceClass
...
...
spec/factories/ci/builds.rb
View file @
9266cd5e
...
@@ -70,6 +70,18 @@ FactoryBot.define do
...
@@ -70,6 +70,18 @@ FactoryBot.define do
status
'created'
status
'created'
end
end
trait
:scheduled
do
schedulable
status
'scheduled'
scheduled_at
1
.
minute
.
since
end
trait
:expired_scheduled
do
schedulable
status
'scheduled'
scheduled_at
1
.
minute
.
ago
end
trait
:manual
do
trait
:manual
do
status
'manual'
status
'manual'
self
.
when
'manual'
self
.
when
'manual'
...
...
spec/models/ci/build_spec.rb
View file @
9266cd5e
...
@@ -209,6 +209,147 @@ describe Ci::Build do
...
@@ -209,6 +209,147 @@ describe Ci::Build do
end
end
end
end
describe
'#schedulable?'
do
subject
{
build
.
schedulable?
}
context
'when build is schedulable'
do
let
(
:build
)
{
create
(
:ci_build
,
:created
,
:schedulable
,
project:
project
)
}
it
{
expect
(
subject
).
to
be_truthy
}
context
'when feature flag is diabled'
do
before
do
stub_feature_flags
(
ci_enable_scheduled_build:
false
)
end
it
{
expect
(
subject
).
to
be_falsy
}
end
end
context
'when build is not schedulable'
do
let
(
:build
)
{
create
(
:ci_build
,
:created
,
project:
project
)
}
it
{
expect
(
subject
).
to
be_falsy
}
end
end
describe
'#schedule'
do
subject
{
build
.
schedule
}
before
do
project
.
add_developer
(
user
)
end
let
(
:build
)
{
create
(
:ci_build
,
:created
,
:schedulable
,
user:
user
,
project:
project
)
}
it
'transits to scheduled'
do
subject
expect
(
build
).
to
be_scheduled
end
it
'updates scheduled_at column'
do
subject
expect
(
build
.
scheduled_at
).
not_to
be_nil
end
it
'schedules BuildScheduleWorker at the right time'
do
Timecop
.
freeze
do
expect
(
Ci
::
BuildScheduleWorker
)
.
to
receive
(
:perform_at
).
with
(
1
.
minute
.
since
,
build
.
id
)
subject
end
end
end
describe
'#unschedule'
do
subject
{
build
.
unschedule
}
context
'when build is scheduled'
do
let
(
:build
)
{
create
(
:ci_build
,
:scheduled
,
pipeline:
pipeline
)
}
it
'cleans scheduled_at column'
do
subject
expect
(
build
.
scheduled_at
).
to
be_nil
end
it
'transits to manual'
do
subject
expect
(
build
).
to
be_manual
end
end
context
'when build is not scheduled'
do
let
(
:build
)
{
create
(
:ci_build
,
:created
,
pipeline:
pipeline
)
}
it
'does not transit status'
do
subject
expect
(
build
).
to
be_created
end
end
end
describe
'#options_scheduled_at'
do
subject
{
build
.
options_scheduled_at
}
let
(
:build
)
{
build_stubbed
(
:ci_build
,
options:
option
)
}
context
'when start_in is 1 day'
do
let
(
:option
)
{
{
start_in:
'1 day'
}
}
it
'returns date after 1 day'
do
Timecop
.
freeze
do
is_expected
.
to
eq
(
1
.
day
.
since
)
end
end
end
context
'when start_in is 1 week'
do
let
(
:option
)
{
{
start_in:
'1 week'
}
}
it
'returns date after 1 week'
do
Timecop
.
freeze
do
is_expected
.
to
eq
(
1
.
week
.
since
)
end
end
end
end
describe
'#enqueue_scheduled'
do
subject
{
build
.
enqueue_scheduled
}
context
'when build is scheduled and the right time has not come yet'
do
let
(
:build
)
{
create
(
:ci_build
,
:scheduled
,
pipeline:
pipeline
)
}
it
'does not transits the status'
do
subject
expect
(
build
).
to
be_scheduled
end
end
context
'when build is scheduled and the right time has already come'
do
let
(
:build
)
{
create
(
:ci_build
,
:expired_scheduled
,
pipeline:
pipeline
)
}
it
'cleans scheduled_at column'
do
subject
expect
(
build
.
scheduled_at
).
to
be_nil
end
it
'transits to pending'
do
subject
expect
(
build
).
to
be_pending
end
end
end
describe
'#any_runners_online?'
do
describe
'#any_runners_online?'
do
subject
{
build
.
any_runners_online?
}
subject
{
build
.
any_runners_online?
}
...
@@ -1193,6 +1334,12 @@ describe Ci::Build do
...
@@ -1193,6 +1334,12 @@ describe Ci::Build do
it
{
is_expected
.
to
be_truthy
}
it
{
is_expected
.
to
be_truthy
}
end
end
context
'when is set to delayed'
do
let
(
:value
)
{
'delayed'
}
it
{
is_expected
.
to
be_truthy
}
end
context
'when set to something else'
do
context
'when set to something else'
do
let
(
:value
)
{
'something else'
}
let
(
:value
)
{
'something else'
}
...
@@ -1463,6 +1610,12 @@ describe Ci::Build do
...
@@ -1463,6 +1610,12 @@ describe Ci::Build do
end
end
end
end
context
'when build is scheduled'
do
subject
{
build_stubbed
(
:ci_build
,
:scheduled
)
}
it
{
is_expected
.
to
be_playable
}
end
context
'when build is not a manual action'
do
context
'when build is not a manual action'
do
subject
{
build_stubbed
(
:ci_build
,
:success
)
}
subject
{
build_stubbed
(
:ci_build
,
:success
)
}
...
...
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