From 1d0054500867bf3a605b6722c3ec99f6cfea5c71 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Wed, 28 Aug 2024 11:22:25 +0400 Subject: [PATCH 1/4] feat: add networkExists() method --- composer.lock | 44 ++++++++++++------------- src/Orchestration/Adapter.php | 5 +++ src/Orchestration/Adapter/DockerAPI.php | 10 ++++++ src/Orchestration/Adapter/DockerCLI.php | 12 +++++++ src/Orchestration/Orchestration.php | 8 +++++ tests/Orchestration/Base.php | 20 +++++++++++ 6 files changed, 77 insertions(+), 22 deletions(-) diff --git a/composer.lock b/composer.lock index 44535d1..e0890e8 100644 --- a/composer.lock +++ b/composer.lock @@ -480,16 +480,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.11.10", + "version": "1.12.0", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "640410b32995914bde3eed26fa89552f9c2c082f" + "reference": "384af967d35b2162f69526c7276acadce534d0e1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/640410b32995914bde3eed26fa89552f9c2c082f", - "reference": "640410b32995914bde3eed26fa89552f9c2c082f", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/384af967d35b2162f69526c7276acadce534d0e1", + "reference": "384af967d35b2162f69526c7276acadce534d0e1", "shasum": "" }, "require": { @@ -534,39 +534,39 @@ "type": "github" } ], - "time": "2024-08-08T09:02:50+00:00" + "time": "2024-08-27T09:18:05+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "9.2.31", + "version": "9.2.32", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "48c34b5d8d983006bd2adc2d0de92963b9155965" + "reference": "85402a822d1ecf1db1096959413d35e1c37cf1a5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/48c34b5d8d983006bd2adc2d0de92963b9155965", - "reference": "48c34b5d8d983006bd2adc2d0de92963b9155965", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/85402a822d1ecf1db1096959413d35e1c37cf1a5", + "reference": "85402a822d1ecf1db1096959413d35e1c37cf1a5", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^4.18 || ^5.0", + "nikic/php-parser": "^4.19.1 || ^5.1.0", "php": ">=7.3", - "phpunit/php-file-iterator": "^3.0.3", - "phpunit/php-text-template": "^2.0.2", - "sebastian/code-unit-reverse-lookup": "^2.0.2", - "sebastian/complexity": "^2.0", - "sebastian/environment": "^5.1.2", - "sebastian/lines-of-code": "^1.0.3", - "sebastian/version": "^3.0.1", - "theseer/tokenizer": "^1.2.0" + "phpunit/php-file-iterator": "^3.0.6", + "phpunit/php-text-template": "^2.0.4", + "sebastian/code-unit-reverse-lookup": "^2.0.3", + "sebastian/complexity": "^2.0.3", + "sebastian/environment": "^5.1.5", + "sebastian/lines-of-code": "^1.0.4", + "sebastian/version": "^3.0.2", + "theseer/tokenizer": "^1.2.3" }, "require-dev": { - "phpunit/phpunit": "^9.3" + "phpunit/phpunit": "^9.6" }, "suggest": { "ext-pcov": "PHP extension that provides line coverage", @@ -575,7 +575,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "9.2-dev" + "dev-main": "9.2.x-dev" } }, "autoload": { @@ -604,7 +604,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.31" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.32" }, "funding": [ { @@ -612,7 +612,7 @@ "type": "github" } ], - "time": "2024-03-02T06:37:42+00:00" + "time": "2024-08-22T04:23:01+00:00" }, { "name": "phpunit/php-file-iterator", diff --git a/src/Orchestration/Adapter.php b/src/Orchestration/Adapter.php index 0fefb74..9f62aba 100644 --- a/src/Orchestration/Adapter.php +++ b/src/Orchestration/Adapter.php @@ -60,6 +60,11 @@ abstract public function networkConnect(string $container, string $network): boo */ abstract public function networkDisconnect(string $container, string $network, bool $force = false): bool; + /** + * Check if a network exists + */ + abstract public function networkExists(string $name): bool; + /** * List Networks * diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 3504f3c..44eb4e5 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -281,6 +281,16 @@ public function networkDisconnect(string $container, string $network, bool $forc return $result['code'] === 200; } + /** + * Check if a network exists + */ + public function networkExists(string $name): bool + { + $result = $this->call('http://localhost/networks/'.$name, 'GET'); + + return $result['code'] === 200; + } + /** * Get usage stats of containers * diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index f4e9019..e23f0b3 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -76,6 +76,18 @@ public function networkDisconnect(string $container, string $network, bool $forc return $result === 0; } + /** + * Check if a network exists + */ + public function networkExists(string $name): bool + { + $output = ''; + + $result = Console::execute('docker network inspect '.$name.' --format "{{.Name}}"', '', $output); + + return $result === 0 && trim($output) === $name; + } + /** * Get usage stats of containers * diff --git a/src/Orchestration/Orchestration.php b/src/Orchestration/Orchestration.php index 5ae5922..6e366b7 100644 --- a/src/Orchestration/Orchestration.php +++ b/src/Orchestration/Orchestration.php @@ -125,6 +125,14 @@ public function networkDisconnect(string $container, string $network, bool $forc return $this->adapter->networkDisconnect($container, $network, $force); } + /** + * Check if a network exists + */ + public function networkExists(string $name): bool + { + return $this->adapter->networkExists($name); + } + /** * Pull Image */ diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 2cac60a..34f53ae 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -716,4 +716,24 @@ public function testUsageStats(): void $stats = static::getOrchestration()->getStats('IDontExist'); } + + public function testNetworkExists(): void + { + $networkName = 'test_network_' . uniqid(); + + // Test non-existent network + $this->assertFalse(static::getOrchestration()->networkExists($networkName)); + + // Create network and test it exists + $response = static::getOrchestration()->createNetwork($networkName); + $this->assertTrue($response); + $this->assertTrue(static::getOrchestration()->networkExists($networkName)); + + // Remove network + $response = static::getOrchestration()->removeNetwork($networkName); + $this->assertTrue($response); + + // Test removed network + $this->assertFalse(static::getOrchestration()->networkExists($networkName)); + } } From 207313a47636b05d294b48f66ae0043c15790465 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Wed, 28 Aug 2024 11:53:28 +0400 Subject: [PATCH 2/4] feat: refactor existing method names --- README.md | 4 ++-- src/Orchestration/Adapter.php | 4 ++-- src/Orchestration/Adapter/DockerAPI.php | 4 ++-- src/Orchestration/Adapter/DockerCLI.php | 4 ++-- src/Orchestration/Orchestration.php | 8 ++++---- tests/Orchestration/Base.php | 16 ++++++++-------- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index fca9c5e..ce415a2 100644 --- a/README.md +++ b/README.md @@ -281,7 +281,7 @@ Once you have initialised your Orchestration object the following methods can be This method creates a new network and returns a boolean value indicating if the network was created successfully. ```php - $orchestration->createNetwork('name', false); + $orchestration->networkCreate('name', false); ```
@@ -305,7 +305,7 @@ Once you have initialised your Orchestration object the following methods can be This method removes a network and returns a boolean value indicating if the network was removed successfully. ```php - $orchestration->removeNetwork('network_id'); + $orchestration->networkRemove('network_id'); ```
diff --git a/src/Orchestration/Adapter.php b/src/Orchestration/Adapter.php index 9f62aba..e70e16f 100644 --- a/src/Orchestration/Adapter.php +++ b/src/Orchestration/Adapter.php @@ -43,12 +43,12 @@ public function filterEnvKey(string $string): string /** * Create Network */ - abstract public function createNetwork(string $name, bool $internal = false): bool; + abstract public function networkCreate(string $name, bool $internal = false): bool; /** * Remove Network */ - abstract public function removeNetwork(string $name): bool; + abstract public function networkRemove(string $name): bool; /** * Connect a container to a network diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 44eb4e5..6c2aff0 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -201,7 +201,7 @@ protected function streamCall(string $url, int $timeout = -1): array /** * Create Network */ - public function createNetwork(string $name, bool $internal = false): bool + public function networkCreate(string $name, bool $internal = false): bool { $body = \json_encode([ 'Name' => $name, @@ -225,7 +225,7 @@ public function createNetwork(string $name, bool $internal = false): bool /** * Remove Network */ - public function removeNetwork(string $name): bool + public function networkRemove(string $name): bool { $result = $this->call('http://localhost/networks/'.$name, 'DELETE'); diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index e23f0b3..dd68550 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -31,7 +31,7 @@ public function __construct(?string $username = null, ?string $password = null) /** * Create Network */ - public function createNetwork(string $name, bool $internal = false): bool + public function networkCreate(string $name, bool $internal = false): bool { $output = ''; @@ -43,7 +43,7 @@ public function createNetwork(string $name, bool $internal = false): bool /** * Remove Network */ - public function removeNetwork(string $name): bool + public function networkRemove(string $name): bool { $output = ''; diff --git a/src/Orchestration/Orchestration.php b/src/Orchestration/Orchestration.php index 6e366b7..d0f52da 100644 --- a/src/Orchestration/Orchestration.php +++ b/src/Orchestration/Orchestration.php @@ -75,17 +75,17 @@ public function parseCommandString(string $command): array /** * Create Network */ - public function createNetwork(string $name, bool $internal = false): bool + public function networkCreate(string $name, bool $internal = false): bool { - return $this->adapter->createNetwork($name, $internal); + return $this->adapter->networkCreate($name, $internal); } /** * Remove Network */ - public function removeNetwork(string $name): bool + public function networkRemove(string $name): bool { - return $this->adapter->removeNetwork($name); + return $this->adapter->networkRemove($name); } /** diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 34f53ae..ac857c3 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -185,15 +185,15 @@ public function testCreateContainer(): void /** * @depends testCreateContainer */ - public function testCreateNetwork(): void + public function testNetworkCreate(): void { - $response = static::getOrchestration()->createNetwork('TestNetwork'); + $response = static::getOrchestration()->networkCreate('TestNetwork'); $this->assertEquals(true, $response); } /** - * @depends testCreateNetwork + * @depends testNetworkCreate */ public function testListNetworks(): void { @@ -211,7 +211,7 @@ public function testListNetworks(): void } /** - * @depends testCreateNetwork + * @depends testNetworkCreate */ public function testNetworkConnect(): void { @@ -260,9 +260,9 @@ public function testNetworkDisconnect(): void /** * @depends testNetworkDisconnect */ - public function testRemoveNetwork(): void + public function testNetworkRemove(): void { - $response = static::getOrchestration()->removeNetwork('TestNetwork'); + $response = static::getOrchestration()->networkRemove('TestNetwork'); $this->assertEquals(true, $response); } @@ -725,12 +725,12 @@ public function testNetworkExists(): void $this->assertFalse(static::getOrchestration()->networkExists($networkName)); // Create network and test it exists - $response = static::getOrchestration()->createNetwork($networkName); + $response = static::getOrchestration()->networkCreate($networkName); $this->assertTrue($response); $this->assertTrue(static::getOrchestration()->networkExists($networkName)); // Remove network - $response = static::getOrchestration()->removeNetwork($networkName); + $response = static::getOrchestration()->networkRemove($networkName); $this->assertTrue($response); // Test removed network From 6fd9d4a3077f67a8c06e5c89b9654741c1515b6f Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Wed, 28 Aug 2024 12:02:51 +0400 Subject: [PATCH 3/4] chore: linter --- tests/Orchestration/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index ac857c3..c2d53b4 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -719,7 +719,7 @@ public function testUsageStats(): void public function testNetworkExists(): void { - $networkName = 'test_network_' . uniqid(); + $networkName = 'test_network_'.uniqid(); // Test non-existent network $this->assertFalse(static::getOrchestration()->networkExists($networkName)); From be8eeee8a9d5544e284ebb217fbf144d71451eff Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Wed, 28 Aug 2024 13:35:22 +0400 Subject: [PATCH 4/4] chore: revert renaming methods --- README.md | 4 ++-- src/Orchestration/Adapter.php | 4 ++-- src/Orchestration/Adapter/DockerAPI.php | 4 ++-- src/Orchestration/Adapter/DockerCLI.php | 4 ++-- src/Orchestration/Orchestration.php | 8 ++++---- tests/Orchestration/Base.php | 16 ++++++++-------- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ce415a2..fca9c5e 100644 --- a/README.md +++ b/README.md @@ -281,7 +281,7 @@ Once you have initialised your Orchestration object the following methods can be This method creates a new network and returns a boolean value indicating if the network was created successfully. ```php - $orchestration->networkCreate('name', false); + $orchestration->createNetwork('name', false); ```
@@ -305,7 +305,7 @@ Once you have initialised your Orchestration object the following methods can be This method removes a network and returns a boolean value indicating if the network was removed successfully. ```php - $orchestration->networkRemove('network_id'); + $orchestration->removeNetwork('network_id'); ```
diff --git a/src/Orchestration/Adapter.php b/src/Orchestration/Adapter.php index e70e16f..9f62aba 100644 --- a/src/Orchestration/Adapter.php +++ b/src/Orchestration/Adapter.php @@ -43,12 +43,12 @@ public function filterEnvKey(string $string): string /** * Create Network */ - abstract public function networkCreate(string $name, bool $internal = false): bool; + abstract public function createNetwork(string $name, bool $internal = false): bool; /** * Remove Network */ - abstract public function networkRemove(string $name): bool; + abstract public function removeNetwork(string $name): bool; /** * Connect a container to a network diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 6c2aff0..44eb4e5 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -201,7 +201,7 @@ protected function streamCall(string $url, int $timeout = -1): array /** * Create Network */ - public function networkCreate(string $name, bool $internal = false): bool + public function createNetwork(string $name, bool $internal = false): bool { $body = \json_encode([ 'Name' => $name, @@ -225,7 +225,7 @@ public function networkCreate(string $name, bool $internal = false): bool /** * Remove Network */ - public function networkRemove(string $name): bool + public function removeNetwork(string $name): bool { $result = $this->call('http://localhost/networks/'.$name, 'DELETE'); diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index dd68550..e23f0b3 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -31,7 +31,7 @@ public function __construct(?string $username = null, ?string $password = null) /** * Create Network */ - public function networkCreate(string $name, bool $internal = false): bool + public function createNetwork(string $name, bool $internal = false): bool { $output = ''; @@ -43,7 +43,7 @@ public function networkCreate(string $name, bool $internal = false): bool /** * Remove Network */ - public function networkRemove(string $name): bool + public function removeNetwork(string $name): bool { $output = ''; diff --git a/src/Orchestration/Orchestration.php b/src/Orchestration/Orchestration.php index d0f52da..6e366b7 100644 --- a/src/Orchestration/Orchestration.php +++ b/src/Orchestration/Orchestration.php @@ -75,17 +75,17 @@ public function parseCommandString(string $command): array /** * Create Network */ - public function networkCreate(string $name, bool $internal = false): bool + public function createNetwork(string $name, bool $internal = false): bool { - return $this->adapter->networkCreate($name, $internal); + return $this->adapter->createNetwork($name, $internal); } /** * Remove Network */ - public function networkRemove(string $name): bool + public function removeNetwork(string $name): bool { - return $this->adapter->networkRemove($name); + return $this->adapter->removeNetwork($name); } /** diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index c2d53b4..5cf7082 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -185,15 +185,15 @@ public function testCreateContainer(): void /** * @depends testCreateContainer */ - public function testNetworkCreate(): void + public function testCreateNetwork(): void { - $response = static::getOrchestration()->networkCreate('TestNetwork'); + $response = static::getOrchestration()->createNetwork('TestNetwork'); $this->assertEquals(true, $response); } /** - * @depends testNetworkCreate + * @depends testCreateNetwork */ public function testListNetworks(): void { @@ -211,7 +211,7 @@ public function testListNetworks(): void } /** - * @depends testNetworkCreate + * @depends testCreateNetwork */ public function testNetworkConnect(): void { @@ -260,9 +260,9 @@ public function testNetworkDisconnect(): void /** * @depends testNetworkDisconnect */ - public function testNetworkRemove(): void + public function testRemoveNetwork(): void { - $response = static::getOrchestration()->networkRemove('TestNetwork'); + $response = static::getOrchestration()->removeNetwork('TestNetwork'); $this->assertEquals(true, $response); } @@ -725,12 +725,12 @@ public function testNetworkExists(): void $this->assertFalse(static::getOrchestration()->networkExists($networkName)); // Create network and test it exists - $response = static::getOrchestration()->networkCreate($networkName); + $response = static::getOrchestration()->createNetwork($networkName); $this->assertTrue($response); $this->assertTrue(static::getOrchestration()->networkExists($networkName)); // Remove network - $response = static::getOrchestration()->networkRemove($networkName); + $response = static::getOrchestration()->removeNetwork($networkName); $this->assertTrue($response); // Test removed network