From d6865e22c1a79852d6275b91f2c307d010139352 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Mon, 10 Sep 2018 14:36:04 +0200 Subject: [PATCH 1/5] Split provisioners. --- Provisioner/AbstractSockProvisioner.php | 29 ++ Provisioner/BuildProvisioner.php | 394 --------------- Provisioner/BuildSockAccountProvisioner.php | 197 ++++++++ .../BuildSockApplicationProvisioner.php | 174 +++++++ Provisioner/BuildSockDatabaseProvisioner.php | 217 ++++++++ Provisioner/DestroyProvisioner.php | 214 -------- Provisioner/DestroySockAccountProvisioner.php | 133 +++++ .../DestroySockApplicationProvisioner.php | 125 +++++ .../DestroySockDatabaseProvisioner.php | 129 +++++ Provisioner/PollingProvisioner.php | 88 ++++ Resources/config/services.yml | 21 +- Service/SockPollerService.php | 67 +++ Tests/Provisioner/AbstractProvisionerTest.php | 4 +- .../BuildSockAccountProvisionerTest.php | 469 ++++++++++++++++++ .../BuildSockApplicationProvisionerTest.php | 299 +++++++++++ ...p => BuildSockDatabaseProvisionerTest.php} | 391 ++------------- ... => DestroySockAccountProvisionerTest.php} | 30 +- .../DestroySockApplicationProvisionerTest.php | 210 ++++++++ .../DestroySockDatabaseProvisionerTest.php | 210 ++++++++ Tests/Provisioner/PollingProvisionerTest.php | 156 ++++++ 20 files changed, 2591 insertions(+), 966 deletions(-) create mode 100644 Provisioner/AbstractSockProvisioner.php delete mode 100644 Provisioner/BuildProvisioner.php create mode 100644 Provisioner/BuildSockAccountProvisioner.php create mode 100644 Provisioner/BuildSockApplicationProvisioner.php create mode 100644 Provisioner/BuildSockDatabaseProvisioner.php delete mode 100644 Provisioner/DestroyProvisioner.php create mode 100644 Provisioner/DestroySockAccountProvisioner.php create mode 100644 Provisioner/DestroySockApplicationProvisioner.php create mode 100644 Provisioner/DestroySockDatabaseProvisioner.php create mode 100644 Provisioner/PollingProvisioner.php create mode 100644 Service/SockPollerService.php create mode 100644 Tests/Provisioner/BuildSockAccountProvisionerTest.php create mode 100644 Tests/Provisioner/BuildSockApplicationProvisionerTest.php rename Tests/Provisioner/{BuildProvisionerTest.php => BuildSockDatabaseProvisionerTest.php} (55%) rename Tests/Provisioner/{DestroyProvisionerTest.php => DestroySockAccountProvisionerTest.php} (90%) create mode 100644 Tests/Provisioner/DestroySockApplicationProvisionerTest.php create mode 100644 Tests/Provisioner/DestroySockDatabaseProvisionerTest.php create mode 100644 Tests/Provisioner/PollingProvisionerTest.php diff --git a/Provisioner/AbstractSockProvisioner.php b/Provisioner/AbstractSockProvisioner.php new file mode 100644 index 0000000..ec7a53c --- /dev/null +++ b/Provisioner/AbstractSockProvisioner.php @@ -0,0 +1,29 @@ +task->getProvisioners(); + if ($provisioners && !in_array(PollingProvisioner::class, $provisioners)) { + $provisioners[] = PollingProvisioner::class; + $this->task->setProvisioners($provisioners); + } + } +} diff --git a/Provisioner/BuildProvisioner.php b/Provisioner/BuildProvisioner.php deleted file mode 100644 index 79680b5..0000000 --- a/Provisioner/BuildProvisioner.php +++ /dev/null @@ -1,394 +0,0 @@ -dataValueService = $dataValueService; - $this->taskLoggerService = $taskLoggerService; - $this->apiService = $apiService; - $this->entityManager = $entityManager; - } - - public function doRun() - { - $applicationEnvironment = $this->task->getApplicationEnvironment(); - $environment = $applicationEnvironment->getEnvironment(); - - /** @var VirtualServer[] $servers */ - $servers = $this->entityManager->getRepository(VirtualServer::class)->findAll(); - - foreach ($servers as $server) { - if ($server->getEnvironment() != $environment) { - continue; - } - - if (!$this->dataValueService->getValue($server, 'manage_sock')) { - continue; - } - - $this->taskLoggerService->addLogHeader( - $this->task, - sprintf('Sock server "%s"', $server->getName()) - ); - - try { - $polling = []; - $polling['accounts'] = $this->createSockAccount($applicationEnvironment, $server); - $polling['applications'] = $this->createSockApplication($applicationEnvironment); - $polling['databases'] = $this->createSockDatabase($applicationEnvironment, $server); - } catch (\Exception $ex) { - $this->taskLoggerService->addFailedLogMessage($this->task, 'Provisioning failed.'); - throw new LoggedException('', 0, $ex); - } - try { - $this->doPolling(array_filter($polling)); - } catch (\Exception $ex) { - $this->taskLoggerService - ->addErrorLogMessage($this->task, $ex->getMessage(), 2) - ->addFailedLogMessage($this->task, 'Provisioning failed.'); - throw new LoggedException('', 0, $ex); - } - - $this->taskLoggerService->addSuccessLogMessage($this->task, 'Provisioning succeeded.'); - } - } - - /** - * @param ApplicationEnvironment $applicationEnvironment - * @param Server $server - * - * @return int - * The sock account id. - */ - protected function createSockAccount(ApplicationEnvironment $applicationEnvironment, VirtualServer $server) - { - $this->taskLoggerService->addLogHeader($this->task, 'Provisioning account', 1); - - try { - $application = $applicationEnvironment->getApplication(); - $parentApplication = $this->dataValueService->getValue($application, 'parent_application'); - $sockServerId = $this->dataValueService->getValue($server, 'sock_server_id'); - - // Check if the server exists. - $this->apiService->getVirtualServer($sockServerId); - - if ($parentApplication) { - $environment = $applicationEnvironment->getEnvironment(); - $parentApplicationEnvironment = $this->entityManager - ->getRepository(ApplicationEnvironment::class) - ->findOneBy(['application' => $parentApplication, 'environment' => $environment]); - - $sockAccountId = $this->dataValueService->getValue($parentApplicationEnvironment, 'sock_account_id'); - $username = $this->dataValueService->getValue($parentApplicationEnvironment, 'sock_ssh_user'); - - if (!$sockAccountId || !$username) { - throw new \Exception('The parent application must be build first.'); - } - - $this->dataValueService->storeValue($applicationEnvironment, 'sock_account_id', $sockAccountId); - $this->dataValueService->storeValue($applicationEnvironment, 'sock_ssh_user', $username); - - $this->taskLoggerService->addInfoLogMessage( - $this->task, - sprintf('Use parent account "%s".', $parentApplication->getName()), - 2 - ); - - return; - } - - $username = $application->getNameCanonical(); - - $this->taskLoggerService->addInfoLogMessage( - $this->task, - sprintf('Check if account "%s" exists', $username), - 2 - ); - - $account = $this->apiService->findAccountByName($username, $sockServerId); - $sshKeyIds = $this->dataValueService->getValue($applicationEnvironment, 'sock_ssh_key'); - - if ($account) { - $this->taskLoggerService->addInfoLogMessage( - $this->task, - sprintf('Found account %s.', $account['id']), - 2 - ); - } else { - $this->taskLoggerService->addInfoLogMessage( - $this->task, - 'No account found.', - 2 - ); - - $account = $this->apiService->createAccount($username, $sockServerId, $sshKeyIds); - - $this->taskLoggerService->addInfoLogMessage( - $this->task, - sprintf('Account %s created.', $account['id']), - 2 - ); - } - - $this->dataValueService->storeValue($applicationEnvironment, 'sock_account_id', $account['id']); - $this->dataValueService->storeValue($applicationEnvironment, 'sock_ssh_user', $username); - - return $account['id']; - } catch (\Exception $ex) { - $this->taskLoggerService - ->addErrorLogMessage($this->task, $ex->getMessage(), 2) - ->addFailedLogMessage($this->task, 'Provisioning account failed.', 2); - - throw $ex; - } - } - - /** - * @param ApplicationEnvironment $applicationEnvironment - * - * @return int - * The sock application id. - */ - protected function createSockApplication(ApplicationEnvironment $applicationEnvironment) - { - $this->taskLoggerService->addLogHeader($this->task, 'Provisioning application', 1); - - try { - $application = $applicationEnvironment->getApplication(); - $applicationName = $application->getNameCanonical(); - $technology = $this->dataValueService->getValue($application, 'sock_application_technology'); - $sockAccountId = $this->dataValueService->getValue($applicationEnvironment, 'sock_account_id'); - - // Check if the account exists. - $this->apiService->getAccount($sockAccountId); - - $this->taskLoggerService->addInfoLogMessage( - $this->task, - sprintf('Check if application "%s" exists.', $applicationName), - 2 - ); - - $application = $this->apiService->findApplicationByName($applicationName, $sockAccountId); - - if ($application) { - $this->taskLoggerService->addInfoLogMessage( - $this->task, - sprintf('Found application %s.', $application['id']), - 2 - ); - } else { - $this->taskLoggerService->addInfoLogMessage( - $this->task, - 'No application found.', - 2 - ); - - $application = $this->apiService->createApplication( - $sockAccountId, - $applicationName, - [$applicationEnvironment->getDomain()], - 'current', - $technology ? $technology : 'php-fpm' - ); - - $this->taskLoggerService->addInfoLogMessage( - $this->task, - sprintf('Application %s created.', $application['id']), - 2 - ); - } - - $this->dataValueService->storeValue($applicationEnvironment, 'sock_application_id', $application['id']); - - return $application['id']; - } catch (\Exception $ex) { - $this->taskLoggerService - ->addErrorLogMessage($this->task, $ex->getMessage(), 2) - ->addFailedLogMessage($this->task, 'Provisioning application failed.', 2); - - throw $ex; - } - } - - /** - * @param ApplicationEnvironment $applicationEnvironment - * - * @return int|null - * The sock database id, null if no database is required. - */ - protected function createSockDatabase(ApplicationEnvironment $applicationEnvironment) - { - $this->taskLoggerService->addLogHeader($this->task, 'Provisioning database', 1); - - if (!$applicationEnvironment->getApplication()->isHasDatabase()) { - $this->taskLoggerService->addInfoLogMessage($this->task, 'No database required.', 2); - return; - } - - try { - $application = $applicationEnvironment->getApplication(); - $environment = $applicationEnvironment->getEnvironment(); - $sockAccountId = $this->dataValueService->getValue($applicationEnvironment, 'sock_account_id'); - - // Check if the account exists - $this->apiService->getAccount($sockAccountId); - - $saveDatabase = false; - - if (!$databaseName = $applicationEnvironment->getDatabaseName()) { - $databaseName = $application->getNameCanonical() . '_' . substr($environment->getName(), 0, 1); - $saveDatabase = true; - } - - if (!$databaseUser = $applicationEnvironment->getDatabaseUser()) { - $databaseUser = $databaseName; - $saveDatabase = true; - } elseif (strlen($databaseUser) > 16) { - $databaseUser = substr($databaseUser, 0, 16); - $saveDatabase = true; - } - - if (!$databasePassword = $applicationEnvironment->getDatabasePassword()) { - $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?"; - $databasePassword = substr(str_shuffle($chars), 0, 15); - $saveDatabase = true; - } - - $this->taskLoggerService->addInfoLogMessage( - $this->task, - sprintf('Check if database "%s" exists', $databaseName), - 2 - ); - - $database = $this->apiService->findDatabaseByName($databaseName, $sockAccountId); - - if ($database) { - $this->taskLoggerService->addInfoLogMessage( - $this->task, - sprintf('Found database %s.', $database['id']), - 2 - ); - } else { - $this->taskLoggerService->addInfoLogMessage( - $this->task, - 'No database found.', - 2 - ); - - $database = $this->apiService->createDatabase( - $sockAccountId, - $databaseName, - $databaseUser, - $databasePassword - ); - - $this->taskLoggerService->addInfoLogMessage( - $this->task, - sprintf('Database %s created.', $database['id']), - 2 - ); - } - - $login = $database['database_grants'][0]['login']; - - $this->taskLoggerService->addInfoLogMessage( - $this->task, - 'Update access grants.', - 2 - ); - - $this->apiService->removeDatabaseLogin($database['id'], $login); - $this->apiService->addDatabaseLogin($database['id'], $databaseUser, $databasePassword); - - $this->dataValueService->storeValue($applicationEnvironment, 'sock_database_id', $database['id']); - - if ($saveDatabase) { - $applicationEnvironment->setDatabaseUser($databaseUser); - $applicationEnvironment->setDatabaseName($databaseName); - $applicationEnvironment->setDatabasePassword($databasePassword); - - $this->entityManager->persist($applicationEnvironment); - $this->entityManager->flush(); - } - return $database['id']; - } catch (\Exception $ex) { - $this->taskLoggerService - ->addErrorLogMessage($this->task, $ex->getMessage(), 2) - ->addFailedLogMessage($this->task, 'Provisioning database failed.', 2); - - throw $ex; - } - } - - private function doPolling(array $polling) - { - $this->taskLoggerService->addInfoLogMessage( - $this->task, - 'Waiting for changes to be applied.', - 2 - ); - - $start = time(); - - do { - $events = false; - foreach ($polling as $type => $sockId) { - if ($events = $this->apiService->getEvents($type, $sockId)) { - break; - } - } - - if (!$events) { - break; - } - - if ((time() - $start) >= 600) { - throw new \Exception( - sprintf( - 'Timeout, waited more then 10 minutes while polling for %s #%s.', - $type, - $sockId - ) - ); - } - - sleep(5); - } while (true); - } - - public function getName() - { - return 'Sock accounts, applications and databases'; - } -} diff --git a/Provisioner/BuildSockAccountProvisioner.php b/Provisioner/BuildSockAccountProvisioner.php new file mode 100644 index 0000000..bdc4645 --- /dev/null +++ b/Provisioner/BuildSockAccountProvisioner.php @@ -0,0 +1,197 @@ +dataValueService = $dataValueService; + $this->taskLoggerService = $taskLoggerService; + $this->apiService = $apiService; + $this->entityManager = $entityManager; + $this->sockPoller = $sockPoller; + } + + public function doRun() + { + $appEnv = $this->task->getApplicationEnvironment(); + $environment = $appEnv->getEnvironment(); + + /** @var VirtualServer[] $servers */ + $servers = $this->entityManager->getRepository(VirtualServer::class)->findAll(); + + foreach ($servers as $server) { + if ($server->getEnvironment() != $environment) { + continue; + } + + if (!$this->dataValueService->getValue($server, 'manage_sock')) { + continue; + } + + $this->taskLoggerService->addLogHeader( + $this->task, + sprintf('Sock server "%s"', $server->getName()) + ); + + try { + $accountId = $this->createSockAccount($appEnv, $server); + if ($accountId) { + $this->sockPoller->addPolling(SockPollerService::POLLING_TYPE_ACCOUNT, $accountId, $this->task); + $this->ensurePollingProvisioner(); + } + } catch (\Exception $ex) { + $this->taskLoggerService->addFailedLogMessage($this->task, 'Provisioning sock account failed.'); + throw new LoggedException('', 0, $ex); + } + + $this->taskLoggerService->addSuccessLogMessage($this->task, 'Provisioning sock account queued.'); + } + } + + /** + * @param ApplicationEnvironment $appEnv + * @param Server $server + * + * @return int + * The sock account id. + */ + protected function createSockAccount(ApplicationEnvironment $appEnv, VirtualServer $server) + { + $this->taskLoggerService->addLogHeader($this->task, 'Provisioning account', 1); + + try { + $application = $appEnv->getApplication(); + $parentApplication = $this->dataValueService->getValue($application, 'parent_application'); + $sockServerId = $this->dataValueService->getValue($server, 'sock_server_id'); + + // Check if the server exists. + $this->apiService->getVirtualServer($sockServerId); + + if ($parentApplication) { + $environment = $appEnv->getEnvironment(); + $parentApplicationEnvironment = $this->entityManager + ->getRepository(ApplicationEnvironment::class) + ->findOneBy(['application' => $parentApplication, 'environment' => $environment]); + + $sockAccountId = $this->dataValueService->getValue($parentApplicationEnvironment, 'sock_account_id'); + $username = $this->dataValueService->getValue($parentApplicationEnvironment, 'sock_ssh_user'); + + if (!$sockAccountId || !$username) { + throw new \Exception('The parent application must be build first.'); + } + + $this->dataValueService->storeValue($appEnv, 'sock_account_id', $sockAccountId); + $this->dataValueService->storeValue($appEnv, 'sock_ssh_user', $username); + + $this->taskLoggerService->addInfoLogMessage( + $this->task, + sprintf('Use parent account "%s".', $parentApplication->getName()), + 2 + ); + + return; + } + + $username = $application->getNameCanonical(); + + $this->taskLoggerService->addInfoLogMessage( + $this->task, + sprintf('Check if account "%s" exists', $username), + 2 + ); + + $account = $this->apiService->findAccountByName($username, $sockServerId); + $sshKeyIds = $this->dataValueService->getValue($appEnv, 'sock_ssh_key'); + + if ($account) { + $this->taskLoggerService->addInfoLogMessage( + $this->task, + sprintf('Found account %s.', $account['id']), + 2 + ); + } else { + $this->taskLoggerService->addInfoLogMessage( + $this->task, + 'No account found.', + 2 + ); + + $account = $this->apiService->createAccount($username, $sockServerId, $sshKeyIds); + + $this->taskLoggerService->addInfoLogMessage( + $this->task, + sprintf('Account %s created.', $account['id']), + 2 + ); + } + + $this->dataValueService->storeValue($appEnv, 'sock_account_id', $account['id']); + $this->dataValueService->storeValue($appEnv, 'sock_ssh_user', $username); + + return $account['id']; + } catch (\Exception $ex) { + $this->taskLoggerService + ->addErrorLogMessage($this->task, $ex->getMessage(), 2) + ->addFailedLogMessage($this->task, 'Provisioning account failed.', 2); + + throw $ex; + } + } + + public function getName() + { + return 'Sock account'; + } +} diff --git a/Provisioner/BuildSockApplicationProvisioner.php b/Provisioner/BuildSockApplicationProvisioner.php new file mode 100644 index 0000000..708a66d --- /dev/null +++ b/Provisioner/BuildSockApplicationProvisioner.php @@ -0,0 +1,174 @@ +dataValueService = $dataValueService; + $this->taskLoggerService = $taskLoggerService; + $this->apiService = $apiService; + $this->entityManager = $entityManager; + $this->sockPoller = $sockPoller; + } + + public function doRun() + { + $appEnv = $this->task->getApplicationEnvironment(); + $environment = $appEnv->getEnvironment(); + + /** @var VirtualServer[] $servers */ + $servers = $this->entityManager->getRepository(VirtualServer::class)->findAll(); + + foreach ($servers as $server) { + if ($server->getEnvironment() != $environment) { + continue; + } + + if (!$this->dataValueService->getValue($server, 'manage_sock')) { + continue; + } + + $this->taskLoggerService->addLogHeader( + $this->task, + sprintf('Sock server "%s"', $server->getName()) + ); + + try { + $appId = $this->createSockApplication($appEnv); + if ($appId) { + $this->sockPoller->addPolling(SockPollerService::POLLING_TYPE_APPLICATION, $appId, $this->task); + $this->ensurePollingProvisioner(); + } + } catch (\Exception $ex) { + $this->taskLoggerService->addFailedLogMessage($this->task, 'Provisioning sock application failed.'); + throw new LoggedException('', 0, $ex); + } + + $this->taskLoggerService->addSuccessLogMessage($this->task, 'Provisioning sock application queued.'); + } + } + + /** + * @param ApplicationEnvironment $appEnv + * + * @return int + * The sock application id. + */ + protected function createSockApplication(ApplicationEnvironment $appEnv) + { + $this->taskLoggerService->addLogHeader($this->task, 'Provisioning application', 1); + + try { + $application = $appEnv->getApplication(); + $applicationName = $application->getNameCanonical(); + $technology = $this->dataValueService->getValue($application, 'sock_application_technology'); + $sockAccountId = $this->dataValueService->getValue($appEnv, 'sock_account_id'); + + // Check if the account exists. + $this->apiService->getAccount($sockAccountId); + + $this->taskLoggerService->addInfoLogMessage( + $this->task, + sprintf('Check if application "%s" exists.', $applicationName), + 2 + ); + + $application = $this->apiService->findApplicationByName($applicationName, $sockAccountId); + + if ($application) { + $this->taskLoggerService->addInfoLogMessage( + $this->task, + sprintf('Found application %s.', $application['id']), + 2 + ); + } else { + $this->taskLoggerService->addInfoLogMessage( + $this->task, + 'No application found.', + 2 + ); + + $application = $this->apiService->createApplication( + $sockAccountId, + $applicationName, + [$appEnv->getDomain()], + 'current', + $technology ? $technology : 'php-fpm' + ); + + $this->taskLoggerService->addInfoLogMessage( + $this->task, + sprintf('Application %s created.', $application['id']), + 2 + ); + } + + $this->dataValueService->storeValue($appEnv, 'sock_application_id', $application['id']); + + return $application['id']; + } catch (\Exception $ex) { + $this->taskLoggerService + ->addErrorLogMessage($this->task, $ex->getMessage(), 2) + ->addFailedLogMessage($this->task, 'Provisioning application failed.', 2); + + throw $ex; + } + } + + public function getName() + { + return 'Sock application'; + } +} diff --git a/Provisioner/BuildSockDatabaseProvisioner.php b/Provisioner/BuildSockDatabaseProvisioner.php new file mode 100644 index 0000000..b917620 --- /dev/null +++ b/Provisioner/BuildSockDatabaseProvisioner.php @@ -0,0 +1,217 @@ +dataValueService = $dataValueService; + $this->taskLoggerService = $taskLoggerService; + $this->apiService = $apiService; + $this->entityManager = $entityManager; + $this->sockPoller = $sockPoller; + } + + public function doRun() + { + $appEnv = $this->task->getApplicationEnvironment(); + $environment = $appEnv->getEnvironment(); + + /** @var VirtualServer[] $servers */ + $servers = $this->entityManager->getRepository(VirtualServer::class)->findAll(); + + foreach ($servers as $server) { + if ($server->getEnvironment() != $environment) { + continue; + } + + if (!$this->dataValueService->getValue($server, 'manage_sock')) { + continue; + } + + $this->taskLoggerService->addLogHeader( + $this->task, + sprintf('Sock server "%s"', $server->getName()) + ); + + try { + $dbId = $this->createSockDatabase($appEnv); + if ($dbId) { + $this->sockPoller->addPolling(SockPollerService::POLLING_TYPE_DATABASE, $dbId, $this->task); + $this->ensurePollingProvisioner(); + } + } catch (\Exception $ex) { + $this->taskLoggerService->addFailedLogMessage($this->task, 'Provisioning sock database failed.'); + throw new LoggedException('', 0, $ex); + } + + $this->taskLoggerService->addSuccessLogMessage($this->task, 'Provisioning sock database queued.'); + } + } + + /** + * @param ApplicationEnvironment $appEnv + * + * @return int|null + * The sock database id, null if no database is required. + */ + protected function createSockDatabase(ApplicationEnvironment $appEnv) + { + $this->taskLoggerService->addLogHeader($this->task, 'Provisioning database', 1); + + if (!$appEnv->getApplication()->isHasDatabase()) { + $this->taskLoggerService->addInfoLogMessage($this->task, 'No database required.', 2); + return; + } + + try { + $application = $appEnv->getApplication(); + $environment = $appEnv->getEnvironment(); + $sockAccountId = $this->dataValueService->getValue($appEnv, 'sock_account_id'); + + // Check if the account exists + $this->apiService->getAccount($sockAccountId); + + $saveDatabase = false; + + if (!$databaseName = $appEnv->getDatabaseName()) { + $databaseName = $application->getNameCanonical() . '_' . substr($environment->getName(), 0, 1); + $saveDatabase = true; + } + + if (!$databaseUser = $appEnv->getDatabaseUser()) { + $databaseUser = $databaseName; + $saveDatabase = true; + } elseif (strlen($databaseUser) > 16) { + $databaseUser = substr($databaseUser, 0, 16); + $saveDatabase = true; + } + + if (!$databasePassword = $appEnv->getDatabasePassword()) { + $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?"; + $databasePassword = substr(str_shuffle($chars), 0, 15); + $saveDatabase = true; + } + + $this->taskLoggerService->addInfoLogMessage( + $this->task, + sprintf('Check if database "%s" exists', $databaseName), + 2 + ); + + $database = $this->apiService->findDatabaseByName($databaseName, $sockAccountId); + + if ($database) { + $this->taskLoggerService->addInfoLogMessage( + $this->task, + sprintf('Found database %s.', $database['id']), + 2 + ); + } else { + $this->taskLoggerService->addInfoLogMessage( + $this->task, + 'No database found.', + 2 + ); + + $database = $this->apiService->createDatabase( + $sockAccountId, + $databaseName, + $databaseUser, + $databasePassword + ); + + $this->taskLoggerService->addInfoLogMessage( + $this->task, + sprintf('Database %s created.', $database['id']), + 2 + ); + } + + $login = $database['database_grants'][0]['login']; + + $this->taskLoggerService->addInfoLogMessage( + $this->task, + 'Update access grants.', + 2 + ); + + $this->apiService->removeDatabaseLogin($database['id'], $login); + $this->apiService->addDatabaseLogin($database['id'], $databaseUser, $databasePassword); + + $this->dataValueService->storeValue($appEnv, 'sock_database_id', $database['id']); + + if ($saveDatabase) { + $appEnv->setDatabaseUser($databaseUser); + $appEnv->setDatabaseName($databaseName); + $appEnv->setDatabasePassword($databasePassword); + + $this->entityManager->persist($appEnv); + $this->entityManager->flush(); + } + return $database['id']; + } catch (\Exception $ex) { + $this->taskLoggerService + ->addErrorLogMessage($this->task, $ex->getMessage(), 2) + ->addFailedLogMessage($this->task, 'Provisioning database failed.', 2); + + throw $ex; + } + } + + public function getName() + { + return 'Sock database'; + } +} diff --git a/Provisioner/DestroyProvisioner.php b/Provisioner/DestroyProvisioner.php deleted file mode 100644 index 33aca0e..0000000 --- a/Provisioner/DestroyProvisioner.php +++ /dev/null @@ -1,214 +0,0 @@ -dataValueService = $dataValueService; - $this->taskLoggerService = $taskLoggerService; - $this->apiService = $apiService; - $this->entityManager = $entityManager; - } - - public function doRun() - { - $applicationEnvironment = $this->task->getApplicationEnvironment(); - $environment = $applicationEnvironment->getEnvironment(); - - /** @var VirtualServer[] $servers */ - $servers = $this->entityManager->getRepository(VirtualServer::class)->findAll(); - - foreach ($servers as $server) { - if ($server->getEnvironment() != $environment) { - continue; - } - - if (!$this->dataValueService->getValue($server, 'manage_sock')) { - continue; - } - - $this->taskLoggerService->addLogHeader( - $this->task, - sprintf('Sock server "%s"', $server->getName()) - ); - - try { - $this->destroySockDatabase($applicationEnvironment); - $this->destroySockApplication($applicationEnvironment); - $this->destroySockAccount($applicationEnvironment); - - try { - $this->entityManager->persist($applicationEnvironment); - $this->entityManager->flush(); - } catch (\Exception $ex) { - $this->taskLoggerService->addWarningLogMessage($this->task, 'Could not remove local Sock data.'); - } - - $this->taskLoggerService->addSuccessLogMessage($this->task, 'Cleanup succeeded.'); - } catch (\Exception $ex) { - $this->taskLoggerService->addFailedLogMessage($this->task, 'Cleanup failed.'); - throw new LoggedException('', 0, $ex); - } - } - } - - /** - * Destroy a sock database for a specific application environment. - * - * @param ApplicationEnvironment $applicationEnvironment - * The application environment to destroy the database for. - * - * @throws ClientException - * When something goes wrong while destroying the database. - */ - protected function destroySockDatabase(ApplicationEnvironment $applicationEnvironment) - { - $this->taskLoggerService->addLogHeader($this->task, 'Removing database', 1); - - if (!$databaseId = $this->dataValueService->getValue($applicationEnvironment, 'sock_database_id')) { - $this->taskLoggerService->addInfoLogMessage($this->task, 'No database to remove.', 2); - return; - } - - try { - $this->apiService->removeDatabase($databaseId); - - $this->dataValueService->storeValue($applicationEnvironment, 'sock_database_id', null); - - $applicationEnvironment->setDatabaseUser(null); - $applicationEnvironment->setDatabaseName(null); - $applicationEnvironment->setDatabasePassword(null); - - $this->taskLoggerService->addSuccessLogMessage( - $this->task, - sprintf('Removed database %s.', $databaseId), - 2 - ); - } catch (\Exception $ex) { - $this->taskLoggerService - ->addErrorLogMessage($this->task, $ex->getMessage(), 2) - ->addFailedLogMessage($this->task, 'Removing database failed.', 2); - - throw $ex; - } - } - - /** - * Destroy a sock application for a specific application environment. - * - * @param ApplicationEnvironment $applicationEnvironment - * The application environment to destroy the application for. - * - * @throws ClientException - * When something goes wrong while destroying the application. - */ - protected function destroySockApplication(ApplicationEnvironment $applicationEnvironment) - { - $this->taskLoggerService->addLogHeader($this->task, 'Removing application', 1); - - if (!$applicationId = $this->dataValueService->getValue($applicationEnvironment, 'sock_application_id')) { - $this->taskLoggerService->addInfoLogMessage($this->task, 'No application to remove.', 2); - return; - } - - try { - $this->apiService->removeApplication($applicationId); - - $this->dataValueService->storeValue($applicationEnvironment, 'sock_application_id', null); - - $this->taskLoggerService->addSuccessLogMessage( - $this->task, - sprintf('Removed application %s.', $applicationId), - 2 - ); - } catch (\Exception $ex) { - $this->taskLoggerService - ->addErrorLogMessage($this->task, $ex->getMessage(), 2) - ->addFailedLogMessage($this->task, 'Removing database failed.', 2); - - throw $ex; - } - } - - /** - * Destroy a sock account for a specific application environment. - * - * @param ApplicationEnvironment $applicationEnvironment - * The application environment to destroy the account for. - * - * @throws ClientException - * When something goes wrong while destroying the account. - */ - protected function destroySockAccount(ApplicationEnvironment $applicationEnvironment) - { - $this->taskLoggerService->addLogHeader($this->task, 'Removing account', 1); - - $application = $applicationEnvironment->getApplication(); - - if (!$accountId = $this->dataValueService->getValue($applicationEnvironment, 'sock_account_id')) { - $this->taskLoggerService->addInfoLogMessage($this->task, 'No accoutn to remove.', 2); - return; - } - - if ($this->dataValueService->getValue($application, 'parent_application')) { - $this->taskLoggerService->addInfoLogMessage($this->task, 'Using parent application account.', 2); - return; - } - - try { - $this->apiService->removeAccount($accountId); - - $this->dataValueService->storeValue($applicationEnvironment, 'sock_account_id', null); - $this->dataValueService->storeValue($applicationEnvironment, 'sock_ssh_user', null); - - $this->taskLoggerService->addSuccessLogMessage( - $this->task, - sprintf('Removed account %s.', $accountId), - 2 - ); - } catch (\Exception $ex) { - $this->taskLoggerService - ->addErrorLogMessage($this->task, $ex->getMessage(), 2) - ->addFailedLogMessage($this->task, 'Removing database failed.', 2); - - throw $ex; - } - } - - public function getName() - { - return 'Sock accounts, applications and databases'; - } -} diff --git a/Provisioner/DestroySockAccountProvisioner.php b/Provisioner/DestroySockAccountProvisioner.php new file mode 100644 index 0000000..6e64c99 --- /dev/null +++ b/Provisioner/DestroySockAccountProvisioner.php @@ -0,0 +1,133 @@ +dataValueService = $dataValueService; + $this->taskLoggerService = $taskLoggerService; + $this->apiService = $apiService; + $this->entityManager = $entityManager; + } + + public function doRun() + { + $appEnv = $this->task->getApplicationEnvironment(); + $environment = $appEnv->getEnvironment(); + + /** @var VirtualServer[] $servers */ + $servers = $this->entityManager->getRepository(VirtualServer::class)->findAll(); + + foreach ($servers as $server) { + if ($server->getEnvironment() != $environment) { + continue; + } + + if (!$this->dataValueService->getValue($server, 'manage_sock')) { + continue; + } + + $this->taskLoggerService->addLogHeader( + $this->task, + sprintf('Sock server "%s"', $server->getName()) + ); + + try { + $this->destroySockAccount($appEnv); + + try { + $this->entityManager->persist($appEnv); + $this->entityManager->flush(); + } catch (\Exception $ex) { + $this->taskLoggerService->addWarningLogMessage($this->task, 'Could not remove local Sock data.'); + } + + $this->taskLoggerService->addSuccessLogMessage($this->task, 'Cleanup succeeded.'); + } catch (\Exception $ex) { + $this->taskLoggerService->addFailedLogMessage($this->task, 'Cleanup failed.'); + throw new LoggedException('', 0, $ex); + } + } + } + + /** + * Destroy a sock account for a specific application environment. + * + * @param ApplicationEnvironment $appEnv + * The application environment to destroy the account for. + * + * @throws ClientException + * When something goes wrong while destroying the account. + */ + protected function destroySockAccount(ApplicationEnvironment $appEnv) + { + $this->taskLoggerService->addLogHeader($this->task, 'Removing account', 1); + + $application = $appEnv->getApplication(); + + if (!$accountId = $this->dataValueService->getValue($appEnv, 'sock_account_id')) { + $this->taskLoggerService->addInfoLogMessage($this->task, 'No accoutn to remove.', 2); + return; + } + + if ($this->dataValueService->getValue($application, 'parent_application')) { + $this->taskLoggerService->addInfoLogMessage($this->task, 'Using parent application account.', 2); + return; + } + + try { + $this->apiService->removeAccount($accountId); + + $this->dataValueService->storeValue($appEnv, 'sock_account_id', null); + $this->dataValueService->storeValue($appEnv, 'sock_ssh_user', null); + + $this->taskLoggerService->addSuccessLogMessage( + $this->task, + sprintf('Removed account %s.', $accountId), + 2 + ); + } catch (\Exception $ex) { + $this->taskLoggerService + ->addErrorLogMessage($this->task, $ex->getMessage(), 2) + ->addFailedLogMessage($this->task, 'Removing database failed.', 2); + + throw $ex; + } + } + + public function getName() + { + return 'Sock account'; + } +} diff --git a/Provisioner/DestroySockApplicationProvisioner.php b/Provisioner/DestroySockApplicationProvisioner.php new file mode 100644 index 0000000..54be658 --- /dev/null +++ b/Provisioner/DestroySockApplicationProvisioner.php @@ -0,0 +1,125 @@ +dataValueService = $dataValueService; + $this->taskLoggerService = $taskLoggerService; + $this->apiService = $apiService; + $this->entityManager = $entityManager; + } + + public function doRun() + { + $appEnv = $this->task->getApplicationEnvironment(); + $environment = $appEnv->getEnvironment(); + + /** @var VirtualServer[] $servers */ + $servers = $this->entityManager->getRepository(VirtualServer::class)->findAll(); + + foreach ($servers as $server) { + if ($server->getEnvironment() != $environment) { + continue; + } + + if (!$this->dataValueService->getValue($server, 'manage_sock')) { + continue; + } + + $this->taskLoggerService->addLogHeader( + $this->task, + sprintf('Sock server "%s"', $server->getName()) + ); + + try { + $this->destroySockApplication($appEnv); + + try { + $this->entityManager->persist($appEnv); + $this->entityManager->flush(); + } catch (\Exception $ex) { + $this->taskLoggerService->addWarningLogMessage($this->task, 'Could not remove local Sock data.'); + } + + $this->taskLoggerService->addSuccessLogMessage($this->task, 'Cleanup succeeded.'); + } catch (\Exception $ex) { + $this->taskLoggerService->addFailedLogMessage($this->task, 'Cleanup failed.'); + throw new LoggedException('', 0, $ex); + } + } + } + + /** + * Destroy a sock application for a specific application environment. + * + * @param ApplicationEnvironment $appEnv + * The application environment to destroy the application for. + * + * @throws ClientException + * When something goes wrong while destroying the application. + */ + protected function destroySockApplication(ApplicationEnvironment $appEnv) + { + $this->taskLoggerService->addLogHeader($this->task, 'Removing application', 1); + + if (!$applicationId = $this->dataValueService->getValue($appEnv, 'sock_application_id')) { + $this->taskLoggerService->addInfoLogMessage($this->task, 'No application to remove.', 2); + return; + } + + try { + $this->apiService->removeApplication($applicationId); + + $this->dataValueService->storeValue($appEnv, 'sock_application_id', null); + + $this->taskLoggerService->addSuccessLogMessage( + $this->task, + sprintf('Removed application %s.', $applicationId), + 2 + ); + } catch (\Exception $ex) { + $this->taskLoggerService + ->addErrorLogMessage($this->task, $ex->getMessage(), 2) + ->addFailedLogMessage($this->task, 'Removing database failed.', 2); + + throw $ex; + } + } + + public function getName() + { + return 'Sock application'; + } +} diff --git a/Provisioner/DestroySockDatabaseProvisioner.php b/Provisioner/DestroySockDatabaseProvisioner.php new file mode 100644 index 0000000..2e84a61 --- /dev/null +++ b/Provisioner/DestroySockDatabaseProvisioner.php @@ -0,0 +1,129 @@ +dataValueService = $dataValueService; + $this->taskLoggerService = $taskLoggerService; + $this->apiService = $apiService; + $this->entityManager = $entityManager; + } + + public function doRun() + { + $appEnv = $this->task->getApplicationEnvironment(); + $environment = $appEnv->getEnvironment(); + + /** @var VirtualServer[] $servers */ + $servers = $this->entityManager->getRepository(VirtualServer::class)->findAll(); + + foreach ($servers as $server) { + if ($server->getEnvironment() != $environment) { + continue; + } + + if (!$this->dataValueService->getValue($server, 'manage_sock')) { + continue; + } + + $this->taskLoggerService->addLogHeader( + $this->task, + sprintf('Sock server "%s"', $server->getName()) + ); + + try { + $this->destroySockDatabase($appEnv); + + try { + $this->entityManager->persist($appEnv); + $this->entityManager->flush(); + } catch (\Exception $ex) { + $this->taskLoggerService->addWarningLogMessage($this->task, 'Could not remove local Sock data.'); + } + + $this->taskLoggerService->addSuccessLogMessage($this->task, 'Cleanup succeeded.'); + } catch (\Exception $ex) { + $this->taskLoggerService->addFailedLogMessage($this->task, 'Cleanup failed.'); + throw new LoggedException('', 0, $ex); + } + } + } + + /** + * Destroy a sock database for a specific application environment. + * + * @param ApplicationEnvironment $appEnv + * The application environment to destroy the database for. + * + * @throws ClientException + * When something goes wrong while destroying the database. + */ + protected function destroySockDatabase(ApplicationEnvironment $appEnv) + { + $this->taskLoggerService->addLogHeader($this->task, 'Removing database', 1); + + if (!$databaseId = $this->dataValueService->getValue($appEnv, 'sock_database_id')) { + $this->taskLoggerService->addInfoLogMessage($this->task, 'No database to remove.', 2); + return; + } + + try { + $this->apiService->removeDatabase($databaseId); + + $this->dataValueService->storeValue($appEnv, 'sock_database_id', null); + + $appEnv->setDatabaseUser(null); + $appEnv->setDatabaseName(null); + $appEnv->setDatabasePassword(null); + + $this->taskLoggerService->addSuccessLogMessage( + $this->task, + sprintf('Removed database %s.', $databaseId), + 2 + ); + } catch (\Exception $ex) { + $this->taskLoggerService + ->addErrorLogMessage($this->task, $ex->getMessage(), 2) + ->addFailedLogMessage($this->task, 'Removing database failed.', 2); + + throw $ex; + } + } + + public function getName() + { + return 'Sock database'; + } +} diff --git a/Provisioner/PollingProvisioner.php b/Provisioner/PollingProvisioner.php new file mode 100644 index 0000000..3facb16 --- /dev/null +++ b/Provisioner/PollingProvisioner.php @@ -0,0 +1,88 @@ +dataValueService = $dataValueService; + $this->taskLoggerService = $taskLoggerService; + $this->apiService = $apiService; + $this->entityManager = $entityManager; + $this->sockPoller = $sockPoller; + } + + public function doRun() + { + try { + $this->sockPoller->doPolling($this->task); + } catch (\Exception $ex) { + $this->taskLoggerService + ->addErrorLogMessage($this->task, $ex->getMessage(), 2) + ->addFailedLogMessage($this->task, 'Provisioning failed.'); + throw new LoggedException('', 0, $ex); + } + + $this->taskLoggerService->addSuccessLogMessage($this->task, 'Provisioning succeeded.'); + } + + public function getName() + { + return 'Polling for sock accounts, applications and databases'; + } + + public function isSelectable() + { + return false; + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 0d469fb..ddba6c6 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -5,12 +5,27 @@ services: public: true tags: - { name: data_type_provider } - DigipolisGent\Domainator9k\SockBundle\Provisioner\BuildProvisioner: + DigipolisGent\Domainator9k\SockBundle\Provisioner\BuildSockAccountProvisioner: + tags: + - { name: domainator.provisioner.build, priority: 53 } + DigipolisGent\Domainator9k\SockBundle\Provisioner\BuildSockApplicationProvisioner: + tags: + - { name: domainator.provisioner.build, priority: 52 } + DigipolisGent\Domainator9k\SockBundle\Provisioner\BuildSockDatabaseProvisioner: + tags: + - { name: domainator.provisioner.build, priority: 51 } + DigipolisGent\Domainator9k\SockBundle\Provisioner\PollingProvisioner: tags: - { name: domainator.provisioner.build, priority: 50 } - DigipolisGent\Domainator9k\SockBundle\Provisioner\DestroyProvisioner: + DigipolisGent\Domainator9k\SockBundle\Provisioner\DestroySockDatabaseProvisioner: + tags: + - { name: domainator.provisioner.destroy, priority: 12 } + DigipolisGent\Domainator9k\SockBundle\Provisioner\DestroySockApplicationProvisioner: + tags: + - { name: domainator.provisioner.destroy, priority: 11 } + DigipolisGent\Domainator9k\SockBundle\Provisioner\DestroySockAccountProvisioner: tags: - - { name: domainator.provisioner.destroy, priority: 50 } + - { name: domainator.provisioner.destroy, priority: 10 } DigipolisGent\Domainator9k\SockBundle\FieldType\ParentApplicationFieldType: public: true tags: diff --git a/Service/SockPollerService.php b/Service/SockPollerService.php new file mode 100644 index 0000000..0a53e7e --- /dev/null +++ b/Service/SockPollerService.php @@ -0,0 +1,67 @@ +taskLoggerService = $taskLoggerService; + $this->apiService = $apiService; + $this->polling = []; + } + + public function addPolling($type, $id, Task $task) { + $this->polling[$task->getId()][$type] = $id; + } + + public function doPolling(Task $task) + { + if (!isset($this->polling[$task->getId()])) { + return; + } + $this->taskLoggerService->addInfoLogMessage( + $task, + 'Waiting for changes to be applied.', + 2 + ); + + $start = time(); + + do { + $events = false; + foreach ($this->polling[$task->getId] as $type => $sockId) { + if ($events = $this->apiService->getEvents($type, $sockId)) { + break; + } + } + + if (!$events) { + break; + } + + if ((time() - $start) >= 600) { + throw new \Exception( + sprintf( + 'Timeout, waited more then 10 minutes while polling for %s #%s.', + $type, + $sockId + ) + ); + } + + sleep(5); + } while (true); + } +} diff --git a/Tests/Provisioner/AbstractProvisionerTest.php b/Tests/Provisioner/AbstractProvisionerTest.php index b88c9fe..c099815 100644 --- a/Tests/Provisioner/AbstractProvisionerTest.php +++ b/Tests/Provisioner/AbstractProvisionerTest.php @@ -120,7 +120,7 @@ protected function getEntityManagerMock(array $functions = array()) protected function getProvisionerMock(array $arguments, array $methods) { $mock = $this - ->getMockBuilder(BuildProvisioner::class) + ->getMockBuilder($this->getProvisionerClass()) ->setMethods(array_keys($methods)) ->setConstructorArgs($arguments) ->getMock(); @@ -187,4 +187,6 @@ protected function invokeProvisionerMethod($provisioner, $methodName) return $method->invokeArgs($provisioner, $args); } + + abstract protected function getProvisionerClass(); } diff --git a/Tests/Provisioner/BuildSockAccountProvisionerTest.php b/Tests/Provisioner/BuildSockAccountProvisionerTest.php new file mode 100644 index 0000000..8835d5a --- /dev/null +++ b/Tests/Provisioner/BuildSockAccountProvisionerTest.php @@ -0,0 +1,469 @@ +setName('prod'); + $prodEnvironment->setProd(true); + + $uatEnvironment = new Environment(); + $uatEnvironment->setName('uat'); + $uatEnvironment->setProd(true); + + $servers = new ArrayCollection(); + + $serverOne = new VirtualServer(); + $serverOne->setEnvironment($uatEnvironment); + $servers->add($serverOne); + + $serverTwo = new VirtualServer(); + $serverTwo->setEnvironment($prodEnvironment); + $servers->add($serverTwo); + + $serverThree = new VirtualServer(); + $serverThree->setEnvironment($prodEnvironment); + $servers->add($serverThree); + + $application = new FooApplication(); + $application->setHasDatabase(true); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setEnvironment($prodEnvironment); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = [ + [ + 'method' => 'getRepository', + 'willReturn' => $this->getRepositoryMock('findAll', $servers) + ] + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => false + ], + [ + 'method' => 'getValue', + 'willReturn' => true + ] + ]; + + $apiServiceFunctions = []; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + + $task = new Task(); + $task->setType(Task::TYPE_BUILD); + $task->setStatus(Task::STATUS_NEW); + $task->setProvisioners([BuildSockAccountProvisioner::class]); + $task->setApplicationEnvironment($applicationEnvironment); + + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + + $arguments = [ + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ]; + $methods = [ + 'createSockAccount' => function () { + return 1; + }, + ]; + + $provisioner = $this->getProvisionerMock($arguments, $methods); + $provisioner->setTask($task); + $provisioner->run(); + } + + public function testCreateSockAccountWithParentApplication() + { + $application = new FooApplication(); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setApplication($application); + $server = new VirtualServer(); + + $parentApplication = new FooApplication(); + + + $entityManagerFunctions = [ + [ + 'method' => 'getRepository', + 'willReturn' => $this->getRepositoryMock('findOneBy', new FooApplication()), + ] + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => $parentApplication + ], + [ + 'method' => 'getValue', + 'willReturn' => 1 + ], + [ + 'method' => 'getValue', + 'willReturn' => 'username' + ], + [ + 'method' => 'getValue', + 'willReturn' => 1 + ], + ]; + + $apiServiceFunctions = [ + [ + 'method' => 'getVirtualServer', + 'willReturn' => null + ] + ]; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + + $provisioner = new BuildSockAccountProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ); + + $this->invokeProvisionerMethod($provisioner, 'createSockAccount', $applicationEnvironment, $server); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage The parent application must be build first. + */ + public function testCreateSockAccountWithParentApplicationNotBuilt() + { + $application = new FooApplication(); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setApplication($application); + $server = new VirtualServer(); + + $parentApplication = new FooApplication(); + + + $entityManagerFunctions = [ + [ + 'method' => 'getRepository', + 'willReturn' => $this->getRepositoryMock('findOneBy', new FooApplication()), + ] + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => $parentApplication + ], + [ + 'method' => 'getValue', + 'willReturn' => 1 + ], + [ + 'method' => 'getValue', + 'willReturn' => null + ], + [ + 'method' => 'getValue', + 'willReturn' => 1 + ], + ]; + + $apiServiceFunctions = [ + [ + 'method' => 'getVirtualServer', + 'willReturn' => null + ] + ]; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + + $provisioner = new BuildSockAccountProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ); + + $this->invokeProvisionerMethod($provisioner, 'createSockAccount', $applicationEnvironment, $server); + } + + public function testCreateSockAccountWithoutParentApplication() + { + $application = new FooApplication(); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setApplication($application); + $server = new VirtualServer(); + + + $entityManagerFunctions = [ + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => false, + ], + [ + 'method' => 'getValue', + 'willReturn' => 1, + ], + [ + 'method' => 'getValue', + 'willReturn' => [ + 1, + 2, + 3, + 4, + 5, + ] + ], + [ + 'method' => 'storeValue', + 'willReturn' => null, + ], + [ + 'method' => 'storeValue', + 'willReturn' => null, + ], + ]; + + $apiServiceFunctions = [ + [ + 'method' => 'getVirtualServer', + 'willReturn' => null, + ], + [ + 'method' => 'findAccountByName', + 'willReturn' => null, + ], + [ + 'method' => 'createAccount', + 'willReturn' => null, + ], + ]; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + + $provisioner = new BuildSockAccountProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ); + + $this->invokeProvisionerMethod($provisioner, 'createSockAccount', $applicationEnvironment, $server); + } + + public function testCreateExistingSockAccount() + { + $application = new FooApplication(); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setApplication($application); + $server = new VirtualServer(); + + + $entityManagerFunctions = [ + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => false, + ], + [ + 'method' => 'getValue', + 'willReturn' => 1, + ], + [ + 'method' => 'getValue', + 'willReturn' => [ + 1, + 2, + 3, + 4, + 5, + ] + ], + [ + 'method' => 'storeValue', + 'willReturn' => null, + ], + [ + 'method' => 'storeValue', + 'willReturn' => null, + ], + ]; + + $account = ['id' => uniqid()]; + + $apiServiceFunctions = [ + [ + 'method' => 'getVirtualServer', + 'willReturn' => null, + ], + [ + 'method' => 'findAccountByName', + 'willReturn' => $account, + ], + ]; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $apiService->expects($this->never())->method('createAccount'); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + + $provisioner = new BuildSockAccountProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ); + + $this->invokeProvisionerMethod($provisioner, 'createSockAccount', $applicationEnvironment, $server); + } + + /** + * @expectedException \DigipolisGent\Domainator9k\CoreBundle\Exception\LoggedException + */ + public function testOnBuildWithException() + { + $prodEnvironment = new Environment(); + $prodEnvironment->setName('prod'); + $prodEnvironment->setProd(true); + + $uatEnvironment = new Environment(); + $uatEnvironment->setName('uat'); + $uatEnvironment->setProd(true); + + $servers = new ArrayCollection(); + + $serverOne = new VirtualServer(); + $serverOne->setEnvironment($uatEnvironment); + $servers->add($serverOne); + + $serverTwo = new VirtualServer(); + $serverTwo->setEnvironment($prodEnvironment); + $servers->add($serverTwo); + + $serverThree = new VirtualServer(); + $serverThree->setEnvironment($prodEnvironment); + $servers->add($serverThree); + + $application = new FooApplication(); + $application->setHasDatabase(true); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setEnvironment($prodEnvironment); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = [ + [ + 'method' => 'getRepository', + 'willReturn' => $this->getRepositoryMock('findAll', $servers) + ] + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => false + ], + [ + 'method' => 'getValue', + 'willReturn' => true + ] + ]; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock(); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + + $task = new Task(); + $task->setType(Task::TYPE_BUILD); + $task->setStatus(Task::STATUS_NEW); + $task->setApplicationEnvironment($applicationEnvironment); + + $arguments = [ + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ]; + $methods = [ + 'createSockAccount' => function () { + throw new ClientException('This is an exception.', $this->getRequestMock()); + }, + ]; + + $provisioner = $this->getProvisionerMock($arguments, $methods); + $provisioner->setTask($task); + $provisioner->run(); + } + + public function testGetName() + { + $dataValueService = $this->getDataValueServiceMock(); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock(); + $entityManager = $this->getEntityManagerMock(); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + $provisioner = new BuildSockAccountProvisioner($dataValueService, $taskLoggerService, $apiService, $entityManager, $sockPoller); + $this->assertEquals($provisioner->getName(), 'Sock account'); + } + + protected function getProvisionerClass() + { + return BuildSockAccountProvisioner::class; + } +} diff --git a/Tests/Provisioner/BuildSockApplicationProvisionerTest.php b/Tests/Provisioner/BuildSockApplicationProvisionerTest.php new file mode 100644 index 0000000..be0990b --- /dev/null +++ b/Tests/Provisioner/BuildSockApplicationProvisionerTest.php @@ -0,0 +1,299 @@ +setName('prod'); + $prodEnvironment->setProd(true); + + $uatEnvironment = new Environment(); + $uatEnvironment->setName('uat'); + $uatEnvironment->setProd(true); + + $servers = new ArrayCollection(); + + $serverOne = new VirtualServer(); + $serverOne->setEnvironment($uatEnvironment); + $servers->add($serverOne); + + $serverTwo = new VirtualServer(); + $serverTwo->setEnvironment($prodEnvironment); + $servers->add($serverTwo); + + $serverThree = new VirtualServer(); + $serverThree->setEnvironment($prodEnvironment); + $servers->add($serverThree); + + $application = new FooApplication(); + $application->setHasDatabase(true); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setEnvironment($prodEnvironment); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = [ + [ + 'method' => 'getRepository', + 'willReturn' => $this->getRepositoryMock('findAll', $servers) + ] + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => false + ], + [ + 'method' => 'getValue', + 'willReturn' => true + ] + ]; + + $apiServiceFunctions = []; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + + $task = new Task(); + $task->setType(Task::TYPE_BUILD); + $task->setStatus(Task::STATUS_NEW); + $task->setApplicationEnvironment($applicationEnvironment); + + $arguments = [ + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ]; + $methods = [ + 'createSockApplication' => function () { + return 2; + }, + ]; + + $provisioner = $this->getProvisionerMock($arguments, $methods); + $provisioner->setTask($task); + $provisioner->run(); + } + + public function testCreateSockApplication() + { + $application = new FooApplication(); + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = [ + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => 4, + ], + ]; + + $apiServiceFunctions = [ + [ + 'method' => 'getAccount', + 'willReturn' => null, + ], + [ + 'method' => 'findApplicationByName', + 'willReturn' => null, + ], + [ + 'method' => 'createApplication', + 'willReturn' => [ + 'id' => 10 + ], + ], + ]; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + + $provisioner = new BuildSockApplicationProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ); + + $this->invokeProvisionerMethod($provisioner, 'createSockApplication', $applicationEnvironment); + } + + public function testCreateExistingSockApplication() + { + $application = new FooApplication(); + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = [ + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => 4, + ], + ]; + + $application = ['id' => uniqid()]; + + $apiServiceFunctions = [ + [ + 'method' => 'getAccount', + 'willReturn' => null, + ], + [ + 'method' => 'findApplicationByName', + 'willReturn' => $application, + ], + ]; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $apiService->expects($this->never())->method('createApplication'); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + + $provisioner = new BuildSockApplicationProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ); + + $this->invokeProvisionerMethod($provisioner, 'createSockApplication', $applicationEnvironment); + } + + /** + * @expectedException \DigipolisGent\Domainator9k\CoreBundle\Exception\LoggedException + */ + public function testOnBuildWithException() + { + $prodEnvironment = new Environment(); + $prodEnvironment->setName('prod'); + $prodEnvironment->setProd(true); + + $uatEnvironment = new Environment(); + $uatEnvironment->setName('uat'); + $uatEnvironment->setProd(true); + + $servers = new ArrayCollection(); + + $serverOne = new VirtualServer(); + $serverOne->setEnvironment($uatEnvironment); + $servers->add($serverOne); + + $serverTwo = new VirtualServer(); + $serverTwo->setEnvironment($prodEnvironment); + $servers->add($serverTwo); + + $serverThree = new VirtualServer(); + $serverThree->setEnvironment($prodEnvironment); + $servers->add($serverThree); + + $application = new FooApplication(); + $application->setHasDatabase(true); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setEnvironment($prodEnvironment); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = [ + [ + 'method' => 'getRepository', + 'willReturn' => $this->getRepositoryMock('findAll', $servers) + ] + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => false + ], + [ + 'method' => 'getValue', + 'willReturn' => true + ] + ]; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock(); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + + $task = new Task(); + $task->setType(Task::TYPE_BUILD); + $task->setStatus(Task::STATUS_NEW); + $task->setApplicationEnvironment($applicationEnvironment); + + $arguments = [ + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller, + ]; + $methods = [ + 'createSockApplication' => function () { + throw new ClientException('This is an exception.', $this->getRequestMock()); + }, + ]; + + $provisioner = $this->getProvisionerMock($arguments, $methods); + $provisioner->setTask($task); + $provisioner->run(); + } + + public function testGetName() + { + $dataValueService = $this->getDataValueServiceMock(); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock(); + $entityManager = $this->getEntityManagerMock(); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + $provisioner = new BuildSockApplicationProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ); + $this->assertEquals($provisioner->getName(), 'Sock application'); + } + + protected function getProvisionerClass() + { + return BuildSockApplicationProvisioner::class; + } +} diff --git a/Tests/Provisioner/BuildProvisionerTest.php b/Tests/Provisioner/BuildSockDatabaseProvisionerTest.php similarity index 55% rename from Tests/Provisioner/BuildProvisionerTest.php rename to Tests/Provisioner/BuildSockDatabaseProvisionerTest.php index a83f4a5..b310945 100644 --- a/Tests/Provisioner/BuildProvisionerTest.php +++ b/Tests/Provisioner/BuildSockDatabaseProvisionerTest.php @@ -7,13 +7,14 @@ use DigipolisGent\Domainator9k\CoreBundle\Entity\Environment; use DigipolisGent\Domainator9k\CoreBundle\Entity\Task; use DigipolisGent\Domainator9k\CoreBundle\Entity\VirtualServer; -use DigipolisGent\Domainator9k\SockBundle\Provisioner\BuildProvisioner; +use DigipolisGent\Domainator9k\SockBundle\Provisioner\BuildSockDatabaseProvisioner; use DigipolisGent\Domainator9k\SockBundle\Service\ApiService; +use DigipolisGent\Domainator9k\SockBundle\Service\SockPollerService; use DigipolisGent\Domainator9k\SockBundle\Tests\Fixtures\FooApplication; use Doctrine\Common\Collections\ArrayCollection; use GuzzleHttp\Exception\ClientException; -class BuildProvisionerTest extends AbstractProvisionerTest +class BuildSockDatabaseProvisionerTest extends AbstractProvisionerTest { public function testOnBuildWithDatabase() @@ -65,42 +66,28 @@ public function testOnBuildWithDatabase() ] ]; - $apiServiceFunctions = [ - [ - 'method' => 'getEvents', - 'with' => ['accounts', 1], - 'willReturn' => [] - ], - [ - 'method' => 'getEvents', - 'with' => ['applications', 2], - 'willReturn' => [] - ], - [ - 'method' => 'getEvents', - 'with' => ['databases', 3], - 'willReturn' => [] - ] - ]; + $apiServiceFunctions = []; $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); $taskLoggerService = $this->getTaskLoggerServiceMock(); $apiService = $this->getApiServiceMock($apiServiceFunctions); $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + $task = new Task(); $task->setType(Task::TYPE_BUILD); $task->setStatus(Task::STATUS_NEW); $task->setApplicationEnvironment($applicationEnvironment); - $arguments = [$dataValueService, $taskLoggerService, $apiService, $entityManager]; + $arguments = [ + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller, + ]; $methods = [ - 'createSockAccount' => function () { - return 1; - }, - 'createSockApplication' => function () { - return 2; - }, 'createSockDatabase' => function () { return 3; } @@ -111,300 +98,6 @@ public function testOnBuildWithDatabase() $provisioner->run(); } - - public function testCreateSockAccountWithParentApplication() - { - $application = new FooApplication(); - - $applicationEnvironment = new ApplicationEnvironment(); - $applicationEnvironment->setApplication($application); - $server = new VirtualServer(); - - $parentApplication = new FooApplication(); - - - $entityManagerFunctions = [ - [ - 'method' => 'getRepository', - 'willReturn' => $this->getRepositoryMock('findOneBy', new FooApplication()), - ] - ]; - - $dataValueServiceFunctions = [ - [ - 'method' => 'getValue', - 'willReturn' => $parentApplication - ], - [ - 'method' => 'getValue', - 'willReturn' => 1 - ], - [ - 'method' => 'getValue', - 'willReturn' => 'username' - ], - [ - 'method' => 'getValue', - 'willReturn' => 1 - ], - ]; - - $apiServiceFunctions = [ - [ - 'method' => 'getVirtualServer', - 'willReturn' => null - ] - ]; - - $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); - $taskLoggerService = $this->getTaskLoggerServiceMock(); - $apiService = $this->getApiServiceMock($apiServiceFunctions); - $entityManager = $this->getEntityManagerMock($entityManagerFunctions); - - $provisioner = new BuildProvisioner( - $dataValueService, - $taskLoggerService, - $apiService, - $entityManager - ); - - $this->invokeProvisionerMethod($provisioner, 'createSockAccount', $applicationEnvironment, $server); - } - - public function testCreateSockAccountWithoutParentApplication() - { - $application = new FooApplication(); - - $applicationEnvironment = new ApplicationEnvironment(); - $applicationEnvironment->setApplication($application); - $server = new VirtualServer(); - - - $entityManagerFunctions = [ - ]; - - $dataValueServiceFunctions = [ - [ - 'method' => 'getValue', - 'willReturn' => false, - ], - [ - 'method' => 'getValue', - 'willReturn' => 1, - ], - [ - 'method' => 'getValue', - 'willReturn' => [ - 1, - 2, - 3, - 4, - 5, - ] - ], - [ - 'method' => 'storeValue', - 'willReturn' => null, - ], - [ - 'method' => 'storeValue', - 'willReturn' => null, - ], - ]; - - $apiServiceFunctions = [ - [ - 'method' => 'getVirtualServer', - 'willReturn' => null, - ], - [ - 'method' => 'findAccountByName', - 'willReturn' => null, - ], - [ - 'method' => 'createAccount', - 'willReturn' => null, - ], - ]; - - $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); - $taskLoggerService = $this->getTaskLoggerServiceMock(); - $apiService = $this->getApiServiceMock($apiServiceFunctions); - $entityManager = $this->getEntityManagerMock($entityManagerFunctions); - - $provisioner = new BuildProvisioner( - $dataValueService, - $taskLoggerService, - $apiService, - $entityManager - ); - - $this->invokeProvisionerMethod($provisioner, 'createSockAccount', $applicationEnvironment, $server); - } - - public function testCreateExistingSockAccount() - { - $application = new FooApplication(); - - $applicationEnvironment = new ApplicationEnvironment(); - $applicationEnvironment->setApplication($application); - $server = new VirtualServer(); - - - $entityManagerFunctions = [ - ]; - - $dataValueServiceFunctions = [ - [ - 'method' => 'getValue', - 'willReturn' => false, - ], - [ - 'method' => 'getValue', - 'willReturn' => 1, - ], - [ - 'method' => 'getValue', - 'willReturn' => [ - 1, - 2, - 3, - 4, - 5, - ] - ], - [ - 'method' => 'storeValue', - 'willReturn' => null, - ], - [ - 'method' => 'storeValue', - 'willReturn' => null, - ], - ]; - - $account = ['id' => uniqid()]; - - $apiServiceFunctions = [ - [ - 'method' => 'getVirtualServer', - 'willReturn' => null, - ], - [ - 'method' => 'findAccountByName', - 'willReturn' => $account, - ], - ]; - - $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); - $taskLoggerService = $this->getTaskLoggerServiceMock(); - $apiService = $this->getApiServiceMock($apiServiceFunctions); - $apiService->expects($this->never())->method('createAccount'); - $entityManager = $this->getEntityManagerMock($entityManagerFunctions); - - $provisioner = new BuildProvisioner( - $dataValueService, - $taskLoggerService, - $apiService, - $entityManager - ); - - $this->invokeProvisionerMethod($provisioner, 'createSockAccount', $applicationEnvironment, $server); - } - - public function testCreateSockApplication() - { - $application = new FooApplication(); - $applicationEnvironment = new ApplicationEnvironment(); - $applicationEnvironment->setApplication($application); - - $entityManagerFunctions = [ - ]; - - $dataValueServiceFunctions = [ - [ - 'method' => 'getValue', - 'willReturn' => 4, - ], - ]; - - $apiServiceFunctions = [ - [ - 'method' => 'getAccount', - 'willReturn' => null, - ], - [ - 'method' => 'findApplicationByName', - 'willReturn' => null, - ], - [ - 'method' => 'createApplication', - 'willReturn' => [ - 'id' => 10 - ], - ], - ]; - - $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); - $taskLoggerService = $this->getTaskLoggerServiceMock(); - $apiService = $this->getApiServiceMock($apiServiceFunctions); - $entityManager = $this->getEntityManagerMock($entityManagerFunctions); - - $provisioner = new BuildProvisioner( - $dataValueService, - $taskLoggerService, - $apiService, - $entityManager - ); - - $this->invokeProvisionerMethod($provisioner, 'createSockApplication', $applicationEnvironment); - } - - public function testCreateExistingSockApplication() - { - $application = new FooApplication(); - $applicationEnvironment = new ApplicationEnvironment(); - $applicationEnvironment->setApplication($application); - - $entityManagerFunctions = [ - ]; - - $dataValueServiceFunctions = [ - [ - 'method' => 'getValue', - 'willReturn' => 4, - ], - ]; - - $application = ['id' => uniqid()]; - - $apiServiceFunctions = [ - [ - 'method' => 'getAccount', - 'willReturn' => null, - ], - [ - 'method' => 'findApplicationByName', - 'willReturn' => $application, - ], - ]; - - $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); - $taskLoggerService = $this->getTaskLoggerServiceMock(); - $apiService = $this->getApiServiceMock($apiServiceFunctions); - $apiService->expects($this->never())->method('createApplication'); - $entityManager = $this->getEntityManagerMock($entityManagerFunctions); - - $provisioner = new BuildProvisioner( - $dataValueService, - $taskLoggerService, - $apiService, - $entityManager - ); - - $this->invokeProvisionerMethod($provisioner, 'createSockApplication', $applicationEnvironment); - } - public function testCreateSockDatabase() { $application = new FooApplication(); @@ -470,12 +163,14 @@ public function testCreateSockDatabase() $taskLoggerService = $this->getTaskLoggerServiceMock(); $apiService = $this->getApiServiceMock($apiServiceFunctions); $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); - $provisioner = new BuildProvisioner( + $provisioner = new BuildSockDatabaseProvisioner( $dataValueService, $taskLoggerService, $apiService, - $entityManager + $entityManager, + $sockPoller ); $this->invokeProvisionerMethod($provisioner, 'createSockDatabase', $applicationEnvironment); @@ -545,12 +240,14 @@ public function testCreateExistingSockDatabase() $apiService = $this->getApiServiceMock($apiServiceFunctions); $apiService->expects($this->never())->method('createDatabase'); $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); - $provisioner = new BuildProvisioner( + $provisioner = new BuildSockDatabaseProvisioner( $dataValueService, $taskLoggerService, $apiService, - $entityManager + $entityManager, + $sockPoller ); $this->invokeProvisionerMethod($provisioner, 'createSockDatabase', $applicationEnvironment); @@ -611,26 +308,23 @@ public function testOnBuildWithoutDatabase() ->getMockBuilder(ApiService::class) ->disableOriginalConstructor() ->getMock(); - $apiService->expects($this->exactly(2)) - ->method('getEvents') - ->withConsecutive(['accounts', 1], ['applications', 2]) - ->willReturn([]); $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); $task = new Task(); $task->setType(Task::TYPE_BUILD); $task->setStatus(Task::STATUS_NEW); $task->setApplicationEnvironment($applicationEnvironment); - $arguments = [$dataValueService, $taskLoggerService, $apiService, $entityManager]; + $arguments = [ + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller, + ]; $methods = [ - 'createSockAccount' => function () { - return 1; - }, - 'createSockApplication' => function () { - return 2; - }, 'createSockDatabase' => function () { return null; } @@ -697,15 +391,22 @@ public function testOnBuildWithException() $taskLoggerService = $this->getTaskLoggerServiceMock(); $apiService = $this->getApiServiceMock(); $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); $task = new Task(); $task->setType(Task::TYPE_BUILD); $task->setStatus(Task::STATUS_NEW); $task->setApplicationEnvironment($applicationEnvironment); - $arguments = [$dataValueService, $taskLoggerService, $apiService, $entityManager]; + $arguments = [ + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller, + ]; $methods = [ - 'createSockAccount' => function () { + 'createSockDatabase' => function () { throw new ClientException('This is an exception.', $this->getRequestMock()); }, ]; @@ -721,7 +422,19 @@ public function testGetName() $taskLoggerService = $this->getTaskLoggerServiceMock(); $apiService = $this->getApiServiceMock(); $entityManager = $this->getEntityManagerMock(); - $provisioner = new BuildProvisioner($dataValueService, $taskLoggerService, $apiService, $entityManager); - $this->assertEquals($provisioner->getName(), 'Sock accounts, applications and databases'); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + $provisioner = new BuildSockDatabaseProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ); + $this->assertEquals($provisioner->getName(), 'Sock database'); + } + + protected function getProvisionerClass() + { + return BuildSockDatabaseProvisioner::class; } } diff --git a/Tests/Provisioner/DestroyProvisionerTest.php b/Tests/Provisioner/DestroySockAccountProvisionerTest.php similarity index 90% rename from Tests/Provisioner/DestroyProvisionerTest.php rename to Tests/Provisioner/DestroySockAccountProvisionerTest.php index ee35e8d..4b0f151 100644 --- a/Tests/Provisioner/DestroyProvisionerTest.php +++ b/Tests/Provisioner/DestroySockAccountProvisionerTest.php @@ -7,13 +7,12 @@ use DigipolisGent\Domainator9k\CoreBundle\Entity\Environment; use DigipolisGent\Domainator9k\CoreBundle\Entity\Task; use DigipolisGent\Domainator9k\CoreBundle\Entity\VirtualServer; -use DigipolisGent\Domainator9k\CoreBundle\Event\DestroyEvent; -use DigipolisGent\Domainator9k\SockBundle\Provisioner\DestroyProvisioner; +use DigipolisGent\Domainator9k\SockBundle\Provisioner\DestroySockAccountProvisioner; use DigipolisGent\Domainator9k\SockBundle\Tests\Fixtures\FooApplication; use Doctrine\Common\Collections\ArrayCollection; use GuzzleHttp\Exception\ClientException; -class DestroyProvisionerTest extends AbstractProvisionerTest +class DestroySockAccountProvisionerTest extends AbstractProvisionerTest { public function testOnDestroy() @@ -87,20 +86,12 @@ public function testOnDestroy() 'method' => 'storeValue', 'willReturn' => null ], - [ - 'method' => 'getValue', - 'willReturn' => null - ], ]; $apiServiceFunctions = [ [ 'method' => 'removeAccount', 'willReturn' => null - ], - [ - 'method' => 'removeApplication', - 'willReturn' => null ] ]; @@ -114,7 +105,7 @@ public function testOnDestroy() $task->setStatus(Task::STATUS_NEW); $task->setApplicationEnvironment($applicationEnvironment); - $provisioner = new DestroyProvisioner( + $provisioner = new DestroySockAccountProvisioner( $dataValueService, $taskLoggerService, $apiService, @@ -124,6 +115,9 @@ public function testOnDestroy() $provisioner->run(); } + /** + * @expectedException \DigipolisGent\Domainator9k\CoreBundle\Exception\LoggedException + */ public function testOnDestroyWithException() { $prodEnvironment = new Environment(); @@ -196,7 +190,7 @@ public function testOnDestroyWithException() $task->setStatus(Task::STATUS_NEW); $task->setApplicationEnvironment($applicationEnvironment); - $provisioner = new DestroyProvisioner( + $provisioner = new DestroySockAccountProvisioner( $dataValueService, $taskLoggerService, $apiService, @@ -212,7 +206,13 @@ public function testGetName() $taskLoggerService = $this->getTaskLoggerServiceMock(); $apiService = $this->getApiServiceMock(); $entityManager = $this->getEntityManagerMock(); - $provisioner = new DestroyProvisioner($dataValueService, $taskLoggerService, $apiService, $entityManager); - $this->assertEquals($provisioner->getName(), 'Sock accounts, applications and databases'); + $provisioner = new DestroySockAccountProvisioner($dataValueService, $taskLoggerService, $apiService, $entityManager); + $this->assertEquals($provisioner->getName(), 'Sock account'); } + + protected function getProvisionerClass() + { + DestroySockAccountProvisioner::class; + } + } diff --git a/Tests/Provisioner/DestroySockApplicationProvisionerTest.php b/Tests/Provisioner/DestroySockApplicationProvisionerTest.php new file mode 100644 index 0000000..f5edfde --- /dev/null +++ b/Tests/Provisioner/DestroySockApplicationProvisionerTest.php @@ -0,0 +1,210 @@ +setName('prod'); + $prodEnvironment->setProd(true); + + $uatEnvironment = new Environment(); + $uatEnvironment->setName('uat'); + $uatEnvironment->setProd(true); + + $servers = new ArrayCollection(); + + $serverOne = new VirtualServer(); + $serverOne->setEnvironment($uatEnvironment); + $servers->add($serverOne); + + $serverTwo = new VirtualServer(); + $serverTwo->setEnvironment($prodEnvironment); + $servers->add($serverTwo); + + $serverThree = new VirtualServer(); + $serverThree->setEnvironment($prodEnvironment); + $servers->add($serverThree); + + $application = new FooApplication(); + $application->setHasDatabase(true); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setEnvironment($prodEnvironment); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = [ + [ + 'method' => 'getRepository', + 'willReturn' => $this->getRepositoryMock('findAll', $servers) + ], + [ + 'method' => 'persist', + 'willReturn' => null + ], + [ + 'method' => 'flush', + 'willReturn' => null + ] + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => null + ], + [ + 'method' => 'getValue', + 'willReturn' => true + ], + [ + 'method' => 'getValue', + 'willReturn' => 5 + ], + [ + 'method' => 'storeValue', + 'willReturn' => null + ], + ]; + + $apiServiceFunctions = [ + [ + 'method' => 'removeApplication', + 'willReturn' => null + ] + ]; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + + $task = new Task(); + $task->setType(Task::TYPE_DESTROY); + $task->setStatus(Task::STATUS_NEW); + $task->setApplicationEnvironment($applicationEnvironment); + + $provisioner = new DestroySockApplicationProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager + ); + $provisioner->setTask($task); + $provisioner->run(); + } + + /** + * @expectedException \DigipolisGent\Domainator9k\CoreBundle\Exception\LoggedException + */ + public function testOnDestroyWithException() + { + $prodEnvironment = new Environment(); + $prodEnvironment->setName('prod'); + $prodEnvironment->setProd(true); + + $uatEnvironment = new Environment(); + $uatEnvironment->setName('uat'); + $uatEnvironment->setProd(true); + + $servers = new ArrayCollection(); + + $serverOne = new VirtualServer(); + $serverOne->setEnvironment($uatEnvironment); + $servers->add($serverOne); + + $serverTwo = new VirtualServer(); + $serverTwo->setEnvironment($prodEnvironment); + $servers->add($serverTwo); + + $serverThree = new VirtualServer(); + $serverThree->setEnvironment($prodEnvironment); + $servers->add($serverThree); + + $application = new FooApplication(); + $application->setHasDatabase(true); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setEnvironment($prodEnvironment); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = [ + [ + 'method' => 'getRepository', + 'willReturn' => $this->getRepositoryMock('findAll', $servers) + ], + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => false + ], + [ + 'method' => 'getValue', + 'willReturn' => true + ], + [ + 'method' => 'getValue', + 'willReturn' => 5 + ], + ]; + + $apiServiceFunctions = []; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + + $apiService + ->expects($this->at(0)) + ->method('removeApplication') + ->willReturnCallback(function () { + throw new ClientException('This is an exception.', $this->getRequestMock()); + }); + + $task = new Task(); + $task->setType(Task::TYPE_DESTROY); + $task->setStatus(Task::STATUS_NEW); + $task->setApplicationEnvironment($applicationEnvironment); + + $provisioner = new DestroySockApplicationProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager + ); + $provisioner->setTask($task); + $provisioner->run(); + } + + public function testGetName() + { + $dataValueService = $this->getDataValueServiceMock(); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock(); + $entityManager = $this->getEntityManagerMock(); + $provisioner = new DestroySockApplicationProvisioner($dataValueService, $taskLoggerService, $apiService, $entityManager); + $this->assertEquals($provisioner->getName(), 'Sock application'); + } + + protected function getProvisionerClass() + { + DestroySockApplicationProvisioner::class; + } + +} diff --git a/Tests/Provisioner/DestroySockDatabaseProvisionerTest.php b/Tests/Provisioner/DestroySockDatabaseProvisionerTest.php new file mode 100644 index 0000000..00b7785 --- /dev/null +++ b/Tests/Provisioner/DestroySockDatabaseProvisionerTest.php @@ -0,0 +1,210 @@ +setName('prod'); + $prodEnvironment->setProd(true); + + $uatEnvironment = new Environment(); + $uatEnvironment->setName('uat'); + $uatEnvironment->setProd(true); + + $servers = new ArrayCollection(); + + $serverOne = new VirtualServer(); + $serverOne->setEnvironment($uatEnvironment); + $servers->add($serverOne); + + $serverTwo = new VirtualServer(); + $serverTwo->setEnvironment($prodEnvironment); + $servers->add($serverTwo); + + $serverThree = new VirtualServer(); + $serverThree->setEnvironment($prodEnvironment); + $servers->add($serverThree); + + $application = new FooApplication(); + $application->setHasDatabase(true); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setEnvironment($prodEnvironment); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = [ + [ + 'method' => 'getRepository', + 'willReturn' => $this->getRepositoryMock('findAll', $servers) + ], + [ + 'method' => 'persist', + 'willReturn' => null + ], + [ + 'method' => 'flush', + 'willReturn' => null + ] + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => null + ], + [ + 'method' => 'getValue', + 'willReturn' => true + ], + [ + 'method' => 'getValue', + 'willReturn' => 5 + ], + [ + 'method' => 'storeValue', + 'willReturn' => null + ], + ]; + + $apiServiceFunctions = [ + [ + 'method' => 'removeDatabase', + 'willReturn' => null + ] + ]; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + + $task = new Task(); + $task->setType(Task::TYPE_DESTROY); + $task->setStatus(Task::STATUS_NEW); + $task->setApplicationEnvironment($applicationEnvironment); + + $provisioner = new DestroySockDatabaseProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager + ); + $provisioner->setTask($task); + $provisioner->run(); + } + + /** + * @expectedException \DigipolisGent\Domainator9k\CoreBundle\Exception\LoggedException + */ + public function testOnDestroyWithException() + { + $prodEnvironment = new Environment(); + $prodEnvironment->setName('prod'); + $prodEnvironment->setProd(true); + + $uatEnvironment = new Environment(); + $uatEnvironment->setName('uat'); + $uatEnvironment->setProd(true); + + $servers = new ArrayCollection(); + + $serverOne = new VirtualServer(); + $serverOne->setEnvironment($uatEnvironment); + $servers->add($serverOne); + + $serverTwo = new VirtualServer(); + $serverTwo->setEnvironment($prodEnvironment); + $servers->add($serverTwo); + + $serverThree = new VirtualServer(); + $serverThree->setEnvironment($prodEnvironment); + $servers->add($serverThree); + + $application = new FooApplication(); + $application->setHasDatabase(true); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setEnvironment($prodEnvironment); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = [ + [ + 'method' => 'getRepository', + 'willReturn' => $this->getRepositoryMock('findAll', $servers) + ], + ]; + + $dataValueServiceFunctions = [ + [ + 'method' => 'getValue', + 'willReturn' => false + ], + [ + 'method' => 'getValue', + 'willReturn' => true + ], + [ + 'method' => 'getValue', + 'willReturn' => 5 + ], + ]; + + $apiServiceFunctions = []; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + + $apiService + ->expects($this->at(0)) + ->method('removeDatabase') + ->willReturnCallback(function () { + throw new ClientException('This is an exception.', $this->getRequestMock()); + }); + + $task = new Task(); + $task->setType(Task::TYPE_DESTROY); + $task->setStatus(Task::STATUS_NEW); + $task->setApplicationEnvironment($applicationEnvironment); + + $provisioner = new DestroySockDatabaseProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager + ); + $provisioner->setTask($task); + $provisioner->run(); + } + + public function testGetName() + { + $dataValueService = $this->getDataValueServiceMock(); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock(); + $entityManager = $this->getEntityManagerMock(); + $provisioner = new DestroySockDatabaseProvisioner($dataValueService, $taskLoggerService, $apiService, $entityManager); + $this->assertEquals($provisioner->getName(), 'Sock database'); + } + + protected function getProvisionerClass() + { + DestroySockDatabaseProvisioner::class; + } + +} diff --git a/Tests/Provisioner/PollingProvisionerTest.php b/Tests/Provisioner/PollingProvisionerTest.php new file mode 100644 index 0000000..1b35f85 --- /dev/null +++ b/Tests/Provisioner/PollingProvisionerTest.php @@ -0,0 +1,156 @@ +setName('prod'); + $prodEnvironment->setProd(true); + + $uatEnvironment = new Environment(); + $uatEnvironment->setName('uat'); + $uatEnvironment->setProd(true); + + $servers = new ArrayCollection(); + + $serverOne = new VirtualServer(); + $serverOne->setEnvironment($uatEnvironment); + $servers->add($serverOne); + + $serverTwo = new VirtualServer(); + $serverTwo->setEnvironment($prodEnvironment); + $servers->add($serverTwo); + + $serverThree = new VirtualServer(); + $serverThree->setEnvironment($prodEnvironment); + $servers->add($serverThree); + + $application = new FooApplication(); + $application->setHasDatabase(true); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setEnvironment($prodEnvironment); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = []; + + $dataValueServiceFunctions = []; + + $apiServiceFunctions = []; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + + $task = new Task(); + $task->setType(Task::TYPE_BUILD); + $task->setStatus(Task::STATUS_NEW); + $task->setProvisioners([BuildSockAccountProvisioner::class]); + $task->setApplicationEnvironment($applicationEnvironment); + + $sockPoller = $this->getMockBuilder(SockPollerService::class)->setConstructorArgs([$taskLoggerService, $apiService])->getMock(); + $sockPoller->expects($this->once())->method('doPolling')->with($task); + + $provisioner = new PollingProvisioner($dataValueService, $taskLoggerService, $apiService, $entityManager, $sockPoller); + $provisioner->setTask($task); + $provisioner->run(); + } + + /** + * @expectedException \DigipolisGent\Domainator9k\CoreBundle\Exception\LoggedException + */ + public function testPollingFailed() + { + $prodEnvironment = new Environment(); + $prodEnvironment->setName('prod'); + $prodEnvironment->setProd(true); + + $uatEnvironment = new Environment(); + $uatEnvironment->setName('uat'); + $uatEnvironment->setProd(true); + + $servers = new ArrayCollection(); + + $serverOne = new VirtualServer(); + $serverOne->setEnvironment($uatEnvironment); + $servers->add($serverOne); + + $serverTwo = new VirtualServer(); + $serverTwo->setEnvironment($prodEnvironment); + $servers->add($serverTwo); + + $serverThree = new VirtualServer(); + $serverThree->setEnvironment($prodEnvironment); + $servers->add($serverThree); + + $application = new FooApplication(); + $application->setHasDatabase(true); + + $applicationEnvironment = new ApplicationEnvironment(); + $applicationEnvironment->setEnvironment($prodEnvironment); + $applicationEnvironment->setApplication($application); + + $entityManagerFunctions = []; + + $dataValueServiceFunctions = []; + + $apiServiceFunctions = []; + + $dataValueService = $this->getDataValueServiceMock($dataValueServiceFunctions); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock($apiServiceFunctions); + $entityManager = $this->getEntityManagerMock($entityManagerFunctions); + + $task = new Task(); + $task->setType(Task::TYPE_BUILD); + $task->setStatus(Task::STATUS_NEW); + $task->setProvisioners([BuildSockAccountProvisioner::class]); + $task->setApplicationEnvironment($applicationEnvironment); + + $sockPoller = $this->getMockBuilder(SockPollerService::class)->setConstructorArgs([$taskLoggerService, $apiService])->getMock(); + $sockPoller->expects($this->once())->method('doPolling') + ->with($task) + ->willThrowException(new \Exception()); + + $provisioner = new PollingProvisioner($dataValueService, $taskLoggerService, $apiService, $entityManager, $sockPoller); + $provisioner->setTask($task); + $provisioner->run(); + } + + public function testGetName() + { + $dataValueService = $this->getDataValueServiceMock(); + $taskLoggerService = $this->getTaskLoggerServiceMock(); + $apiService = $this->getApiServiceMock(); + $entityManager = $this->getEntityManagerMock(); + $sockPoller = new SockPollerService($taskLoggerService, $apiService); + $provisioner = new PollingProvisioner( + $dataValueService, + $taskLoggerService, + $apiService, + $entityManager, + $sockPoller + ); + $this->assertEquals($provisioner->getName(), 'Polling for sock accounts, applications and databases'); + } + + protected function getProvisionerClass() + { + return PollingProvisioner::class; + } +} From 617b6f8678978e3920cbd8cc7cf4f6a9a04e1057 Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Mon, 10 Sep 2018 14:55:53 +0200 Subject: [PATCH 2/5] Try to fix travis, add PHP 7.2 to tests. --- .travis.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7a73aa6..d0a08ec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,14 @@ language: php php: - 7.1 + - 7.2 + +before_install: + - echo "memory_limit=2G" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini install: - composer self-update - - composer install + - COMPOSER_MEMORY_LIMIT=-1 travis_retry composer install --prefer-dist --no-interaction before_script: - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter @@ -16,4 +20,4 @@ script: - vendor/bin/phpunit --disallow-test-output --strict-coverage -d error_reporting=-1 --coverage-clover=build/logs/clover.xml Tests after_script: - - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT \ No newline at end of file + - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT From 59406693a5035bb34fc5b6c1f8937018f755ab9c Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Mon, 10 Sep 2018 15:03:23 +0200 Subject: [PATCH 3/5] Codeclimate fixes. --- Provisioner/AbstractSockProvisioner.php | 4 ++-- Provisioner/BuildSockAccountProvisioner.php | 6 +++--- Service/SockPollerService.php | 6 ++++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Provisioner/AbstractSockProvisioner.php b/Provisioner/AbstractSockProvisioner.php index ec7a53c..1556c4a 100644 --- a/Provisioner/AbstractSockProvisioner.php +++ b/Provisioner/AbstractSockProvisioner.php @@ -22,8 +22,8 @@ protected function ensurePollingProvisioner() { $provisioners = $this->task->getProvisioners(); if ($provisioners && !in_array(PollingProvisioner::class, $provisioners)) { - $provisioners[] = PollingProvisioner::class; - $this->task->setProvisioners($provisioners); + $provisioners[] = PollingProvisioner::class; + $this->task->setProvisioners($provisioners); } } } diff --git a/Provisioner/BuildSockAccountProvisioner.php b/Provisioner/BuildSockAccountProvisioner.php index bdc4645..4be6bb4 100644 --- a/Provisioner/BuildSockAccountProvisioner.php +++ b/Provisioner/BuildSockAccountProvisioner.php @@ -121,12 +121,12 @@ protected function createSockAccount(ApplicationEnvironment $appEnv, VirtualServ if ($parentApplication) { $environment = $appEnv->getEnvironment(); - $parentApplicationEnvironment = $this->entityManager + $parentAppEnv = $this->entityManager ->getRepository(ApplicationEnvironment::class) ->findOneBy(['application' => $parentApplication, 'environment' => $environment]); - $sockAccountId = $this->dataValueService->getValue($parentApplicationEnvironment, 'sock_account_id'); - $username = $this->dataValueService->getValue($parentApplicationEnvironment, 'sock_ssh_user'); + $sockAccountId = $this->dataValueService->getValue($parentAppEnv, 'sock_account_id'); + $username = $this->dataValueService->getValue($parentAppEnv, 'sock_ssh_user'); if (!$sockAccountId || !$username) { throw new \Exception('The parent application must be build first.'); diff --git a/Service/SockPollerService.php b/Service/SockPollerService.php index 0a53e7e..00d70d3 100644 --- a/Service/SockPollerService.php +++ b/Service/SockPollerService.php @@ -5,7 +5,8 @@ use DigipolisGent\Domainator9k\CoreBundle\Entity\Task; use DigipolisGent\Domainator9k\CoreBundle\Service\TaskLoggerService; -class SockPollerService { +class SockPollerService +{ protected $taskLoggerService; protected $apiService; @@ -22,7 +23,8 @@ public function __construct(TaskLoggerService $taskLoggerService, ApiService $ap $this->polling = []; } - public function addPolling($type, $id, Task $task) { + public function addPolling($type, $id, Task $task) + { $this->polling[$task->getId()][$type] = $id; } From b6e03a66a3268655dd52ab4cb614315ff72cb92f Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Tue, 11 Sep 2018 15:56:02 +0200 Subject: [PATCH 4/5] Fix use statements. --- FieldType/DatabaseHostFieldType.php | 5 ----- FieldType/SshKeyChoiceFieldType.php | 1 - Provisioner/AbstractSockProvisioner.php | 7 ------- Tests/Provisioner/BuildSockAccountProvisionerTest.php | 1 - Tests/Provisioner/BuildSockApplicationProvisionerTest.php | 1 - 5 files changed, 15 deletions(-) diff --git a/FieldType/DatabaseHostFieldType.php b/FieldType/DatabaseHostFieldType.php index fcd2355..9fec4fe 100644 --- a/FieldType/DatabaseHostFieldType.php +++ b/FieldType/DatabaseHostFieldType.php @@ -4,13 +4,8 @@ namespace DigipolisGent\Domainator9k\SockBundle\FieldType; use DigipolisGent\Domainator9k\CoreBundle\Entity\ApplicationEnvironment; -use DigipolisGent\Domainator9k\CoreBundle\Entity\VirtualServer; -use DigipolisGent\SettingBundle\FieldType\BooleanFieldType; use DigipolisGent\SettingBundle\FieldType\StringFieldType; use DigipolisGent\SettingBundle\Service\DataValueService; -use Doctrine\ORM\EntityManagerInterface; -use Symfony\Component\Validator\Constraints\Callback; -use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * Class ManageSockFieldType diff --git a/FieldType/SshKeyChoiceFieldType.php b/FieldType/SshKeyChoiceFieldType.php index a82067e..5399d1d 100644 --- a/FieldType/SshKeyChoiceFieldType.php +++ b/FieldType/SshKeyChoiceFieldType.php @@ -3,7 +3,6 @@ namespace DigipolisGent\Domainator9k\SockBundle\FieldType; -use DigipolisGent\Domainator9k\CiTypes\JenkinsBundle\Entity\GroovyScript; use DigipolisGent\Domainator9k\CoreBundle\Entity\ApplicationEnvironment; use DigipolisGent\Domainator9k\SockBundle\Service\ApiService; use DigipolisGent\SettingBundle\FieldType\AbstractFieldType; diff --git a/Provisioner/AbstractSockProvisioner.php b/Provisioner/AbstractSockProvisioner.php index 1556c4a..31d55e4 100644 --- a/Provisioner/AbstractSockProvisioner.php +++ b/Provisioner/AbstractSockProvisioner.php @@ -2,14 +2,7 @@ namespace DigipolisGent\Domainator9k\SockBundle\Provisioner; -use DigipolisGent\Domainator9k\CoreBundle\Entity\ApplicationEnvironment; -use DigipolisGent\Domainator9k\CoreBundle\Entity\VirtualServer; -use DigipolisGent\Domainator9k\CoreBundle\Exception\LoggedException; use DigipolisGent\Domainator9k\CoreBundle\Provisioner\AbstractProvisioner; -use DigipolisGent\Domainator9k\CoreBundle\Service\TaskLoggerService; -use DigipolisGent\Domainator9k\SockBundle\Service\ApiService; -use DigipolisGent\SettingBundle\Service\DataValueService; -use Doctrine\ORM\EntityManagerInterface; /** * Class AbstractSockProvisioner diff --git a/Tests/Provisioner/BuildSockAccountProvisionerTest.php b/Tests/Provisioner/BuildSockAccountProvisionerTest.php index 8835d5a..60fca1b 100644 --- a/Tests/Provisioner/BuildSockAccountProvisionerTest.php +++ b/Tests/Provisioner/BuildSockAccountProvisionerTest.php @@ -7,7 +7,6 @@ use DigipolisGent\Domainator9k\CoreBundle\Entity\Environment; use DigipolisGent\Domainator9k\CoreBundle\Entity\Task; use DigipolisGent\Domainator9k\CoreBundle\Entity\VirtualServer; -use DigipolisGent\Domainator9k\SockBundle\Provisioner\BuildProvisioner; use DigipolisGent\Domainator9k\SockBundle\Provisioner\BuildSockAccountProvisioner; use DigipolisGent\Domainator9k\SockBundle\Service\SockPollerService; use DigipolisGent\Domainator9k\SockBundle\Tests\Fixtures\FooApplication; diff --git a/Tests/Provisioner/BuildSockApplicationProvisionerTest.php b/Tests/Provisioner/BuildSockApplicationProvisionerTest.php index be0990b..76edb70 100644 --- a/Tests/Provisioner/BuildSockApplicationProvisionerTest.php +++ b/Tests/Provisioner/BuildSockApplicationProvisionerTest.php @@ -7,7 +7,6 @@ use DigipolisGent\Domainator9k\CoreBundle\Entity\Environment; use DigipolisGent\Domainator9k\CoreBundle\Entity\Task; use DigipolisGent\Domainator9k\CoreBundle\Entity\VirtualServer; -use DigipolisGent\Domainator9k\SockBundle\Provisioner\BuildProvisioner; use DigipolisGent\Domainator9k\SockBundle\Provisioner\BuildSockApplicationProvisioner; use DigipolisGent\Domainator9k\SockBundle\Service\SockPollerService; use DigipolisGent\Domainator9k\SockBundle\Tests\Fixtures\FooApplication; From e0a523bf56b9e4d892c2427c1e29ea240d6bed3d Mon Sep 17 00:00:00 2001 From: Jelle Sebreghts Date: Wed, 12 Sep 2018 16:46:33 +0200 Subject: [PATCH 5/5] Move the polling type constant to the provisionsers themselves. --- Provisioner/BuildSockAccountProvisioner.php | 5 +++-- Provisioner/BuildSockApplicationProvisioner.php | 3 ++- Provisioner/BuildSockDatabaseProvisioner.php | 3 ++- Provisioner/DestroySockAccountProvisioner.php | 2 +- Service/SockPollerService.php | 4 ---- Tests/Provisioner/BuildSockAccountProvisionerTest.php | 1 - Tests/Provisioner/BuildSockApplicationProvisionerTest.php | 1 - Tests/Provisioner/BuildSockDatabaseProvisionerTest.php | 1 - Tests/Provisioner/DestroySockAccountProvisionerTest.php | 1 - Tests/Provisioner/DestroySockApplicationProvisionerTest.php | 1 - Tests/Provisioner/DestroySockDatabaseProvisionerTest.php | 1 - Tests/Provisioner/PollingProvisionerTest.php | 1 - 12 files changed, 8 insertions(+), 16 deletions(-) diff --git a/Provisioner/BuildSockAccountProvisioner.php b/Provisioner/BuildSockAccountProvisioner.php index 4be6bb4..b5c0c51 100644 --- a/Provisioner/BuildSockAccountProvisioner.php +++ b/Provisioner/BuildSockAccountProvisioner.php @@ -12,12 +12,13 @@ use Doctrine\ORM\EntityManagerInterface; /** - * Class BuildProvisioner + * Class BuildSockAccountProvisioner * * @package DigipolisGent\Domainator9k\SockBundle\Provisioner */ class BuildSockAccountProvisioner extends AbstractSockProvisioner { + const POLLING_TYPE = 'accounts'; /** * @var ApiService @@ -88,7 +89,7 @@ public function doRun() try { $accountId = $this->createSockAccount($appEnv, $server); if ($accountId) { - $this->sockPoller->addPolling(SockPollerService::POLLING_TYPE_ACCOUNT, $accountId, $this->task); + $this->sockPoller->addPolling(static::POLLING_TYPE, $accountId, $this->task); $this->ensurePollingProvisioner(); } } catch (\Exception $ex) { diff --git a/Provisioner/BuildSockApplicationProvisioner.php b/Provisioner/BuildSockApplicationProvisioner.php index 708a66d..d34d7fb 100644 --- a/Provisioner/BuildSockApplicationProvisioner.php +++ b/Provisioner/BuildSockApplicationProvisioner.php @@ -18,6 +18,7 @@ */ class BuildSockApplicationProvisioner extends AbstractSockProvisioner { + const POLLING_TYPE = 'applications'; /** * @var ApiService @@ -88,7 +89,7 @@ public function doRun() try { $appId = $this->createSockApplication($appEnv); if ($appId) { - $this->sockPoller->addPolling(SockPollerService::POLLING_TYPE_APPLICATION, $appId, $this->task); + $this->sockPoller->addPolling(static::POLLING_TYPE, $appId, $this->task); $this->ensurePollingProvisioner(); } } catch (\Exception $ex) { diff --git a/Provisioner/BuildSockDatabaseProvisioner.php b/Provisioner/BuildSockDatabaseProvisioner.php index b917620..0945434 100644 --- a/Provisioner/BuildSockDatabaseProvisioner.php +++ b/Provisioner/BuildSockDatabaseProvisioner.php @@ -18,6 +18,7 @@ */ class BuildSockDatabaseProvisioner extends AbstractSockProvisioner { + const POLLING_TYPE = 'databases'; /** * @var ApiService @@ -88,7 +89,7 @@ public function doRun() try { $dbId = $this->createSockDatabase($appEnv); if ($dbId) { - $this->sockPoller->addPolling(SockPollerService::POLLING_TYPE_DATABASE, $dbId, $this->task); + $this->sockPoller->addPolling(static::POLLING_TYPE, $dbId, $this->task); $this->ensurePollingProvisioner(); } } catch (\Exception $ex) { diff --git a/Provisioner/DestroySockAccountProvisioner.php b/Provisioner/DestroySockAccountProvisioner.php index 6e64c99..37518ca 100644 --- a/Provisioner/DestroySockAccountProvisioner.php +++ b/Provisioner/DestroySockAccountProvisioner.php @@ -13,7 +13,7 @@ use GuzzleHttp\Exception\ClientException; /** - * Class BuildProvisioner + * Class DestroySockAccountProvisioner * * @package DigipolisGent\Domainator9k\SockBundle\Provisioner */ diff --git a/Service/SockPollerService.php b/Service/SockPollerService.php index 00d70d3..0a0db0c 100644 --- a/Service/SockPollerService.php +++ b/Service/SockPollerService.php @@ -12,10 +12,6 @@ class SockPollerService protected $apiService; protected $polling; - const POLLING_TYPE_ACCOUNT = 'accounts'; - const POLLING_TYPE_APPLICATION = 'applications'; - const POLLING_TYPE_DATABASE = 'databases'; - public function __construct(TaskLoggerService $taskLoggerService, ApiService $apiService) { $this->taskLoggerService = $taskLoggerService; diff --git a/Tests/Provisioner/BuildSockAccountProvisionerTest.php b/Tests/Provisioner/BuildSockAccountProvisionerTest.php index 60fca1b..52abc71 100644 --- a/Tests/Provisioner/BuildSockAccountProvisionerTest.php +++ b/Tests/Provisioner/BuildSockAccountProvisionerTest.php @@ -1,6 +1,5 @@