Skip to content

Commit

Permalink
Move integration tests to phpunit (#387)
Browse files Browse the repository at this point in the history
* moved integration tests to phpunit 
* updated integration tests description in CONTRIBUTING.md
* added `name` parameter to `patchUser` so the sample file works properly
* added `Retrievable` interface to `VolumeType`
* added `HasWaiterTrait` to `Compute::Image`
* added `Token::validate()` function to check if Identity token is valid

---------

Co-authored-by: k0ka <k0ka@users.noreply.github.com>
  • Loading branch information
k0ka and k0ka authored Jan 17, 2024
1 parent 45bd478 commit 2335c75
Show file tree
Hide file tree
Showing 156 changed files with 4,751 additions and 3,124 deletions.
5 changes: 3 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php_cs.dist export-ignore
/.php-cs-fixer.dist.php export-ignore
/.readthedocs.yaml export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/phpunit.sample.xml.dist export-ignore
/phpunit.xml.dist export-ignore
18 changes: 4 additions & 14 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,9 @@ jobs:
echo OS_FLAVOR=1
echo OS_DOMAIN_ID=default
} >> "$GITHUB_ENV"
- name: Execute Block Storage v2 tests
- name: Check if Block Storage API v2 must be tested
if: matrix.block_storage_v2 == true
run: php ./tests/integration/run.php -s=BlockStorage -v=v2
- name: Execute Block Storage v3 tests
run: php ./tests/integration/run.php -s=BlockStorage -v=v3
- name: Execute Compute tests
run: php ./tests/integration/run.php -s=Compute
- name: Execute Identity tests
run: php ./tests/integration/run.php -s=Identity
- name: Execute Images tests
run: php ./tests/integration/run.php -s=Images
- name: Execute Networking tests
run: php ./tests/integration/run.php -s=Networking
- name: Execute Object Storage tests
run: php ./tests/integration/run.php -s=ObjectStore
run: echo "OS_BLOCK_STORAGE_V2=1" >> "$GITHUB_ENV"
- name: Execute Integration tests via PhpUnit
run: vendor/bin/phpunit --configuration ./phpunit.sample.xml.dist

40 changes: 0 additions & 40 deletions .travis.yml

This file was deleted.

80 changes: 41 additions & 39 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ The second argument to `setupMockResponse` is an external file, storing a string
We use phpunit, so you run this at the project root:

```bash
phpunit
vendor/bin/phpunit
```

### Integration tests
Expand All @@ -203,29 +203,46 @@ service charges from your provider. Although most tests handle their own
teardown procedures, it is always worth manually checking that resources are
deleted after the test suite finishes.

It is recommended to run integration test in a separate OpenStack project, so that you can easily clean up all resources.

We use all of our sample files as live integration tests, achieving the dual aim of reducing code duplication and
ensuring that our samples actually work.

### Setting up dev environment

