From ee3d5b3fc72670c28c7d135609a4b59ff910c014 Mon Sep 17 00:00:00 2001 From: Floran Brutel Date: Wed, 19 Nov 2014 04:41:31 -0500 Subject: [PATCH 1/2] Add diagnostics to Server --- lib/OpenCloud/Compute/Resource/Server.php | 21 +++++++++++++++++++ .../Tests/Compute/Resource/ServerTest.php | 5 +++++ .../Tests/Compute/_response/Extensions.resp | 8 +++++++ 3 files changed, 34 insertions(+) diff --git a/lib/OpenCloud/Compute/Resource/Server.php b/lib/OpenCloud/Compute/Resource/Server.php index 20a7692e5..2d42f840e 100644 --- a/lib/OpenCloud/Compute/Resource/Server.php +++ b/lib/OpenCloud/Compute/Resource/Server.php @@ -775,4 +775,25 @@ public function resume() return $this->action($object); } + + /** + * Get server diagnostics + * + * Gets basic usage data for a specified server. + * + * @api + * @return object + */ + public function diagnostics() + { + // The diagnostics is only available when the os-server-diagnostics extension is installed. + $this->checkExtension('os-server-diagnostics'); + + $url = $this->getUrl('diagnostics'); + + $response = $this->getClient()->get($url)->send(); + $body = Formatter::decode($response); + + return $body ?: (object) array(); + } } diff --git a/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php b/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php index eb741f96e..f98efa103 100644 --- a/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php +++ b/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php @@ -376,4 +376,9 @@ public function test_Create_With_Bootable_Volume_Delete_On_Termination() $this->assertEquals(0, $obj->boot_index); $this->assertEquals(true, $obj->delete_on_termination); } + + public function test_Diagnostics() + { + $this->assertTrue(is_object($this->server->diagnostics())); + } } diff --git a/tests/OpenCloud/Tests/Compute/_response/Extensions.resp b/tests/OpenCloud/Tests/Compute/_response/Extensions.resp index d1b920829..ee590d812 100644 --- a/tests/OpenCloud/Tests/Compute/_response/Extensions.resp +++ b/tests/OpenCloud/Tests/Compute/_response/Extensions.resp @@ -119,6 +119,14 @@ Server: Jetty(8.0.y.z-SNAPSHOT) "alias": "os-keypairs", "description": "Keypair Support." }, + { + "updated": "2011-12-21T00:00:00+00:00", + "name": "ServerDiagnostics", + "links": [], + "namespace": "http://docs.openstack.org/compute/ext/server-diagnostics/api/v1.1", + "alias": "os-server-diagnostics", + "description": "Allow Admins to view server diagnostics through server action." + }, { "updated": "2012-03-07T14:46:43Z", "name": "OSNetworksV2", From 2ad609e746040614a866ae3fc766a209b3b454c3 Mon Sep 17 00:00:00 2001 From: Floran Brutel Date: Wed, 19 Nov 2014 05:40:30 -0500 Subject: [PATCH 2/2] Mock the response for Diagnostics test --- .../Tests/Compute/Resource/ServerTest.php | 7 +++++- .../Tests/Compute/_response/Diagnostics.resp | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/OpenCloud/Tests/Compute/_response/Diagnostics.resp diff --git a/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php b/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php index f98efa103..c97aabb76 100644 --- a/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php +++ b/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php @@ -379,6 +379,11 @@ public function test_Create_With_Bootable_Volume_Delete_On_Termination() public function test_Diagnostics() { - $this->assertTrue(is_object($this->server->diagnostics())); + $this->addMockSubscriber($this->getTestFilePath('Diagnostics')); + $diagnostics = $this->server->diagnostics(); + $this->assertInternalType('object', $diagnostics); + $this->assertEquals(524288, $diagnostics->memory); + $this->assertEquals(-1, $diagnostics->vda_errors); + $this->assertEquals(662, $diagnostics->vnet1_tx_packets); } } diff --git a/tests/OpenCloud/Tests/Compute/_response/Diagnostics.resp b/tests/OpenCloud/Tests/Compute/_response/Diagnostics.resp new file mode 100644 index 000000000..e9fdec80d --- /dev/null +++ b/tests/OpenCloud/Tests/Compute/_response/Diagnostics.resp @@ -0,0 +1,24 @@ +HTTP/1.1 200 OK +Date: Fri, 15 Nov 2013 14:12:58 GMT +Content-Length: 1364 +Content-Type: application/json +X-Compute-Request-Id: req-164dc435-90c2-48ce-9d69-96f170c518c8 +Server: Jetty(8.0.y.z-SNAPSHOT) + +{ + "cpu0_time": 17300000000, + "memory": 524288, + "vda_errors": -1, + "vda_read": 262144, + "vda_read_req": 112, + "vda_write": 5778432, + "vda_write_req": 488, + "vnet1_rx": 2070139, + "vnet1_rx_drop": 0, + "vnet1_rx_errors": 0, + "vnet1_rx_packets": 26701, + "vnet1_tx": 140208, + "vnet1_tx_drop": 0, + "vnet1_tx_errors": 0, + "vnet1_tx_packets": 662 +} \ No newline at end of file