Skip to content

Commit 89d0277

Browse files
authored
Merge pull request #1524 from keboola/marek-DMD-166-remove-reader-ws
Update: DMD-166 - Remove readers WS test
2 parents 23fe81d + 538e1f4 commit 89d0277

2 files changed

Lines changed: 95 additions & 101 deletions

File tree

phpunit.xml.dist

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,10 @@
312312
<file>tests/Backend/ExternalBuckets/SnowflakeBucketsRefreshTablesInformationTest.php</file>
313313
<file>tests/Backend/ExternalBuckets/SnowflakeRegisterExternalBucketInSecureDataShareTest.php</file>
314314
</testsuite>
315-
<testsuite name="sync-snowflake-byodb">
316-
<file>tests/Backend/ExternalBuckets/DenyExternalBucketsInInputMappingTest.php</file>
317-
</testsuite>
315+
<testsuite name="sync-snowflake-byodb">
316+
<file>tests/Backend/ExternalBuckets/DenyExternalBucketsInInputMappingTest.php</file>
317+
<directory>tests/Backend/ReaderWorkspaces</directory>
318+
</testsuite>
318319
<testsuite name="snowflake-network-policies">
319320
<directory>tests/Backend/NetworkPolicies</directory>
320321
</testsuite>
Lines changed: 91 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Backend\Snowflake;
3+
namespace Backend\ReaderWorkspaces;
44

55
use Doctrine\DBAL\Exception\DriverException;
66
use Keboola\Csv\CsvFile;
@@ -11,20 +11,21 @@
1111
use Keboola\StorageApi\Workspaces;
1212
use Keboola\Test\Backend\WorkspaceConnectionTrait;
1313
use Keboola\Test\Backend\WorkspaceCredentialsAssertTrait;
14+
use Keboola\Test\Backend\Workspaces\Backend\SnowflakeWorkspaceBackendDBAL;
1415
use Keboola\Test\Backend\Workspaces\Backend\WorkspaceBackendFactory;
15-
use Keboola\Test\Backend\Workspaces\ParallelWorkspacesTestCase;
16+
use Keboola\Test\Backend\Workspaces\WorkspacesTestCase;
1617
use Keboola\Test\Utils\PemKeyCertificateGenerator;
18+
use Keboola\Test\Utils\SnowflakeConnectionUtils;
1719

