Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add network api #57

Merged
merged 2 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,152 @@ public function execInspect($exec)
)->then(array($this->parser, 'expectJson'));
}

/**
* List networks.
*
* @return PromiseInterface Promise<array>
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkList
*/
public function networkList()
{
return $this->browser->get(
$this->uri->expand(
'/networks',
array()
)
)->then(array($this->parser, 'expectJson'));
}

/**
* Inspect network.
*
* @param string $network The network id or name
*
* @return PromiseInterface Promise<array>
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkInspect
*/
public function networkInspect($network)
{
return $this->browser->get(
$this->uri->expand(
'/networks/{network}',
array(
'network' => $network
)
)
)->then(array($this->parser, 'expectJson'));
}

/**
* Remove network.
*
* @param string $network The network id or name
*
* @return PromiseInterface Promise<null>
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkRemove
*/
public function networkRemove($network)
{
return $this->browser->delete(
$this->uri->expand(
'/networks/{network}',
array(
'network' => $network
)
)
)->then(array($this->parser, 'expectEmpty'));
}

/**
* Create network.
*
* @param string $name The network name
* @param array $config (optional) The network configuration
*
* @return PromiseInterface Promise<array>
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkCreate
*/
public function networkCreate($name, $config = array())
{
$config['Name'] = $name;

return $this->postJson(
$this->uri->expand(
'/networks/create'
),
$config
)->then(array($this->parser, 'expectJson'));
}

/**
* Connect container to network
*
* @param string $network The network id or name
* @param string $container The id or name of the container to connect to network
* @param array $endpointConfig (optional) Configuration for a network endpoint
*
* @return PromiseInterface Promise<array>
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkConnect
*/
public function networkConnect($network, $container, $endpointConfig = array())
{
return $this->postJson(
$this->uri->expand(
'/networks/{network}/connect',
array(
'network' => $network
)
),
array(
'Container' => $container,
'EndpointConfig' => $endpointConfig ? json_encode($endpointConfig) : null
)
)->then(array($this->parser, 'expectJson'));
}

/**
* Disconnect container from network.
*
* @param string $network The id or name of network
* @param string $container The id or name of container to disconnect
* @param bool $force (optional) Force the disconnect
*
* @return PromiseInterface Promise<null>
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkDisconnect
*/
public function networkDisconnect($network, $container, $force = false)
{
return $this->postJson(
$this->uri->expand(
'/networks/{network}/disconnect',
array(
'network' => $network
)
),
array(
'Container' => $container,
'Force' => $this->boolArg($force)
)
)->then(array($this->parser, 'expectEmpty'));
}

/**
* Remove all unused networks.
*
* @return PromiseInterface Promise<array>
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkPrune
*/
public function networkPrune()
{
return $this->postJson(
$this->uri->expand(
'/networks/prune',
array()
),
array()
)->then(array($this->parser, 'expectJson'));
}

private function postJson($url, $data)
{
$body = $this->json($data);
Expand Down
58 changes: 58 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,64 @@ public function testExecInspect()
$this->expectPromiseResolveWith($json, $this->client->execInspect(123));
}

public function testNetworkList()
{
$json = array();
$this->expectRequestFlow('get', '/networks', $this->createResponseJson($json), 'expectJson');

$this->expectPromiseResolveWith($json, $this->client->networkList());
}

public function testNetworkInspect()
{
$json = array();
$this->expectRequestFlow('get', '/networks/123', $this->createResponseJson($json), 'expectJson');

$this->expectPromiseResolveWith($json, $this->client->networkInspect(123));
}

public function testNetworkRemove()
{
$json = array();
$this->expectRequestFlow('delete', '/networks/123', $this->createResponse(), 'expectEmpty');

$this->expectPromiseResolveWith('', $this->client->networkRemove(123));
}

public function testNetworkCreate()
{
$json = array();
$config = array();
$this->expectRequestFlow('post', '/networks/create', $this->createResponseJson($json), 'expectJson');

$this->expectPromiseResolveWith($json, $this->client->networkCreate($config));
}

public function testNetworkConnect()
{
$json = array();
$config = array();
$this->expectRequestFlow('post', '/networks/123/connect', $this->createResponseJson($json), 'expectJson');

$this->expectPromiseResolveWith($json, $this->client->networkConnect(123, $config));
}

public function testNetworkDisconnect()
{
$json = array();
$this->expectRequestFlow('post', '/networks/123/disconnect', $this->createResponse(), 'expectEmpty');

$this->expectPromiseResolveWith('', $this->client->networkDisconnect(123, 'abc'));
}

public function testNetworkPrune()
{
$json = array();
$this->expectRequestFlow('post', '/networks/prune', $this->createResponseJson($json), 'expectJson');

$this->expectPromiseResolveWith($json, $this->client->networkPrune());
}

private function expectRequestFlow($method, $url, ResponseInterface $response, $parser)
{
$return = (string)$response->getBody();
Expand Down
61 changes: 61 additions & 0 deletions tests/FunctionalClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,65 @@ public function testContainerList()

$this->loop->run();
}

/**
* @depends testImageInspectCheckIfBusyboxExists
*/
public function testCreateConnectDisconnectAndRemoveNetwork()
{
$containerConfig = array(
'Image' => 'busybox',
'Cmd' => array('echo', 'test')
);
$networkName = uniqid('reactphp-docker');

$promise = $this->client->containerCreate($containerConfig);
$container = Block\await($promise, $this->loop);

$promise = $this->client->containerStart($container['Id']);
$ret = Block\await($promise, $this->loop);

$start = microtime(true);

$promise = $this->client->networkCreate($networkName);
$network = Block\await($promise, $this->loop);

$this->assertNotNull($network['Id']);
$this->assertEquals('', $network['Warning']);

$promise = $this->client->networkConnect($network['Id'], $container['Id']);
$ret = Block\await($promise, $this->loop);

$this->assertEquals('', $ret);

$promise = $this->client->networkDisconnect($network['Id'], $container['Id'], false);
$ret = Block\await($promise, $this->loop);

$this->assertEquals('', $ret);

$promise = $this->client->networkRemove($network['Id']);
$ret = Block\await($promise, $this->loop);

$this->assertEquals('', $ret);

$end = microtime(true);

$promise = $this->client->containerStop($container['Id']);
$ret = Block\await($promise, $this->loop);

$promise = $this->client->containerRemove($container['Id']);
$ret = Block\await($promise, $this->loop);

// get all events between starting and removing for this container
$promise = $this->client->events($start, $end, array('network' => array($network['Id'])));
$ret = Block\await($promise, $this->loop);

// expects "create", "connect", "disconnect", "destroy" events
//$this->assertEquals(4, count($ret));
$this->assertEquals(3, count($ret));
$this->assertEquals('create', $ret[0]['Action']);
//$this->assertEquals('connect', $ret[1]['Action']);
$this->assertEquals('disconnect', $ret[1]['Action']);
$this->assertEquals('destroy', $ret[2]['Action']);
}
}