From 1d0054500867bf3a605b6722c3ec99f6cfea5c71 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Wed, 28 Aug 2024 11:22:25 +0400 Subject: [PATCH] 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)); + } }