Skip to content

Commit

Permalink
Merge pull request #49 from php-opencloud/refactor
Browse files Browse the repository at this point in the history
add layer-3 integration tests; add fixes
  • Loading branch information
Jamie Hannaford committed Apr 11, 2016
2 parents dea5adc + 2066717 commit 79b59e0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
5 changes: 3 additions & 2 deletions samples/networking/v2/floatingIPs/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
'region' => '{region}',
'user' => [
'id' => '{userId}',
'password' => '{password}'
'password' => '{password}',
],
'scope' => ['project' => ['id' => '{projectId}']]
'scope' => ['project' => ['id' => '{projectId}']],
]);

$networking = $openstack->networkingV2ExtLayer3();
Expand All @@ -18,4 +18,5 @@
$ip = $networking->createFloatingIp([
"floatingNetworkId" => "{networkId}",
"portId" => "{portId}",
'fixedIpAddress' => '{fixedIpAddress}',
]);
2 changes: 1 addition & 1 deletion src/Networking/v2/Extensions/Layer3/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function putFloatingIp(): array
'jsonKey' => 'floatingip',
'params' => [
'id' => $this->params->idPath(),
'floatingNetworkId' => $this->params->floatingNetworkIdJson(),
'floatingNetworkId' => $this->notRequired($this->params->floatingNetworkIdJson()),
'fixedIpAddress' => $this->params->fixedIpAddressJson(),
'floatingIpAddress' => $this->params->floatingIpAddressJson(),
'portId' => $this->params->portIdJson(),
Expand Down
5 changes: 5 additions & 0 deletions src/Networking/v2/Extensions/Layer3/Models/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OpenStack\Networking\v2\Extensions\Layer3\Models;

use OpenCloud\Common\Resource\HasWaiterTrait;
use OpenCloud\Common\Resource\OperatorResource;
use OpenCloud\Common\Resource\Creatable;
use OpenCloud\Common\Resource\Deletable;
Expand All @@ -15,6 +16,8 @@
*/
class Router extends OperatorResource implements Listable, Creatable, Retrievable, Updateable, Deletable
{
use HasWaiterTrait;

/** @var string */
public $status;

Expand All @@ -36,6 +39,8 @@ class Router extends OperatorResource implements Listable, Creatable, Retrievabl
/** @var string */
public $id;

protected $resourceKey = 'router';

protected $aliases = [
'external_gateway_info' => 'externalGatewayInfo',
'admin_state_up' => 'adminStateUp',
Expand Down
83 changes: 66 additions & 17 deletions tests/integration/Networking/v2/Layer3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OpenStack\Integration\Networking\v2;

use Guzzle\Tests\Service\Mock\Command\Sub\Sub;
use OpenCloud\Integration\TestCase;
use OpenStack\Networking\v2\Extensions\Layer3\Models\FloatingIp;
use OpenStack\Networking\v2\Models\Network;
Expand Down Expand Up @@ -36,50 +37,89 @@ public function teardown()
$this->deleteItems($this->getService()->listFloatingIps());
}

private function createNetwork(): Network
private function createNetwork(bool $routerAccessible = true): Network
{
$network = $this->getV2Service()->createNetwork(['name' => $this->randomStr(), 'routerAccessible' => true]);
$network = $this->getV2Service()->createNetwork([
'name' => $this->randomStr(),
'routerAccessible' => $routerAccessible,
]);
$network->waitUntilActive();
return $network;
}

private function createSubnet(Network $network): Subnet
private function createSubnet(Network $network, string $cidr = '192.168.199.0/24'): Subnet
{
return $this->getV2Service()->createSubnet([
'networkId' => $network->id,
'name' => $this->randomStr(),
'ipVersion' => 4,
'cidr' => '192.168.199.0/24',
'cidr' => $cidr,
]);
}

private function createPort(Network $network): Port
{
return $this->getV2Service()->createPort(['networkId' => $network->id, 'name' => $this->randomStr()]);
return $this->getV2Service()->createPort([
'networkId' => $network->id,
'name' => $this->randomStr(),
]);
}

private function findSubnetIp(Port $port, Subnet $subnet): string
{
foreach ($port->fixedIps as $fixedIp) {
if ($fixedIp['subnet_id'] == $subnet->id) {
return $fixedIp['ip_address'];
}
}

return '';
}

public function floatingIps()
{
$this->logStep('Creating network');
$network = $this->createNetwork();
$this->logStep('Creating external network');
$externalNetwork = $this->createNetwork();

$this->logStep('Creating subnet for external network %id%', ['%id%' => $externalNetwork->id]);
$this->createSubnet($externalNetwork, '10.0.0.0/24');

$this->logStep('Creating subnet for network %id%', ['%id%' => $network->id]);
$this->createSubnet($network);
$this->logStep('Creating internal network');
$internalNetwork = $this->createNetwork(false);

$this->logStep('Creating port for network %id%', ['%id%' => $network->id]);
$port1 = $this->createPort($network);
$this->logStep('Creating subnet for internal network %id%', ['%id%' => $internalNetwork->id]);
$subnet = $this->createSubnet($internalNetwork);

$this->logStep('Creating router for external network %id%', ['%id%' => $externalNetwork->id]);
$router = $this->getService()->createRouter([
'name' => $this->randomStr(),
'externalGatewayInfo' => [
'networkId' => $externalNetwork->id,
'enableSnat' => true,
],
]);

$this->logStep('Create interface for subnet %subnet% and router %router%', [
'%subnet%' => $subnet->id, '%router%' => $router->id,
]);
$router->addInterface(['subnetId' => $subnet->id]);

$this->logStep('Creating port for internal network %id%', ['%id%' => $internalNetwork->id]);
$port1 = $this->createPort($internalNetwork);
$fixedIp = $this->findSubnetIp($port1, $subnet);

$replacements = [
'{networkId}' => $network->id,
'{portId}' => $port1->id,
'{networkId}' => $externalNetwork->id,
'{portId}' => $port1->id,
'{fixedIpAddress}' => $fixedIp,
];

$this->logStep('Create floating IP');
/** @var FloatingIp $ip */
$path = $this->sampleFile($replacements, 'floatingIPs/create.php');
require_once $path;
$this->assertInstanceOf(FloatingIp::class, $ip);
$this->assertEquals($network->id, $ip->floatingNetworkId);
$this->assertEquals($externalNetwork->id, $ip->floatingNetworkId);
$this->assertEquals($port1->id, $ip->portId);

$this->logStep('List floating IPs');
Expand All @@ -93,17 +133,26 @@ public function floatingIps()
$this->assertInstanceOf(FloatingIp::class, $ip);

$this->logStep('Update floating IP');
$port2 = $this->createPort($network);
$port2 = $this->createPort($internalNetwork);
$replacements['{newPortId}'] = $port2->id;
$path = $this->sampleFile($replacements, 'floatingIPs/update.php');
require_once $path;

$this->logStep('Delete floating IP');
$path = $this->sampleFile($replacements, 'floatingIPs/update.php');
$path = $this->sampleFile($replacements, 'floatingIPs/delete.php');
require_once $path;

$router->removeInterface(['subnetId' => $subnet->id]);
$router->delete();
$router->waitUntilDeleted();

$port1->delete();
$port2->delete();
$network->delete();

$internalNetwork->delete();
$internalNetwork->waitUntilDeleted();

$externalNetwork->delete();
$externalNetwork->waitUntilDeleted();
}
}

0 comments on commit 79b59e0

Please sign in to comment.