Testing Role-Based Job Scheduling Implementation in Whenever
This test suite validates role-based job scheduling functionality in the Whenever gem, focusing on how jobs are filtered and executed based on specified roles. It ensures proper handling of role assignments and job execution across different role configurations.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
javan/whenever
test/functional/output_jobs_for_roles_test.rb
require 'test_helper'
class OutputJobsForRolesTest < Whenever::TestCase
test "one role requested and specified on the job" do
output = Whenever.cron :roles => [:role1], :string => \
<<-file
every 2.hours, :roles => [:role1] do
command "blahblah"
end
file
assert_equal two_hours + " /bin/bash -l -c 'blahblah'
", output
end
test "one role requested but none specified on the job" do
output = Whenever.cron :roles => [:role1], :string => \
<<-file
every 2.hours do
command "blahblah"
end
file
# this should output the job because not specifying a role means "all roles"
assert_equal two_hours + " /bin/bash -l -c 'blahblah'
", output
end
test "no roles requested but one specified on the job" do
output = Whenever.cron \
<<-file
every 2.hours, :roles => [:role1] do
command "blahblah"
end
file
# this should output the job because not requesting roles means "all roles"
assert_equal two_hours + " /bin/bash -l -c 'blahblah'
", output
end
test "a different role requested than the one specified on the job" do
output = Whenever.cron :roles => [:role1], :string => \
<<-file
every 2.hours, :roles => [:role2] do
command "blahblah"
end
file
assert_equal "", output
end
test "with 2 roles requested and a job defined for each" do
output = Whenever.cron :roles => [:role1, :role2], :string => \
<<-file
every 2.hours, :roles => [:role1] do
command "role1_cmd"
end
every :hour, :roles => [:role2] do
command "role2_cmd"
end
file
assert_match two_hours + " /bin/bash -l -c 'role1_cmd'", output
assert_match "0 * * * * /bin/bash -l -c 'role2_cmd'", output
end
end