|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Keboola\Test\Backend\CommonPart1; |
| 6 | + |
| 7 | +use DateTime; |
| 8 | +use DateTimeInterface; |
| 9 | +use Keboola\StorageApi\ClientException; |
| 10 | +use Keboola\StorageApi\Workspaces; |
| 11 | +use Keboola\Test\StorageApiTestCase; |
| 12 | + |
| 13 | +class ScheduledTasksTest extends StorageApiTestCase |
| 14 | +{ |
| 15 | + private const NON_EXISTING_BUCKET_ID = 'in.c-API-tests-nevim-dal'; |
| 16 | + private const EXISTING_BUCKET_NAME = 'test-successful-schedule'; |
| 17 | + |
| 18 | + public function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + |
| 22 | + $this->allowTestForBackendsOnly( |
| 23 | + [self::BACKEND_SNOWFLAKE], |
| 24 | + 'Test implemented only for Snowflake backend.', |
| 25 | + ); |
| 26 | + |
| 27 | + $this->initEmptyTestBucketsForParallelTests(); |
| 28 | + } |
| 29 | + |
| 30 | + public function testFailWithNonExternalBucket(): void |
| 31 | + { |
| 32 | + $bucketId = $this->getTestBucketId(); |
| 33 | + |
| 34 | + $this->expectException(ClientException::class); |
| 35 | + $this->expectExceptionCode(400); |
| 36 | + $this->expectExceptionMessage('Bucket refresh is possible for external buckets only.'); |
| 37 | + |
| 38 | + $this->_client->scheduleBucketRefresh($bucketId, '0 16 2 12 0'); |
| 39 | + } |
| 40 | + |
| 41 | + public function testSuccessfulSchedule(): void |
| 42 | + { |
| 43 | + $this->dropBucketIfExists( |
| 44 | + $this->_client, |
| 45 | + sprintf('%s.%s', self::STAGE_IN, self::EXISTING_BUCKET_NAME), |
| 46 | + true, |
| 47 | + ); |
| 48 | + |
| 49 | + $workspaces = new Workspaces($this->_client); |
| 50 | + $workspace = $workspaces->createWorkspace(); |
| 51 | + |
| 52 | + $bucketId = $this->_client->registerBucket( |
| 53 | + self::EXISTING_BUCKET_NAME, |
| 54 | + [$workspace['connection']['database'], $workspace['connection']['schema']], |
| 55 | + self::STAGE_IN, |
| 56 | + 'Workspace bucket registered as external', |
| 57 | + self::BACKEND_SNOWFLAKE, |
| 58 | + self::EXISTING_BUCKET_NAME, |
| 59 | + ); |
| 60 | + |
| 61 | + $started = new DateTime(); |
| 62 | + |
| 63 | + // Schedule task for non-existing bucket |
| 64 | + $exception = null; |
| 65 | + try { |
| 66 | + $this->_client->scheduleBucketRefresh(self::NON_EXISTING_BUCKET_ID, '* * * * *'); |
| 67 | + $this->fail('Scheduling bucket with invalid cron expression should fail.'); |
| 68 | + } catch (ClientException $e) { |
| 69 | + $exception = $e; |
| 70 | + } |
| 71 | + |
| 72 | + $this->assertInstanceOf(ClientException::class, $exception); |
| 73 | + $this->assertSame( |
| 74 | + sprintf( |
| 75 | + 'The bucket "%s" was not found in the project "%d"', |
| 76 | + self::NON_EXISTING_BUCKET_ID, |
| 77 | + $this->getProjectId($this->_client), |
| 78 | + ), |
| 79 | + $exception->getMessage(), |
| 80 | + ); |
| 81 | + $this->assertSame(404, $exception->getCode()); |
| 82 | + |
| 83 | + // Schedule a task with invalid cron expression |
| 84 | + $exception = null; |
| 85 | + try { |
| 86 | + $this->_client->scheduleBucketRefresh($bucketId, 'každou středu v 13:42'); |
| 87 | + $this->fail('Executing query with invalid cron expression should fail.'); |
| 88 | + } catch (ClientException $e) { |
| 89 | + $exception = $e; |
| 90 | + } |
| 91 | + |
| 92 | + $this->assertInstanceOf(ClientException::class, $exception); |
| 93 | + $this->assertSame( |
| 94 | + "Invalid request:\n - cronExpression.cronExpression: \"Invalid format\"", |
| 95 | + $exception->getMessage(), |
| 96 | + ); |
| 97 | + $this->assertSame(400, $exception->getCode()); |
| 98 | + |
| 99 | + // Schedule task |
| 100 | + $task1 = $this->_client->scheduleBucketRefresh($bucketId, '42 13 * * 3'); |
| 101 | + |
| 102 | + $this->assertSame( |
| 103 | + ['uuid', 'job', 'relatedEntity', 'relatedId', 'cronExpression', 'createdAt'], |
| 104 | + array_keys($task1), |
| 105 | + ); |
| 106 | + |
| 107 | + $this->assertSame($task1['job'], 'bucketRefresh'); |
| 108 | + $this->assertSame($task1['relatedEntity'], 'bucket'); |
| 109 | + $this->assertSame($task1['relatedId'], $bucketId); |
| 110 | + $this->assertSame($task1['cronExpression'], '42 13 * * 3'); |
| 111 | + |
| 112 | + $createdAt = DateTime::createFromFormat(DateTimeInterface::RFC3339, $task1['createdAt']); |
| 113 | + $this->assertTrue($createdAt > $started); |
| 114 | + |
| 115 | + // Schedule another task |
| 116 | + $task2 = $this->_client->scheduleBucketRefresh($bucketId, '0 20 24 12 *'); |
| 117 | + |
| 118 | + // Bucket with scheduled tasks |
| 119 | + $bucketWithTasks = $this->_client->getBucket($bucketId); |
| 120 | + |
| 121 | + $this->assertArrayHasKey('scheduledTasks', $bucketWithTasks); |
| 122 | + |
| 123 | + $this->assertCount(2, $bucketWithTasks['scheduledTasks']); |
| 124 | + |
| 125 | + $this->assertSame( |
| 126 | + ['uuid', 'job', 'relatedEntity', 'relatedId', 'cronExpression', 'createdAt'], |
| 127 | + array_keys($bucketWithTasks['scheduledTasks'][0]), |
| 128 | + ); |
| 129 | + |
| 130 | + // Delete tasks |
| 131 | + $this->_client->deleteScheduledTask($task1['uuid']); |
| 132 | + |
| 133 | + // Tasks were successfully deleted |
| 134 | + $bucketWithoutTasks = $this->_client->getBucket($bucketId); |
| 135 | + |
| 136 | + $this->assertArrayHasKey('scheduledTasks', $bucketWithoutTasks); |
| 137 | + $this->assertCount(1, $bucketWithoutTasks['scheduledTasks']); |
| 138 | + |
| 139 | + // Drop bucket |
| 140 | + // Related scheduled tasks are deleted in cascade, but we're not able to verify it). |
| 141 | + $this->_client->dropBucket($bucketId); |
| 142 | + } |
| 143 | +} |
0 commit comments