diff --git a/app/models/job_template.rb b/app/models/job_template.rb index d440906..796f595 100644 --- a/app/models/job_template.rb +++ b/app/models/job_template.rb @@ -1,57 +1,58 @@ class JobTemplate < ActiveRecord::Base establish_connection Baas::settings[:local_db] enum job_type: { backup: 0, restore: 1, verify: 2, admin: 3 } belongs_to :host belongs_to :fileset belongs_to :schedule - validates :name, :schedule_id, :fileset_id, presence: true + validates :name, :fileset_id, presence: true + validates :schedule_id, presence: true, unless: :restore? before_save :set_job_type scope :enabled, -> { where(enabled: true) } # configurable DEFAULT_OPTIONS = { storage: :File, pool: :Default, messages: :Standard, priority: 10, :'Write Bootstrap' => '"/var/lib/bacula/%c.bsr"' } def to_bacula_config_array ['Job {'] + DEFAULT_OPTIONS.map { |k,v| " #{k.capitalize} = #{v}" } + options_array.map { |x| " #{x}" } + ['}'] end def priority DEFAULT_OPTIONS[:priority] end def enabled_human enabled? ? 'yes' : 'no' end private # Sets the default job_type as backup def set_job_type self.job_type = :backup if job_type.nil? end def options_array result = restore? ? ['Where = "/tmp/bacula-restores"'] : [] result += [ "Name = \"#{name}\"", "FileSet = \"#{fileset.name}\"", "Client = \"#{host.name}\"", "Type = \"#{job_type.capitalize}\"", "Schedule = \"#{schedule.name}\"" ] end end diff --git a/spec/factories/job_template.rb b/spec/factories/job_template.rb index 16df7ed..2d1e885 100644 --- a/spec/factories/job_template.rb +++ b/spec/factories/job_template.rb @@ -1,8 +1,18 @@ FactoryGirl.define do - factory :job_template do + factory :jobtemplate, class: JobTemplate do host fileset - schedule sequence(:name) { |n| "Job #{n}" } + + trait :backup do + job_type :backup + schedule + end + + trait :restore do + job_type :restore + end + + factory :job_template, traits: [:backup] end end diff --git a/spec/models/job_template_spec.rb b/spec/models/job_template_spec.rb index c417f38..c3d8fe7 100644 --- a/spec/models/job_template_spec.rb +++ b/spec/models/job_template_spec.rb @@ -1,28 +1,46 @@ require 'spec_helper' describe JobTemplate do + context 'validates' do + it 'name must be present' do + expect(JobTemplate.new).to have(1).errors_on(:name) + end + + it 'fileset_id must be present' do + expect(JobTemplate.new).to have(1).errors_on(:fileset_id) + end + + it 'schedule_id must be present' do + expect(JobTemplate.new).to have(1).errors_on(:schedule_id) + end + + it 'schedule_id must NOT be present for :restore jobs' do + expect(JobTemplate.new(job_type: :restore)).to have(0).errors_on(:schedule_id) + end + end + describe '#to_bacula_config_array' do let(:job_template) { FactoryGirl.create(:job_template) } subject { job_template.to_bacula_config_array } it 'has a Job structure' do expect(subject.first).to eq('Job {') expect(subject.last).to eq('}') end JobTemplate::DEFAULT_OPTIONS.each do |k, v| it "assigns #{k.capitalize} param" do expect(subject).to include(" #{k.capitalize} = #{v}") end end end context 'when no job_type is given' do let(:job_template) { FactoryGirl.create(:job_template) } it 'sets the job_type to :backup' do expect(job_template).to be_backup end end end