Skip to content

Commit

Permalink
chore(tests): simplifies compute system test logic (#4047)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored May 25, 2021
1 parent d36f2c8 commit 0df2ac3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 71 deletions.
50 changes: 18 additions & 32 deletions Compute/tests/System/V1/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Google\Cloud\Compute\Tests\System\V1;

use Google\ApiCore\ApiException;
use Google\Cloud\Compute\V1\Address;
use Google\Cloud\Compute\V1\AddressesClient;
use Google\Cloud\Compute\V1\RegionOperationsClient;
Expand All @@ -29,7 +30,6 @@ class AddressTest extends SystemTestCase
protected static $projectId;
protected static $name;
protected static $regionOperationsClient;
private static $dirty;

public static function setUpBeforeClass(): void
{
Expand All @@ -39,50 +39,29 @@ public static function setUpBeforeClass(): void
}
self::$addressesClient = new AddressesClient();
self::$regionOperationsClient = new RegionOperationsClient();
}

public function setUp(): void
{
self::$name = "gapicphp" . strval(rand(100000, 999999));
self::$dirty = false;
}

public function tearDown(): void
{
if (self::$dirty == true) {
self::$addressesClient->delete(self::$name, self::$projectId, self::REGION);
}
}

public static function tearDownAfterClass(): void
{
self::$addressesClient->close();
}

private function insertAddress(): void
public function testInsert(): void
{
$addressResource = new Address();
$addressResource->setName(self::$name);
$op = self::$addressesClient->insert($addressResource, self::$projectId, self::REGION);
$this->waitForRegionalOp($op);
self::$dirty = true;
}

private function waitForRegionalOp($operation): void
{
self::$regionOperationsClient->wait($operation->getName(), self::$projectId, self::REGION);
}

public function testInsert(): void
{
$this->insertAddress();
self::$regionOperationsClient->wait($op->getName(), self::$projectId, self::REGION);
$address = self::$addressesClient->get(self::$name, self::$projectId, self::REGION);
self::assertEquals($address->getName(), self::$name);
$this->assertEquals($address->getName(), self::$name);
}

/**
* @depends testInsert
*/
public function testList(): void
{
$this->insertAddress();
$presented = false;
$addressList = self::$addressesClient->list(self::$projectId, self::REGION);
foreach ($addressList->iterateAllElements() as $element) {
Expand All @@ -91,14 +70,21 @@ public function testList(): void
$presented = true;
}
}
self::assertEquals(true, $presented);
$this->assertEquals(true, $presented);
}

/**
* @depends testInsert
*/
public function testDelete(): void
{
$this->insertAddress();
$op = self::$addressesClient->delete(self::$name, self::$projectId, self::REGION);
$this->waitForRegionalOp($op);
self::$dirty = false;
self::$regionOperationsClient->wait($op->getName(), self::$projectId, self::REGION);
try {
self::$addressesClient->get(self::$name, self::$projectId, self::REGION);
$this->fail('The deleted instance still exists');
} catch (ApiException $e) {
$this->assertEquals(404, $e->getCode());
}
}
}
73 changes: 34 additions & 39 deletions Compute/tests/System/V1/SmokeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class SmokeTest extends SystemTestCase
protected static $machineType;
protected static $name;
protected static $zoneOperationsClient;
private static $dirty;


public static function setUpBeforeClass(): void
{
Expand All @@ -59,27 +57,15 @@ public static function setUpBeforeClass(): void
self::$projectId,
self::ZONE
);
}

public function setUp(): void
{
self::$name = "gapicphp" . strval(rand(100000, 999999));
self::$dirty = false;
}

public function tearDown(): void
{
if (self::$dirty == true) {
self::$instancesClient->delete(self::$name, self::$projectId, self::ZONE);
}
}

public static function tearDownAfterClass(): void
{
self::$instancesClient->close();
}

private function insertInstance(): void
public function testInsertInstance(): void
{
$disk = new AttachedDisk([
'boot' => true,
Expand All @@ -104,30 +90,33 @@ private function insertInstance(): void
self::$projectId,
self::ZONE
);
$this->waitForZonalOp($operation);
self::$dirty = true;
}

public function testInsertInstance()
{
$this->insertInstance();
self::$zoneOperationsClient->wait($operation->getName(), self::$projectId, self::ZONE);

$instance = self::$instancesClient->get(
self::$name,
self::$projectId,
self::ZONE
);
self::assertEquals(self::$name, $instance->getName());
self::assertEquals(self::$machineType, $instance->getMachineType());
$this->assertEquals(self::$name, $instance->getName());
$this->assertEquals(self::$machineType, $instance->getMachineType());
}

/**
* @depends testInsertInstance
*/
public function testPatchInstance()
{
$shieldedInstanceConfigResource = new ShieldedInstanceConfig();
$shieldedInstanceConfigResource->setEnableSecureBoot(true);
$this->insertInstance();

self::$instancesClient->stop(self::$name, self::$projectId, self::ZONE);
while (true){
$instance = $this->getInstance();
$instance = self::$instancesClient->get(
self::$name,
self::$projectId,
self::ZONE
);
if ($instance->getStatus() == Instance\Status::TERMINATED) {
break;
}
Expand All @@ -139,28 +128,34 @@ public function testPatchInstance()
} catch (ApiException $e) {
$this->fail("update method failed" . $e->getMessage());
}
$this->waitForZonalOp($op);
$instance = $this->getInstance();
self::assertEquals(true, $instance->getShieldedInstanceConfig()->getEnableSecureBoot());

self::$zoneOperationsClient->wait($op->getName(), self::$projectId, self::ZONE);

$instance = self::$instancesClient->get(
self::$name,
self::$projectId,
self::ZONE
);

$this->assertEquals(true, $instance->getShieldedInstanceConfig()->getEnableSecureBoot());
}

private function waitForZonalOp($operation): void
/**
* @depends testInsertInstance
*/
public function testDeleteInstance(): void
{
$op = self::$instancesClient->delete(self::$name, self::$projectId, self::ZONE);
self::$zoneOperationsClient->wait($op->getName(), self::$projectId, self::ZONE);

try {
self::$zoneOperationsClient->wait($operation->getName(), self::$projectId, self::ZONE);
self::$instancesClient->get(self::$name, self::$projectId, self::ZONE);
$this->fail('The deleted instance still exists');
} catch (ApiException $e) {
$this->fail("Wait on zonal operation failed" . $e->getMessage());
$this->assertEquals(404, $e->getCode());
}
}

private function getInstance(): Instance
{
return self::$instancesClient->get(
self::$name,
self::$projectId,
self::ZONE
);
}
public function testAPIError()
{
$this->expectException(ApiException::class);
Expand Down

0 comments on commit 0df2ac3

Please sign in to comment.