Skip to content

Commit

Permalink
Resume suspend server (#394)
Browse files Browse the repository at this point in the history
* Implement resuming and suspending of servers

---------

Co-authored-by: Martin Zurowietz <[email protected]>
  • Loading branch information
k0ka and mzur authored Jan 30, 2024
1 parent 857fcc8 commit b0e560d
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 1 deletion.
21 changes: 21 additions & 0 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ jobs:
name: Deploy OpenStack ${{ matrix.name }} and run integration tests with php ${{matrix.php_version}}
steps:
- uses: actions/checkout@v2

- name: get cache directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v3
with:
path: |
Expand All @@ -54,13 +56,16 @@ jobs:
key: ${{ runner.os }}-composer-${{ matrix.php_version }}-${{ hashFiles('**.composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-${{ matrix.php_version }}-
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
extensions: curl
tools: composer:v2
coverage: none

- run: composer install --prefer-dist --no-interaction --no-progress

- name: Restore devstack cache
uses: actions/cache@v3
with:
Expand All @@ -69,6 +74,7 @@ jobs:
!/opt/stack/data
~/devstack/
key: ${{ runner.os }}-openstack-${{ matrix.openstack_version }}-${{ github.workflow }}

- name: Deploy devstack
uses: EmilienM/[email protected]
with:
Expand All @@ -81,6 +87,7 @@ jobs:
[filter:versioned_writes]
allow_object_versioning = true
enabled_services: 's-account,s-container,s-object,s-proxy,s-bak'

- name: Set env variables
run: |
{
Expand All @@ -96,9 +103,23 @@ jobs:
echo OS_FLAVOR=1
echo OS_DOMAIN_ID=default
} >> "$GITHUB_ENV"
- name: Check if Block Storage API v2 must be tested
if: matrix.block_storage_v2 == true
run: echo "OS_BLOCK_STORAGE_V2=1" >> "$GITHUB_ENV"

- name: Execute Integration tests via PhpUnit
run: vendor/bin/phpunit --configuration ./phpunit.sample.xml.dist

- name: Collect logs
if: ${{ failure() }}
run: |
set -x
journalctl >failure-logs
- name: Save logs
if: ${{ failure() }}
uses: actions/upload-artifact@v3
with:
name: failure-logs
path: failure-logs
11 changes: 11 additions & 0 deletions doc/services/compute/v2/servers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,14 @@ You can also refine by network label:
$ipAddresses = $server->listAddresses([
'networkLabel' => '{networkLabel}',
]);
Suspend
-------

.. sample:: Compute/v2/images/suspend.php

Resume
------

.. sample:: Compute/v2/images/resume.php

17 changes: 17 additions & 0 deletions samples/Compute/v2/servers/resume.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
'authUrl' => '{authUrl}',
'region' => '{region}',
'user' => [
'id' => '{userId}',
'password' => '{password}',
],
]);

$service = $openstack->computeV2();
$server = $service->getServer(['id' => '{serverId}']);

$server->resume();
17 changes: 17 additions & 0 deletions samples/Compute/v2/servers/suspend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
'authUrl' => '{authUrl}',
'region' => '{region}',
'user' => [
'id' => '{userId}',
'password' => '{password}',
],
]);

$service = $openstack->computeV2();
$server = $service->getServer(['id' => '{serverId}']);

$server->suspend();
24 changes: 24 additions & 0 deletions src/Compute/v2/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,30 @@ public function stopServer(): array
];
}

public function resumeServer(): array
{
return [
'method' => 'POST',
'path' => 'servers/{id}/action',
'params' => [
'id' => $this->params->urlId('server'),
'resume' => $this->params->nullAction(),
],
];
}

public function suspendServer(): array
{
return [
'method' => 'POST',
'path' => 'servers/{id}/action',
'params' => [
'id' => $this->params->urlId('server'),
'suspend' => $this->params->nullAction(),
],
];
}

public function rebuildServer(): array
{
return [
Expand Down
22 changes: 22 additions & 0 deletions src/Compute/v2/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ public function stop()
]);
}

/**
* Resumes server.
*/
public function resume(): void
{
$this->execute($this->api->resumeServer(), [
'id' => $this->id,
'resume' => null,
]);
}

/**
* Suspends server.
*/
public function suspend(): void
{
$this->execute($this->api->suspendServer(), [
'id' => $this->id,
'suspend' => null,
]);
}

/**
* Rebuilds the server.
*
Expand Down
32 changes: 31 additions & 1 deletion tests/sample/Compute/v2/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public function testGetVncConsole(Server $createdServer)
{
/** @var array $console */
require_once $this->sampleFile('servers/get_server_vnc_console.php', [
'{serverId}' => $createdServer->id
'{serverId}' => $createdServer->id,
]);

$this->assertIsArray($console);
Expand All @@ -331,6 +331,9 @@ public function testGetVncConsole(Server $createdServer)
*/
public function testGetConsoleOutput(Server $createdServer)
{
// wait for the server to be ready
sleep(5);

/** @var string $consoleOutput */
require_once $this->sampleFile('servers/get_server_console_output.php', ['{serverId}' => $createdServer->id]);

Expand Down Expand Up @@ -361,4 +364,31 @@ public function testDelete(Server $createdServer)
$this->expectException(BadResponseError::class);
$createdServer->retrieve();
}

public function testSuspend()
{
$server = $this->createServer();

require_once $this->sampleFile('servers/suspend.php', ['{serverId}' => $server->id]);

$server->waitUntil('SUSPENDED');
$this->assertEquals('SUSPENDED', $server->status);

return $server;
}

/**
* @depends testSuspend
*/
public function testResume(Server $server)
{
$this->assertEquals('SUSPENDED', $server->status);

require_once $this->sampleFile('servers/resume.php', ['{serverId}' => $server->id]);

$server->waitUntil('ACTIVE', 300);
$this->assertEquals('ACTIVE', $server->status);

$this->deleteServer($server);
}
}
5 changes: 5 additions & 0 deletions tests/sample/Compute/v2/VolumeAttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public function testAttach(): VolumeAttachment
{
$server = $this->createServer();

// let's wait for the server to be completely up
// https://bugs.launchpad.net/nova/+bug/1998148
// https://bugs.launchpad.net/nova/+bug/1960346
sleep(10);

$volume = $this->getCachedService(Service::class)->createVolume(
[
'name' => $this->randomStr(),
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/Compute/v2/Models/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ public function test_it_stops()
self::assertNull($this->server->stop());
}

public function test_it_resumes()
{
$expectedJson = ['resume' => null];

$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], new Response(202));

$this->assertNull($this->server->resume());
}

public function test_it_suspends()
{
$expectedJson = ['suspend' => null];

$this->setupMock('POST', 'servers/serverId/action', $expectedJson, [], new Response(202));

$this->assertNull($this->server->suspend());
}

public function test_it_resizes()
{
$expectedJson = ['resize' => ['flavorRef' => 'flavorId']];
Expand Down

0 comments on commit b0e560d

Please sign in to comment.