Commit 21a7aed9 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Update tests to cover trigger schedule

parent 03088552
......@@ -16,24 +16,19 @@
.checkbox
= schedule_fields.label :active do
= schedule_fields.check_box :active
%strong Schedule trigger
%strong Schedule trigger (experimental)
.help-block
If checked, this trigger will be executed periodically according to `cron`, `cron_timezone` and `ref`
= link_to icon('question-circle'), help_page_path('user/project/pipelines/settings', anchor: 'visibility-of-pipelines')
If checked, this trigger will be executed periodically according to `Cron` and `Timezone`.
= link_to icon('question-circle'), help_page_path('ci/triggers', anchor: 'schedule')
.form-group
= schedule_fields.label :cron, "Cron", class: "label-light"
= schedule_fields.text_field :cron, class: "form-control", title: 'Trigger Schedule cron is required.', placeholder: "0 1 * * *"
= schedule_fields.text_field :cron, class: "form-control", title: 'Cron specification is required.', placeholder: "0 1 * * *"
.form-group
= schedule_fields.label :cron, "Timezone", class: "label-light"
= schedule_fields.text_field :cron_timezone, class: "form-control", title: 'Trigger Schedule cron_timezone is required.', placeholder: "UTC"
= schedule_fields.text_field :cron_timezone, class: "form-control", title: 'Timezone is required.', placeholder: "UTC"
.form-group
- schedule_ref = @trigger.trigger_schedule.ref || @project.default_branch
= schedule_fields.label :ref, "Branch or tag", class: "label-light"
= hidden_field_tag 'trigger[trigger_schedule_attributes][ref]', schedule_ref
= dropdown_tag(schedule_ref,
options: { toggle_class: 'js-branch-select wide',
filter: true, dropdown_class: "dropdown-menu-selectable", placeholder: "Search branches",
data: { selected: schedule_ref, field_name: 'trigger[trigger_schedule_attributes][ref]' } })
= schedule_fields.text_field :ref, class: "form-control", title: 'Branch or tag is required.', placeholder: "master"
.help-block Existing branch name, tag
= f.submit btn_text, class: "btn btn-save"
......
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170406115029) do
ActiveRecord::Schema.define(version: 20170407122426) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -311,6 +311,8 @@ ActiveRecord::Schema.define(version: 20170406115029) do
t.string "cron"
t.string "cron_timezone"
t.datetime "next_run_at"
t.string "ref"
t.boolean "active"
end
add_index "ci_trigger_schedules", ["next_run_at"], name: "index_ci_trigger_schedules_on_next_run_at", using: :btree
......
......@@ -3,9 +3,11 @@ FactoryGirl.define do
trigger factory: :ci_trigger_for_trigger_schedule
cron '0 1 * * *'
cron_timezone Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
ref 'master'
active true
after(:build) do |trigger_schedule, evaluator|
trigger_schedule.update!(project: trigger_schedule.trigger.project)
trigger_schedule.project ||= trigger_schedule.trigger.project
end
trait :nightly do
......
......@@ -77,6 +77,53 @@ feature 'Triggers', feature: true, js: true do
expect(page.find('.flash-notice')).to have_content 'Trigger was successfully updated.'
expect(page.find('.triggers-list')).to have_content new_trigger_title
end
context 'scheduled triggers' do
let!(:trigger) do
create(:ci_trigger, owner: user, project: @project, description: trigger_title)
end
context 'enabling schedule' do
before do
visit edit_namespace_project_trigger_path(@project.namespace, @project, trigger)
end
scenario 'do fill form with valid data and save' do
find('#trigger_trigger_schedule_attributes_active').click
fill_in 'trigger_trigger_schedule_attributes_cron', with: '1 * * * *'
fill_in 'trigger_trigger_schedule_attributes_cron_timezone', with: 'UTC'
fill_in 'trigger_trigger_schedule_attributes_ref', with: 'master'
click_button 'Save trigger'
expect(page.find('.flash-notice')).to have_content 'Trigger was successfully updated.'
end
scenario 'do not fill form with valid data and save' do
find('#trigger_trigger_schedule_attributes_active').click
click_button 'Save trigger'
expect(page).to have_content 'The form contains the following errors'
end
end
context 'enabling schedule' do
before do
trigger.create_trigger_schedule(project: trigger.project, active: true)
visit edit_namespace_project_trigger_path(@project.namespace, @project, trigger)
end
scenario 'disable and save form' do
find('#trigger_trigger_schedule_attributes_active').click
click_button 'Save trigger'
expect(page.find('.flash-notice')).to have_content 'Trigger was successfully updated.'
visit edit_namespace_project_trigger_path(@project.namespace, @project, trigger)
checkbox = find_field('trigger_trigger_schedule_attributes_active')
expect(checkbox).not_to be_checked
end
end
end
end
describe 'trigger "Take ownership" workflow' do
......@@ -125,14 +172,6 @@ feature 'Triggers', feature: true, js: true do
describe 'show triggers workflow' do
scenario 'contains trigger description placeholder' do
expect(page.find('#trigger_description')['placeholder']).to eq 'Trigger description'
expect(page.find('#trigger_trigger_schedule_attributes_cron')['placeholder']).to eq '0 1 * * *'
expect(page.find('#trigger_trigger_schedule_attributes_cron_timezone')['placeholder']).to eq 'UTC'
expect(page.find('#trigger_ref')['placeholder']).to eq 'master'
end
scenario 'show checkbox for registration of scheduled trigger and checked off defaultly' do
expect(page.find('#trigger_trigger_schedule_attributes__destroy')['type']).to eq 'checkbox'
expect(page.find('#trigger_trigger_schedule_attributes__destroy')['value']).to eq '0'
end
scenario 'show "legacy" badge for legacy trigger' do
......
......@@ -8,24 +8,39 @@ describe TriggerScheduleWorker do
end
context 'when there is a scheduled trigger within next_run_at' do
let(:next_run_at) { 2.days.ago }
let!(:trigger_schedule) do
create(:ci_trigger_schedule, :nightly)
end
before do
trigger_schedule = create(:ci_trigger_schedule, :nightly)
time_future = Time.now + 10.days
allow(Time).to receive(:now).and_return(time_future)
@next_time = Gitlab::Ci::CronParser.new(trigger_schedule.cron, trigger_schedule.cron_timezone).next_time_from(time_future)
trigger_schedule.update_column(:next_run_at, next_run_at)
end
it 'creates a new trigger request' do
expect { worker.perform }.to change { Ci::TriggerRequest.count }.by(1)
expect { worker.perform }.to change { Ci::TriggerRequest.count }
end
it 'creates a new pipeline' do
expect { worker.perform }.to change { Ci::Pipeline.count }.by(1)
expect { worker.perform }.to change { Ci::Pipeline.count }
expect(Ci::Pipeline.last).to be_pending
end
it 'updates next_run_at' do
expect { worker.perform }.to change { Ci::TriggerSchedule.last.next_run_at }.to(@next_time)
worker.perform
expect(trigger_schedule.reload.next_run_at).not_to eq(next_run_at)
end
context 'inactive schedule' do
before do
trigger_schedule.update(active: false)
end
it 'does not create a new trigger' do
expect { worker.perform }.not_to change { Ci::TriggerRequest.count }
end
end
end
......@@ -42,10 +57,7 @@ describe TriggerScheduleWorker do
end
context 'when next_run_at is nil' do
before do
trigger_schedule = create(:ci_trigger_schedule, :nightly)
trigger_schedule.update_attribute(:next_run_at, nil)
end
let!(:trigger_schedule) { create(:ci_trigger_schedule, :nightly, next_run_at: nil) }
it 'does not create a new pipeline' do
expect { worker.perform }.not_to change { Ci::Pipeline.count }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment