From 3142fc9bfd6d3a0041fd6fd5ba7cfb816e59dee8 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 5 Jun 2019 14:40:36 -0700 Subject: [PATCH] Adds "release_version" flag to gcloudWrapper::deploy (#64) --- src/TestUtils/GcloudWrapper.php | 53 ++++++++++++++++++---------- test/TestUtils/GcloudWrapperTest.php | 37 +++++++++++++++++++ 2 files changed, 72 insertions(+), 18 deletions(-) diff --git a/src/TestUtils/GcloudWrapper.php b/src/TestUtils/GcloudWrapper.php index bb48dc2..d021edd 100644 --- a/src/TestUtils/GcloudWrapper.php +++ b/src/TestUtils/GcloudWrapper.php @@ -112,16 +112,34 @@ public function setDir($dir) /** * Deploy the app to the Google Cloud Platform. * - * @param string $targets optional The yaml files for deployments. - * @param bool $promote optional true if you want to promote the new app. - * @param int $retries optional Number of retries upon failure. + * @param array $options list of options + * $targets string The yaml files for deployments. + * $promote bool True if you want to promote the new app. + * $retries int Number of retries upon failure. + * $release_version string Run using "alpha" or "beta" version of gcloud deploy * @return bool true if deployment suceeds, false upon failure. */ - public function deploy( - $targets = 'app.yaml', - $promote = false, - $retries = self::DEFAULT_RETRIES - ) { + public function deploy($options = []) + { + // Handle old function signature + if (!is_array($options)) { + $options = array_filter([ + 'targets' => @func_get_arg(0), + 'promote' => @func_get_arg(1), + ]) + array_filter([ + 'retries' => @func_get_arg(2), + ], 'is_numeric'); + } + $options = array_merge([ + 'targets' => 'app.yaml', + 'promote' => false, + 'retries' => self::DEFAULT_RETRIES, + 'release_version' => null, + ], $options); + if (!in_array($options['release_version'], [null, 'alpha', 'beta'])) { + $this->errorLog('release_version must be "alpha" or "beta"'); + return false; + } if ($this->deployed) { $this->errorLog('The app has been already deployed.'); return false; @@ -131,16 +149,15 @@ public function deploy( $this->errorLog('Can not chdir to ' . $this->dir); return false; } - $cmd = "gcloud -q " . self::GCLOUD_APP . " deploy " - . "--project " . $this->project . " " - . "--version " . $this->version . " "; - if ($promote) { - $cmd .= "--promote "; - } else { - $cmd .= "--no-promote "; - } - $cmd .= $targets; - $ret = $this->execWithRetry($cmd, $retries); + $cmd = sprintf('gcloud -q %s%s deploy --project %s --version %s %s %s', + $options['release_version'] ? $options['release_version'] . ' ' : '', + self::GCLOUD_APP, + $this->project, + $this->version, + $options['promote'] ? '--promote' : '--no-promote', + $options['targets'] + ); + $ret = $this->execWithRetry($cmd, $options['retries']); chdir($orgDir); if ($ret) { $this->deployed = true; diff --git a/test/TestUtils/GcloudWrapperTest.php b/test/TestUtils/GcloudWrapperTest.php index 77d70dc..c6c63a9 100644 --- a/test/TestUtils/GcloudWrapperTest.php +++ b/test/TestUtils/GcloudWrapperTest.php @@ -110,6 +110,43 @@ public function testDeployAndDeleteWithCustomArgs() $this->mockGcloudWrapper->delete('myservice', 4); } + public function testDeployWithArgsArray() + { + $this->mockGcloudWrapper = $this->getMockBuilder( + '\Google\Cloud\TestUtils\GcloudWrapper' + ) + ->setMethods(array('execWithRetry')) + ->setConstructorArgs(array('project', 'version')) + ->getMock(); + $this->mockGcloudWrapper->method('execWithRetry')->willReturn(true); + $deployCmd = 'gcloud -q beta app deploy ' + . '--project project --version version ' + . '--promote app.yaml backend.yaml'; + $deleteCmd = 'gcloud -q app versions delete ' + . '--service myservice version ' + . '--project project'; + $this->mockGcloudWrapper->expects($this->exactly(2)) + ->method('execWithRetry') + ->withConsecutive( + array($this->equalTo($deployCmd), $this->equalTo(4)), + array($this->equalTo($deleteCmd), $this->equalTo(4)) + ); + + $this->mockGcloudWrapper->deploy([ + 'targets' => 'app.yaml backend.yaml', + 'promote' => true, + 'retries' => 4, + 'release_version' => 'beta', + ]); + + $this->assertEquals( + 'https://version-dot-project.appspot.com', + $this->mockGcloudWrapper->getBaseUrl() + ); + + $this->mockGcloudWrapper->delete('myservice', 4); + } + public function testRunAndStopWithDefault() { $this->mockGcloudWrapper = $this->getMockBuilder(