Back to Repositories

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

The test suite provides comprehensive coverage of role-based job scheduling scenarios in Whenever:

  • Single role assignment and matching
  • Default role behavior when none specified
  • Multiple role configurations
  • Role mismatch handling
  • Integration with cron output generation

Implementation Analysis

The testing approach uses Minitest to verify role-based job filtering logic. It implements multiple test cases that validate different role configurations and their impact on job scheduling. The tests utilize Whenever’s cron output generation to verify correct job inclusion/exclusion based on role specifications.

Technical Details

Testing tools and configuration:

  • Minitest framework for test implementation
  • Whenever::TestCase as base test class
  • String-based job definitions for test scenarios
  • Assert and assert_match for verification
  • Custom helper methods for time formatting

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for specific role scenarios
  • Clear test naming conventions
  • Comprehensive edge case coverage
  • Consistent assertion patterns
  • Proper test setup and teardown

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