Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jan 23, 2024
1 parent eec9fce commit 70fd953
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Spatie\Health\Checks\Result;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;

class RecentBackupCheck extends Check
class BackupsCheck extends Check
{
protected ?string $locatedAt = null;

Expand All @@ -18,6 +18,8 @@ class RecentBackupCheck extends Check

protected int $minimumSizeInMegabytes = 0;

protected int $minimumNumberOfBackups = 0;

public function locatedAt(string $globPath): self
{
$this->locatedAt = $globPath;
Expand Down Expand Up @@ -46,6 +48,13 @@ public function atLeastSizeInMb(int $minimumSizeInMegabytes): self
return $this;
}

public function atLeastNumberOfBackups(int $minimumNumberOfBackups): self
{
$this->minimumNumberOfBackups = $minimumNumberOfBackups;

return $this;
}

public function run(): Result
{
$files = collect(File::glob($this->locatedAt));
Expand All @@ -66,6 +75,10 @@ public function run(): Result
return Result::make()->failed('No backups found that are large enough');
}

if ($eligableBackups->count() < $this->minimumNumberOfBackups) {
return Result::make()->failed('Not enough backups found');
}

if ($this->youngestShouldHaveBeenMadeBefore) {
if ($this->youngestBackupIsToolOld($eligableBackups)) {
return Result::make()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

use Spatie\Health\Checks\Checks\RecentBackupCheck;
use Spatie\Health\Checks\Checks\BackupsCheck;
use Spatie\Health\Enums\Status;
use Spatie\Health\Facades\Health;
use Spatie\TemporaryDirectory\TemporaryDirectory;
use function Spatie\PestPluginTestTime\testTime;

beforeEach(function() {
$this->recentBackupCheck = RecentBackupCheck::new();
$this->backupsCheck = BackupsCheck::new();

Health::checks([
RecentBackupCheck::new(),
BackupsCheck::new(),
]);

$this->temporaryDirectory = TemporaryDirectory::make(getTemporaryDirectory())->force()->empty();
Expand All @@ -22,7 +22,7 @@
it('it will succeed if a file with the given glob exist', function() {
addTestFile($this->temporaryDirectory->path('hey.zip'));

$result = $this->recentBackupCheck
$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->run();

Expand All @@ -32,15 +32,15 @@
it('it will fail if a file with the given glob does not exist', function() {
addTestFile($this->temporaryDirectory->path('hey.other'));

$result = $this->recentBackupCheck
$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->run();

expect($result)->status->toBe(Status::failed());
});

it('will fail if the given directory does not exist', function() {
$result = $this->recentBackupCheck
$result = $this->backupsCheck
->locatedAt('non-existing-directory')
->run();

Expand All @@ -50,7 +50,7 @@
it('will fail if the backup is smaller than the given size', function() {
addTestFile($this->temporaryDirectory->path('hey.zip'), sizeInMb: 4);

$result = $this->recentBackupCheck
$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->atLeastSizeInMb(5)
->run();
Expand All @@ -61,7 +61,7 @@
it('will pass if the backup is at least than the given size', function(int $sizeInMb) {
addTestFile($this->temporaryDirectory->path('hey.zip'), sizeInMb: $sizeInMb);

$result = $this->recentBackupCheck
$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->atLeastSizeInMb(5)
->run();
Expand All @@ -77,15 +77,15 @@

testTime()->addMinutes(4);

$result = $this->recentBackupCheck
$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->youngestBackShouldHaveBeenMadeBefore(now()->subMinutes(5)->startOfMinute())
->run();
expect($result)->status->toBe(Status::ok());

testTime()->addMinute();

$result = $this->recentBackupCheck
$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->youngestBackShouldHaveBeenMadeBefore(now()->subMinutes(5))
->run();
Expand All @@ -97,7 +97,7 @@

testTime()->addMinutes(4);

$result = $this->recentBackupCheck
$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->oldestBackShouldHaveBeenMadeAfter(now()->subMinutes(5))
->run();
Expand All @@ -106,12 +106,30 @@

testTime()->addMinute();

$result = $this->recentBackupCheck
$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->oldestBackShouldHaveBeenMadeAfter(now()->subMinutes(5))
->run();
expect($result)->status->toBe(Status::failed());
});

it('can check that there are enough backups', function() {
addTestFile($this->temporaryDirectory->path('first.zip'));

$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->atLeastNumberOfBackups(2)
->run();
expect($result)->status->toBe(Status::failed());

addTestFile($this->temporaryDirectory->path('second.zip'));

$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->atLeastNumberOfBackups(2)
->run();
expect($result)->status->toBe(Status::ok());
});



0 comments on commit 70fd953

Please sign in to comment.