|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RenokiCo\PhpK8s\Test; |
| 4 | + |
| 5 | +use GuzzleHttp\Psr7\Response; |
| 6 | +use Psr\Http\Message\ResponseInterface; |
| 7 | +use RenokiCo\PhpK8s\Enums\Operation; |
| 8 | +use RenokiCo\PhpK8s\Kinds\K8sPod; |
| 9 | +use RenokiCo\PhpK8s\KubernetesCluster; |
| 10 | + |
| 11 | +class KubernetesClusterTest extends TestCase |
| 12 | +{ |
| 13 | + public function test_log_operations_return_raw_json_logs_as_strings(): void |
| 14 | + { |
| 15 | + $expectedLogs = '{"status":200,"message":"ok","container":{"size":123}}'; |
| 16 | + |
| 17 | + $cluster = new class('http://127.0.0.1:8080', new Response(200, [], $expectedLogs)) extends KubernetesCluster |
| 18 | + { |
| 19 | + public function __construct(?string $url, private readonly ResponseInterface $response) |
| 20 | + { |
| 21 | + parent::__construct($url); |
| 22 | + } |
| 23 | + |
| 24 | + public function call(string $method, string $path, string $payload = '', array $query = ['pretty' => 1], array $options = []): ResponseInterface |
| 25 | + { |
| 26 | + return $this->response; |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + $result = $cluster |
| 31 | + ->setResourceClass(K8sPod::class) |
| 32 | + ->runOperation(Operation::LOG, '/api/v1/namespaces/default/pods/example/log', ''); |
| 33 | + |
| 34 | + $this->assertSame($expectedLogs, $result); |
| 35 | + } |
| 36 | + |
| 37 | + public function test_non_log_operations_still_hydrate_resources_from_json(): void |
| 38 | + { |
| 39 | + $cluster = new class('http://127.0.0.1:8080', new Response(200, [], '{"kind":"Pod","metadata":{"name":"example"}}')) extends KubernetesCluster |
| 40 | + { |
| 41 | + public function __construct(?string $url, private readonly ResponseInterface $response) |
| 42 | + { |
| 43 | + parent::__construct($url); |
| 44 | + } |
| 45 | + |
| 46 | + public function call(string $method, string $path, string $payload = '', array $query = ['pretty' => 1], array $options = []): ResponseInterface |
| 47 | + { |
| 48 | + return $this->response; |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + $result = $cluster |
| 53 | + ->setResourceClass(K8sPod::class) |
| 54 | + ->runOperation(Operation::GET, '/api/v1/namespaces/default/pods/example', ''); |
| 55 | + |
| 56 | + $this->assertInstanceOf(K8sPod::class, $result); |
| 57 | + $this->assertSame('example', $result->getName()); |
| 58 | + } |
| 59 | +} |
0 commit comments