Skip to content

Commit 60871d1

Browse files
committed
test(spanner): clear stale backups before running BackupTest
1 parent fd50515 commit 60871d1

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Spanner/tests/System/BackupTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
use Google\Cloud\Core\Exception\BadRequestException;
2121
use Google\Cloud\Core\Exception\ConflictException;
22+
use Google\Cloud\Core\Exception\FailedPreconditionException;
23+
use Google\Cloud\Core\Exception\ServiceException;
2224
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2325
use Google\Cloud\Core\Testing\System\SystemTestCase;
2426
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
@@ -78,13 +80,17 @@ public static function setUpTestFixtures(): void
7880
self::$deletionQueue->add(function () {
7981
self::getDatabaseInstance(self::$dbName1)->drop();
8082
});
83+
} else {
84+
self::cleanUpPendingBackups(self::$dbName1);
8185
}
8286

8387
if (!self::$dbName2 = getenv('GOOGLE_CLOUD_SPANNER_TEST_BACKUP_DATABASE_2')) {
8488
self::$dbName2 = uniqid(self::TESTING_PREFIX);
8589
self::$deletionQueue->add(function () {
8690
self::getDatabaseInstance(self::$dbName2)->drop();
8791
});
92+
} else {
93+
self::cleanUpPendingBackups(self::$dbName2);
8894
}
8995

9096
$db1 = self::getDatabaseInstance(self::$dbName1);
@@ -179,6 +185,9 @@ public function testCreateBackup()
179185
$this->assertNotNull($metadata);
180186
}
181187

188+
/**
189+
* @depends testCreateBackup
190+
*/
182191
public function testCreateBackupRequestFailed()
183192
{
184193
$backupId = uniqid(self::BACKUP_PREFIX);
@@ -209,6 +218,9 @@ public function testCreateBackupRequestFailed()
209218
$this->assertFalse($backup->exists());
210219
}
211220

221+
/**
222+
* @depends testCreateBackup
223+
*/
212224
public function testCreateBackupInvalidArgument()
213225
{
214226
$backupId = uniqid(self::BACKUP_PREFIX);
@@ -734,4 +746,26 @@ private function pollWithExtendedTimeout($op)
734746

735747
return $op;
736748
}
749+
750+
private static function cleanUpPendingBackups($dbName)
751+
{
752+
$dbFullName = self::getDatabaseInstance($dbName)->name();
753+
try {
754+
foreach (self::$instance->backupOperations() as $op) {
755+
if (!$op->done()) {
756+
$metadata = $op->info()['metadata'] ?? [];
757+
if (isset($metadata['database']) && $metadata['database'] === $dbFullName) {
758+
try {
759+
$op->cancel();
760+
$op->pollUntilComplete(['maxPollingDurationSeconds' => 120]);
761+
} catch (\Exception $e) {
762+
// Ignore exceptions from cancelled operations
763+
}
764+
}
765+
}
766+
}
767+
} catch (\Exception $e) {
768+
// Ignore errors
769+
}
770+
}
737771
}

0 commit comments

Comments
 (0)