Skip to content

Commit

Permalink
Refactor to use refactored staging-provider library
Browse files Browse the repository at this point in the history
  • Loading branch information
odinuv committed Sep 16, 2024
1 parent 1d4ee58 commit 5c8f68b
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 199 deletions.
3 changes: 3 additions & 0 deletions Tests/BaseRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
use Keboola\ObjectEncryptor\ObjectEncryptorFactory;
use Keboola\StorageApi\BranchAwareClient;
use Keboola\StorageApi\Client;
use Keboola\StorageApi\Components;
use Keboola\StorageApi\DevBranches;
use Keboola\StorageApi\Options\Components\ListComponentConfigurationsOptions;
use Keboola\StorageApi\Options\Components\ListComponentsOptions;
use Keboola\StorageApiBranch\Branch;
use Keboola\StorageApiBranch\ClientWrapper;
use Monolog\Handler\TestHandler;
Expand Down
2 changes: 1 addition & 1 deletion Tests/Docker/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testImageDigestInvalid()
$image = ImageFactory::getImage(new NullLogger(), $imageConfig, true);
$image->prepare([]);
preg_match('#@sha256:(.*)$#', $image->getImageDigests()[0], $matches);
self::assertNotEmpty($matches);
self::assertCount(2, $matches);
$imageConfig = new Component([
'data' => [
'definition' => [
Expand Down
139 changes: 29 additions & 110 deletions Tests/Runner/DataLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1406,60 +1406,11 @@ public function testWorkspaceCleanupSuccess(): void
$configuration->setConfiguration([]);
$componentsApi = new Components($this->clientWrapper->getBasicClient());
$configId = $componentsApi->addConfiguration($configuration)['id'];
$workspaceData = $componentsApi->createConfigurationWorkspace(
$componentId,
$configId,
['backend' => 'snowflake'],
);
$workspaces = new Workspaces($this->clientWrapper->getBasicClient());
$passwordResetData = $workspaces->resetWorkspacePassword($workspaceData['id']);

$matcher = self::exactly(2);
$clientMock->expects($matcher)
->method('apiPostJson')
->with(self::callback(function (string $url, array $data = []) use ($matcher, $configId, $workspaceData) {
switch ($matcher->getInvocationCount()) {
case 1:
self::assertSame(
sprintf(
'components/keboola.runner-workspace-test/configs/%s/workspaces?async=1',
$configId,
),
$url,
);
self::assertSame([], $data);
break;
case 2:
self::assertSame(
sprintf('workspaces/%s/password', $workspaceData['id']),
$url,
);
self::assertSame(
[],
$data,
);
break;
default:
self::fail('Unexpected call');
}
return true;
}))
->willReturnCallback(function () use ($matcher, $workspaceData, $passwordResetData) {
return match ($matcher->getInvocationCount()) {
1 => $workspaceData,
2 => $passwordResetData,
default => self::fail('Unexpected call')
};
});
$clientMock->expects(self::once())
->method('apiDelete')
->with(sprintf('workspaces/%s?async=1', $workspaceData['id']))
->willReturnCallback(function () use ($workspaceData) {
$this->clientWrapper->getBasicClient()->apiDelete(sprintf(
'workspaces/%s?async=1',
$workspaceData['id'],
));
});

$clientMock->expects(self::never())
->method('apiPostJson');
$clientMock->expects(self::never())
->method('apiDelete');

$clientWrapperMock = $this->createMock(ClientWrapper::class);
$clientWrapperMock->method('getBasicClient')->willReturn($clientMock);
Expand All @@ -1473,15 +1424,17 @@ public function testWorkspaceCleanupSuccess(): void
new JobDefinition([], $component, $configId),
new OutputFilter(10000),
);
// immediately calling cleanWorkspace without using it means it was not initialized
$dataLoader->cleanWorkspace();

$listOptions = new ListConfigurationWorkspacesOptions();
$listOptions->setComponentId($componentId)->setConfigurationId($configId);
$workspaces = $componentsApi->listConfigurationWorkspaces($listOptions);
self::assertCount(0, $workspaces);
$componentsApi->deleteConfiguration($componentId, $configId);
}

public function testWorkspaceCleanupSuccessWhenWorkspaceWasAlreadyInitialized(): void
public function testWorkspaceCleanupWhenInitialized(): void
{
$componentId = 'keboola.runner-workspace-test';
$component = new Component([
Expand All @@ -1507,59 +1460,15 @@ public function testWorkspaceCleanupSuccessWhenWorkspaceWasAlreadyInitialized():
$configuration->setConfiguration([]);
$componentsApi = new Components($this->clientWrapper->getBasicClient());
$configId = $componentsApi->addConfiguration($configuration)['id'];
$workspaceData = $componentsApi->createConfigurationWorkspace(
$componentId,
$configId,
['backend' => 'snowflake'],
);
$workspaces = new Workspaces($this->clientWrapper->getBasicClient());
$passwordResetData = $workspaces->resetWorkspacePassword($workspaceData['id']);

$matcher = self::exactly(2);
$clientMock->expects($matcher)
->method('apiPostJson')
->with(self::callback(function (string $url, array $data = []) use ($matcher, $configId, $workspaceData) {
switch ($matcher->getInvocationCount()) {
case 1:
self::assertSame(
sprintf(
'components/keboola.runner-workspace-test/configs/%s/workspaces?async=1',
$configId,
),
$url,
);
self::assertSame([], $data);
break;
case 2:
self::assertSame(
sprintf('workspaces/%s/password', $workspaceData['id']),
$url,
);
self::assertSame(
[],
$data,
);
break;
default:
self::fail('Unexpected call');
}
return true;
}))
->willReturnCallback(function () use ($matcher, $workspaceData, $passwordResetData) {
return match ($matcher->getInvocationCount()) {
1 => $workspaceData,
2 => $passwordResetData,
default => self::fail('Unexpected call')
};

$clientMock->method('apiPostJson')
->willReturnCallback(function (...$args) {
return $this->clientWrapper->getBasicClient()->apiPostJson(...$args);
});
$clientMock->expects(self::once())
->method('apiDelete')
->with(sprintf('workspaces/%s?async=1', $workspaceData['id']))
->willReturnCallback(function () use ($workspaceData) {
$this->clientWrapper->getBasicClient()->apiDelete(sprintf(
'workspaces/%s?async=1',
$workspaceData['id'],
));
->willReturnCallback(function (...$args) {
return $this->clientWrapper->getBasicClient()->apiDelete(...$args);
});

$clientWrapperMock = $this->createMock(ClientWrapper::class);
Expand All @@ -1574,6 +1483,8 @@ public function testWorkspaceCleanupSuccessWhenWorkspaceWasAlreadyInitialized():
new JobDefinition([], $component, $configId),
new OutputFilter(10000),
);

// this causes the workspaces to initialize
$workspace = $dataLoader->getWorkspaceCredentials();

self::assertArrayHasKey('host', $workspace);
Expand Down Expand Up @@ -1611,11 +1522,15 @@ public function testWorkspaceCleanupFailure(): void
]);
$clientMock = $this->createMock(BranchAwareClient::class);
$clientMock->method('verifyToken')->willReturn($this->clientWrapper->getBasicClient()->verifyToken());

// exception is not thrown outside
$clientMock->expects(self::once())->method('apiPostJson')->willThrowException(
new ClientException('boo'),
);
$clientMock->method('apiPostJson')
->willReturnCallback(function (...$args) {
return $this->clientWrapper->getBasicClient()->apiPostJson(...$args);
});
$clientMock->expects(self::once())
->method('apiDelete')
->willThrowException(
new ClientException('boo'),
);

$clientWrapperMock = $this->createMock(ClientWrapper::class);
$clientWrapperMock->method('getBasicClient')->willReturn($clientMock);
Expand All @@ -1636,6 +1551,10 @@ public function testWorkspaceCleanupFailure(): void
new JobDefinition([], $component, $configId),
new OutputFilter(10000),
);
$credentials = $dataLoader->getWorkspaceCredentials();
self::assertArrayHasKey('host', $credentials);
self::assertArrayHasKey('password', $credentials);

$dataLoader->cleanWorkspace();
self::assertTrue($logger->hasErrorThatContains('Failed to cleanup workspace: boo'));
$componentsApi->deleteConfiguration('keboola.runner-workspace-test', $configId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Keboola\DockerBundle\Docker\JobDefinition;
use Keboola\DockerBundle\Docker\OutputFilter\OutputFilter;
use Keboola\DockerBundle\Docker\Runner\DataLoader\DataLoader;
use Keboola\DockerBundle\Docker\Runner\DataLoader\WorkspaceProviderFactoryFactory;
use Keboola\DockerBundle\Docker\Runner\DataLoader\WorkspaceProviderFactory;
use Keboola\DockerBundle\Docker\Runner\WorkingDirectory;
use Keboola\DockerBundle\Exception\ApplicationException;
use Keboola\DockerBundle\Tests\BaseDataLoaderTest;
Expand Down Expand Up @@ -91,7 +91,7 @@ public function testAbsWorkspaceNoConfig()
$dataLoader->storeOutput();

$credentials = $dataLoader->getWorkspaceCredentials();
self::assertEquals(['connectionString', 'container'], array_keys($credentials));
self::assertEquals(['container', 'connectionString'], array_keys($credentials));
self::assertStringStartsWith('BlobEndpoint=https://', $credentials['connectionString']);
self::assertTrue($logger->hasNoticeThatContains('Created a new ephemeral workspace.'));
$dataLoader->cleanWorkspace();
Expand Down Expand Up @@ -147,7 +147,7 @@ public function testAbsWorkspaceConfigNoWorkspace()
$dataLoader->storeOutput();

$credentials = $dataLoader->getWorkspaceCredentials();
self::assertEquals(['connectionString', 'container'], array_keys($credentials));
self::assertEquals(['container', 'connectionString'], array_keys($credentials));
self::assertStringStartsWith('BlobEndpoint=https://', $credentials['connectionString']);
$workspaces = $componentsApi->listConfigurationWorkspaces(
(new ListConfigurationWorkspacesOptions())
Expand Down Expand Up @@ -224,7 +224,7 @@ public function testAbsWorkspaceConfigOneWorkspace()
$dataLoader->storeOutput();

$credentials = $dataLoader->getWorkspaceCredentials();
self::assertEquals(['connectionString', 'container'], array_keys($credentials));
self::assertEquals(['container', 'connectionString'], array_keys($credentials));
self::assertStringStartsWith('BlobEndpoint=https://', $credentials['connectionString']);
$workspaces = $componentsApi->listConfigurationWorkspaces(
(new ListConfigurationWorkspacesOptions())
Expand Down Expand Up @@ -302,12 +302,12 @@ public function testAbsWorkspaceConfigMultipleWorkspace()
$clientWrapper->method('getBranchClient')->willReturn($client);
$logger = new TestLogger();
try {
$workspaceFactory = new WorkspaceProviderFactoryFactory(
$workspaceFactory = new WorkspaceProviderFactory(
new Components($clientWrapper->getBranchClient()),
new Workspaces($clientWrapper->getBranchClient()),
$logger,
);
$workspaceFactory->getWorkspaceProviderFactory(
$workspaceFactory->getWorkspaceStaging(
'workspace-abs',
$component,
$configurationId,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"keboola/output-mapping": "^24.17",
"keboola/php-utils": "^5.0",
"keboola/sandboxes-api-php-client": "^6.7",
"keboola/staging-provider": "^7.1",
"keboola/staging-provider": "dev-odin-KAB-257 as 7.1.0",
"keboola/storage-api-client": "^v15.0",
"keboola/storage-api-php-client-branch-wrapper": "^6.0",
"monolog/monolog": "^2.5",
Expand Down
Loading

0 comments on commit 5c8f68b

Please sign in to comment.