18-
class WorkspacesReaderTest extends ParallelWorkspacesTestCase
20+
class WorkspacesReaderTest extends WorkspacesTestCase
1921
{
2022
use WorkspaceConnectionTrait;
2123
use WorkspaceCredentialsAssertTrait;
22-
24+
use SnowflakeConnectionUtils;
2325

2426
public function setUp(): void
2527
{
2628
parent::setUp();
27-
$this->initEmptyTestBucketsForParallelTests();
2829

2930
$components = new Components($this->_client);
3031
foreach ($components->listComponents() as $component) {
@@ -43,33 +44,8 @@ public function setUp(): void
4344

4445
public function testLoadToReaderAccount(): void
4546
{
46-
$componentId = 'wr-db';
47-
$configurationId = 'main-1';
48-
$defaultBranchId = $this->getDefaultBranchId($this);
49-
$branchClient = $this->getBranchAwareDefaultClient($defaultBranchId);
50-
// create configuration
51-
$components = new Components($branchClient);
52-
$components->addConfiguration((new Configuration())
53-
->setComponentId('wr-db')
54-
->setConfigurationId('main-1')
55-
->setName('readerWS')
56-
->setDescription('some desc'));
57-
58-
$components = new Components($branchClient);
59-
$workspaces = new Workspaces($branchClient);
60-
61-
$key = (new PemKeyCertificateGenerator())->createPemKeyCertificate(null);
62-
63-
$workspace = $components->createConfigurationWorkspace(
64-
$componentId,
65-
$configurationId,
66-
[
67-
'useCase' => 'reader',
68-
'backend' => 'snowflake',
69-
'loginType' => WorkspaceLoginType::SNOWFLAKE_SERVICE_KEYPAIR,
70-
'publicKey' => $key->getPublicKey(),
71-
],
72-
);
47+
$workspaces = $this->createWorkspaces();
48+
$workspace = $this->prepareWorkspace();
7349

7450
//setup test tables
7551
$tableId = $this->_client->createTableAsync(
@@ -95,51 +71,27 @@ public function testLoadToReaderAccount(): void
9571
],
9672
]);
9773

98-
$workspace['connection']['privateKey'] = $key->getPrivateKey();
99-
10074
// create the connection after LOAD!! because the schema will be created by LOAD
101-
$db = WorkspaceBackendFactory::createWorkspaceBackend($workspace, true);
75+
$db = WorkspaceBackendFactory::createWorkspaceForSnowflakeDbal($workspace);
10276

10377
$data = $db->fetchAll('languages');
10478
$this->assertCount(5, $data);
10579

10680
$data = $db->fetchAll('languages_filtered');
10781
$this->assertCount(1, $data);
82+
83+
// Ensure workspace is removed correctly and reader account is deleted
84+
$this->ensureWorkspaceIsRemoved($workspaces, $db, (int) $workspace['id']);
10885
}
10986

11087
public function testLoadCloneToReaderAccount(): void
11188
{
112-
$componentId = 'wr-db';
113-
$configurationId = 'main-1';
114-
$defaultBranchId = $this->getDefaultBranchId($this);
115-
$branchClient = $this->getBranchAwareDefaultClient($defaultBranchId);
116-
// create configuration
117-
$components = new Components($branchClient);
118-
$components->addConfiguration((new Configuration())
119-
->setComponentId('wr-db')
120-
->setConfigurationId('main-1')
121-
->setName('readerWS_clone')
122-
->setDescription('some desc'));
123-
124-
$components = new Components($branchClient);
125-
$workspaces = new Workspaces($branchClient);
126-
127-
$key = (new PemKeyCertificateGenerator())->createPemKeyCertificate(null);
128-
129-
$workspace = $components->createConfigurationWorkspace(
130-
$componentId,
131-
$configurationId,
132-
[
133-
'useCase' => 'reader',
134-
'backend' => 'snowflake',
135-
'loginType' => WorkspaceLoginType::SNOWFLAKE_SERVICE_KEYPAIR,
136-
'publicKey' => $key->getPublicKey(),
137-
],
138-
);
89+
$workspaces = $this->createWorkspaces();
90+
$workspace = $this->prepareWorkspace();
13991

14092
//setup test tables
14193
$tableId = $this->_client->createTableAsync(
142-
$this->getTestBucketId(self::STAGE_IN),
94+
$this->getTestBucketId(),
14395
'languages',
14496
new CsvFile(__DIR__ . '/../../_data/languages.csv'),
14597
);
@@ -157,53 +109,26 @@ public function testLoadCloneToReaderAccount(): void
157109
],
158110
]);
159111

160-
$workspace['connection']['privateKey'] = $key->getPrivateKey();
161-
162112
// create the connection after LOAD!! because the schema will be created by LOAD
163-
$db = WorkspaceBackendFactory::createWorkspaceBackend($workspace, true);
113+
$db = WorkspaceBackendFactory::createWorkspaceForSnowflakeDbal($workspace);
164114

165115
$data = $db->fetchAll('languages');
166116
$this->assertCount(5, $data);
167117

168118
$data = $db->fetchAll('langs');
169119
$this->assertCount(5, $data);
120+
121+
// Ensure workspace is removed correctly and reader account is deleted
122+
$this->ensureWorkspaceIsRemoved($workspaces, $db, (int) $workspace['id']);
170123
}
171124

172125
public function testResetPublicKeyForReaderWorkspace(): void
173126
{
174-
$this->expectNotToPerformAssertions();
127+
$workspaces = $this->createWorkspaces();
128+
$workspace = $this->prepareWorkspace();
175129

176-
$componentId = 'wr-db';
177-
$configurationId = 'main-1';
178-
$defaultBranchId = $this->getDefaultBranchId($this);
179-
$branchClient = $this->getBranchAwareDefaultClient($defaultBranchId);
180-
// create configuration
181-
$components = new Components($branchClient);
182-
$components->addConfiguration((new Configuration())
183-
->setComponentId('wr-db')
184-
->setConfigurationId('main-1')
185-
->setName('readerWS')
186-
->setDescription('some desc'));
187-
188-
$components = new Components($branchClient);
189-
$workspaces = new Workspaces($branchClient);
190-
191-
$key = (new PemKeyCertificateGenerator())->createPemKeyCertificate(null);
192-
193-
$workspace = $components->createConfigurationWorkspace(
194-
$componentId,
195-
$configurationId,
196-
[
197-
'useCase' => 'reader',
198-
'backend' => 'snowflake',
199-
'loginType' => WorkspaceLoginType::SNOWFLAKE_SERVICE_KEYPAIR,
200-
'publicKey' => $key->getPublicKey(),
201-
],
202-
);
203-
204-
//setup test tables
205130
$tableId = $this->_client->createTableAsync(
206-
$this->getTestBucketId(self::STAGE_IN),
131+
$this->getTestBucketId(),
207132
'languages',
208133
new CsvFile(__DIR__ . '/../../_data/languages.csv'),
209134
);
@@ -222,7 +147,6 @@ public function testResetPublicKeyForReaderWorkspace(): void
222147
]);
223148

