|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\MagentoCloud\Test\Functional\Acceptance; |
| 9 | + |
| 10 | +use CliTester; |
| 11 | +use Codeception\Example; |
| 12 | +use Magento\CloudDocker\Test\Functional\Codeception\Docker; |
| 13 | +use Exception; |
| 14 | + |
| 15 | +/** |
| 16 | + * Checks split database functionality |
| 17 | + * |
| 18 | + */ |
| 19 | +abstract class SplitDbCest extends AbstractCest |
| 20 | +{ |
| 21 | + /** |
| 22 | + * {@inheritDoc} |
| 23 | + * @param CliTester $I |
| 24 | + */ |
| 25 | + public function _before(CliTester $I): void |
| 26 | + { |
| 27 | + // Do nothing |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param CliTester $I |
| 32 | + * @param Example $data |
| 33 | + * @throws Exception |
| 34 | + * @dataProvider dataProviderMagentoCloudVersions |
| 35 | + */ |
| 36 | + public function testSplitDb(CliTester $I, Example $data) |
| 37 | + { |
| 38 | + $this->prepareWorkplace($I, $data['version']); |
| 39 | + $I->writeEnvMagentoYaml(['stage' => ['global' => ['SCD_ON_DEMAND' => true]]]); |
| 40 | + |
| 41 | + // Deploy 'Split Db' in an environment without prepared architecture |
| 42 | + $I->generateDockerCompose('--mode=production'); |
| 43 | + |
| 44 | + foreach ($this->variationsDataPartWithoutSplitDbArch() as $variationData) { |
| 45 | + $this->setSplitDbTypesIntoMagentoEnvYaml($I, $variationData['splitDbTypes']); |
| 46 | + $I->runDockerComposeCommand('run build cloud-build'); |
| 47 | + $I->seeInOutput($variationData['messages']); |
| 48 | + $I->stopEnvironment(); |
| 49 | + } |
| 50 | + |
| 51 | + // Deploy 'Split Db' with the unavailable Split Db types |
| 52 | + $this->setSplitDbTypesIntoMagentoEnvYaml($I, ['quote', 'sales']); |
| 53 | + $this->runDeploy($I); |
| 54 | + $I->seeInOutput( |
| 55 | + 'Enabling a split database will be skipped.' |
| 56 | + . ' Relationship do not have configuration for next types: sales, quote' |
| 57 | + ); |
| 58 | + $this->checkEnvPhpConfig($I, [], ['checkout', 'sales']); |
| 59 | + $this->checkMagentoFront($I); |
| 60 | + |
| 61 | + $I->stopEnvironment(true); |
| 62 | + |
| 63 | + // Prepare config for deploy Split Db |
| 64 | + $services = $I->readServicesYaml(); |
| 65 | + $magentoApp = $I->readAppMagentoYaml(); |
| 66 | + $services['mysql-quote']['type'] = 'mysql:10.2'; |
| 67 | + $services['mysql-sales']['type'] = 'mysql:10.2'; |
| 68 | + $magentoApp['relationships']['database-quote'] = 'mysql-quote:mysql'; |
| 69 | + $magentoApp['relationships']['database-sales'] = 'mysql-sales:mysql'; |
| 70 | + $I->writeServicesYaml($services); |
| 71 | + $I->writeAppMagentoYaml($magentoApp); |
| 72 | + |
| 73 | + // Restore app/etc after build phase |
| 74 | + $I->runDockerComposeCommand('run build bash -c "cp -r /app/init/app/etc /app/app"'); |
| 75 | + |
| 76 | + // Deploy 'Split Db' in an environment with prepared architecture. Case with upgrade |
| 77 | + $I->generateDockerCompose('--mode=production'); |
| 78 | + foreach ($this->variationsDataPartWithSplitDbArch() as $variationData) { |
| 79 | + $this->setSplitDbTypesIntoMagentoEnvYaml($I, $variationData['splitDbTypes']); |
| 80 | + $I->startEnvironment(); |
| 81 | + $I->runDockerComposeCommand('run deploy cloud-deploy'); |
| 82 | + $I->seeInOutput($variationData['messages']); |
| 83 | + $this->checkEnvPhpConfig($I, $variationData['expectedExists'], $variationData['expectedNotExist']); |
| 84 | + $this->checkMagentoFront($I); |
| 85 | + $I->stopEnvironment(true); |
| 86 | + } |
| 87 | + |
| 88 | + $I->stopEnvironment(); |
| 89 | + |
| 90 | + // Install with Split db |
| 91 | + $this->setSplitDbTypesIntoMagentoEnvYaml($I, ['quote', 'sales']); |
| 92 | + $this->runDeploy($I); |
| 93 | + $I->seeInOutput([ |
| 94 | + 'INFO: Quote tables were split to DB magento2 in db-quote', |
| 95 | + 'INFO: Running setup upgrade.', |
| 96 | + 'INFO: Sales tables were split to DB magento2 in db-sales', |
| 97 | + 'INFO: Running setup upgrade.', |
| 98 | + ]); |
| 99 | + $this->checkEnvPhpConfig($I, ['checkout', 'sales']); |
| 100 | + $this->checkMagentoFront($I); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @return array |
| 105 | + */ |
| 106 | + abstract protected function dataProviderMagentoCloudVersions(): array; |
| 107 | + |
| 108 | + /** |
| 109 | + * @return array |
| 110 | + */ |
| 111 | + private function variationsDataPartWithoutSplitDbArch(): array |
| 112 | + { |
| 113 | + return [ |
| 114 | + 'Deploy \'Split Db\' with the wrong Split Db type' => [ |
| 115 | + 'messages' => [ |
| 116 | + 'Fix configuration with given suggestions:', |
| 117 | + 'Environment configuration is not valid.', |
| 118 | + 'Correct the following items in your .magento.env.yaml file:', |
| 119 | + 'The SPLIT_DB variable contains an invalid value of type string. Use the following type: array.', |
| 120 | + ], |
| 121 | + 'splitDbTypes' => 'quote', |
| 122 | + ], |
| 123 | + 'Deploy \'Split Db\' with the invalid Split Db label' => [ |
| 124 | + 'messages' => [ |
| 125 | + 'Fix configuration with given suggestions:', |
| 126 | + 'Environment configuration is not valid.', |
| 127 | + 'Correct the following items in your .magento.env.yaml file:', |
| 128 | + 'The SPLIT_DB variable contains the invalid value.', |
| 129 | + 'It should be an array with following values: [quote, sales].' |
| 130 | + ], |
| 131 | + 'splitDbTypes' => ['checkout'], |
| 132 | + ], |
| 133 | + 'Deploy \'Split Db\' with the invalid and valid Split Db labels' => [ |
| 134 | + 'messages' => [ |
| 135 | + 'Fix configuration with given suggestions:', |
| 136 | + 'Environment configuration is not valid.', |
| 137 | + 'Correct the following items in your .magento.env.yaml file:', |
| 138 | + 'The SPLIT_DB variable contains the invalid value.', |
| 139 | + 'It should be an array with following values: [quote, sales].', |
| 140 | + ], |
| 141 | + 'splitDbTypes' => ['quote', 'checkout'], |
| 142 | + ] |
| 143 | + ]; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @return array |
| 148 | + */ |
| 149 | + private function variationsDataPartWithSplitDbArch(): array |
| 150 | + { |
| 151 | + return [ |
| 152 | + 'Run splitting database for `quote` tables' => [ |
| 153 | + 'splitDbTypes' => ['quote'], |
| 154 | + 'messages' => [ |
| 155 | + 'INFO: Quote tables were split to DB magento2 in db-quote', |
| 156 | + 'INFO: Running setup upgrade.', |
| 157 | + ], |
| 158 | + 'expectedExists' => ['checkout'], |
| 159 | + 'expectedNotExist' => ['sales'], |
| 160 | + ], |
| 161 | + 'Split Db type was deleted' => [ |
| 162 | + 'splitDbTypes' => null, |
| 163 | + 'messages' => 'The SPLIT_DB variable is missing the configuration for split connection types: quote', |
| 164 | + 'expectedExists' => ['checkout'], |
| 165 | + 'expectedNotExist' => ['sales'], |
| 166 | + ], |
| 167 | + 'Split Db current type was deleted and new type added' => [ |
| 168 | + 'splitDbTypes' => ['sales'], |
| 169 | + 'messages' => 'The SPLIT_DB variable is missing the configuration for split connection types: quote', |
| 170 | + 'expectedExists' => ['checkout'], |
| 171 | + 'expectedNotExist' => ['sales'], |
| 172 | + ], |
| 173 | + 'Split Db current type was returned' => [ |
| 174 | + 'splitDbTypes' => ['sales', 'quote'], |
| 175 | + 'messages' => [ |
| 176 | + 'INFO: Sales tables were split to DB magento2 in db-sales', |
| 177 | + 'INFO: Running setup upgrade.', |
| 178 | + ], |
| 179 | + 'expectedExists' => ['checkout', 'sales'], |
| 180 | + 'expectedNotExist' => [], |
| 181 | + ] |
| 182 | + ]; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * @param CliTester $I |
| 187 | + */ |
| 188 | + private function checkMagentoFront(CliTester $I) |
| 189 | + { |
| 190 | + $I->runDockerComposeCommand('run deploy cloud-post-deploy'); |
| 191 | + $I->amOnPage('/'); |
| 192 | + $I->see('Home page'); |
| 193 | + $I->see('CMS homepage content goes here.'); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * @param CliTester $I |
| 198 | + * @param array $exists |
| 199 | + * @param array $notExist |
| 200 | + */ |
| 201 | + private function checkEnvPhpConfig(CliTester $I, array $exists = [], array $notExist = []) |
| 202 | + { |
| 203 | + $destination = sys_get_temp_dir() . '/app/etc/env.php'; |
| 204 | + $I->downloadFromContainer('/app/etc/env.php', $destination, Docker::DEPLOY_CONTAINER); |
| 205 | + $config = require $destination; |
| 206 | + |
| 207 | + foreach ($exists as $item) { |
| 208 | + $I->assertArrayHasKey($item, $config['db']['connection']); |
| 209 | + $I->assertArrayHasKey($item, $config['resource']); |
| 210 | + } |
| 211 | + foreach ($notExist as $item) { |
| 212 | + $I->assertArrayNotHasKey($item, $config['db']['connection']); |
| 213 | + $I->assertArrayNotHasKey($item, $config['resource']); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * @param CliTester $I |
| 219 | + */ |
| 220 | + private function runDeploy(CliTester $I) |
| 221 | + { |
| 222 | + $I->startEnvironment(); |
| 223 | + $I->runDockerComposeCommand('run build cloud-build'); |
| 224 | + $I->runDockerComposeCommand('run deploy cloud-deploy'); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * @param CliTester $I |
| 229 | + * @param $splitTypes |
| 230 | + */ |
| 231 | + private function setSplitDbTypesIntoMagentoEnvYaml(CliTester $I, $splitTypes = null) |
| 232 | + { |
| 233 | + $config = $I->readEnvMagentoYaml(); |
| 234 | + if (null !== $splitTypes) { |
| 235 | + $config['stage']['deploy']['SPLIT_DB'] = $splitTypes; |
| 236 | + } else { |
| 237 | + unset($config['stage']['deploy']['SPLIT_DB']); |
| 238 | + } |
| 239 | + $I->writeEnvMagentoYaml($config); |
| 240 | + } |
| 241 | +} |
0 commit comments