In order to run integration tests, you need to have a working OpenStack environment.
You can use [DevStack](http://docs.openstack.org/developer/devstack/) to set up a local OpenStack environment.
DevStack will make substantial changes to your system during installation. Only run DevStack on servers or
virtual machines that are dedicated to this purpose.

Installation is pretty straightforward. You can use guide on their [website](http://docs.openstack.org/developer/devstack/).

Here is currently used `local.conf` for DevStack:

```ini
[[local|localrc]]
ADMIN_PASSWORD=secret
DATABASE_PASSWORD=root
RABBIT_PASSWORD=secret
SERVICE_PASSWORD=secret
SWIFT_HASH=1234123412341234
LOGFILE=/tmp/devstack-logs/devstack.log
USE_PYTHON3=True
INSTALL_TEMPEST=False
GIT_BASE=https://github.com
ENABLED_SERVICES+=,-horizon,-dstat,-tempest,s-account,s-container,s-object,s-proxy,s-bak
SWIFT_ENABLE_TEMPURLS=True
SWIFT_TEMPURL_KEY=secretkey

[[post-config|$SWIFT_CONFIG_PROXY_SERVER]]
[filter:versioned_writes]
allow_object_versioning = true
```

### Setting up environment variables

Rename `env_test.sh.dist` as `env_test.sh` and replace values according to your OpenStack instance configuration.
Completed file may look as following.


```bash
#!/usr/bin/env bash
export OS_AUTH_URL="http://1.2.3.4:5000/v3"
export OS_REGION="RegionOne"
export OS_REGION_NAME="RegionOne"
export OS_USER_ID="536068bcb1b946ff8e2f10eff6543f9c"
export OS_USERNAME="admin"
export OS_PASSWORD="2251639ecaea442b"
export OS_PROJECT_ID="b62b3bebf9e84e4eb11aafcd8c58db3f"
export OS_PROJECT_NAME="admin"
export OS_RESIZE_FLAVOR=2 #Must be a valid flavor ID
export OS_FLAVOR=1 #Must be a valid flavor ID
export OS_DOMAIN_ID="default"
```
If you are using DevStack with config file above, the values would be filled automatically.

To export environment variables, run
```bash
Expand All @@ -236,23 +253,12 @@ Additionally, integration tests require image called `cirros` exists.

### Running integration tests

You interact with integration tests through a runner script:
We use phpunit, so you run this at the project root:

```bash
php ./tests/integration/run.php [-s=BlockStorage|Compute|Identity|Images|Networking|ObjectStore] [--debug=1|2]
vendor/bin/phpunit --configuration ./phpunit.sample.xml.dist
```

It supports these command-line flags:

| Flag | Description | Example |
| ---- | ----------- | ------- |
| `-s` `--service` | Allows you to refine tests by a particular service. A service corresponds to top-level directories in the `./integration` directory, meaning that `compute` and `identity` are services because they exist as sub-directories there. If omitted, all services are run.|Run compute service: `php ./tests/integration/run.php -s compute` Run all tests: `php ./tests/integration/run.php`|
| `-v` `--version` | Allows you to refine by a particular service version. A version corresponds to the sub-directories inside a service directory, meaning that `v2` is a supported version of `compute` because it exists as a sub-directory inside the `compute` directory. If omitted, all versions are run.|Run v2 Compute tests: `php ./tests/integration/run.php -s compute -v v2` Run all compute tests: `php ./tests/integration/run.php -s compute`|
| `-t` `--test` | Allows you to refine by a particular test. Tests are defined in classes like `integration\OpenStack\Compute\v2`. Each test method manually references a sample file. To refine which tests are run, list the name of the method in this class. If omitted, all tests are run.|Run create server test: `php ./tests/integration/run.php -s compute -v v2 -t createServer` Run all compute v2 tests: `php ./tests/integration/run.php -s compute -v v2`|
| `--debug` |||
| `--help` | A help screen is returned and no tests run | `php ./tests/integration/run.php --help`


## Style guide

We follow [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) for our
Expand Down Expand Up @@ -281,14 +287,11 @@ required to adhere to:
### 1. Providing feedback
On of the easiest ways to get readily involved in our project is to let us know
One of the easiest ways to get readily involved in our project is to let us know
about your experiences using our SDK. Feedback like this is incredibly useful
to us, because it allows us to refine and change features based on what our
users want and expect of us. There are a bunch of ways to get in contact! You
can [ping us](https://developer.rackspace.com/support/) via e-mail, talk to us on irc
(#rackspace-dev on freenode), [tweet us](https://twitter.com/rackspace), or
submit an issue on our [bug tracker](/issues). Things you might like to tell us
are:
users want and expect of us. You can submit an issue on our [bug tracker](/issues).
Things you might like to tell us are:
* how easy was it to start using our SDK?
* did it meet your expectations? If not, why not?
Expand All @@ -309,8 +312,7 @@ breakthroughs on it so far.
We have three forms of documentation:

* short README documents that briefly introduce a topic
* reference documentation
* user documentation on http://docs.php-opencloud.com that includes
* user documentation on https://php-openstack-sdk.readthedocs.io that includes
getting started guides, installation guides and code samples

If you feel that a certain section could be improved - whether it's to clarify
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"autoload-dev": {
"psr-4": {
"OpenStack\\Integration\\": "tests/integration/",
"OpenStack\\Sample\\": "tests/sample/",
"OpenStack\\Test\\": "tests/unit/"
}
},
Expand Down
4 changes: 2 additions & 2 deletions doc/services/compute/v2/servers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ This operation will reset the state of the server.
Reboot server
-------------

This operation will reboot a server. Please be aware that you must specify whether you want to initiate a HARD or
SOFT reboot (you specify this as a string argument).
This operation will reboot a server. You can specify HARD reboot by passing argument Enum::REBOOT_HARD to the function.
Otherwise, the default is a SOFT reboot.

.. sample:: Compute/v2/servers/reboot_server.php
.. refdoc:: OpenStack/Compute/v2/Models/Server.html#method_reboot
Expand Down
21 changes: 11 additions & 10 deletions env_test.sh.dist
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env bash
export OS_AUTH_URL=""
export OS_REGION=""
export OS_REGION_NAME=""
export OS_USER_ID=""
export OS_USERNAME=""
export OS_PASSWORD=""
export OS_PROJECT_ID=""
export OS_PROJECT_NAME=""
export OS_RESIZE_FLAVOR=""
export OS_FLAVOR=""

export OS_AUTH_URL="$(grep -oP -m 1 "(?<=auth_url: )(.*)\$" /etc/openstack/clouds.yaml)/v3"
export OS_REGION="RegionOne"
export OS_REGION_NAME="RegionOne"
export OS_USER_ID=$(openstack --os-cloud=devstack-admin user show admin -f value -c id)
export OS_USERNAME="admin"
export OS_PASSWORD="secret"
export OS_PROJECT_ID=$(openstack --os-cloud=devstack-admin project show admin -f value -c id)
export OS_PROJECT_NAME="admin"
export OS_RESIZE_FLAVOR="c1"
export OS_FLAVOR=1
export OS_DOMAIN_ID="default"
23 changes: 23 additions & 0 deletions phpunit.sample.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
beStrictAboutOutputDuringTests="true"
stopOnError="true"
stopOnFailure="true"
printerClass="\PHPUnit\Util\TestDox\CliTestDoxPrinter"
verbose="true"
>
<testsuites>
<testsuite name="Sample">
<directory>tests/sample</directory>
</testsuite>
</testsuites>
<php>
<ini name="error_reporting" value=""/>
<ini name="display_errors" value="On"/>
<ini name="display_startup_errors" value="On"/>
</php>
</phpunit>
23 changes: 8 additions & 15 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php" colors="true" beStrictAboutOutputDuringTests="true">
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
beStrictAboutOutputDuringTests="true"
>
<testsuites>
<testsuite name="OpenStack">
<testsuite name="Unit">
<directory>tests/unit</directory>
</testsuite>
</testsuites>
Expand All @@ -13,14 +16,4 @@
<ini name="display_errors" value="On"/>
<ini name="display_startup_errors" value="On"/>
</php>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
<exclude>
<directory suffix="Interface.php">./src</directory>
<directory suffix="Api.php">./src</directory>
<directory suffix="Params.php">./src</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
2 changes: 1 addition & 1 deletion samples/BlockStorage/v3/snapshots/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
$snapshots = $service->listSnapshots();

foreach ($snapshots as $snapshot) {
/** @var $snapshot \OpenStack\BlockStorage\v2\Models\Snapshot */
/** @var \OpenStack\BlockStorage\v2\Models\Snapshot $snapshot */
}
2 changes: 1 addition & 1 deletion samples/BlockStorage/v3/snapshots/list_sorted.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
$snapshots = $service->listSnapshots(false, ['sortKey' => '{sortKey}', 'sortDir' => '{sortDir}']);

foreach ($snapshots as $snapshot) {
/** @var $snapshot \OpenStack\BlockStorage\v2\Models\Snapshot */
/** @var \OpenStack\BlockStorage\v2\Models\Snapshot $snapshot */
}
2 changes: 1 addition & 1 deletion samples/BlockStorage/v3/volume_types/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
$volumeTypes = $service->listVolumeTypes();

foreach ($volumeTypes as $volumeType) {
/** @var $volumeType \OpenStack\BlockStorage\v2\VolumeType */
/** @var \OpenStack\BlockStorage\v2\Models\VolumeType $volumeType */
}
2 changes: 1 addition & 1 deletion samples/BlockStorage/v3/volumes/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
$volumes = $service->listVolumes();

foreach ($volumes as $volume) {
/** @var $volume \OpenStack\BlockStorage\v2\Models\Volume */
/** @var \OpenStack\BlockStorage\v2\Models\Volume $volume */
}
4 changes: 2 additions & 2 deletions samples/BlockStorage/v3/volumes/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
'authUrl' => '{authUrl}',
'region' => '{region}',
'user' => ['id' => '{userId}', 'password' => '{password}'],
'scope' => ['project' => ['id' => '{projectId}']]
'scope' => ['project' => ['id' => '{projectId}']],
]);

$service = $openstack->blockStorageV3();

$volume = $service->getVolume('{volumeId}');

$volume->name = '{newName}';
$volume->name = '{newName}';
$volume->description = '{newDescription}';

$volume->update();
1 change: 1 addition & 0 deletions samples/Compute/v2/flavors/list_flavors.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
$flavors = $compute->listFlavors();

foreach ($flavors as $flavor) {
/** @var \OpenStack\Compute\v2\Models\Flavor $flavor */
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@

$compute = $openstack->computeV2(['region' => '{region}']);

/** @var HypervisorStatistic $hypervisorStatistics */
$hypervisorStatistics = $compute->getHypervisorStatistics();
4 changes: 1 addition & 3 deletions samples/Compute/v2/hypervisors/list_hypervisors.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use OpenStack\Compute\v2\Models\Hypervisor;

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
Expand All @@ -19,5 +17,5 @@
$hypervisors = $compute->listHypervisors();

foreach ($hypervisors as $hypervisor) {
/**@var Hypervisor $hypervisor*/
/** @var \OpenStack\Compute\v2\Models\Hypervisor $hypervisor */
}
2 changes: 1 addition & 1 deletion samples/Compute/v2/images/delete_image_metadata_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

$image = $compute->getImage(['id' => '{imageId}']);

$image->deleteMetadataItem('key');
$image->deleteMetadataItem('{key}');
1 change: 1 addition & 0 deletions samples/Compute/v2/images/list_images.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
$images = $compute->listImages(['status' => 'ACTIVE']);

foreach ($images as $image) {
/** @var \OpenStack\Compute\v2\Models\Image $image */
}
Loading

0 comments on commit 2335c75

Please sign in to comment.