224149
// create the connection after LOAD!! because the schema will be created by LOAD
225-
$workspace['connection']['privateKey'] = $key->getPrivateKey();
226150
$connection = WorkspaceBackendFactory::createWorkspaceForSnowflakeDbal($workspace);
227151
$connection->executeQuery('SELECT 1;');
228152

@@ -246,5 +170,74 @@ public function testResetPublicKeyForReaderWorkspace(): void
246170
// New should be working always
247171
$newConnection->getDb()->close();
248172
$newConnection->getDb()->executeQuery('SELECT 1;');
173+
174+
// Ensure workspace is removed correctly and reader account is deleted
175+
$this->ensureWorkspaceIsRemoved($workspaces, $newConnection, (int) $workspace['id']);
176+
}
177+
178+
private function ensureWorkspaceIsRemoved(Workspaces $workspaces, SnowflakeWorkspaceBackendDBAL $connection, int $workspaceId): void
179+
{
180+
$backendConnection = $this->ensureSnowflakeConnection();
181+
$verifiedToken = $this->_client->verifyToken();
182+
$organizationId = $verifiedToken['organization']['id'];
183+
$result = $backendConnection->fetchAllAssociative("SHOW MANAGED ACCOUNTS LIKE '%_READER_ACCOUNT_{$organizationId}';");
184+
185+
self::assertCount(1, $result);
186+
187+
$workspaces->deleteWorkspace($workspaceId);
188+
189+
// Cannot execute query on removed workspace
190+
try {
191+
$connection->executeQuery('SELECT 1;');
192+
$this->fail('Removed workspace should not be accessible');
193+
} catch (DriverException $driverException) {
194+
self::assertStringContainsString('Session no longer exists', $driverException->getMessage());
195+
}
196+
197+
$result = $backendConnection->fetchAllAssociative("SHOW MANAGED ACCOUNTS LIKE '%_READER_ACCOUNT_{$organizationId}';");
198+
199+
self::assertCount(0, $result);
200+
}
201+
202+
private function prepareWorkspace(): array
203+
{
204+
$componentId = 'wr-db';
205+
$configurationId = 'main-1';
206+
$defaultBranchId = $this->getDefaultBranchId($this);
207+
$branchClient = $this->getBranchAwareDefaultClient($defaultBranchId);
208+
// create configuration
209+
$components = new Components($branchClient);
210+
$components->addConfiguration((new Configuration())
211+
->setComponentId('wr-db')
212+
->setConfigurationId('main-1')
213+
->setName('readerWS')
214+
->setDescription('some desc'));
215+
216+
$components = new Components($branchClient);
217+
218+
$key = (new PemKeyCertificateGenerator())->createPemKeyCertificate(null);
219+
220+
$workspace = $components->createConfigurationWorkspace(
221+
$componentId,
222+
$configurationId,
223+
[
224+
'useCase' => 'reader',
225+
'backend' => 'snowflake',
226+
'loginType' => WorkspaceLoginType::SNOWFLAKE_SERVICE_KEYPAIR,
227+
'publicKey' => $key->getPublicKey(),
228+
],
229+
);
230+
231+
$workspace['connection']['privateKey'] = $key->getPrivateKey();
232+
233+
return $workspace;
234+
}
235+
236+
private function createWorkspaces(): Workspaces
237+
{
238+
$defaultBranchId = $this->getDefaultBranchId($this);
239+
$branchClient = $this->getBranchAwareDefaultClient($defaultBranchId);
240+
241+
return new Workspaces($branchClient);
249242
}
250243
}

0 commit comments

Comments
 (0)