-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from pulibrary/add-file-absence-check
Adding FileAbsence provider to check that a file remains absent
- Loading branch information
Showing
4 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'health_monitor/providers/base' | ||
|
||
module HealthMonitor | ||
module Providers | ||
class FileAbsenceException < StandardError; end | ||
|
||
class FileAbsence < Base | ||
class Configuration < Base::Configuration | ||
DEFAULT_FILENAME = nil | ||
attr_accessor :filename | ||
|
||
def initialize(provider) | ||
super(provider) | ||
|
||
@filename = DEFAULT_FILENAME | ||
end | ||
end | ||
|
||
def check! | ||
return unless File.exist?(configuration.filename) | ||
|
||
raise FileAbsenceException.new("Unwanted file #{configuration.filename} exists!") | ||
end | ||
|
||
private | ||
|
||
def configuration_class | ||
::HealthMonitor::Providers::FileAbsence::Configuration | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe HealthMonitor::Providers::FileAbsence do | ||
subject { described_class.new } | ||
|
||
context 'with defaults' do | ||
it { expect(subject.configuration.name).to eq('FileAbsence') } | ||
it { expect(subject.configuration.filename).to eq(HealthMonitor::Providers::FileAbsence::Configuration::DEFAULT_FILENAME) } | ||
end | ||
|
||
describe '#name' do | ||
it { expect(subject.name).to eq('FileAbsence') } | ||
end | ||
|
||
describe '#check!' do | ||
let(:filename) { 'bad-file' } | ||
|
||
before do | ||
subject.request = test_request | ||
subject.configure do |config| | ||
config.filename = filename | ||
end | ||
allow(File).to receive('exist?').with(filename).and_return(false) | ||
end | ||
|
||
context 'with a standard connection' do | ||
it 'checks the file' do | ||
subject.check! | ||
expect(File).to have_received('exist?') | ||
end | ||
|
||
it 'succesfully checks' do | ||
expect { | ||
subject.check! | ||
}.not_to raise_error | ||
end | ||
|
||
context 'when failing' do | ||
before do | ||
allow(File).to receive('exist?').with(filename).and_return(true) | ||
end | ||
|
||
it 'fails check!' do | ||
expect { | ||
subject.check! | ||
}.to raise_error(HealthMonitor::Providers::FileAbsenceException) | ||
end | ||
|
||
it 'checks against the configured solr url' do | ||
expect { | ||
subject.check! | ||
}.to raise_error(HealthMonitor::Providers::FileAbsenceException) | ||
expect(File).to have_received('exist?') | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '#configure' do | ||
before do | ||
subject.configure | ||
end | ||
|
||
let(:filename) { 'public/bad-file' } | ||
|
||
it 'filename can be configured' do | ||
expect { | ||
subject.configure do |config| | ||
config.filename = filename | ||
end | ||
}.to change { subject.configuration.filename }.to(filename) | ||
end | ||
end | ||
end |