add more tests

add first version to start crons
adjust code to return now if cron is overdue
This commit is contained in:
Steffen Kötte 2016-01-18 16:59:10 +01:00
parent 9b24312445
commit da33cff0eb
2 changed files with 99 additions and 11 deletions

View File

@ -3,22 +3,38 @@ module Travis::API::V3
belongs_to :branch belongs_to :branch
def self.start_all
self.all.each do |cron|
cron.start if cron.next_build_time <= Time.now
end
end
def start
raise ServerError, 'repository does not have a github_id'.freeze unless branch.repository.github_id
payload = {
repository: { id: branch.repository.github_id, owner_name: branch.repository.owner_name, name: branch.repository.name },
user: { id: user.id },
message: '',
branch: branch,
config: {}
}
token = "TODO: Figure out where to get this (branch.repository.owner ?)"
perform_async(:build_request, type: 'cron'.freeze, credentials: { token: token }, payload: JSON.dump(payload))
payload
end
def next_build_time def next_build_time
if disable_by_build if (disable_by_build) && (last_non_cron_build_date > last_planned_time)
if last_non_cron_build_date > last_planned_time
return after_next_planned_time return after_next_planned_time
else elsif last_cron_build_date >= last_planned_time
return next_planned_time
end
else
if last_cron_build_date >= last_planned_time
return next_planned_time return next_planned_time
else else
return Time.now return Time.now
end end
end end
end
def next_planned_time def next_planned_time
now = DateTime.now now = DateTime.now

View File

@ -155,5 +155,77 @@ describe Travis::API::V3::Models::Cron do
end end
describe "build starts now if next build time is in the past" do
before do
# nothing, this time
# time freeze is performed in examples
end
after do
Timecop.return
end
it "for daily builds with disable_by_build true" do
Timecop.travel(DateTime.new(2015, 12, 31, 16))
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: true)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 1, 19))
expect(cron.next_build_time).to be == DateTime.now
build.destroy
cron.destroy
end
it "for daily builds with disable_by_build false" do
Timecop.travel(DateTime.new(2015, 12, 31, 16))
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: false)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 1, 19))
expect(cron.next_build_time).to be == DateTime.now
build.destroy
cron.destroy
end
it "for weekly builds with disable_by_build true" do
Timecop.travel(DateTime.new(2015, 12, 31, 16))
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: true)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 7, 19))
expect(cron.next_build_time).to be == DateTime.now
build.destroy
cron.destroy
end
it "for weekly builds with disable_by_build false" do
Timecop.travel(DateTime.new(2015, 12, 31, 16))
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: false)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 7, 19))
expect(cron.next_build_time).to be == DateTime.now
build.destroy
cron.destroy
end
it "for monthly builds with disable_by_build true" do
Timecop.travel(DateTime.new(2015, 12, 31, 16))
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: true)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 31, 19))
expect(cron.next_build_time).to be == DateTime.now
build.destroy
cron.destroy
end
it "for monthly builds with disable_by_build false" do
Timecop.travel(DateTime.new(2015, 12, 31, 16))
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: false)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 31, 19))
expect(cron.next_build_time).to be == DateTime.now
build.destroy
cron.destroy
end
end
end end