Merge pull request #551 from puppetlabs/vmpooler-dns-gcp

(RE-15124) Implement DNS Plugins and Remove api v1 and v2
This commit is contained in:
Jake Spain 2023-03-28 15:46:07 -04:00 committed by GitHub
commit b6819de326
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
49 changed files with 3011 additions and 6167 deletions

View file

@ -12,8 +12,7 @@
# SupportedStyles: with_first_parameter, with_fixed_indentation # SupportedStyles: with_first_parameter, with_fixed_indentation
Layout/ParameterAlignment: Layout/ParameterAlignment:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 9 # Offense count: 9
# Cop supports --auto-correct. # Cop supports --auto-correct.
@ -22,15 +21,13 @@ Layout/ParameterAlignment:
Layout/CaseIndentation: Layout/CaseIndentation:
Exclude: Exclude:
- 'lib/vmpooler/api/helpers.rb' - 'lib/vmpooler/api/helpers.rb'
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
Layout/ClosingParenthesisIndentation: Layout/ClosingParenthesisIndentation:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
@ -61,16 +58,14 @@ Layout/EmptyLinesAroundModuleBody:
Layout/FirstHashElementIndentation: Layout/FirstHashElementIndentation:
Exclude: Exclude:
- 'lib/vmpooler/api/helpers.rb' - 'lib/vmpooler/api/helpers.rb'
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: Width, IgnoredPatterns. # Configuration parameters: Width, IgnoredPatterns.
Layout/IndentationWidth: Layout/IndentationWidth:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
@ -78,8 +73,7 @@ Layout/IndentationWidth:
# SupportedStyles: symmetrical, new_line, same_line # SupportedStyles: symmetrical, new_line, same_line
Layout/MultilineMethodCallBraceLayout: Layout/MultilineMethodCallBraceLayout:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
@ -93,16 +87,14 @@ Layout/SpaceAroundEqualsInParameterDefault:
# Cop supports --auto-correct. # Cop supports --auto-correct.
Layout/SpaceAroundKeyword: Layout/SpaceAroundKeyword:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment. # Configuration parameters: AllowForAlignment.
Layout/SpaceAroundOperators: Layout/SpaceAroundOperators:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 8 # Offense count: 8
# Cop supports --auto-correct. # Cop supports --auto-correct.
@ -117,16 +109,14 @@ Layout/SpaceInsideHashLiteralBraces:
# Cop supports --auto-correct. # Cop supports --auto-correct.
Layout/SpaceInsideParens: Layout/SpaceInsideParens:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 2 # Offense count: 2
# Configuration parameters: AllowSafeAssignment. # Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition: Lint/AssignmentInCondition:
Exclude: Exclude:
- 'lib/vmpooler/api/helpers.rb' - 'lib/vmpooler/api/helpers.rb'
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 2 # Offense count: 2
Lint/SuppressedException: Lint/SuppressedException:
@ -158,8 +148,7 @@ Lint/UselessAssignment:
Style/AndOr: Style/AndOr:
Exclude: Exclude:
- 'lib/vmpooler/api/helpers.rb' - 'lib/vmpooler/api/helpers.rb'
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
Style/CaseEquality: Style/CaseEquality:
@ -180,8 +169,7 @@ Style/For:
Style/HashSyntax: Style/HashSyntax:
Exclude: Exclude:
- 'lib/vmpooler/api/helpers.rb' - 'lib/vmpooler/api/helpers.rb'
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 4 # Offense count: 4
# Cop supports --auto-correct. # Cop supports --auto-correct.
@ -189,8 +177,7 @@ Style/HashSyntax:
Style/IfUnlessModifier: Style/IfUnlessModifier:
Exclude: Exclude:
- 'lib/vmpooler/api/helpers.rb' - 'lib/vmpooler/api/helpers.rb'
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 3 # Offense count: 3
# Cop supports --auto-correct. # Cop supports --auto-correct.
@ -198,15 +185,13 @@ Style/IfUnlessModifier:
# SupportedStyles: both, prefix, postfix # SupportedStyles: both, prefix, postfix
Style/NegatedIf: Style/NegatedIf:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 3 # Offense count: 3
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/Not: Style/Not:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
@ -215,30 +200,26 @@ Style/Not:
Style/NumericPredicate: Style/NumericPredicate:
Exclude: Exclude:
- 'spec/**/*' - 'spec/**/*'
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 2 # Offense count: 2
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/ParallelAssignment: Style/ParallelAssignment:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: AllowSafeAssignment. # Configuration parameters: AllowSafeAssignment.
Style/ParenthesesAroundCondition: Style/ParenthesesAroundCondition:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 2 # Offense count: 2
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/PerlBackrefs: Style/PerlBackrefs:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist. # Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist.
@ -254,8 +235,7 @@ Naming/PredicateName:
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/RedundantParentheses: Style/RedundantParentheses:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 2 # Offense count: 2
# Cop supports --auto-correct. # Cop supports --auto-correct.
@ -276,8 +256,7 @@ Style/RedundantSelf:
# SupportedStyles: single_quotes, double_quotes # SupportedStyles: single_quotes, double_quotes
Style/StringLiterals: Style/StringLiterals:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
@ -292,8 +271,7 @@ Style/TernaryParentheses:
# SupportedStyles: snake_case, camelCase # SupportedStyles: snake_case, camelCase
Naming/VariableName: Naming/VariableName:
Exclude: Exclude:
- 'lib/vmpooler/api/v1.rb' - 'lib/vmpooler/api/v3.rb'
- 'lib/vmpooler/api/v2.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.

View file

@ -4,6 +4,7 @@
- [VMPooler](#vmpooler) - [VMPooler](#vmpooler)
- [Usage](#usage) - [Usage](#usage)
- [Migrating to v3](#migrating-to-v3)
- [v2.0.0 note](#v200-note) - [v2.0.0 note](#v200-note)
- [Installation](#installation) - [Installation](#installation)
- [Dependencies](#dependencies) - [Dependencies](#dependencies)
@ -30,6 +31,38 @@ VMPooler provides configurable 'pools' of instantly-available (pre-provisioned)
At [Puppet, Inc.](http://puppet.com) we run acceptance tests on thousands of disposable VMs every day. VMPooler manages the life cycle of these VMs from request through deletion, with options available to pool ready instances, and provision on demand. At [Puppet, Inc.](http://puppet.com) we run acceptance tests on thousands of disposable VMs every day. VMPooler manages the life cycle of these VMs from request through deletion, with options available to pool ready instances, and provision on demand.
### Migrating to v3
Starting with the v3.x release, management of DNS records is implemented as DNS plugins, similar to compute providers. This means each pool configuration should be pointing to a configuration object in `:dns_config` to determine it's method of record management.
For those using the global `DOMAIN` environment variable or global `:config.domain` key, this means records were not previously being managed by VMPooler (presumably managed via dynamic dns), so it's value should be moved to `:dns_configs:<INSERT_YOUR_OWN_SYMBOL>:domain` with the value for `dns_class` for the config set to `dynamic-dns`.
For example, the following < v3.x configuration:
```yaml
:config:
domain: 'example.com'
```
becomes:
```yaml
:dns_configs:
:example:
dns_class: dynamic-dns
domain: 'example.com'
```
Then any pools that should have records created via the dns config above should now reference the named dns config in the `dns_plugin` key:
```yaml
:pools:
- name: 'debian-8-x86_64'
dns_plugin: 'example'
```
For those using the GCE provider, [vmpooler-provider-gce](https://github.com/puppetlabs/vmpooler-provider-gce), as of version 1.x the DNS management has been decoupled. See <https://github.com/puppetlabs/vmpooler-provider-gce#migrating-to-v1>
### v2.0.0 note ### v2.0.0 note
As of version 2.0.0, all providers other than the dummy one are now separate gems. Historically the vSphere provider was included within VMPooler itself. That code has been moved to the [puppetlabs/vmpooler-provider-vsphere](https://github.com/puppetlabs/vmpooler-provider-vsphere) repository and the `vmpooler-provider-vsphere` gem. To migrate from VMPooler 1.x to 2.0 you will need to ensure that `vmpooler-provider-vsphere` is installed along side the `vmpooler` gem. See the [Provider API](docs/PROVIDER_API.md) docs for more information. As of version 2.0.0, all providers other than the dummy one are now separate gems. Historically the vSphere provider was included within VMPooler itself. That code has been moved to the [puppetlabs/vmpooler-provider-vsphere](https://github.com/puppetlabs/vmpooler-provider-vsphere) repository and the `vmpooler-provider-vsphere` gem. To migrate from VMPooler 1.x to 2.0 you will need to ensure that `vmpooler-provider-vsphere` is installed along side the `vmpooler` gem. See the [Provider API](docs/PROVIDER_API.md) docs for more information.

View file

@ -1,936 +0,0 @@
# Table of contents
1. [API](#API)
2. [Token operations](#token)
3. [VM operations](#vmops)
4. [Add disks](#adddisks)
5. [VM snapshots](#vmsnapshots)
6. [Status and metrics](#statusmetrics)
7. [Pool configuration](#poolconfig)
8. [Ondemand VM provisioning](#ondemandvm)
### API <a name="API"></a>
vmpooler provides a REST API for VM management. The following examples use `curl` for communication.
#### Token operations <a name="token"></a>
Token-based authentication can be used when requesting or modifying VMs. The `/token` route can be used to create, query, or delete tokens. See the provided YAML configuration example, [vmpooler.yaml.example](vmpooler.yaml.example), for information on configuring an authentication store to use when performing token operations.
##### GET /token
Get a list of issued tokens.
Return codes:
* 200 OK
* 401 when not authorized
* 404 when config:auth not found or other error
```
$ curl -u jdoe --url vmpooler.example.com/api/v1/token
Enter host password for user 'jdoe':
```
```json
{
"ok": true,
"utpg2i2xswor6h8ttjhu3d47z53yy47y": {
"created": "2015-04-28 19:17:47 -0700"
}
}
```
##### POST /token
Generate a new authentication token.
Return codes:
* 200 OK
* 401 when not authorized
* 404 when config:auth not found
```
$ curl -X POST -u jdoe --url vmpooler.example.com/api/v1/token
Enter host password for user 'jdoe':
```
```json
{
"ok": true,
"token": "utpg2i2xswor6h8ttjhu3d47z53yy47y"
}
```
##### GET /token/&lt;token&gt;
Get information about an existing token (including associated VMs).
Return codes:
* 200 OK
* 404 when config:auth or token not found
```
$ curl --url vmpooler.example.com/api/v1/token/utpg2i2xswor6h8ttjhu3d47z53yy47y
```
```json
{
"ok": true,
"utpg2i2xswor6h8ttjhu3d47z53yy47y": {
"user": "jdoe",
"created": "2015-04-28 19:17:47 -0700",
"last": "2015-11-04 12:28:37 -0700",
"vms": {
"running": [
"dqs4914g2wjyy5w",
"hul7ib0ssr0f4o0"
]
}
}
}
```
##### DELETE /token/&lt;token&gt;
Delete an authentication token.
Return codes:
* 200 OK
* 401 when not authorized
* 404 when config:auth not found
```
$ curl -X DELETE -u jdoe --url vmpooler.example.com/api/v1/token/utpg2i2xswor6h8ttjhu3d47z53yy47y
Enter host password for user 'jdoe':
```
```json
{
"ok": true
}
```
#### VM operations <a name="vmops"></a>
##### GET /vm
Retrieve a list of available VM pools.
Return codes:
* 200 OK
```
$ curl --url vmpooler.example.com/api/v1/vm
```
```json
[
"debian-7-i386",
"debian-7-x86_64"
]
```
##### POST /vm
Useful for batch operations; post JSON (see format below), get back allocated VMs.
If an authentication store is configured, an authentication token supplied via the `X-AUTH-TOKEN` HTTP header will modify a VM's default lifetime. See the provided YAML configuration example, [vmpooler.yaml.example](vmpooler.yaml.example), and the 'token operations' section above for more information.
Return codes:
* 200 OK
* 404 when sending invalid JSON in the request body or requesting an invalid VM pool name
* 503 when the vm failed to allocate a vm, or the pool is empty
```
$ curl -d '{"debian-7-i386":"2","debian-7-x86_64":"1"}' --url vmpooler.example.com/api/v1/vm
```
```json
{
"ok": true,
"debian-7-i386": {
"hostname": [
"o41xtodlvnvu5cw",
"khirruvwfjlmx3y"
]
},
"debian-7-x86_64": {
"hostname": "y91qbrpbfj6d13q"
},
"domain": "example.com"
}
```
**NOTE: Returns either all requested VMs or no VMs.**
##### POST /vm/&lt;pool&gt;
Check-out a VM or VMs.
Return codes:
* 200 OK
* 404 when sending invalid JSON in the request body or requesting an invalid VM pool name
* 503 when the vm failed to allocate a vm, or the pool is empty
```
$ curl -d --url vmpooler.example.com/api/v1/vm/debian-7-i386
```
```json
{
"ok": true,
"debian-7-i386": {
"hostname": "fq6qlpjlsskycq6"
},
"domain": "example.com"
}
```
Multiple VMs can be requested by using multiple query parameters in the URL:
```
$ curl -d --url vmpooler.example.com/api/v1/vm/debian-7-i386+debian-7-i386+debian-7-x86_64
```
```json
{
"ok": true,
"debian-7-i386": {
"hostname": [
"sc0o4xqtodlul5w",
"4m4dkhqiufnjmxy"
]
},
"debian-7-x86_64": {
"hostname": "zb91y9qbrbf6d3q"
},
"domain": "example.com"
}
```
**NOTE: Returns either all requested VMs or no VMs.**
##### GET /vm/&lt;hostname&gt;
Query metadata information for a checked-out VM.
Return codes:
* 200 OK
* 404 when requesting an invalid VM hostname
```
$ curl --url vmpooler.example.com/api/v1/vm/pxpmtoonx7fiqg6
```
```json
{
"ok": true,
"pxpmtoonx7fiqg6": {
"template": "centos-6-x86_64",
"lifetime": 12,
"running": 3,
"remaining": 9,
"state": "running",
"tags": {
"department": "engineering",
"user": "jdoe"
},
"ip": "192.168.0.1",
"domain": "example.com",
"host": "host1.example.com",
"migrated": "true"
}
}
```
##### PUT /vm/&lt;hostname&gt;
Modify a checked-out VM.
The following are valid PUT parameters and their required data structures:
parameter | description | required structure
--------- | ----------- | ------------------
*lifetime* | VM TTL (in hours) | integer
*tags* | free-form VM tagging | hash
Any modifications can be verified using the [GET /vm/&lt;hostname&gt;](#get-vmhostname) endpoint.
If an authentication store is configured, an authentication token is required (via the `X-AUTH-TOKEN` HTTP header) to access this route. See the provided YAML configuration example, [vmpooler.yaml.example](vmpooler.yaml.example), and the 'token operations' section above for more information.
Return codes:
* 200 OK
* 401 when you need an auth token
* 404 when requesting an invalid VM hostname
* 400 when supplied PUT parameters fail validation
```
$ curl -X PUT -d '{"lifetime":"2"}' --url vmpooler.example.com/api/v1/vm/fq6qlpjlsskycq6
```
```json
{
"ok": true
}
```
```
$ curl -X PUT -d '{"tags":{"department":"engineering","user":"jdoe"}}' --url vmpooler.example.com/api/v1/vm/fq6qlpjlsskycq6
```
```json
{
"ok": true
}
```
##### DELETE /vm/&lt;hostname&gt;
Schedule a checked-out VM for deletion.
Return codes:
* 200 OK
* 401 when you need an auth token
* 404 when requesting an invalid VM hostname
```
$ curl -X DELETE --url vmpooler.example.com/api/v1/vm/fq6qlpjlsskycq6
```
```json
{
"ok": true
}
```
#### Adding additional disk(s) <a name="adddisks"></a>
##### POST /vm/&lt;hostname&gt;/disk/&lt;size&gt;
Add an additional disk to a running VM.
Return codes:
* 202 OK
* 401 when you need an auth token
* 404 when requesting an invalid VM hostname or size is not an integer
````
$ curl -X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.example.com/api/v1/vm/fq6qlpjlsskycq6/disk/8
````
````json
{
"ok": true,
"fq6qlpjlsskycq6": {
"disk": "+8gb"
}
}
````
Provisioning and attaching disks can take a moment, but once the task completes it will be reflected in a `GET /vm/<hostname>` query:
````
$ curl --url vmpooler.example.com/api/v1/vm/fq6qlpjlsskycq6
````
````json
{
"ok": true,
"fq6qlpjlsskycq6": {
"template": "debian-7-x86_64",
"lifetime": 2,
"running": 0.08,
"state": "running",
"disk": [
"+8gb"
],
"domain": "delivery.puppetlabs.net"
}
}
````
#### VM snapshots <a name="vmsnapshots"></a>
##### POST /vm/&lt;hostname&gt;/snapshot
Create a snapshot of a running VM.
Return codes:
* 202 OK
* 401 when you need an auth token
* 404 when requesting an invalid VM hostname
````
$ curl -X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.example.com/api/v1/vm/fq6qlpjlsskycq6/snapshot
````
````json
{
"ok": true,
"fq6qlpjlsskycq6": {
"snapshot": "n4eb4kdtp7rwv4x158366vd9jhac8btq"
}
}
````
Snapshotting a live VM can take a moment, but once the snapshot task completes it will be reflected in a `GET /vm/<hostname>` query:
````
$ curl --url vmpooler.example.com/api/v1/vm/fq6qlpjlsskycq6
````
````json
{
"ok": true,
"fq6qlpjlsskycq6": {
"template": "debian-7-x86_64",
"lifetime": 2,
"running": 0.08,
"state": "running",
"snapshots": [
"n4eb4kdtp7rwv4x158366vd9jhac8btq"
],
"domain": "delivery.puppetlabs.net"
}
}
````
##### POST /vm/&lt;hostname&gt;/snapshot/&lt;snapshot&gt;
Revert a VM back to a snapshot.
Return codes:
* 202 OK
* 401 when you need an auth token
* 404 when requesting an invalid VM hostname or snapshot is not valid
````
$ curl X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.example.com/api/v1/vm/fq6qlpjlsskycq6/snapshot/n4eb4kdtp7rwv4x158366vd9jhac8btq
````
````json
{
"ok": true
}
````
#### Status and metrics <a name="statusmetrics"></a>
##### GET /status
A "live" status endpoint, representing the current state of the service.
```
$ curl --url vmpooler.example.com/api/v1/status
```
```json
{
"capacity": {
"current": 716,
"total": 717,
"percent": 99.9
},
"clone": {
"duration": {
"average": 8.8,
"min": 2.79,
"max": 69.76
},
"count": {
"total": 1779
}
},
"queue": {
"pending": 1,
"cloning": 0,
"booting": 1,
"ready": 716,
"running": 142,
"completed": 0,
"total": 859
},
"status": {
"ok": true,
"message": "Battle station fully armed and operational."
}
}
```
If there are empty pools, the "status" section will convey this:
```json
"status": {
"ok": false,
"message": "Found 2 empty pools.",
"empty": [
"centos-6-x86_64",
"debian-7-x86_64"
]
}
```
The top level sections are: "capacity", "queue", "clone", "boot", "pools" and "status".
If the query parameter 'view' is provided, it will be used to select which top level
element to compute and return. Select them by specifying which one you want in a comma
separated list.
For example `vmpooler.example.com/api/v1/status?view=capacity,boot`
##### GET /summary[?from=YYYY-MM-DD[&to=YYYY-MM-DD]]
Returns a summary, or report, for the timespan between `from` and `to` (inclusive)
parameters. The response includes both an overall and daily view of tracked
metrics, such as boot and cloning durations.
Any omitted query parameter will default to now/today. A request without any
parameters will result in the current day's summary.
Return codes:
* 200 OK
* 400 Invalid date format or range
```
$ curl --url vmpooler.example.com/api/v1/summary
```
```json
{
"boot": {
"duration": {
"average": 106.6,
"min": 83.09,
"max": 121.06,
"total": 639.36,
},
"count": {
"average": 6,
"min": 6,
"max": 6,
"total": 6,
}
},
"clone": {
"duration": {
"average": 4.6,
"min": 2.78,
"max": 8.1,
"total": 63.94,
},
"count": {
"average": 14,
"min": 14,
"max": 14,
"total": 14,
}
},
"tag": {
"department": {
"engineering": 14,
"help desk": 10,
"IT": 44,
"total": 68
},
"user": {
"arodgers": 54,
"cmatthews": 10,
"jnelson": 4,
"total": 68
}
},
"daily": [
{
"date": "2015-03-11",
"boot": {
"duration": {
"average": 106.6,
"min": 83.09,
"max": 121.06,
"total": 639.36
},
"count": {
"total": 6
}
},
"clone": {
"duration": {
"average": 4.6,
"min": 2.78,
"max": 8.1,
"total": 63.94
},
"count": {
"total": 14
}
},
"tag": {
"department": {
"engineering": 14,
"help desk": 10,
"IT": 44,
"total": 68
},
"user": {
"arodgers": 54,
"cmatthews": 10,
"jnelson": 4,
"total": 68
}
}
}
]
}
```
```
$ curl -G -d 'from=2015-03-10' -d 'to=2015-03-11' --url vmpooler.example.com/api/v1/summary
```
```json
{
"boot": {...},
"clone": {...},
"daily": [
{
"date": "2015-03-10",
"boot": {
"duration": {
"average": 0,
"min": 0,
"max": 0,
"total": 0
},
"count": {
"total": 0
}
},
"clone": {
"duration": {
"average": 0,
"min": 0,
"max": 0,
"total": 0
},
"count": {
"total": 0
}
},
"tag": { }
},
{
"date": "2015-03-11",
"boot": {
"duration": {
"average": 106.6,
"min": 83.09,
"max": 121.06,
"total": 639.36
},
"count": {
"total": 6
}
},
"clone": {
"duration": {
"average": 4.6,
"min": 2.78,
"max": 8.1,
"total": 63.94
},
"count": {
"total": 14
}
},
"tag": { }
}
]
}
```
You can also query only the specific top level section you want by including it after `summary/`.
The valid sections are "boot", "clone" or "tag" eg. `vmpooler.example.com/api/v1/summary/boot/`.
You can further drill-down the data by specifying the second level parameter to query eg
`vmpooler.example.com/api/v1/summary/tag/created_by`
##### GET /poolstat?pool=FOO
For parameter `pool`, containing a comma separated list of pool names to query, this endpoint returns
each of the pool's ready, max and alias information. It can be used to get a fast response for
the required pools instead of using the /status API endpoint
Return codes
* 200 OK
```
$ curl https://vmpooler.example.com/api/v1/poolstat?pool=centos-6-x86_64
```
```json
{
"pools": {
"centos-6-x86_64": {
"ready": 25,
"max": 25,
"alias": [
"centos-6-64",
"centos-6-amd64"
]
}
}
}
```
##### GET /totalrunning
Fast endpoint to return the total number of VMs in a 'running' state
Return codes
* 200 OK
```
$ curl https://vmpooler.example.com/api/v1/totalrunning
```
```json
{
"running": 362
}
```
#### Managing pool configuration via API <a name="poolconfig"></a>
##### GET /config
Returns the running pool configuration
Return codes
* 200 OK
* 400 No configuration found
```
$ curl https://vmpooler.example.com/api/v1/config
```
```json
{
"pool_configuration": [
{
"name": "redhat-7-x86_64",
"template": "templates/redhat-7.2-x86_64-0.0.3",
"folder": "vmpooler/redhat-7-x86_64",
"datastore": "stor1",
"size": 1,
"datacenter": "dc1",
"provider": "vsphere",
"capacity": 1,
"major": "redhat",
"template_ready": true
}
],
"status": {
"ok": true
}
}
```
Note: to enable poolsize and pooltemplate config endpoints it is necessary to set 'experimental_features: true' in your vmpooler configuration. A 405 is returned when you attempt to interact with these endpoints when this configuration option is not set.
##### POST /config/poolsize
Change pool size without having to restart the service.
All pool template changes requested must be for pools that exist in the vmpooler configuration running, or a 404 code will be returned
When a pool size is changed due to the configuration posted a 201 status will be returned. When the pool configuration is valid, but will not result in any changes, 200 is returned.
Pool size configuration changes persist through application restarts, and take precedence over a pool size value configured in the pool configuration provided when the application starts. This persistence is dependent on redis. So, if the redis data is lost then the configuration updates revert to those provided at startup at the next application start.
An authentication token is required in order to change pool configuration when authentication is configured.
Responses:
* 200 - No changes required
* 201 - Changes made on at least one pool with changes requested
* 400 - An invalid configuration was provided causing requested changes to fail
* 404 - An unknown error occurred
* 405 - The endpoint is disabled because experimental features are disabled
```
$ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"2","debian-7-x86_64":"1"}' --url https://vmpooler.example.com/api/v1/config/poolsize
```
```json
{
"ok": true
}
```
##### DELETE /config/poolsize/&lt;pool&gt;
Delete an overridden pool size. This results in the values from VMPooler's config being used.
Return codes:
* 200 - when nothing was changed but no error occurred
* 201 - size reset successful
* 401 - when not authorized
* 404 - pool does not exist
* 405 - The endpoint is disabled because experimental features are disabled
```
$ curl -X DELETE -u jdoe --url vmpooler.example.com/api/v1/poolsize/almalinux-8-x86_64
```
```json
{
"ok": true,
"pool_size_before_overrides": 2,
"pool_size_before_reset": 4
}
```
##### POST /config/pooltemplate
Change the template configured for a pool, and replenish the pool with instances built from the new template.
All pool template changes requested must be for pools that exist in the vmpooler configuration running, or a 404 code will be returned
When a pool template is changed due to the configuration posted a 201 status will be returned. When the pool configuration is valid, but will not result in any changes, 200 is returned.
A pool template being updated will cause the following actions, which are logged in vmpooler.log:
* Destroy all instances for the pool template being updated that are in the ready and pending state
* Halt repopulating the pool while creating template deltas for the newly configured template
* Unblock pool population and let the pool replenish with instances based on the newly configured template
Pool template changes persist through application restarts, and take precedence over a pool template configured in the pool configuration provided when the application starts. This persistence is dependent on redis. As a result, if the redis data is lost then the configuration values revert to those provided at startup at the next application start.
An authentication token is required in order to change pool configuration when authentication is configured.
Responses:
* 200 - No changes required
* 201 - Changes made on at least one pool with changes requested
* 400 - An invalid configuration was provided causing requested changes to fail
* 404 - An unknown error occurred
* 405 - The endpoint is disabled because experimental features are disabled
```
$ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"templates/debian-7-i386"}' --url https://vmpooler.example.com/api/v1/config/pooltemplate
```
```json
{
"ok": true
}
```
##### DELETE /config/pooltemplate/&lt;pool&gt;
Delete an overridden pool template. This results in the values from VMPooler's config being used.
Return codes:
* 200 - when nothing was changed but no error occurred
* 201 - template reset successful
* 401 - when not authorized
* 404 - pool does not exist
* 405 - The endpoint is disabled because experimental features are disabled
```
$ curl -X DELETE -u jdoe --url vmpooler.example.com/api/v1/pooltemplate/almalinux-8-x86_64
```
```json
{
"ok": true,
"template_before_overrides": "templates/almalinux-8-x86_64-0.0.2",
"template_before_reset": "templates/almalinux-8-x86_64-0.0.3-beta"
}
```
##### POST /poolreset
Clear all pending and ready instances in a pool, and deploy replacements
All pool reset requests must be for pools that exist in the vmpooler configuration running, or a 404 code will be returned.
When a pool reset is requested a 201 status will be returned.
A pool reset will cause vmpooler manager to log that it has cleared ready and pending instances.
For poolreset to be available it is necessary to enable experimental features. Additionally, the request must be performed with an authentication token when authentication is configured.
Responses:
* 201 - Pool reset requested received
* 400 - An invalid configuration was provided causing requested changes to fail
* 404 - An unknown error occurred
* 405 - The endpoint is disabled because experimental features are disabled
```
$ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"1"}' --url https://vmpooler.example.com/api/v1/poolreset
```
```json
{
"ok": true
}
```
#### Ondemand VM operations <a name="ondemandvm"></a>
Ondemand VM operations offer a user an option to directly request instances to be allocated for use. This can be very useful when supporting a wide range of images because idle instances can be eliminated.
##### POST /ondemandvm
All instance types requested must match a pool name or alias in the running application configuration, or a 404 code will be returned
When a provisioning request is accepted the API will return an indication that the request is successful. You may then poll /ondemandvm to monitor request status.
An authentication token is required in order to request instances on demand when authentication is configured.
Responses:
* 201 - Provisioning request accepted
* 400 - Payload contains invalid JSON and cannot be parsed
* 401 - No auth token provided, or provided auth token is not valid, and auth is enabled
* 403 - Request exceeds the configured per pool maximum
* 404 - A pool was requested, which is not available in the running configuration, or an unknown error occurred.
* 409 - A request of the matching ID has already been created
```
$ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"4"}' --url https://vmpooler.example.com/api/v1/ondemandvm
```
```json
{
"ok": true,
"request_id": "e3ff6271-d201-4f31-a315-d17f4e15863a"
}
```
##### GET /ondemandvm
Get the status of an ondemandvm request that has already been posted.
When the request is ready the ready status will change to 'true'.
The number of instances pending vs ready will be reflected in the API response.
Responses:
* 200 - The API request was successful and the status is ok
* 202 - The request is not ready yet
* 404 - The request can not be found, or an unknown error occurred
```
$ curl https://vmpooler.example.com/api/v1/ondemandvm/e3ff6271-d201-4f31-a315-d17f4e15863a
```
```json
{
"ok": true,
"request_id": "e3ff6271-d201-4f31-a315-d17f4e15863a",
"ready": false,
"debian-7-i386": {
"ready": "3",
"pending": "1"
}
}
```
```json
{
"ok": true,
"request_id": "e3ff6271-d201-4f31-a315-d17f4e15863a",
"ready": true,
"debian-7-i386": {
"hostname": [
"vm1",
"vm2",
"vm3",
"vm4"
]
}
}
```
##### DELETE /ondemandvm
Delete a ondemand request
Deleting a ondemand request will delete any instances created for the request and mark the backend data for expiration in two weeks. Any subsequent attempts to retrieve request data will indicate it has been deleted.
Responses:
* 200 - The API request was sucessful. A message will indicate if the request has already been deleted.
* 401 - No auth token provided, or provided auth token is not valid, and auth is enabled
* 404 - The request can not be found, or an unknown error occurred.
```
$ curl -X DELETE https://vmpooler.example.com/api/v1/ondemandvm/e3ff6271-d201-4f31-a315-d17f4e15863a
```
```json
{
"ok": true
}
```

View file

@ -12,6 +12,15 @@
vmpooler provides a REST API for VM management. The following examples use `curl` for communication. vmpooler provides a REST API for VM management. The following examples use `curl` for communication.
## Major change in V3 versus V2
The api/v1 and api/v2 endpoints have been removed. Additionally, the generic api endpoint that reroutes to a versioned endpoint has been removed.
The api/v3 endpoint removes the deprecated "domain" key returned in some of the operations like getting a VM, etc. If there is a "domain" configured in the top level configuration or for a specific provider,
the hostname now returns an FQDN including that domain. That is to say, we can now have multiple, different domains for each pool instead of only a single domain for all pools, or a domain restricted to a particular provider.
Clients using some of the direct API paths (without specifying api/v1 or api/v2) will now now need to specify the versioned endpoint (api/v3).
## Major change in V2 versus V1 ## Major change in V2 versus V1
The api/v2 endpoint removes a separate "domain" key returned in some of the operations like getting a VM, etc. If there is a "domain" configured in the top level configuration or for a specific provider, The api/v2 endpoint removes a separate "domain" key returned in some of the operations like getting a VM, etc. If there is a "domain" configured in the top level configuration or for a specific provider,
@ -41,7 +50,7 @@ Return codes:
* 404 when config:auth not found or other error * 404 when config:auth not found or other error
``` ```
$ curl -u jdoe --url vmpooler.example.com/api/v2/token $ curl -u jdoe --url vmpooler.example.com/api/v3/token
Enter host password for user 'jdoe': Enter host password for user 'jdoe':
``` ```
```json ```json
@ -63,7 +72,7 @@ Return codes:
* 404 when config:auth not found * 404 when config:auth not found
``` ```
$ curl -X POST -u jdoe --url vmpooler.example.com/api/v2/token $ curl -X POST -u jdoe --url vmpooler.example.com/api/v3/token
Enter host password for user 'jdoe': Enter host password for user 'jdoe':
``` ```
```json ```json
@ -82,7 +91,7 @@ Return codes:
* 404 when config:auth or token not found * 404 when config:auth or token not found
``` ```
$ curl --url vmpooler.example.com/api/v2/token/utpg2i2xswor6h8ttjhu3d47z53yy47y $ curl --url vmpooler.example.com/api/v3/token/utpg2i2xswor6h8ttjhu3d47z53yy47y
``` ```
```json ```json
{ {
@ -111,7 +120,7 @@ Return codes:
* 404 when config:auth not found * 404 when config:auth not found
``` ```
$ curl -X DELETE -u jdoe --url vmpooler.example.com/api/v2/token/utpg2i2xswor6h8ttjhu3d47z53yy47y $ curl -X DELETE -u jdoe --url vmpooler.example.com/api/v3/token/utpg2i2xswor6h8ttjhu3d47z53yy47y
Enter host password for user 'jdoe': Enter host password for user 'jdoe':
``` ```
```json ```json
@ -130,7 +139,7 @@ Return codes:
* 200 OK * 200 OK
``` ```
$ curl --url vmpooler.example.com/api/v2/vm $ curl --url vmpooler.example.com/api/v3/vm
``` ```
```json ```json
[ [
@ -151,7 +160,7 @@ Return codes:
* 503 when the vm failed to allocate a vm, or the pool is empty * 503 when the vm failed to allocate a vm, or the pool is empty
``` ```
$ curl -d '{"debian-7-i386":"2","debian-7-x86_64":"1"}' --url vmpooler.example.com/api/v2/vm $ curl -d '{"debian-7-i386":"2","debian-7-x86_64":"1"}' --url vmpooler.example.com/api/v3/vm
``` ```
```json ```json
{ {
@ -180,7 +189,7 @@ Return codes:
* 503 when the vm failed to allocate a vm, or the pool is empty * 503 when the vm failed to allocate a vm, or the pool is empty
``` ```
$ curl -d --url vmpooler.example.com/api/v2/vm/debian-7-i386 $ curl -d --url vmpooler.example.com/api/v3/vm/debian-7-i386
``` ```
```json ```json
{ {
@ -194,7 +203,7 @@ $ curl -d --url vmpooler.example.com/api/v2/vm/debian-7-i386
Multiple VMs can be requested by using multiple query parameters in the URL: Multiple VMs can be requested by using multiple query parameters in the URL:
``` ```
$ curl -d --url vmpooler.example.com/api/v2/vm/debian-7-i386+debian-7-i386+debian-7-x86_64 $ curl -d --url vmpooler.example.com/api/v3/vm/debian-7-i386+debian-7-i386+debian-7-x86_64
``` ```
```json ```json
@ -223,7 +232,7 @@ Return codes:
* 404 when requesting an invalid VM hostname * 404 when requesting an invalid VM hostname
``` ```
$ curl --url vmpooler.example.com/api/v2/vm/pxpmtoonx7fiqg6 $ curl --url vmpooler.example.com/api/v3/vm/pxpmtoonx7fiqg6
``` ```
```json ```json
{ {
@ -267,7 +276,7 @@ Return codes:
* 400 when supplied PUT parameters fail validation * 400 when supplied PUT parameters fail validation
``` ```
$ curl -X PUT -d '{"lifetime":"2"}' --url vmpooler.example.com/api/v2/vm/fq6qlpjlsskycq6 $ curl -X PUT -d '{"lifetime":"2"}' --url vmpooler.example.com/api/v3/vm/fq6qlpjlsskycq6
``` ```
```json ```json
{ {
@ -276,7 +285,7 @@ $ curl -X PUT -d '{"lifetime":"2"}' --url vmpooler.example.com/api/v2/vm/fq6qlpj
``` ```
``` ```
$ curl -X PUT -d '{"tags":{"department":"engineering","user":"jdoe"}}' --url vmpooler.example.com/api/v2/vm/fq6qlpjlsskycq6 $ curl -X PUT -d '{"tags":{"department":"engineering","user":"jdoe"}}' --url vmpooler.example.com/api/v3/vm/fq6qlpjlsskycq6
``` ```
```json ```json
{ {
@ -294,7 +303,7 @@ Return codes:
* 404 when requesting an invalid VM hostname * 404 when requesting an invalid VM hostname
``` ```
$ curl -X DELETE --url vmpooler.example.com/api/v2/vm/fq6qlpjlsskycq6 $ curl -X DELETE --url vmpooler.example.com/api/v3/vm/fq6qlpjlsskycq6
``` ```
```json ```json
{ {
@ -314,7 +323,7 @@ Return codes:
* 404 when requesting an invalid VM hostname or size is not an integer * 404 when requesting an invalid VM hostname or size is not an integer
```` ````
$ curl -X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.example.com/api/v2/vm/fq6qlpjlsskycq6/disk/8 $ curl -X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.example.com/api/v3/vm/fq6qlpjlsskycq6/disk/8
```` ````
````json ````json
{ {
@ -328,7 +337,7 @@ $ curl -X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.e
Provisioning and attaching disks can take a moment, but once the task completes it will be reflected in a `GET /vm/<hostname>` query: Provisioning and attaching disks can take a moment, but once the task completes it will be reflected in a `GET /vm/<hostname>` query:
```` ````
$ curl --url vmpooler.example.com/api/v2/vm/fq6qlpjlsskycq6 $ curl --url vmpooler.example.com/api/v3/vm/fq6qlpjlsskycq6
```` ````
````json ````json
{ {
@ -358,7 +367,7 @@ Return codes:
* 404 when requesting an invalid VM hostname * 404 when requesting an invalid VM hostname
```` ````
$ curl -X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.example.com/api/v2/vm/fq6qlpjlsskycq6/snapshot $ curl -X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.example.com/api/v3/vm/fq6qlpjlsskycq6/snapshot
```` ````
````json ````json
{ {
@ -372,7 +381,7 @@ $ curl -X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.e
Snapshotting a live VM can take a moment, but once the snapshot task completes it will be reflected in a `GET /vm/<hostname>` query: Snapshotting a live VM can take a moment, but once the snapshot task completes it will be reflected in a `GET /vm/<hostname>` query:
```` ````
$ curl --url vmpooler.example.com/api/v2/vm/fq6qlpjlsskycq6 $ curl --url vmpooler.example.com/api/v3/vm/fq6qlpjlsskycq6
```` ````
````json ````json
{ {
@ -399,7 +408,7 @@ Return codes:
* 404 when requesting an invalid VM hostname or snapshot is not valid * 404 when requesting an invalid VM hostname or snapshot is not valid
```` ````
$ curl X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.example.com/api/v2/vm/fq6qlpjlsskycq6/snapshot/n4eb4kdtp7rwv4x158366vd9jhac8btq $ curl X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.example.com/api/v3/vm/fq6qlpjlsskycq6/snapshot/n4eb4kdtp7rwv4x158366vd9jhac8btq
```` ````
````json ````json
{ {
@ -414,7 +423,7 @@ $ curl X POST -H X-AUTH-TOKEN:a9znth9dn01t416hrguu56ze37t790bl --url vmpooler.ex
A "live" status endpoint, representing the current state of the service. A "live" status endpoint, representing the current state of the service.
``` ```
$ curl --url vmpooler.example.com/api/v2/status $ curl --url vmpooler.example.com/api/v3/status
``` ```
```json ```json
{ {
@ -466,7 +475,7 @@ The top level sections are: "capacity", "queue", "clone", "boot", "pools" and "s
If the query parameter 'view' is provided, it will be used to select which top level If the query parameter 'view' is provided, it will be used to select which top level
element to compute and return. Select them by specifying which one you want in a comma element to compute and return. Select them by specifying which one you want in a comma
separated list. separated list.
For example `vmpooler.example.com/api/v2/status?view=capacity,boot` For example `vmpooler.example.com/api/v3/status?view=capacity,boot`
##### GET /summary[?from=YYYY-MM-DD[&to=YYYY-MM-DD]] ##### GET /summary[?from=YYYY-MM-DD[&to=YYYY-MM-DD]]
@ -483,7 +492,7 @@ Return codes:
``` ```
$ curl --url vmpooler.example.com/api/v2/summary $ curl --url vmpooler.example.com/api/v3/summary
``` ```
```json ```json
{ {
@ -575,7 +584,7 @@ $ curl --url vmpooler.example.com/api/v2/summary
``` ```
$ curl -G -d 'from=2015-03-10' -d 'to=2015-03-11' --url vmpooler.example.com/api/v2/summary $ curl -G -d 'from=2015-03-10' -d 'to=2015-03-11' --url vmpooler.example.com/api/v3/summary
``` ```
```json ```json
{ {
@ -639,9 +648,9 @@ $ curl -G -d 'from=2015-03-10' -d 'to=2015-03-11' --url vmpooler.example.com/api
``` ```
You can also query only the specific top level section you want by including it after `summary/`. You can also query only the specific top level section you want by including it after `summary/`.
The valid sections are "boot", "clone" or "tag" eg. `vmpooler.example.com/api/v2/summary/boot/`. The valid sections are "boot", "clone" or "tag" eg. `vmpooler.example.com/api/v3/summary/boot/`.
You can further drill-down the data by specifying the second level parameter to query eg You can further drill-down the data by specifying the second level parameter to query eg
`vmpooler.example.com/api/v2/summary/tag/created_by` `vmpooler.example.com/api/v3/summary/tag/created_by`
##### GET /poolstat?pool=FOO ##### GET /poolstat?pool=FOO
@ -653,7 +662,7 @@ Return codes
* 200 OK * 200 OK
``` ```
$ curl https://vmpooler.example.com/api/v2/poolstat?pool=centos-6-x86_64 $ curl https://vmpooler.example.com/api/v3/poolstat?pool=centos-6-x86_64
``` ```
```json ```json
{ {
@ -678,7 +687,7 @@ Return codes
* 200 OK * 200 OK
``` ```
$ curl https://vmpooler.example.com/api/v2/totalrunning $ curl https://vmpooler.example.com/api/v3/totalrunning
``` ```
```json ```json
@ -700,7 +709,7 @@ Return codes
* 400 No configuration found * 400 No configuration found
``` ```
$ curl https://vmpooler.example.com/api/v2/config $ curl https://vmpooler.example.com/api/v3/config
``` ```
```json ```json
{ {
@ -744,7 +753,7 @@ Responses:
* 404 - An unknown error occurred * 404 - An unknown error occurred
* 405 - The endpoint is disabled because experimental features are disabled * 405 - The endpoint is disabled because experimental features are disabled
``` ```
$ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"2","debian-7-x86_64":"1"}' --url https://vmpooler.example.com/api/v2/config/poolsize $ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"2","debian-7-x86_64":"1"}' --url https://vmpooler.example.com/api/v3/config/poolsize
``` ```
```json ```json
{ {
@ -764,7 +773,7 @@ Return codes:
* 405 - The endpoint is disabled because experimental features are disabled * 405 - The endpoint is disabled because experimental features are disabled
``` ```
$ curl -X DELETE -u jdoe --url vmpooler.example.com/api/v2/poolsize/almalinux-8-x86_64 $ curl -X DELETE -u jdoe --url vmpooler.example.com/api/v3/poolsize/almalinux-8-x86_64
``` ```
```json ```json
{ {
@ -798,7 +807,7 @@ Responses:
* 404 - An unknown error occurred * 404 - An unknown error occurred
* 405 - The endpoint is disabled because experimental features are disabled * 405 - The endpoint is disabled because experimental features are disabled
``` ```
$ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"templates/debian-7-i386"}' --url https://vmpooler.example.com/api/v2/config/pooltemplate $ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"templates/debian-7-i386"}' --url https://vmpooler.example.com/api/v3/config/pooltemplate
``` ```
```json ```json
{ {
@ -818,7 +827,7 @@ Return codes:
* 405 - The endpoint is disabled because experimental features are disabled * 405 - The endpoint is disabled because experimental features are disabled
``` ```
$ curl -X DELETE -u jdoe --url vmpooler.example.com/api/v2/pooltemplate/almalinux-8-x86_64 $ curl -X DELETE -u jdoe --url vmpooler.example.com/api/v3/pooltemplate/almalinux-8-x86_64
``` ```
```json ```json
{ {
@ -846,7 +855,7 @@ Responses:
* 404 - An unknown error occurred * 404 - An unknown error occurred
* 405 - The endpoint is disabled because experimental features are disabled * 405 - The endpoint is disabled because experimental features are disabled
``` ```
$ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"1"}' --url https://vmpooler.example.com/api/v2/poolreset $ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"1"}' --url https://vmpooler.example.com/api/v3/poolreset
``` ```
```json ```json
{ {
@ -874,7 +883,7 @@ Responses:
* 404 - A pool was requested, which is not available in the running configuration, or an unknown error occurred. * 404 - A pool was requested, which is not available in the running configuration, or an unknown error occurred.
* 409 - A request of the matching ID has already been created * 409 - A request of the matching ID has already been created
``` ```
$ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"4"}' --url https://vmpooler.example.com/api/v2/ondemandvm $ curl -X POST -H "Content-Type: application/json" -d '{"debian-7-i386":"4"}' --url https://vmpooler.example.com/api/v3/ondemandvm
``` ```
```json ```json
{ {
@ -896,7 +905,7 @@ Responses:
* 202 - The request is not ready yet * 202 - The request is not ready yet
* 404 - The request can not be found, or an unknown error occurred * 404 - The request can not be found, or an unknown error occurred
``` ```
$ curl https://vmpooler.example.com/api/v2/ondemandvm/e3ff6271-d201-4f31-a315-d17f4e15863a $ curl https://vmpooler.example.com/api/v3/ondemandvm/e3ff6271-d201-4f31-a315-d17f4e15863a
``` ```
```json ```json
{ {
@ -936,7 +945,7 @@ Responses:
* 401 - No auth token provided, or provided auth token is not valid, and auth is enabled * 401 - No auth token provided, or provided auth token is not valid, and auth is enabled
* 404 - The request can not be found, or an unknown error occurred. * 404 - The request can not be found, or an unknown error occurred.
``` ```
$ curl -X DELETE https://vmpooler.example.com/api/v2/ondemandvm/e3ff6271-d201-4f31-a315-d17f4e15863a $ curl -X DELETE https://vmpooler.example.com/api/v3/ondemandvm/e3ff6271-d201-4f31-a315-d17f4e15863a
``` ```
```json ```json
{ {

View file

@ -19,11 +19,6 @@ Provide the entire configuration as a blob of yaml. Individual parameters passed
Path to a the file to use when loading the vmpooler configuration. This is only evaluated if `VMPOOLER_CONFIG` has not been specified. Path to a the file to use when loading the vmpooler configuration. This is only evaluated if `VMPOOLER_CONFIG` has not been specified.
### DOMAIN
This environment variable is to set the configuration config.domain
(optional)
### REDIS\_SERVER ### REDIS\_SERVER
The redis server to use for vmpooler. The redis server to use for vmpooler.

View file

@ -23,9 +23,13 @@
allowed_tags: allowed_tags:
- 'created_by' - 'created_by'
- 'project' - 'project'
domain: 'example.com'
prefix: 'poolvm-' prefix: 'poolvm-'
:dns_configs:
:example:
dns_class: dynamic-dns
domain: 'example.com'
:pools: :pools:
- name: 'debian-7-i386' - name: 'debian-7-i386'
alias: [ 'debian-7-32' ] alias: [ 'debian-7-32' ]
@ -36,6 +40,7 @@
timeout: 15 timeout: 15
ready_ttl: 1440 ready_ttl: 1440
provider: dummy provider: dummy
dns_plugin: 'example'
- name: 'debian-7-i386-stringalias' - name: 'debian-7-i386-stringalias'
alias: 'debian-7-32-stringalias' alias: 'debian-7-32-stringalias'
template: 'Templates/debian-7-i386' template: 'Templates/debian-7-i386'
@ -45,6 +50,7 @@
timeout: 15 timeout: 15
ready_ttl: 1440 ready_ttl: 1440
provider: dummy provider: dummy
dns_plugin: 'example'
- name: 'debian-7-x86_64' - name: 'debian-7-x86_64'
alias: [ 'debian-7-64', 'debian-7-amd64' ] alias: [ 'debian-7-64', 'debian-7-amd64' ]
template: 'Templates/debian-7-x86_64' template: 'Templates/debian-7-x86_64'
@ -54,6 +60,7 @@
timeout: 15 timeout: 15
ready_ttl: 1440 ready_ttl: 1440
provider: dummy provider: dummy
dns_plugin: 'example'
- name: 'debian-7-i386-noalias' - name: 'debian-7-i386-noalias'
template: 'Templates/debian-7-i386' template: 'Templates/debian-7-i386'
folder: 'Pooled VMs/debian-7-i386' folder: 'Pooled VMs/debian-7-i386'
@ -62,6 +69,7 @@
timeout: 15 timeout: 15
ready_ttl: 1440 ready_ttl: 1440
provider: dummy provider: dummy
dns_plugin: 'example'
- name: 'debian-7-x86_64-alias-otherpool-extended' - name: 'debian-7-x86_64-alias-otherpool-extended'
alias: [ 'debian-7-x86_64' ] alias: [ 'debian-7-x86_64' ]
template: 'Templates/debian-7-x86_64' template: 'Templates/debian-7-x86_64'
@ -71,4 +79,4 @@
timeout: 15 timeout: 15
ready_ttl: 1440 ready_ttl: 1440
provider: dummy provider: dummy
dns_plugin: 'example'

View file

@ -76,7 +76,10 @@ module Vmpooler
parsed_config[:config]['prefix'] = ENV['PREFIX'] || parsed_config[:config]['prefix'] || '' parsed_config[:config]['prefix'] = ENV['PREFIX'] || parsed_config[:config]['prefix'] || ''
parsed_config[:config]['logfile'] = ENV['LOGFILE'] if ENV['LOGFILE'] parsed_config[:config]['logfile'] = ENV['LOGFILE'] if ENV['LOGFILE']
parsed_config[:config]['site_name'] = ENV['SITE_NAME'] if ENV['SITE_NAME'] parsed_config[:config]['site_name'] = ENV['SITE_NAME'] if ENV['SITE_NAME']
parsed_config[:config]['domain'] = ENV['DOMAIN'] if ENV['DOMAIN'] if !parsed_config[:config]['domain'].nil? || !ENV['DOMAIN'].nil?
puts '[!] [error] The "domain" config setting has been removed in v3. Please see the docs for migrating the domain config to use a dns plugin at https://github.com/puppetlabs/vmpooler/blob/main/README.md#migrating-to-v3'
exit 1
end
parsed_config[:config]['clone_target'] = ENV['CLONE_TARGET'] if ENV['CLONE_TARGET'] parsed_config[:config]['clone_target'] = ENV['CLONE_TARGET'] if ENV['CLONE_TARGET']
parsed_config[:config]['timeout'] = string_to_int(ENV['TIMEOUT']) if ENV['TIMEOUT'] parsed_config[:config]['timeout'] = string_to_int(ENV['TIMEOUT']) if ENV['TIMEOUT']
parsed_config[:config]['vm_lifetime_auth'] = string_to_int(ENV['VM_LIFETIME_AUTH']) if ENV['VM_LIFETIME_AUTH'] parsed_config[:config]['vm_lifetime_auth'] = string_to_int(ENV['VM_LIFETIME_AUTH']) if ENV['VM_LIFETIME_AUTH']

View file

@ -3,7 +3,7 @@
module Vmpooler module Vmpooler
class API < Sinatra::Base class API < Sinatra::Base
# Load API components # Load API components
%w[helpers dashboard reroute v1 v2 request_logger healthcheck].each do |lib| %w[helpers dashboard v3 request_logger healthcheck].each do |lib|
require "vmpooler/api/#{lib}" require "vmpooler/api/#{lib}"
end end
# Load dashboard components # Load dashboard components
@ -52,9 +52,7 @@ module Vmpooler
use Vmpooler::Dashboard use Vmpooler::Dashboard
use Vmpooler::API::Dashboard use Vmpooler::API::Dashboard
use Vmpooler::API::Reroute use Vmpooler::API::V3
use Vmpooler::API::V1
use Vmpooler::API::V2
end end
# Get thee started O WebServer # Get thee started O WebServer

View file

@ -181,8 +181,7 @@ module Vmpooler
/^\d{4}-\d{2}-\d{2}$/ === date_str /^\d{4}-\d{2}-\d{2}$/ === date_str
end end
# NOTE: domain is not needed here, so we should update the callers of this method def hostname_shorten(hostname)
def hostname_shorten(hostname, domain=nil)
hostname[/[^.]+/] hostname[/[^.]+/]
end end

View file

@ -1,89 +0,0 @@
# frozen_string_literal: true
module Vmpooler
class API
class Reroute < Sinatra::Base
api_version = '1'
get '/status/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called /status/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/status")
end
get '/summary/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called /summary/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/summary")
end
get '/summary/:route/?:key?/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called /summary/:route/?:key?/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/summary/#{params[:route]}/#{params[:key]}")
end
get '/token/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called /token/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/token")
end
post '/token/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called post /token/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/token")
end
get '/token/:token/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called /token/:token/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/token/#{params[:token]}")
end
delete '/token/:token/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called delete /token/:token/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/token/#{params[:token]}")
end
get '/vm/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called /vm? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/vm")
end
post '/vm/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called post /vm? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/vm")
end
post '/vm/:template/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called post /vm/:template/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:template]}")
end
get '/vm/:hostname/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called /vm/:hostname/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}")
end
delete '/vm/:hostname/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called delete /vm/:hostname/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}")
end
put '/vm/:hostname/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called put /vm/:hostname/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}")
end
post '/vm/:hostname/snapshot/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called post /vm/:hostname/snapshot/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}/snapshot")
end
post '/vm/:hostname/snapshot/:snapshot/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called post /vm/:hostname/snapshot/:snapshot/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}/snapshot/#{params[:snapshot]}")
end
put '/vm/:hostname/disk/:size/?' do
puts "DEPRECATION WARNING a client (#{request.user_agent}) called put /vm/:hostname/disk/:size/? and got redirected to api_version=1, this behavior will change in the next major version, please modify the client to use v2 in advance"
call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}/disk/#{params[:size]}")
end
end
end
end

File diff suppressed because it is too large Load diff

View file

@ -1,505 +0,0 @@
# frozen_string_literal: true
require 'vmpooler/api/v1'
module Vmpooler
class API
class V2 < Vmpooler::API::V1
api_version = '2'
api_prefix = "/api/v#{api_version}"
def full_config
Vmpooler::API.settings.config
end
def get_template_aliases(template)
tracer.in_span("Vmpooler::API::V2.#{__method__}") do
result = []
aliases = Vmpooler::API.settings.config[:alias]
if aliases
result += aliases[template] if aliases[template].is_a?(Array)
template_backends << aliases[template] if aliases[template].is_a?(String)
end
result
end
end
# Fetch a single vm from a pool
#
# @param [String] template
# The template that the vm should be created from
#
# @return [Tuple] vmname, vmpool, vmtemplate
# Returns a tuple containing the vm's name, the pool it came from, and
# what template was used, if successful. Otherwise the tuple contains.
# nil values.
def fetch_single_vm(template)
tracer.in_span("Vmpooler::API::V2.#{__method__}") do
template_backends = [template]
aliases = Vmpooler::API.settings.config[:alias]
if aliases
template_backends += aliases[template] if aliases[template].is_a?(Array)
template_backends << aliases[template] if aliases[template].is_a?(String)
pool_index = pool_index(pools)
weighted_pools = {}
template_backends.each do |t|
next unless pool_index.key? t
index = pool_index[t]
clone_target = pools[index]['clone_target'] || config['clone_target']
next unless config.key?('backend_weight')
weight = config['backend_weight'][clone_target]
if weight
weighted_pools[t] = weight
end
end
if weighted_pools.count == template_backends.count
pickup = Pickup.new(weighted_pools)
selection = pickup.pick
template_backends.delete(selection)
template_backends.unshift(selection)
else
first = template_backends.sample
template_backends.delete(first)
template_backends.unshift(first)
end
end
checkoutlock.synchronize do
template_backends.each do |template_backend|
vms = backend.smembers("vmpooler__ready__#{template_backend}")
next if vms.empty?
vm = vms.pop
smoved = backend.smove("vmpooler__ready__#{template_backend}", "vmpooler__running__#{template_backend}", vm)
if smoved
return [vm, template_backend, template]
end
end
[nil, nil, nil]
end
end
end
# The domain in the result body will be set to the one associated with the
# last vm added. The part of the response is only being retained for
# backwards compatibility as the hostnames are now fqdn's instead of bare
# hostnames. This change is a result of now being able to specify a domain
# per pool. If no vm's in the result had a domain sepcified then the
# domain key will be omitted similar to how it was previously omitted if
# the global option domain wasn't specified.
def atomically_allocate_vms(payload)
tracer.in_span("Vmpooler::API::V2.#{__method__}") do |span|
result = { 'ok' => false }
failed = false
vms = [] # vmpool, vmname, vmtemplate
validate_token(backend) if Vmpooler::API.settings.config[:auth] and has_token?
payload.each do |requested, count|
count.to_i.times do |_i|
vmname, vmpool, vmtemplate = fetch_single_vm(requested)
if vmname
account_for_starting_vm(vmpool, vmname)
vms << [vmpool, vmname, vmtemplate]
metrics.increment("checkout.success.#{vmpool}")
update_user_metrics('allocate', vmname) if Vmpooler::API.settings.config[:config]['usage_stats']
else
failed = true
metrics.increment("checkout.empty.#{requested}")
break
end
end
end
if failed
vms.each do |(vmpool, vmname, _vmtemplate)|
return_vm_to_ready_state(vmpool, vmname)
end
span.add_event('error', attributes: {
'error.type' => 'Vmpooler::API::V2.atomically_allocate_vms',
'error.message' => '503 due to failing to allocate one or more vms'
})
status 503
else
vm_names = []
vms.each do |(vmpool, vmname, vmtemplate)|
vmdomain = Parsing.get_domain_for_pool(full_config, vmpool)
if vmdomain
vmfqdn = "#{vmname}.#{vmdomain}"
update_result_hosts(result, vmtemplate, vmfqdn)
vm_names.append(vmfqdn)
else
update_result_hosts(result, vmtemplate, vmname)
vm_names.append(vmname)
end
end
span.set_attribute('vmpooler.vm_names', vm_names.join(',')) unless vm_names.empty?
result['ok'] = true
end
result
end
end
def generate_ondemand_request(payload)
tracer.in_span("Vmpooler::API::V2.#{__method__}") do |span|
result = { 'ok': false }
requested_instances = payload.reject { |k, _v| k == 'request_id' }
if too_many_requested?(requested_instances)
e_message = "requested amount of instances exceeds the maximum #{config['max_ondemand_instances_per_request']}"
result['message'] = e_message
status 403
span.add_event('error', attributes: {
'error.type' => 'Vmpooler::API::V2.generate_ondemand_request',
'error.message' => "403 due to #{e_message}"
})
return result
end
score = Time.now.to_i
request_id = payload['request_id']
request_id ||= generate_request_id
result['request_id'] = request_id
span.set_attribute('vmpooler.request_id', request_id)
if backend.exists?("vmpooler__odrequest__#{request_id}")
e_message = "request_id '#{request_id}' has already been created"
result['message'] = e_message
status 409
span.add_event('error', attributes: {
'error.type' => 'Vmpooler::API::V2.generate_ondemand_request',
'error.message' => "409 due to #{e_message}"
})
metrics.increment('ondemandrequest_generate.duplicaterequests')
return result
end
status 201
platforms_with_aliases = []
requested_instances.each do |poolname, count|
selection = evaluate_template_aliases(poolname, count)
selection.map { |selected_pool, selected_pool_count| platforms_with_aliases << "#{poolname}:#{selected_pool}:#{selected_pool_count}" }
end
platforms_string = platforms_with_aliases.join(',')
return result unless backend.zadd('vmpooler__provisioning__request', score, request_id)
backend.hset("vmpooler__odrequest__#{request_id}", 'requested', platforms_string)
if Vmpooler::API.settings.config[:auth] and has_token?
token_token = request.env['HTTP_X_AUTH_TOKEN']
token_user = backend.hget("vmpooler__token__#{token_token}", 'user')
backend.hset("vmpooler__odrequest__#{request_id}", 'token:token', token_token)
backend.hset("vmpooler__odrequest__#{request_id}", 'token:user', token_user)
span.set_attribute('enduser.id', token_user)
end
result[:ok] = true
metrics.increment('ondemandrequest_generate.success')
result
end
end
# Endpoints that use overridden methods
post "#{api_prefix}/vm/?" do
content_type :json
result = { 'ok' => false }
metrics.increment('http_requests_vm_total.post.vm.checkout')
payload = JSON.parse(request.body.read)
if payload
invalid = invalid_templates(payload)
if invalid.empty?
result = atomically_allocate_vms(payload)
else
invalid.each do |bad_template|
metrics.increment("checkout.invalid.#{bad_template}")
end
status 404
end
else
metrics.increment('checkout.invalid.unknown')
status 404
end
JSON.pretty_generate(result)
end
post "#{api_prefix}/vm/:template/?" do
content_type :json
result = { 'ok' => false }
metrics.increment('http_requests_vm_total.get.vm.template')
payload = extract_templates_from_query_params(params[:template])
if payload
invalid = invalid_templates(payload)
if invalid.empty?
result = atomically_allocate_vms(payload)
else
invalid.each do |bad_template|
metrics.increment("checkout.invalid.#{bad_template}")
end
status 404
end
else
metrics.increment('checkout.invalid.unknown')
status 404
end
JSON.pretty_generate(result)
end
get "#{api_prefix}/vm/:hostname/?" do
content_type :json
metrics.increment('http_requests_vm_total.get.vm.hostname')
result = {}
status 404
result['ok'] = false
params[:hostname] = hostname_shorten(params[:hostname], nil)
rdata = backend.hgetall("vmpooler__vm__#{params[:hostname]}")
unless rdata.empty?
status 200
result['ok'] = true
result[params[:hostname]] = {}
result[params[:hostname]]['template'] = rdata['template']
result[params[:hostname]]['lifetime'] = (rdata['lifetime'] || config['vm_lifetime']).to_i
if rdata['destroy']
result[params[:hostname]]['running'] = ((Time.parse(rdata['destroy']) - Time.parse(rdata['checkout'])) / 60 / 60).round(2) if rdata['checkout']
result[params[:hostname]]['state'] = 'destroyed'
elsif rdata['checkout']
result[params[:hostname]]['running'] = ((Time.now - Time.parse(rdata['checkout'])) / 60 / 60).round(2)
result[params[:hostname]]['remaining'] = ((Time.parse(rdata['checkout']) + rdata['lifetime'].to_i*60*60 - Time.now) / 60 / 60).round(2)
result[params[:hostname]]['start_time'] = Time.parse(rdata['checkout']).to_datetime.rfc3339
result[params[:hostname]]['end_time'] = (Time.parse(rdata['checkout']) + rdata['lifetime'].to_i*60*60).to_datetime.rfc3339
result[params[:hostname]]['state'] = 'running'
elsif rdata['check']
result[params[:hostname]]['state'] = 'ready'
else
result[params[:hostname]]['state'] = 'pending'
end
rdata.keys.each do |key|
if key.match('^tag\:(.+?)$')
result[params[:hostname]]['tags'] ||= {}
result[params[:hostname]]['tags'][$1] = rdata[key]
end
if key.match('^snapshot\:(.+?)$')
result[params[:hostname]]['snapshots'] ||= []
result[params[:hostname]]['snapshots'].push($1)
end
end
if rdata['disk']
result[params[:hostname]]['disk'] = rdata['disk'].split(':')
end
# Look up IP address of the hostname
begin
ipAddress = TCPSocket.gethostbyname(params[:hostname])[3]
rescue StandardError
ipAddress = ""
end
result[params[:hostname]]['ip'] = ipAddress
if rdata['pool']
vmdomain = Parsing.get_domain_for_pool(full_config, rdata['pool'])
if vmdomain
result[params[:hostname]]['fqdn'] = "#{params[:hostname]}.#{vmdomain}"
end
end
result[params[:hostname]]['host'] = rdata['host'] if rdata['host']
result[params[:hostname]]['migrated'] = rdata['migrated'] if rdata['migrated']
end
JSON.pretty_generate(result)
end
post "#{api_prefix}/ondemandvm/?" do
content_type :json
metrics.increment('http_requests_vm_total.post.ondemand.requestid')
need_token! if Vmpooler::API.settings.config[:auth]
result = { 'ok' => false }
begin
payload = JSON.parse(request.body.read)
if payload
invalid = invalid_templates(payload.reject { |k, _v| k == 'request_id' })
if invalid.empty?
result = generate_ondemand_request(payload)
else
result[:bad_templates] = invalid
invalid.each do |bad_template|
metrics.increment("ondemandrequest_fail.invalid.#{bad_template}")
end
status 404
end
else
metrics.increment('ondemandrequest_fail.invalid.unknown')
status 404
end
rescue JSON::ParserError
span = OpenTelemetry::Trace.current_span
span.status = OpenTelemetry::Trace::Status.error('JSON payload could not be parsed')
status 400
result = {
'ok' => false,
'message' => 'JSON payload could not be parsed'
}
end
JSON.pretty_generate(result)
end
post "#{api_prefix}/ondemandvm/:template/?" do
content_type :json
result = { 'ok' => false }
metrics.increment('http_requests_vm_total.delete.ondemand.template')
need_token! if Vmpooler::API.settings.config[:auth]
payload = extract_templates_from_query_params(params[:template])
if payload
invalid = invalid_templates(payload.reject { |k, _v| k == 'request_id' })
if invalid.empty?
result = generate_ondemand_request(payload)
else
result[:bad_templates] = invalid
invalid.each do |bad_template|
metrics.increment("ondemandrequest_fail.invalid.#{bad_template}")
end
status 404
end
else
metrics.increment('ondemandrequest_fail.invalid.unknown')
status 404
end
JSON.pretty_generate(result)
end
get "#{api_prefix}/ondemandvm/:requestid/?" do
content_type :json
metrics.increment('http_requests_vm_total.get.ondemand.request')
status 404
result = check_ondemand_request(params[:requestid])
JSON.pretty_generate(result)
end
def check_ondemand_request(request_id)
tracer.in_span("Vmpooler::API::V2.#{__method__}") do |span|
span.set_attribute('vmpooler.request_id', request_id)
result = { 'ok' => false }
request_hash = backend.hgetall("vmpooler__odrequest__#{request_id}")
if request_hash.empty?
e_message = "no request found for request_id '#{request_id}'"
result['message'] = e_message
span.add_event('error', attributes: {
'error.type' => 'Vmpooler::API::V2.check_ondemand_request',
'error.message' => e_message
})
return result
end
result['request_id'] = request_id
result['ready'] = false
result['ok'] = true
status 202
case request_hash['status']
when 'ready'
result['ready'] = true
Parsing.get_platform_pool_count(request_hash['requested']) do |platform_alias, pool, _count|
instances = backend.smembers("vmpooler__#{request_id}__#{platform_alias}__#{pool}")
domain = Parsing.get_domain_for_pool(full_config, pool)
instances.map! { |instance| instance.concat(".#{domain}") } if domain
if result.key?(platform_alias)
result[platform_alias][:hostname] = result[platform_alias][:hostname] + instances
else
result[platform_alias] = { 'hostname': instances }
end
end
status 200
when 'failed'
result['message'] = "The request failed to provision instances within the configured ondemand_request_ttl '#{config['ondemand_request_ttl']}'"
status 200
when 'deleted'
result['message'] = 'The request has been deleted'
status 200
else
Parsing.get_platform_pool_count(request_hash['requested']) do |platform_alias, pool, count|
instance_count = backend.scard("vmpooler__#{request_id}__#{platform_alias}__#{pool}")
instances_pending = count.to_i - instance_count.to_i
if result.key?(platform_alias) && result[platform_alias].key?(:ready)
result[platform_alias][:ready] = (result[platform_alias][:ready].to_i + instance_count).to_s
result[platform_alias][:pending] = (result[platform_alias][:pending].to_i + instances_pending).to_s
else
result[platform_alias] = {
'ready': instance_count.to_s,
'pending': instances_pending.to_s
}
end
end
end
result
end
end
# Endpoints that only use bits from the V1 api are called here
# Note that traces will be named based on the route used in the V1 api
# but the http.url trace attribute will still have the actual requested url in it
delete "#{api_prefix}/*" do
versionless_path_info = request.path_info.delete_prefix("#{api_prefix}/")
request.path_info = "/api/v1/#{versionless_path_info}"
call env
end
get "#{api_prefix}/*" do
versionless_path_info = request.path_info.delete_prefix("#{api_prefix}/")
request.path_info = "/api/v1/#{versionless_path_info}"
call env
end
post "#{api_prefix}/*" do
versionless_path_info = request.path_info.delete_prefix("#{api_prefix}/")
request.path_info = "/api/v1/#{versionless_path_info}"
call env
end
put "#{api_prefix}/*" do
versionless_path_info = request.path_info.delete_prefix("#{api_prefix}/")
request.path_info = "/api/v1/#{versionless_path_info}"
call env
end
end
end
end

1761
lib/vmpooler/api/v3.rb Normal file

File diff suppressed because it is too large Load diff

91
lib/vmpooler/dns.rb Normal file
View file

@ -0,0 +1,91 @@
# frozen_string_literal: true
require 'pathname'
module Vmpooler
class Dns
# Load one or more VMPooler DNS plugin gems by name
#
# @param names [Array<String>] The list of gem names to load
def self.load_by_name(names)
names = Array(names)
instance = new
names.map { |name| instance.load_from_gems(name) }.flatten
end
# Returns the plugin class for the specified dns config by name
#
# @param config [Object] The entire VMPooler config object
# @param name [Symbol] The name of the dns config key to get the dns class
# @return [String] The plugin class for the specifid dns config
def self.get_dns_plugin_class_by_name(config, name)
dns_configs = config[:dns_configs].keys
plugin_class = ''
dns_configs.map do |dns_config_name|
plugin_class = config[:dns_configs][dns_config_name]['dns_class'] if dns_config_name.to_s == name
end
plugin_class
end
# Returns the domain for the specified pool
#
# @param config [String] - the full config structure
# @param pool_name [String] - the name of the pool
# @return [String] - domain name for pool, which is set via reference to the dns_configs block
def self.get_domain_for_pool(config, pool_name)
pool = config[:pools].find { |p| p['name'] == pool_name }
pool_dns_config = pool['dns_plugin']
dns_configs = config[:dns_configs].keys
dns_configs.map do |dns_config_name|
return config[:dns_configs][dns_config_name]['domain'] if dns_config_name.to_s == pool_dns_config
end
end
# Returns the plugin domain for the specified dns config by name
#
# @param config [Object] The entire VMPooler config object
# @param name [Symbol] The name of the dns config key to get the dns domain
# @return [String] The domain for the specifid dns config
def self.get_dns_plugin_domain_by_name(config, name)
dns_configs = config[:dns_configs].keys
dns_configs.map do |dns_config_name|
return config[:dns_configs][dns_config_name]['domain'] if dns_config_name.to_s == name
end
end
# Returns a list of DNS plugin classes specified in the vmpooler configuration
#
# @param config [Object] The entire VMPooler config object
# @return nil || [Array<String>] A list of DNS plugin classes
def self.get_dns_plugin_config_classes(config)
return nil unless config[:dns_configs]
dns_configs = config[:dns_configs].keys
dns_plugins = dns_configs.map do |dns_config_name|
if config[:dns_configs][dns_config_name] && config[:dns_configs][dns_config_name]['dns_class']
config[:dns_configs][dns_config_name]['dns_class'].to_s
else
dns_config_name.to_s
end
end.compact.uniq
# dynamic-dns is not actually a class, it's just used as a value to denote
# that dynamic dns is used so no loading or record management is needed
dns_plugins.delete('dynamic-dns')
dns_plugins
end
# Load a single DNS plugin gem by name
#
# @param name [String] The name of the DNS plugin gem to load
# @return [String] The full require path to the specified gem
def load_from_gems(name = nil)
require_path = "vmpooler/dns/#{name.gsub('-', '/')}"
require require_path
require_path
end
end
end

81
lib/vmpooler/dns/base.rb Normal file
View file

@ -0,0 +1,81 @@
# frozen_string_literal: true
module Vmpooler
class PoolManager
class Dns
class Base
# These defs must be overidden in child classes
# Helper Methods
# Global Logger object
attr_reader :logger
# Global Metrics object
attr_reader :metrics
# Provider options passed in during initialization
attr_reader :dns_options
def initialize(config, logger, metrics, redis_connection_pool, name, options)
@config = config
@logger = logger
@metrics = metrics
@redis = redis_connection_pool
@dns_plugin_name = name
@dns_options = options
logger.log('s', "[!] Creating dns plugin '#{name}'")
end
def pool_config(pool_name)
# Get the configuration of a specific pool
@config[:pools].each do |pool|
return pool if pool['name'] == pool_name
end
nil
end
# Returns this dns plugin's configuration
#
# @returns [Hashtable] This dns plugins's configuration from the config file. Returns nil if the dns plugin config does not exist
def dns_config
@config[:dns_configs].each do |dns|
# Convert the symbol from the config into a string for comparison
return (dns[1].nil? ? {} : dns[1]) if dns[0].to_s == @dns_plugin_name
end
nil
end
def global_config
# This entire VM Pooler config
@config
end
def name
@dns_plugin_name
end
def get_ip(vm_name)
@redis.with_metrics do |redis|
redis.hget("vmpooler__vm__#{vm_name}", 'ip')
end
end
# returns
# Array[String] : Array of pool names this provider services
def provided_pools
@config[:pools].select { |pool| pool['dns_config'] == name }.map { |pool| pool['name'] }
end
def create_or_replace_record(hostname)
raise("#{self.class.name} does not implement create_or_replace_record #{hostname}")
end
def delete_record(hostname)
raise("#{self.class.name} does not implement delete_record for #{hostname}")
end
end
end
end
end

View file

@ -1,5 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'vmpooler/dns'
require 'vmpooler/providers' require 'vmpooler/providers'
require 'vmpooler/util/parsing' require 'vmpooler/util/parsing'
require 'spicy-proton' require 'spicy-proton'
@ -26,6 +27,9 @@ module Vmpooler
# VM Provider objects # VM Provider objects
$providers = Concurrent::Hash.new $providers = Concurrent::Hash.new
# VM DNS objects
$dns_plugins = Concurrent::Hash.new
# Our thread-tracker object # Our thread-tracker object
$threads = Concurrent::Hash.new $threads = Concurrent::Hash.new
@ -39,6 +43,9 @@ module Vmpooler
# load specified providers from config file # load specified providers from config file
load_used_providers load_used_providers
# load specified dns plugins from config file
load_used_dns_plugins
end end
def config def config
@ -60,7 +67,7 @@ module Vmpooler
to_set[k] = pool[k] to_set[k] = pool[k]
end end
to_set['alias'] = pool['alias'].join(',') if to_set.key?('alias') to_set['alias'] = pool['alias'].join(',') if to_set.key?('alias')
to_set['domain'] = Parsing.get_domain_for_pool(config, pool['name']) to_set['domain'] = Vmpooler::Dns.get_domain_for_pool(config, pool['name'])
redis.hmset("vmpooler__pool__#{pool['name']}", to_set.to_a.flatten) unless to_set.empty? redis.hmset("vmpooler__pool__#{pool['name']}", to_set.to_a.flatten) unless to_set.empty?
end end
previously_configured_pools.each do |pool| previously_configured_pools.each do |pool|
@ -106,6 +113,11 @@ module Vmpooler
def remove_nonexistent_vm(vm, pool, redis) def remove_nonexistent_vm(vm, pool, redis)
redis.srem("vmpooler__pending__#{pool}", vm) redis.srem("vmpooler__pending__#{pool}", vm)
dns_plugin = get_dns_plugin_class_for_pool(pool)
dns_plugin_class_name = get_dns_plugin_class_name_for_pool(pool)
domain = get_dns_plugin_domain_for_pool(pool)
fqdn = "#{vm}.#{domain}"
dns_plugin.delete_record(fqdn) unless dns_plugin_class_name == 'dynamic-dns'
$logger.log('d', "[!] [#{pool}] '#{vm}' no longer exists. Removing from pending.") $logger.log('d', "[!] [#{pool}] '#{vm}' no longer exists. Removing from pending.")
end end
@ -323,10 +335,10 @@ module Vmpooler
end end
# Clone a VM # Clone a VM
def clone_vm(pool_name, provider, request_id = nil, pool_alias = nil) def clone_vm(pool_name, provider, dns_plugin, request_id = nil, pool_alias = nil)
Thread.new do Thread.new do
begin begin
_clone_vm(pool_name, provider, request_id, pool_alias) _clone_vm(pool_name, provider, dns_plugin, request_id, pool_alias)
rescue StandardError => e rescue StandardError => e
if request_id if request_id
$logger.log('s', "[!] [#{pool_name}] failed while cloning VM for request #{request_id} with an error: #{e}") $logger.log('s', "[!] [#{pool_name}] failed while cloning VM for request #{request_id} with an error: #{e}")
@ -368,12 +380,8 @@ module Vmpooler
max_hostname_retries = 3 max_hostname_retries = 3
while hostname_retries < max_hostname_retries while hostname_retries < max_hostname_retries
hostname, hostname_available = generate_and_check_hostname hostname, hostname_available = generate_and_check_hostname
domain = Parsing.get_domain_for_pool(config, pool_name) domain = Vmpooler::Dns.get_domain_for_pool(config, pool_name)
if domain
fqdn = "#{hostname}.#{domain}" fqdn = "#{hostname}.#{domain}"
else
fqdn = hostname
end
# skip dns check if the provider is set to skip_dns_check_before_creating_vm # skip dns check if the provider is set to skip_dns_check_before_creating_vm
provider = get_provider_for_pool(pool_name) provider = get_provider_for_pool(pool_name)
@ -414,9 +422,9 @@ module Vmpooler
[dns_ip, false] [dns_ip, false]
end end
def _clone_vm(pool_name, provider, request_id = nil, pool_alias = nil) def _clone_vm(pool_name, provider, dns_plugin, request_id = nil, pool_alias = nil)
new_vmname = find_unique_hostname(pool_name) new_vmname = find_unique_hostname(pool_name)
pool_domain = Parsing.get_domain_for_pool(config, pool_name) pool_domain = Vmpooler::Dns.get_domain_for_pool(config, pool_name)
mutex = vm_mutex(new_vmname) mutex = vm_mutex(new_vmname)
mutex.synchronize do mutex.synchronize do
@redis.with_metrics do |redis| @redis.with_metrics do |redis|
@ -426,7 +434,7 @@ module Vmpooler
redis.hset("vmpooler__vm__#{new_vmname}", 'clone', Time.now) redis.hset("vmpooler__vm__#{new_vmname}", 'clone', Time.now)
redis.hset("vmpooler__vm__#{new_vmname}", 'template', pool_name) # This value is used to represent the pool. redis.hset("vmpooler__vm__#{new_vmname}", 'template', pool_name) # This value is used to represent the pool.
redis.hset("vmpooler__vm__#{new_vmname}", 'pool', pool_name) redis.hset("vmpooler__vm__#{new_vmname}", 'pool', pool_name)
redis.hset("vmpooler__vm__#{new_vmname}", 'domain', pool_domain) if pool_domain redis.hset("vmpooler__vm__#{new_vmname}", 'domain', pool_domain)
redis.hset("vmpooler__vm__#{new_vmname}", 'request_id', request_id) if request_id redis.hset("vmpooler__vm__#{new_vmname}", 'request_id', request_id) if request_id
redis.hset("vmpooler__vm__#{new_vmname}", 'pool_alias', pool_alias) if pool_alias redis.hset("vmpooler__vm__#{new_vmname}", 'pool_alias', pool_alias) if pool_alias
redis.exec redis.exec
@ -437,16 +445,25 @@ module Vmpooler
start = Time.now start = Time.now
provider.create_vm(pool_name, new_vmname) provider.create_vm(pool_name, new_vmname)
finish = format('%<time>.2f', time: Time.now - start) finish = format('%<time>.2f', time: Time.now - start)
$logger.log('s', "[+] [#{pool_name}] '#{new_vmname}' cloned in #{finish} seconds")
$metrics.timing("clone.#{pool_name}", finish)
$logger.log('d', "[ ] [#{pool_name}] Obtaining IP for '#{new_vmname}'")
ip_start = Time.now
ip = provider.get_vm_ip_address(new_vmname, pool_name)
ip_finish = format('%<time>.2f', time: Time.now - ip_start)
$logger.log('s', "[+] [#{pool_name}] Obtained IP for '#{new_vmname}' in #{ip_finish} seconds")
@redis.with_metrics do |redis| @redis.with_metrics do |redis|
redis.pipelined do |pipeline| redis.pipelined do |pipeline|
pipeline.hset("vmpooler__clone__#{Date.today}", "#{pool_name}:#{new_vmname}", finish) pipeline.hset("vmpooler__clone__#{Date.today}", "#{pool_name}:#{new_vmname}", finish)
pipeline.hset("vmpooler__vm__#{new_vmname}", 'clone_time', finish) pipeline.hset("vmpooler__vm__#{new_vmname}", 'clone_time', finish)
pipeline.hset("vmpooler__vm__#{new_vmname}", 'ip', ip)
end end
end end
$logger.log('s', "[+] [#{pool_name}] '#{new_vmname}' cloned in #{finish} seconds")
$metrics.timing("clone.#{pool_name}", finish) dns_plugin_class_name = get_dns_plugin_class_name_for_pool(pool_name)
dns_plugin.create_or_replace_record(new_vmname) unless dns_plugin_class_name == 'dynamic-dns'
rescue StandardError rescue StandardError
@redis.with_metrics do |redis| @redis.with_metrics do |redis|
redis.pipelined do |pipeline| redis.pipelined do |pipeline|
@ -466,10 +483,10 @@ module Vmpooler
end end
# Destroy a VM # Destroy a VM
def destroy_vm(vm, pool, provider) def destroy_vm(vm, pool, provider, dns_plugin)
Thread.new do Thread.new do
begin begin
_destroy_vm(vm, pool, provider) _destroy_vm(vm, pool, provider, dns_plugin)
rescue StandardError => e rescue StandardError => e
$logger.log('d', "[!] [#{pool}] '#{vm}' failed while destroying the VM with an error: #{e}") $logger.log('d', "[!] [#{pool}] '#{vm}' failed while destroying the VM with an error: #{e}")
raise raise
@ -477,7 +494,7 @@ module Vmpooler
end end
end end
def _destroy_vm(vm, pool, provider) def _destroy_vm(vm, pool, provider, dns_plugin)
mutex = vm_mutex(vm) mutex = vm_mutex(vm)
return if mutex.locked? return if mutex.locked?
@ -494,6 +511,11 @@ module Vmpooler
start = Time.now start = Time.now
provider.destroy_vm(pool, vm) provider.destroy_vm(pool, vm)
domain = get_dns_plugin_domain_for_pool(pool)
fqdn = "#{vm}.#{domain}"
dns_plugin_class_name = get_dns_plugin_class_name_for_pool(pool)
dns_plugin.delete_record(fqdn) unless dns_plugin_class_name == 'dynamic-dns'
redis.srem("vmpooler__completed__#{pool}", vm) redis.srem("vmpooler__completed__#{pool}", vm)
@ -632,6 +654,12 @@ module Vmpooler
result result
end end
# load only dns plugins used in config file
def load_used_dns_plugins
dns_plugins = Vmpooler::Dns.get_dns_plugin_config_classes(config)
Vmpooler::Dns.load_by_name(dns_plugins)
end
# load only providers used in config file # load only providers used in config file
def load_used_providers def load_used_providers
Vmpooler::Providers.load_by_name(used_providers) Vmpooler::Providers.load_by_name(used_providers)
@ -679,6 +707,31 @@ module Vmpooler
$providers[provider_name] $providers[provider_name]
end end
def get_dns_plugin_class_name_for_pool(pool_name)
pool = $config[:pools].find { |p| p['name'] == pool_name }
return nil unless pool
plugin_name = pool.fetch('dns_plugin')
Vmpooler::Dns.get_dns_plugin_class_by_name(config, plugin_name)
end
def get_dns_plugin_class_for_pool(pool_name)
pool = $config[:pools].find { |p| p['name'] == pool_name }
return nil unless pool
plugin_name = pool.fetch('dns_plugin')
plugin_class = Vmpooler::Dns.get_dns_plugin_class_by_name(config, plugin_name)
$dns_plugins[plugin_class]
end
def get_dns_plugin_domain_for_pool(pool_name)
pool = $config[:pools].find { |p| p['name'] == pool_name }
return nil unless pool
plugin_name = pool.fetch('dns_plugin')
Vmpooler::Dns.get_dns_plugin_domain_by_name(config, plugin_name)
end
def check_disk_queue(maxloop = 0, loop_delay = 5) def check_disk_queue(maxloop = 0, loop_delay = 5)
$logger.log('d', '[*] [disk_manager] starting worker thread') $logger.log('d', '[*] [disk_manager] starting worker thread')
@ -1223,7 +1276,8 @@ module Vmpooler
if inventory[vm] if inventory[vm]
begin begin
pool_check_response[:destroyed_vms] += 1 pool_check_response[:destroyed_vms] += 1
destroy_vm(vm, pool_name, provider) dns_plugin = get_dns_plugin_class_for_pool(pool_name)
destroy_vm(vm, pool_name, provider, dns_plugin)
rescue StandardError => e rescue StandardError => e
redis.pipelined do |pipeline| redis.pipelined do |pipeline|
pipeline.srem("vmpooler__completed__#{pool_name}", vm) pipeline.srem("vmpooler__completed__#{pool_name}", vm)
@ -1290,6 +1344,8 @@ module Vmpooler
$metrics.gauge("ready.#{pool_name}", ready) $metrics.gauge("ready.#{pool_name}", ready)
$metrics.gauge("running.#{pool_name}", running) $metrics.gauge("running.#{pool_name}", running)
dns_plugin = get_dns_plugin_class_for_pool(pool_name)
unless pool_size == 0 unless pool_size == 0
if redis.get("vmpooler__empty__#{pool_name}") if redis.get("vmpooler__empty__#{pool_name}")
redis.del("vmpooler__empty__#{pool_name}") unless ready == 0 redis.del("vmpooler__empty__#{pool_name}") unless ready == 0
@ -1304,7 +1360,7 @@ module Vmpooler
begin begin
redis.incr('vmpooler__tasks__clone') redis.incr('vmpooler__tasks__clone')
pool_check_response[:cloned_vms] += 1 pool_check_response[:cloned_vms] += 1
clone_vm(pool_name, provider) clone_vm(pool_name, provider, dns_plugin)
rescue StandardError => e rescue StandardError => e
$logger.log('s', "[!] [#{pool_name}] clone failed during check_pool with an error: #{e}") $logger.log('s', "[!] [#{pool_name}] clone failed during check_pool with an error: #{e}")
redis.decr('vmpooler__tasks__clone') redis.decr('vmpooler__tasks__clone')
@ -1390,6 +1446,16 @@ module Vmpooler
raise("Provider '#{provider_class}' is unknown for pool with provider name '#{provider_name}'") if provider_klass.nil? raise("Provider '#{provider_class}' is unknown for pool with provider name '#{provider_name}'") if provider_klass.nil?
end end
def create_dns_object(config, logger, metrics, redis_connection_pool, dns_class, dns_name, options)
dns_klass = Vmpooler::PoolManager::Dns
dns_klass.constants.each do |classname|
next unless classname.to_s.casecmp(dns_class) == 0
return dns_klass.const_get(classname).new(config, logger, metrics, redis_connection_pool, dns_name, options)
end
raise("DNS '#{dns_class}' is unknown for pool with dns name '#{dns_name}'") if dns_klass.nil?
end
def check_ondemand_requests(maxloop = 0, def check_ondemand_requests(maxloop = 0,
loop_delay_min = CHECK_LOOP_DELAY_MIN_DEFAULT, loop_delay_min = CHECK_LOOP_DELAY_MIN_DEFAULT,
loop_delay_max = CHECK_LOOP_DELAY_MAX_DEFAULT, loop_delay_max = CHECK_LOOP_DELAY_MAX_DEFAULT,
@ -1473,20 +1539,21 @@ module Vmpooler
pool_alias, pool, count, request_id = request.split(':') pool_alias, pool, count, request_id = request.split(':')
count = count.to_i count = count.to_i
provider = get_provider_for_pool(pool) provider = get_provider_for_pool(pool)
dns_plugin = get_dns_plugin_class_for_pool(pool)
slots = ondemand_clone_limit - clone_count slots = ondemand_clone_limit - clone_count
break if slots == 0 break if slots == 0
if slots >= count if slots >= count
count.times do count.times do
redis.incr('vmpooler__tasks__ondemandclone') redis.incr('vmpooler__tasks__ondemandclone')
clone_vm(pool, provider, request_id, pool_alias) clone_vm(pool, provider, dns_plugin, request_id, pool_alias)
end end
redis.zrem(queue_key, request) redis.zrem(queue_key, request)
else else
remaining_count = count - slots remaining_count = count - slots
slots.times do slots.times do
redis.incr('vmpooler__tasks__ondemandclone') redis.incr('vmpooler__tasks__ondemandclone')
clone_vm(pool, provider, request_id, pool_alias) clone_vm(pool, provider, dns_plugin, request_id, pool_alias)
end end
redis.pipelined do |pipeline| redis.pipelined do |pipeline|
pipeline.zrem(queue_key, request) pipeline.zrem(queue_key, request)
@ -1601,6 +1668,7 @@ module Vmpooler
# Create the providers # Create the providers
$config[:pools].each do |pool| $config[:pools].each do |pool|
provider_name = pool['provider'] provider_name = pool['provider']
dns_plugin_name = pool['dns_plugin']
# The provider_class parameter can be defined in the provider's data eg # The provider_class parameter can be defined in the provider's data eg
# :providers: # :providers:
# :vsphere: # :vsphere:
@ -1621,12 +1689,22 @@ module Vmpooler
else else
provider_class = $config[:providers][provider_name.to_sym]['provider_class'] provider_class = $config[:providers][provider_name.to_sym]['provider_class']
end end
begin begin
$providers[provider_name] = create_provider_object($config, $logger, $metrics, @redis, provider_class, provider_name, {}) if $providers[provider_name].nil? $providers[provider_name] = create_provider_object($config, $logger, $metrics, @redis, provider_class, provider_name, {}) if $providers[provider_name].nil?
rescue StandardError => e rescue StandardError => e
$logger.log('s', "Error while creating provider for pool #{pool['name']}: #{e}") $logger.log('s', "Error while creating provider for pool #{pool['name']}: #{e}")
raise raise
end end
dns_plugin_class = $config[:dns_configs][dns_plugin_name.to_sym]['dns_class']
begin
$dns_plugins[dns_plugin_class] = create_dns_object($config, $logger, $metrics, @redis, dns_plugin_class, dns_plugin_name, {}) if $dns_plugins[dns_plugin_class].nil?
rescue StandardError => e
$logger.log('s', "Error while creating dns plugin for pool #{pool['name']}: #{e}")
raise
end
end end
purge_unused_vms_and_resources purge_unused_vms_and_resources

View file

@ -58,6 +58,10 @@ module Vmpooler
nil nil
end end
def dns_config(dns_config_name)
Vmpooler::Dns.get_dns_plugin_domain_by_name(@config, dns_config_name)
end
# returns # returns
# [Hashtable] : The entire VMPooler configuration # [Hashtable] : The entire VMPooler configuration
def global_config def global_config
@ -257,6 +261,10 @@ module Vmpooler
raise("#{self.class.name} does not implement purge_unconfigured_resources") raise("#{self.class.name} does not implement purge_unconfigured_resources")
end end
def get_vm_ip_address(vm_name, pool_name)
raise("#{self.class.name} does not implement get_vm_ip_address for vm #{vm_name} in pool #{pool_name}")
end
# DEPRECATED if a provider does not implement the new method, it will hit this base class method # DEPRECATED if a provider does not implement the new method, it will hit this base class method
# and return a deprecation message # and return a deprecation message
def purge_unconfigured_folders(_deprecated, _deprecated2, allowlist) def purge_unconfigured_folders(_deprecated, _deprecated2, allowlist)

View file

@ -29,10 +29,10 @@ Date.prototype.yyyymmdd = function() {
var data_url = { var data_url = {
'capacity': '/dashboard/stats/vmpooler/pool', 'capacity': '/dashboard/stats/vmpooler/pool',
'pools' : '/api/v1/vm', 'pools' : '/api/v3/vm',
'running' : '/dashboard/stats/vmpooler/running', 'running' : '/dashboard/stats/vmpooler/running',
'status' : '/api/v1/status', 'status' : '/api/v3/status',
'summary' : '/api/v1/summary' 'summary' : '/api/v3/summary'
}; };

View file

@ -12,25 +12,5 @@ module Vmpooler
yield platform_alias, pool, count yield platform_alias, pool, count
end end
end end
# @param config [String] - the full config structure
# @param pool_name [String] - the name of the pool
# @return [String] - domain name for pool, if set in the provider for the pool or in the config block
def self.get_domain_for_pool(config, pool_name)
pool = config[:pools].find { |p| p['name'] == pool_name }
return nil unless pool
provider_name = pool.fetch('provider', 'vsphere') # see vmpooler.yaml.example where it states defaulting to vsphere
if config[:providers] && config[:providers][provider_name.to_sym] && config[:providers][provider_name.to_sym]['domain']
domain = config[:providers][provider_name.to_sym]['domain']
elsif config[:config] && config[:config]['domain']
domain = config[:config]['domain']
else
domain = nil
end
domain
end
end end
end end

View file

@ -8,4 +8,5 @@
- name: 'pool05' - name: 'pool05'
size: 5 size: 5
provider: dummy provider: dummy
dns_plugin: 'example'
ready_ttl: 5 ready_ttl: 5

View file

@ -23,9 +23,13 @@
allowed_tags: allowed_tags:
- 'created_by' - 'created_by'
- 'project' - 'project'
domain: 'example.com'
prefix: 'poolvm-' prefix: 'poolvm-'
:dns_configs:
:example:
dns_class: dynamic-dns
domain: 'example.com'
# Uncomment the lines below to suppress metrics to STDOUT # Uncomment the lines below to suppress metrics to STDOUT
# :statsd: # :statsd:
# server: 'localhost' # server: 'localhost'
@ -36,8 +40,10 @@
- name: 'pool01' - name: 'pool01'
size: 5 size: 5
provider: dummy provider: dummy
dns_plugin: 'example'
ready_ttl: 5 ready_ttl: 5
- name: 'pool02' - name: 'pool02'
size: 5 size: 5
provider: dummy provider: dummy
dns_plugin: 'example'
ready_ttl: 5 ready_ttl: 5

View file

@ -23,9 +23,13 @@
allowed_tags: allowed_tags:
- 'created_by' - 'created_by'
- 'project' - 'project'
domain: 'example.com'
prefix: 'poolvm-' prefix: 'poolvm-'
:dns_configs:
:example:
dns_class: dynamic-dns
domain: 'example.com'
# Uncomment the lines below to suppress metrics to STDOUT # Uncomment the lines below to suppress metrics to STDOUT
# :statsd: # :statsd:
# server: 'localhost' # server: 'localhost'
@ -36,8 +40,10 @@
- name: 'pool03' - name: 'pool03'
size: 5 size: 5
provider: dummy provider: dummy
dns_plugin: 'example'
ready_ttl: 5 ready_ttl: 5
- name: 'pool04' - name: 'pool04'
size: 5 size: 5
provider: dummy provider: dummy
dns_plugin: 'example'
ready_ttl: 5 ready_ttl: 5

45
spec/fixtures/vmpooler_domain.yaml vendored Normal file
View file

@ -0,0 +1,45 @@
---
:providers:
:dummy:
:redis:
server: 'localhost'
:auth:
provider: dummy
:tagfilter:
url: '(.*)\/'
:config:
site_name: 'vmpooler'
# Need to change this on Windows
logfile: '/var/log/vmpooler.log'
task_limit: 10
timeout: 15
vm_checktime: 1
vm_lifetime: 12
vm_lifetime_auth: 24
allowed_tags:
- 'created_by'
- 'project'
domain: 'example.com'
prefix: 'poolvm-'
# Uncomment the lines below to suppress metrics to STDOUT
# :statsd:
# server: 'localhost'
# prefix: 'vmpooler'
# port: 8125
:pools:
- name: 'pool01'
size: 5
provider: dummy
dns_plugin: 'example'
ready_ttl: 5
- name: 'pool02'
size: 5
provider: dummy
dns_plugin: 'example'
ready_ttl: 5

View file

@ -14,6 +14,16 @@ class MockLogger
end end
end end
class MockPoolManagerDnsBase
def delete_record(hostname)
end
def create_or_replace_record(hostname)
end
end
def expect_json(ok = true, http = 200) def expect_json(ok = true, http = 200)
expect(last_response.header['Content-Type']).to eq('application/json') expect(last_response.header['Content-Type']).to eq('application/json')

View file

@ -1,402 +0,0 @@
require 'spec_helper'
require 'rack/test'
describe Vmpooler::API::V1 do
include Rack::Test::Methods
def app()
Vmpooler::API
end
# Added to ensure no leakage in rack state from previous tests.
# Removes all routes, filters, middleware and extension hooks from the current class
# https://rubydoc.info/gems/sinatra/Sinatra/Base#reset!-class_method
before(:each) do
app.reset!
end
let(:config) {
{
config: {
'site_name' => 'test pooler',
'vm_lifetime_auth' => 2,
'experimental_features' => true
},
pools: [
{'name' => 'pool1', 'size' => 5, 'template' => 'templates/pool1', 'clone_target' => 'default_cluster'},
{'name' => 'pool2', 'size' => 10}
],
pools_at_startup: [
{'name' => 'pool1', 'size' => 5, 'template' => 'templates/pool1', 'clone_target' => 'default_cluster'},
{'name' => 'pool2', 'size' => 10}
],
statsd: { 'prefix' => 'stats_prefix'},
alias: { 'poolone' => 'pool1' },
pool_names: [ 'pool1', 'pool2', 'poolone' ]
}
}
describe '/config/pooltemplate' do
let(:prefix) { '/api/v1' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:current_time) { Time.now }
before(:each) do
expect(app).to receive(:run!).once
app.execute([:api], config, redis, metrics, nil)
app.settings.set :config, auth: false
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
end
describe 'DELETE /config/pooltemplate/:pool' do
it 'resets a pool template' do
post "#{prefix}/config/pooltemplate", '{"pool1":"templates/new_template"}'
delete "#{prefix}/config/pooltemplate/pool1"
expect_json(ok = true, http = 201)
expected = {
ok: true,
template_before_reset: 'templates/new_template',
template_before_overrides: 'templates/pool1'
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'succeeds when the pool has not been overridden' do
delete "#{prefix}/config/pooltemplate/pool1"
expect_json(ok = true, http = 200)
end
it 'fails on nonexistent pools' do
delete "#{prefix}/config/pooltemplate/poolpoolpool"
expect_json(ok = false, http = 404)
end
context 'with experimental features disabled' do
before(:each) do
config[:config]['experimental_features'] = false
end
it 'should return 405' do
delete "#{prefix}/config/pooltemplate/pool1"
expect_json(ok = false, http = 405)
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
end
describe 'POST /config/pooltemplate' do
it 'updates a pool template' do
post "#{prefix}/config/pooltemplate", '{"pool1":"templates/new_template"}'
expect_json(ok = true, http = 201)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails on nonexistent pools' do
post "#{prefix}/config/pooltemplate", '{"poolpoolpool":"templates/newtemplate"}'
expect_json(ok = false, http = 400)
end
it 'updates multiple pools' do
post "#{prefix}/config/pooltemplate", '{"pool1":"templates/new_template","pool2":"templates/new_template2"}'
expect_json(ok = true, http = 201)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails when not all pools exist' do
post "#{prefix}/config/pooltemplate", '{"pool1":"templates/new_template","pool3":"templates/new_template2"}'
expect_json(ok = false, http = 400)
expected = {
ok: false,
bad_templates: ['pool3']
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'returns no changes when the template does not change' do
post "#{prefix}/config/pooltemplate", '{"pool1":"templates/pool1"}'
expect_json(ok = true, http = 200)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails when a invalid template parameter is provided' do
post "#{prefix}/config/pooltemplate", '{"pool1":"template1"}'
expect_json(ok = false, http = 400)
expected = {
ok: false,
bad_templates: ['pool1']
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails when a template starts with /' do
post "#{prefix}/config/pooltemplate", '{"pool1":"/template1"}'
expect_json(ok = false, http = 400)
expected = {
ok: false,
bad_templates: ['pool1']
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails when a template ends with /' do
post "#{prefix}/config/pooltemplate", '{"pool1":"template1/"}'
expect_json(ok = false, http = 400)
expected = {
ok: false,
bad_templates: ['pool1']
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
context 'with experimental features disabled' do
before(:each) do
config[:config]['experimental_features'] = false
end
it 'should return 405' do
post "#{prefix}/config/pooltemplate", '{"pool1":"template/template1"}'
expect_json(ok = false, http = 405)
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
end
describe 'DELETE /config/poolsize' do
it 'resets a pool size' do
post "#{prefix}/config/poolsize", '{"pool1":"2"}'
delete "#{prefix}/config/poolsize/pool1"
expect_json(ok = true, http = 201)
expected = {
ok: true,
pool_size_before_reset: 2,
pool_size_before_overrides: 5
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails when a specified pool does not exist' do
delete "#{prefix}/config/poolsize/pool10"
expect_json(ok = false, http = 404)
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'succeeds when a pool has not been overridden' do
delete "#{prefix}/config/poolsize/pool1"
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool_size_before_reset: 5,
pool_size_before_overrides: 5
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
context 'with experimental features disabled' do
before(:each) do
config[:config]['experimental_features'] = false
end
it 'should return 405' do
delete "#{prefix}/config/poolsize/pool1"
expect_json(ok = false, http = 405)
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
end
describe 'POST /config/poolsize' do
it 'changes a pool size' do
post "#{prefix}/config/poolsize", '{"pool1":"2"}'
expect_json(ok = true, http = 201)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'changes a pool size for multiple pools' do
post "#{prefix}/config/poolsize", '{"pool1":"2","pool2":"2"}'
expect_json(ok = true, http = 201)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails when a specified pool does not exist' do
post "#{prefix}/config/poolsize", '{"pool10":"2"}'
expect_json(ok = false, http = 400)
expected = {
ok: false,
not_configured: ['pool10']
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'succeeds with 200 when no change is required' do
post "#{prefix}/config/poolsize", '{"pool1":"5"}'
expect_json(ok = true, http = 200)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'succeeds with 201 when at least one pool changes' do
post "#{prefix}/config/poolsize", '{"pool1":"5","pool2":"5"}'
expect_json(ok = true, http = 201)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails when a non-integer value is provided for size' do
post "#{prefix}/config/poolsize", '{"pool1":"four"}'
expect_json(ok = false, http = 400)
expected = {
ok: false,
not_configured: ['pool1']
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails when a negative value is provided for size' do
post "#{prefix}/config/poolsize", '{"pool1":"-1"}'
expect_json(ok = false, http = 400)
expected = {
ok: false,
not_configured: ['pool1']
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
context 'with experimental features disabled' do
before(:each) do
config[:config]['experimental_features'] = false
end
it 'should return 405' do
post "#{prefix}/config/poolsize", '{"pool1":"1"}'
expect_json(ok = false, http = 405)
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
end
describe 'POST /config/clonetarget' do
it 'changes the clone target' do
post "#{prefix}/config/clonetarget", '{"pool1":"cluster1"}'
expect_json(ok = true, http = 201)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'changes a pool size for multiple pools' do
post "#{prefix}/config/clonetarget", '{"pool1":"cluster1","pool2":"cluster2"}'
expect_json(ok = true, http = 201)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails when a specified pool does not exist' do
post "#{prefix}/config/clonetarget", '{"pool10":"cluster1"}'
expect_json(ok = false, http = 400)
expected = {
ok: false,
bad_templates: ['pool10']
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'succeeds with 200 when no change is required' do
post "#{prefix}/config/clonetarget", '{"pool1":"default_cluster"}'
expect_json(ok = true, http = 200)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'succeeds with 201 when at least one pool changes' do
post "#{prefix}/config/clonetarget", '{"pool1":"default_cluster","pool2":"cluster2"}'
expect_json(ok = true, http = 201)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
context 'with experimental features disabled' do
before(:each) do
config[:config]['experimental_features'] = false
end
it 'should return 405' do
post "#{prefix}/config/clonetarget", '{"pool1":"cluster1"}'
expect_json(ok = false, http = 405)
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
end
describe 'GET /config' do
let(:prefix) { '/api/v1' }
it 'returns pool configuration when set' do
get "#{prefix}/config"
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result['pool_configuration']).to eq(config[:pools])
end
end
end
end

View file

@ -1,210 +0,0 @@
require 'spec_helper'
require 'rack/test'
describe Vmpooler::API::V1 do
include Rack::Test::Methods
def app()
Vmpooler::API
end
# Added to ensure no leakage in rack state from previous tests.
# Removes all routes, filters, middleware and extension hooks from the current class
# https://rubydoc.info/gems/sinatra/Sinatra/Base#reset!-class_method
before(:each) do
app.reset!
end
describe '/token' do
let(:prefix) { '/api/v1' }
let(:current_time) { Time.now }
let(:config) { {
config: {}
} }
before(:each) do
expect(app).to receive(:run!).once
app.execute([:api], config, redis, nil, nil)
end
describe 'GET /token' do
context '(auth not configured)' do
let(:config) { {
config: {},
auth: false
} }
it 'returns a 404' do
get "#{prefix}/token"
expect_json(ok = false, http = 404)
end
end
context '(auth configured)' do
let(:config) {
{
config: {},
auth: {
'provider' => 'dummy'
}
}
}
let(:username_str) { 'admin' }
let(:password_str) { 's3cr3t' }
it 'returns a 401 if not authed' do
get "#{prefix}/token"
expect_json(ok = false, http = 401)
end
it 'returns a list of tokens if authed' do
create_token "abc", "admin", current_time
authorize 'admin', 's3cr3t'
get "#{prefix}/token"
expect_json(ok = true, http = 200)
expect(JSON.parse(last_response.body)['abc']['created']).to eq(current_time.to_s)
end
end
end
describe 'POST /token' do
context '(auth not configured)' do
let(:config) { {
config: {},
auth: false
} }
it 'returns a 404' do
post "#{prefix}/token"
expect_json(ok = false, http = 404)
end
end
context '(auth configured)' do
let(:config) {
{
config: {},
auth: {
'provider' => 'dummy'
}
}
}
it 'returns a 401 if not authed' do
post "#{prefix}/token"
expect_json(ok = false, http = 401)
end
it 'returns a newly created token if authed' do
authorize 'admin', 's3cr3t'
post "#{prefix}/token"
expect_json(ok = true, http = 200)
returned_token = JSON.parse(last_response.body)['token']
expect(returned_token.length).to be(32)
expect(get_token_data(returned_token)['user']).to eq("admin")
end
end
end
end
describe '/token/:token' do
let(:prefix) { '/api/v1' }
let(:current_time) { Time.now }
before(:each) do
expect(app).to receive(:run!).once
app.execute([:api], config, redis, nil, nil)
app.settings.set :config, config
app.settings.set :redis, redis
end
def create_vm_for_token(token, pool, vm)
redis.sadd("vmpooler__running__#{pool}", vm)
redis.hset("vmpooler__vm__#{vm}", "token:token", token)
end
describe 'GET /token/:token' do
context '(auth not configured)' do
let(:config) { {
config: {},
auth: false
} }
it 'returns a 404' do
get "#{prefix}/token/this"
expect_json(ok = false, http = 404)
end
end
context '(auth configured)' do
let(:config) { {
config: {},
auth: true,
pools: [
{'name' => 'pool1', 'size' => 5}
]
} }
it 'returns a token' do
create_token "mytoken", "admin", current_time
create_vm_for_token "mytoken", "pool1", "vmhostname"
get "#{prefix}/token/mytoken"
expect_json(ok = true, http = 200)
expect(JSON.parse(last_response.body)['ok']).to eq(true)
expect(JSON.parse(last_response.body)['mytoken']['user']).to eq('admin')
expect(JSON.parse(last_response.body)['mytoken']['vms']['running']).to include('vmhostname')
end
end
end
describe 'DELETE /token/:token' do
context '(auth not configured)' do
let(:config) { {
config: {},
auth: false
} }
it 'returns a 404' do
delete "#{prefix}/token/this"
expect_json(ok = false, http = 404)
end
end
context '(auth configured)' do
let(:config) {
{
config: {},
auth: {
'provider' => 'dummy'
}
}
}
it 'returns a 401 if not authed' do
delete "#{prefix}/token/this"
expect_json(ok = false, http = 401)
end
it 'deletes a token if authed' do
create_token("mytoken", "admin", current_time)
authorize 'admin', 's3cr3t'
delete "#{prefix}/token/mytoken"
expect_json(ok = true, http = 200)
end
it 'fails if token does not exist' do
authorize 'admin', 's3cr3t'
delete "#{prefix}/token/missingtoken"
expect_json(ok = false, http = 401) # TODO: should this be 404?
end
end
end
end
end

View file

@ -1,435 +0,0 @@
require 'spec_helper'
require 'rack/test'
describe Vmpooler::API::V1 do
include Rack::Test::Methods
def app()
Vmpooler::API
end
# Added to ensure no leakage in rack state from previous tests.
# Removes all routes, filters, middleware and extension hooks from the current class
# https://rubydoc.info/gems/sinatra/Sinatra/Base#reset!-class_method
before(:each) do
app.reset!
end
describe '/vm' do
let(:prefix) { '/api/v1' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:config) {
{
config: {
'site_name' => 'test pooler',
'vm_lifetime_auth' => 2
},
pools: [
{'name' => 'pool1', 'size' => 5},
{'name' => 'pool2', 'size' => 10},
{'name' => 'pool3', 'size' => 10}
],
statsd: { 'prefix' => 'stats_prefix'},
alias: { 'poolone' => ['pool1'] },
pool_names: [ 'pool1', 'pool2', 'pool3', 'poolone', 'genericpool' ]
}
}
let(:current_time) { Time.now }
let(:vmname) { 'abcdefghijkl' }
let(:checkoutlock) { Mutex.new }
before(:each) do
expect(app).to receive(:run!).once
app.execute([:api], config, redis, metrics, nil)
app.settings.set :config, auth: false
app.settings.set :checkoutlock, checkoutlock
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
end
describe 'GET /vm/:hostname' do
it 'returns correct information on a running vm' do
create_running_vm 'pool1', vmname, redis
expect(TCPSocket).to receive(:gethostbyname).and_raise(RuntimeError)
get "#{prefix}/vm/#{vmname}"
expect_json(ok = true, http = 200)
response_body = (JSON.parse(last_response.body)[vmname])
expect(response_body["template"]).to eq("pool1")
expect(response_body["lifetime"]).to eq(0)
expect(response_body["running"]).to be >= 0
expect(response_body["remaining"]).to be <= 0
expect(response_body["start_time"]).to eq(current_time.to_datetime.rfc3339)
expect(response_body["end_time"]).to eq(current_time.to_datetime.rfc3339)
expect(response_body["state"]).to eq("running")
expect(response_body["ip"]).to eq("")
expect(response_body["host"]).to eq("host1")
end
end
describe 'POST /vm' do
let(:socket) { double('socket') }
it 'returns a single VM' do
create_ready_vm 'pool1', vmname, redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"pool1":"1"}'
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool1: {
hostname: vmname
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'returns a single VM for an alias' do
create_ready_vm 'pool1', vmname, redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"poolone":"1"}'
expect_json(ok = true, http = 200)
expected = {
ok: true,
poolone: {
hostname: vmname
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails on nonexistant pools' do
post "#{prefix}/vm", '{"poolpoolpool":"1"}'
expect_json(ok = false, http = 404)
end
it 'returns 503 for empty pool when aliases are not defined' do
app.settings.config.delete(:alias)
app.settings.config[:pool_names] = ['pool1', 'pool2']
create_ready_vm 'pool1', vmname, redis
post "#{prefix}/vm/pool1"
post "#{prefix}/vm/pool1"
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
end
it 'returns 503 for empty pool referenced by alias' do
create_ready_vm 'pool2', vmname, redis
post "#{prefix}/vm/poolone"
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
end
it 'returns multiple VMs' do
create_ready_vm 'pool1', vmname, redis
create_ready_vm 'pool2', 'qrstuvwxyz012345', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"pool1":"1","pool2":"1"}'
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool1: {
hostname: vmname
},
pool2: {
hostname: 'qrstuvwxyz012345'
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'returns multiple VMs even when multiple instances from the same pool are requested' do
create_ready_vm 'pool1', '1abcdefghijklmnop', redis
create_ready_vm 'pool1', '2abcdefghijklmnop', redis
create_ready_vm 'pool2', 'qrstuvwxyz012345', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"pool1":"2","pool2":"1"}'
expected = {
ok: true,
pool1: {
hostname: [ '1abcdefghijklmnop', '2abcdefghijklmnop' ]
},
pool2: {
hostname: 'qrstuvwxyz012345'
}
}
result = JSON.parse(last_response.body)
expect(result['ok']).to eq(true)
expect(result['pool1']['hostname']).to include('1abcdefghijklmnop', '2abcdefghijklmnop')
expect(result['pool2']['hostname']).to eq('qrstuvwxyz012345')
expect_json(ok = true, http = 200)
end
it 'returns multiple VMs even when multiple instances from multiple pools are requested' do
create_ready_vm 'pool1', '1abcdefghijklmnop', redis
create_ready_vm 'pool1', '2abcdefghijklmnop', redis
create_ready_vm 'pool2', '1qrstuvwxyz012345', redis
create_ready_vm 'pool2', '2qrstuvwxyz012345', redis
create_ready_vm 'pool2', '3qrstuvwxyz012345', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"pool1":"2","pool2":"3"}'
expected = {
ok: true,
pool1: {
hostname: [ '1abcdefghijklmnop', '2abcdefghijklmnop' ]
},
pool2: {
hostname: [ '1qrstuvwxyz012345', '2qrstuvwxyz012345', '3qrstuvwxyz012345' ]
}
}
result = JSON.parse(last_response.body)
expect(result['ok']).to eq(true)
expect(result['pool1']['hostname']).to include('1abcdefghijklmnop', '2abcdefghijklmnop')
expect(result['pool2']['hostname']).to include('1qrstuvwxyz012345', '2qrstuvwxyz012345', '3qrstuvwxyz012345')
expect_json(ok = true, http = 200)
end
it 'returns VMs from multiple backend pools requested by an alias' do
Vmpooler::API.settings.config[:alias]['genericpool'] = ['pool1', 'pool2', 'pool3']
create_ready_vm 'pool1', '1abcdefghijklmnop', redis
create_ready_vm 'pool2', '2abcdefghijklmnop', redis
create_ready_vm 'pool3', '1qrstuvwxyz012345', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"genericpool":"3"}'
expected = {
ok: true,
genericpool: {
hostname: [ '1abcdefghijklmnop', '2abcdefghijklmnop', '1qrstuvwxyz012345' ]
}
}
result = JSON.parse(last_response.body)
expect(result['ok']).to eq(true)
expect(result['genericpool']['hostname']).to include('1abcdefghijklmnop', '2abcdefghijklmnop', '1qrstuvwxyz012345')
expect_json(ok = true, http = 200)
end
it 'returns the first VM that was moved to the ready state when checking out a VM' do
create_ready_vm 'pool1', '1abcdefghijklmnop', redis
create_ready_vm 'pool1', '2abcdefghijklmnop', redis
create_ready_vm 'pool1', '3abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"pool1":"1"}'
expected = {
ok: true,
"pool1": {
"hostname": "1abcdefghijklmnop"
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = true, http = 200)
end
it 'fails when not all requested vms can be allocated' do
create_ready_vm 'pool1', '1abcdefghijklmnop', redis
post "#{prefix}/vm", '{"pool1":"1","pool2":"1"}'
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
end
it 'returns any checked out vms to their pools when not all requested vms can be allocated' do
create_ready_vm 'pool1', '1abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"pool1":"1","pool2":"1"}'
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
expect(pool_has_ready_vm?('pool1', '1abcdefghijklmnop', redis)).to eq(true)
end
it 'fails when not all requested vms can be allocated, when requesting multiple instances from a pool' do
create_ready_vm 'pool1', '1abcdefghijklmnop', redis
post "#{prefix}/vm", '{"pool1":"2","pool2":"1"}'
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
end
it 'returns any checked out vms to their pools when not all requested vms can be allocated, when requesting multiple instances from a pool' do
create_ready_vm 'pool1', '1abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"pool1":"2","pool2":"1"}'
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
expect(pool_has_ready_vm?('pool1', '1abcdefghijklmnop', redis)).to eq(true)
end
it 'fails when not all requested vms can be allocated, when requesting multiple instances from multiple pools' do
create_ready_vm 'pool1', '1abcdefghijklmnop', redis
post "#{prefix}/vm", '{"pool1":"2","pool2":"3"}'
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
end
it 'returns any checked out vms to their pools when not all requested vms can be allocated, when requesting multiple instances from multiple pools' do
create_ready_vm 'pool1', '1abcdefghijklmnop', redis
create_ready_vm 'pool1', '2abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"pool1":"2","pool2":"3"}'
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
expect(pool_has_ready_vm?('pool1', '1abcdefghijklmnop', redis)).to eq(true)
expect(pool_has_ready_vm?('pool1', '2abcdefghijklmnop', redis)).to eq(true)
end
it 'returns the second VM when the first fails to respond' do
create_ready_vm 'pool1', vmname, redis
create_ready_vm 'pool1', "2#{vmname}", redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).with(vmname, nil).and_raise('mockerror')
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).with("2#{vmname}", nil).and_return(socket)
post "#{prefix}/vm", '{"pool1":"1"}'
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool1: {
hostname: "2#{vmname}"
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect(pool_has_ready_vm?('pool1', vmname, redis)).to be false
end
context '(auth not configured)' do
it 'does not extend VM lifetime if auth token is provided' do
app.settings.set :config, auth: false
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"pool1":"1"}', {
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
}
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool1: {
hostname: 'abcdefghijklmnop'
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
vm = fetch_vm('abcdefghijklmnop')
expect(vm['lifetime']).to be_nil
end
end
context '(auth configured)' do
it 'extends VM lifetime if auth token is provided' do
app.settings.set :config, auth: true
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"pool1":"1"}', {
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
}
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool1: {
hostname: 'abcdefghijklmnop'
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
vm = fetch_vm('abcdefghijklmnop')
expect(vm['lifetime'].to_i).to eq(2)
end
it 'does not extend VM lifetime if auth token is not provided' do
app.settings.set :config, auth: true
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm", '{"pool1":"1"}'
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool1: {
hostname: 'abcdefghijklmnop'
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
vm = fetch_vm('abcdefghijklmnop')
expect(vm['lifetime']).to be_nil
end
end
end
end
end

View file

@ -1,327 +0,0 @@
require 'spec_helper'
require 'rack/test'
describe Vmpooler::API::V1 do
include Rack::Test::Methods
def app()
Vmpooler::API
end
# Added to ensure no leakage in rack state from previous tests.
# Removes all routes, filters, middleware and extension hooks from the current class
# https://rubydoc.info/gems/sinatra/Sinatra/Base#reset!-class_method
before(:each) do
app.reset!
end
describe '/vm/:template' do
let(:prefix) { '/api/v1' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:config) {
{
config: {
'site_name' => 'test pooler',
'vm_lifetime_auth' => 2,
},
providers: { vsphere: {} },
pools: [
{'name' => 'pool1', 'size' => 5},
{'name' => 'pool2', 'size' => 10},
{'name' => 'poolone', 'size' => 0}
],
statsd: { 'prefix' => 'stats_prefix'},
alias: { 'poolone' => 'pool1' },
pool_names: [ 'pool1', 'pool2', 'poolone' ]
}
}
let(:current_time) { Time.now }
let(:socket) { double('socket') }
let(:checkoutlock) { Mutex.new }
before(:each) do
expect(app).to receive(:run!).once
app.execute([:api], config, redis, metrics, nil)
app.settings.set :config, auth: false
app.settings.set :checkoutlock, checkoutlock
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
end
describe 'POST /vm/:template' do
it 'returns a single VM' do
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm/pool1", ''
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool1: {
hostname: 'abcdefghijklmnop'
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'returns a single VM for an alias' do
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm/poolone", ''
expected = {
ok: true,
poolone: {
hostname: 'abcdefghijklmnop'
}
}
expect_json(ok = true, http = 200)
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails on nonexistant pools' do
post "#{prefix}/vm/poolpoolpool", ''
expect_json(ok = false, http = 404)
end
it 'returns 503 for empty pool when aliases are not defined' do
app.settings.config.delete(:alias)
app.settings.config[:pool_names] = ['pool1', 'pool2']
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
post "#{prefix}/vm/pool1"
post "#{prefix}/vm/pool1"
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
end
it 'returns 503 for empty pool referenced by alias' do
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
post "#{prefix}/vm/poolone"
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
end
it 'returns multiple VMs' do
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
create_ready_vm 'pool2', 'qrstuvwxyz012345', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm/pool1+pool2", ''
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool1: {
hostname: 'abcdefghijklmnop'
},
pool2: {
hostname: 'qrstuvwxyz012345'
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'returns multiple VMs even when multiple instances from multiple pools are requested' do
create_ready_vm 'pool1', '1abcdefghijklmnop', redis
create_ready_vm 'pool1', '2abcdefghijklmnop', redis
create_ready_vm 'pool2', '1qrstuvwxyz012345', redis
create_ready_vm 'pool2', '2qrstuvwxyz012345', redis
create_ready_vm 'pool2', '3qrstuvwxyz012345', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm/pool1+pool1+pool2+pool2+pool2", ''
expected = {
ok: true,
pool1: {
hostname: [ '1abcdefghijklmnop', '2abcdefghijklmnop' ]
},
pool2: {
hostname: [ '1qrstuvwxyz012345', '2qrstuvwxyz012345', '3qrstuvwxyz012345' ]
}
}
result = JSON.parse(last_response.body)
expect(result['ok']).to eq(true)
expect(result['pool1']['hostname']).to include('1abcdefghijklmnop', '2abcdefghijklmnop')
expect(result['pool2']['hostname']).to include('1qrstuvwxyz012345', '2qrstuvwxyz012345', '3qrstuvwxyz012345')
expect_json(ok = true, http = 200)
end
it 'fails when not all requested vms can be allocated' do
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
post "#{prefix}/vm/pool1+pool2", ''
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
end
it 'returns any checked out vms to their pools when not all requested vms can be allocated' do
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm/pool1+pool2", ''
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
expect(pool_has_ready_vm?('pool1', 'abcdefghijklmnop', redis)).to eq(true)
end
it 'fails when not all requested vms can be allocated, when requesting multiple instances from a pool' do
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
create_ready_vm 'pool1', '0123456789012345', redis
post "#{prefix}/vm/pool1+pool1+pool2", ''
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
end
it 'returns any checked out vms to their pools when not all requested vms can be allocated, when requesting multiple instances from a pool' do
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
create_ready_vm 'pool1', '0123456789012345', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm/pool1+pool1+pool2", ''
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
expect(pool_has_ready_vm?('pool1', 'abcdefghijklmnop', redis)).to eq(true)
expect(pool_has_ready_vm?('pool1', '0123456789012345', redis)).to eq(true)
end
it 'fails when not all requested vms can be allocated, when requesting multiple instances from multiple pools' do
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
create_ready_vm 'pool2', '0123456789012345', redis
post "#{prefix}/vm/pool1+pool1+pool2+pool2+pool2", ''
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
end
it 'returns any checked out vms to their pools when not all requested vms can be allocated, when requesting multiple instances from multiple pools' do
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
create_ready_vm 'pool2', '0123456789012345', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm/pool1+pool1+pool2+pool2+pool2", ''
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect_json(ok = false, http = 503)
expect(pool_has_ready_vm?('pool1', 'abcdefghijklmnop', redis)).to eq(true)
expect(pool_has_ready_vm?('pool2', '0123456789012345', redis)).to eq(true)
end
context '(auth not configured)' do
it 'does not extend VM lifetime if auth token is provided' do
app.settings.set :config, auth: false
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm/pool1", '', {
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
}
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool1: {
hostname: 'abcdefghijklmnop'
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
vm = fetch_vm('abcdefghijklmnop')
expect(vm['lifetime']).to be_nil
end
end
context '(auth configured)' do
it 'extends VM lifetime if auth token is provided' do
app.settings.set :config, auth: true
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm/pool1", '', {
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
}
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool1: {
hostname: 'abcdefghijklmnop'
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
vm = fetch_vm('abcdefghijklmnop')
expect(vm['lifetime'].to_i).to eq(2)
end
it 'does not extend VM lifetime if auth token is not provided' do
app.settings.set :config, auth: true
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).and_return(socket)
post "#{prefix}/vm/pool1", ''
expected = {
ok: true,
pool1: {
hostname: 'abcdefghijklmnop'
}
}
expect_json(ok = true, http = 200)
expect(last_response.body).to eq(JSON.pretty_generate(expected))
vm = fetch_vm('abcdefghijklmnop')
expect(vm['lifetime']).to be_nil
end
end
end
end
end

View file

@ -1,412 +0,0 @@
require 'spec_helper'
require 'rack/test'
describe Vmpooler::API::V2 do
include Rack::Test::Methods
def app()
Vmpooler::API
end
# Added to ensure no leakage in rack state from previous tests.
# Removes all routes, filters, middleware and extension hooks from the current class
# https://rubydoc.info/gems/sinatra/Sinatra/Base#reset!-class_method
before(:each) do
app.reset!
end
describe '/ondemandvm' do
let(:prefix) { '/api/v2' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:config) {
{
config: {
'site_name' => 'test pooler',
'vm_lifetime_auth' => 2,
'max_ondemand_instances_per_request' => 50,
'backend_weight' => {
'compute1' => 5,
'compute2' => 0
}
},
pools: [
{'name' => 'pool1', 'size' => 0, 'clone_target' => 'compute1'},
{'name' => 'pool2', 'size' => 0, 'clone_target' => 'compute2'},
{'name' => 'pool3', 'size' => 0, 'clone_target' => 'compute1'}
],
alias: {
'poolone' => ['pool1'],
'pool2' => ['pool1']
},
pool_names: [ 'pool1', 'pool2', 'pool3', 'poolone' ],
providers: {
:dummy => {
'domain' => 'dummy.com'
}
}
}
}
let(:current_time) { Time.now }
let(:vmname) { 'abcdefghijkl' }
let(:checkoutlock) { Mutex.new }
let(:uuid) { SecureRandom.uuid }
before(:each) do
expect(app).to receive(:run!).once
app.execute([:api], config, redis, metrics, nil)
app.settings.set :config, auth: false
app.settings.set :checkoutlock, checkoutlock
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
config[:pools].each do |pool|
redis.sadd('vmpooler__pools', pool['name'])
end
end
describe 'POST /ondemandvm' do
context 'with a configured pool' do
context 'with no request_id provided in payload' do
before(:each) do
expect(SecureRandom).to receive(:uuid).and_return(uuid)
end
it 'generates a request_id when none is provided' do
post "#{prefix}/ondemandvm", '{"pool1":"1"}'
expect_json(true, 201)
expected = {
"ok": true,
"request_id": uuid
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'uses a configured platform to fulfill a ondemand request' do
post "#{prefix}/ondemandvm", '{"poolone":"1"}'
expect_json(true, 201)
expected = {
"ok": true,
"request_id": uuid
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'creates a provisioning request in redis' do
expect(redis).to receive(:zadd).with('vmpooler__provisioning__request', Integer, uuid).and_return(1)
post "#{prefix}/ondemandvm", '{"poolone":"1"}'
end
it 'sets a platform string in redis for the request to indicate selected platforms' do
expect(redis).to receive(:hset).with("vmpooler__odrequest__#{uuid}", 'requested', 'poolone:pool1:1')
post "#{prefix}/ondemandvm", '{"poolone":"1"}'
end
context 'with a backend of 0 weight' do
before(:each) do
config[:config]['backend_weight']['compute1'] = 0
end
it 'sets the platform string in redis for the request to indicate the selected platforms' do
expect(redis).to receive(:hset).with("vmpooler__odrequest__#{uuid}", 'requested', 'pool1:pool1:1')
post "#{prefix}/ondemandvm", '{"pool1":"1"}'
end
end
it 'sets the platform string in redis for the request to indicate the selected platforms using weight' do
expect(redis).to receive(:hset).with("vmpooler__odrequest__#{uuid}", 'requested', 'pool2:pool1:1')
post "#{prefix}/ondemandvm", '{"pool2":"1"}'
end
context 'with domain set in the config' do
let(:domain) { 'example.com' }
before(:each) do
config[:config]['domain'] = domain
end
it 'should include domain in the return reply' do
post "#{prefix}/ondemandvm", '{"poolone":"1"}'
expect_json(true, 201)
expected = {
"ok": true,
"request_id": uuid,
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
end
context 'with a resource request that exceeds the specified limit' do
let(:max_instances) { 50 }
before(:each) do
config[:config]['max_ondemand_instances_per_request'] = max_instances
end
it 'should reject the request with a message' do
post "#{prefix}/ondemandvm", '{"pool1":"51"}'
expect_json(false, 403)
expected = {
"ok": false,
"message": "requested amount of instances exceeds the maximum #{max_instances}"
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
context 'with request_id provided in the payload' do
it 'uses the given request_id when provided' do
post "#{prefix}/ondemandvm", '{"pool1":"1","request_id":"1234"}'
expect_json(true, 201)
expected = {
"ok": true,
"request_id": "1234"
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'returns 409 conflict error when the request_id has been used' do
post "#{prefix}/ondemandvm", '{"pool1":"1","request_id":"1234"}'
post "#{prefix}/ondemandvm", '{"pool1":"1","request_id":"1234"}'
expect_json(false, 409)
expected = {
"ok": false,
"request_id": "1234",
"message": "request_id '1234' has already been created"
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
context 'with auth configured' do
it 'sets the token and user' do
app.settings.set :config, auth: true
expect(SecureRandom).to receive(:uuid).and_return(uuid)
allow(redis).to receive(:hset)
expect(redis).to receive(:hset).with("vmpooler__odrequest__#{uuid}", 'token:token', 'abcdefghijklmnopqrstuvwxyz012345')
expect(redis).to receive(:hset).with("vmpooler__odrequest__#{uuid}", 'token:user', 'jdoe')
post "#{prefix}/ondemandvm", '{"pool1":"1"}', {
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
}
end
end
end
context 'with a pool that is not configured' do
let(:badpool) { 'pool4' }
it 'returns the bad template' do
post "#{prefix}/ondemandvm", '{"pool4":"1"}'
expect_json(false, 404)
expected = {
"ok": false,
"bad_templates": [ badpool ]
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
it 'returns 400 and a message when JSON is invalid' do
post "#{prefix}/ondemandvm", '{"pool1":"1}'
expect_json(false, 400)
expected = {
"ok": false,
"message": "JSON payload could not be parsed"
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
describe 'GET /ondemandvm' do
it 'returns 404 with message when request is not found' do
get "#{prefix}/ondemandvm/#{uuid}"
expect_json(false, 404)
expected = {
"ok": false,
"message": "no request found for request_id '#{uuid}'"
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
context 'when the request is found' do
let(:score) { current_time }
let(:platforms_string) { 'pool1:pool1:1' }
before(:each) do
create_ondemand_request_for_test(uuid, score, platforms_string, redis)
end
it 'returns 202 while the request is waiting' do
get "#{prefix}/ondemandvm/#{uuid}"
expect_json(true, 202)
expected = {
"ok": true,
"request_id": uuid,
"ready": false,
"pool1": {
"ready": "0",
"pending": "1"
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
context 'with ready instances' do
before(:each) do
create_ondemand_vm(vmname, uuid, 'pool1', 'pool1', redis)
set_ondemand_request_status(uuid, 'ready', redis)
end
it 'returns 200 with hostnames when the request is ready' do
get "#{prefix}/ondemandvm/#{uuid}"
expect_json(true, 200)
expected = {
"ok": true,
"request_id": uuid,
"ready": true,
"pool1": {
"hostname": [
vmname
]
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
context 'with domain set' do
let(:domain) { 'example.com' }
before(:each) do
config[:config]['domain'] = domain
end
it 'should include the domain in the hostname as fqdn, not a separate key unlike in v1' do
get "#{prefix}/ondemandvm/#{uuid}"
expected = {
"ok": true,
"request_id": uuid,
"ready": true,
"pool1": {
"hostname": [
"#{vmname}.#{domain}"
]
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
context 'with domain set in the provider' do
let(:domain) { 'dummy.com' }
before(:each) do
config[:pools][0]['provider'] = 'dummy'
end
it 'should include the domain in the hostname as fqdn, not a separate key unlike in v1' do
get "#{prefix}/ondemandvm/#{uuid}"
expected = {
"ok": true,
"request_id": uuid,
"ready": true,
"pool1": {
"hostname": [
"#{vmname}.#{domain}"
]
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
end
context 'with a deleted request' do
before(:each) do
set_ondemand_request_status(uuid, 'deleted', redis)
end
it 'returns a message that the request has been deleted' do
get "#{prefix}/ondemandvm/#{uuid}"
expect_json(true, 200)
expected = {
"ok": true,
"request_id": uuid,
"ready": false,
"message": "The request has been deleted"
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
context 'with a failed request' do
let(:ondemand_request_ttl) { 5 }
before(:each) do
config[:config]['ondemand_request_ttl'] = ondemand_request_ttl
set_ondemand_request_status(uuid, 'failed', redis)
end
it 'returns a message that the request has failed' do
get "#{prefix}/ondemandvm/#{uuid}"
expect_json(true, 200)
expected = {
"ok": true,
"request_id": uuid,
"ready": false,
"message": "The request failed to provision instances within the configured ondemand_request_ttl '#{ondemand_request_ttl}'"
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
end
end
describe 'DELETE /ondemandvm' do
let(:expiration) { 129_600_0 }
it 'returns 404 with message when request is not found' do
delete "#{prefix}/ondemandvm/#{uuid}"
expect_json(false, 404)
expected = {
"ok": false,
"message": "no request found for request_id '#{uuid}'"
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
context 'when the request is found' do
let(:platforms_string) { 'pool1:pool1:1' }
let(:score) { current_time.to_i }
before(:each) do
create_ondemand_request_for_test(uuid, score, platforms_string, redis)
end
it 'returns 200 for a deleted request' do
delete "#{prefix}/ondemandvm/#{uuid}"
expect_json(true, 200)
expected = { 'ok': true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'marks the request hash for expiration in two weeks' do
expect(redis).to receive(:expire).with("vmpooler__odrequest__#{uuid}", expiration)
delete "#{prefix}/ondemandvm/#{uuid}"
end
context 'with running instances' do
let(:pool) { 'pool1' }
let(:pool_alias) { pool }
before(:each) do
create_ondemand_vm(vmname, uuid, pool, pool_alias, redis)
end
it 'moves allocated instances to the completed queue' do
expect(redis).to receive(:smove).with("vmpooler__running__#{pool}", "vmpooler__completed__#{pool}", vmname)
delete "#{prefix}/ondemandvm/#{uuid}"
end
it 'deletes the set tracking instances allocated for the request' do
expect(redis).to receive(:del).with("vmpooler__#{uuid}__#{pool_alias}__#{pool}")
delete "#{prefix}/ondemandvm/#{uuid}"
end
end
end
end
end
end

View file

@ -1,120 +0,0 @@
require 'spec_helper'
require 'rack/test'
describe Vmpooler::API::V2 do
include Rack::Test::Methods
def app()
Vmpooler::API
end
after(:each) do
Vmpooler::API.reset!
end
let(:config) {
{
config: {
'site_name' => 'test pooler',
'vm_lifetime_auth' => 2,
'experimental_features' => true
},
pools: [
{'name' => 'pool1', 'size' => 5, 'template' => 'templates/pool1', 'clone_target' => 'default_cluster'},
{'name' => 'pool2', 'size' => 10}
],
statsd: { 'prefix' => 'stats_prefix'},
alias: { 'poolone' => 'pool1' },
pool_names: [ 'pool1', 'pool2', 'poolone' ]
}
}
describe '/poolreset' do
let(:prefix) { '/api/v2' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:current_time) { Time.now }
before(:each) do
expect(app).to receive(:run!).once
app.execute([:api], config, redis, metrics, nil)
app.settings.set :config, auth: false
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
end
describe 'POST /poolreset' do
it 'refreshes ready and pending instances from a pool' do
post "#{prefix}/poolreset", '{"pool1":"1"}'
expect_json(ok = true, http = 201)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails on nonexistent pools' do
post "#{prefix}/poolreset", '{"poolpoolpool":"1"}'
expect_json(ok = false, http = 400)
end
it 'resets multiple pools' do
post "#{prefix}/poolreset", '{"pool1":"1","pool2":"1"}'
expect_json(ok = true, http = 201)
expected = { ok: true }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'fails when not all pools exist' do
post "#{prefix}/poolreset", '{"pool1":"1","pool3":"1"}'
expect_json(ok = false, http = 400)
expected = {
ok: false,
bad_pools: ['pool3']
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
context 'with experimental features disabled' do
before(:each) do
config[:config]['experimental_features'] = false
end
it 'should return 405' do
post "#{prefix}/poolreset", '{"pool1":"1"}'
expect_json(ok = false, http = 405)
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
it 'should return 400 for invalid json' do
post "#{prefix}/poolreset", '{"pool1":"1}'
expect_json(ok = false, http = 400)
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'should return 400 with a bad pool name' do
post "#{prefix}/poolreset", '{"pool11":"1"}'
expect_json(ok = false, http = 400)
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
it 'should return 404 when there is no payload' do
post "#{prefix}/poolreset"
expect_json(ok = false, http = 404)
expected = { ok: false }
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
end
end

View file

@ -1,247 +0,0 @@
require 'spec_helper'
require 'rack/test'
def has_set_tag?(vm, tag, value)
value == redis.hget("vmpooler__vm__#{vm}", "tag:#{tag}")
end
describe Vmpooler::API::V2 do
include Rack::Test::Methods
def app()
Vmpooler::API
end
# Added to ensure no leakage in rack state from previous tests.
# Removes all routes, filters, middleware and extension hooks from the current class
# https://rubydoc.info/gems/sinatra/Sinatra/Base#reset!-class_method
before(:each) do
app.reset!
end
describe 'status and metrics endpoints' do
let(:prefix) { '/api/v2' }
let(:config) {
{
config: {
'site_name' => 'test pooler',
'vm_lifetime_auth' => 2,
},
pools: [
{'name' => 'pool1', 'size' => 5, 'alias' => ['poolone', 'poolun']},
{'name' => 'pool2', 'size' => 10},
{'name' => 'pool3', 'size' => 10, 'alias' => 'NotArray'}
]
}
}
let(:current_time) { Time.now }
before(:each) do
expect(app).to receive(:run!).once
app.execute([:api], config, redis, nil, nil)
app.settings.set :config, auth: false
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
end
describe 'GET /status' do
it 'returns the configured maximum size for each pool' do
get "#{prefix}/status/"
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["pools"]["pool1"]["max"]).to be(5)
expect(result["pools"]["pool2"]["max"]).to be(10)
end
it 'returns the number of ready vms for each pool' do
3.times {|i| create_ready_vm("pool1", "vm-#{i}", redis) }
get "#{prefix}/status/"
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["pools"]["pool1"]["ready"]).to be(3)
expect(result["pools"]["pool2"]["ready"]).to be(0)
end
it 'returns the number of running vms for each pool' do
3.times {|i| create_running_vm("pool1", "vm-#{i}", redis) }
4.times {|i| create_running_vm("pool2", "vm-#{i}", redis) }
get "#{prefix}/status/"
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["pools"]["pool1"]["running"]).to be(3)
expect(result["pools"]["pool2"]["running"]).to be(4)
end
it 'returns the number of pending vms for each pool' do
3.times {|i| create_pending_vm("pool1", "vm-#{i}", redis) }
4.times {|i| create_pending_vm("pool2", "vm-#{i}", redis) }
get "#{prefix}/status/"
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["pools"]["pool1"]["pending"]).to be(3)
expect(result["pools"]["pool2"]["pending"]).to be(4)
end
it 'returns aliases if configured in the pool' do
get "#{prefix}/status/"
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["pools"]["pool1"]["alias"]).to eq(['poolone', 'poolun'])
expect(result["pools"]["pool2"]["alias"]).to be(nil)
expect(result["pools"]["pool3"]["alias"]).to eq('NotArray')
end
it '(for v1 backwards compatibility) lists any empty pools in the status section' do
get "#{prefix}/status/"
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["status"]["empty"].sort).to eq(["pool1", "pool2", "pool3"])
end
end
describe 'GET /status with view query parameter' do
it 'returns capacity when specified' do
get "#{prefix}/status?view=capacity"
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["capacity"]).to_not be(nil)
expect(result["queue"]).to be(nil)
expect(result["clone"]).to be(nil)
expect(result["boot"]).to be(nil)
expect(result["pools"]).to be(nil)
expect(result["status"]).to_not be(nil)
end
it 'returns pools and queue when specified' do
get "#{prefix}/status?view=pools,queue"
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["capacity"]).to be(nil)
expect(result["queue"]).to_not be(nil)
expect(result["clone"]).to be(nil)
expect(result["boot"]).to be(nil)
expect(result["pools"]).to_not be(nil)
expect(result["status"]).to_not be(nil)
end
it 'does nothing with invalid view names' do
get "#{prefix}/status?view=clone,boot,invalidThingToView"
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["capacity"]).to be(nil)
expect(result["queue"]).to be(nil)
expect(result["clone"]).to_not be(nil)
expect(result["boot"]).to_not be(nil)
expect(result["pools"]).to be(nil)
expect(result["status"]).to_not be(nil)
end
it 'returns everything when view is not specified' do
get "#{prefix}/status"
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["capacity"]).to_not be(nil)
expect(result["queue"]).to_not be(nil)
expect(result["clone"]).to_not be(nil)
expect(result["boot"]).to_not be(nil)
expect(result["pools"]).to_not be(nil)
expect(result["status"]).to_not be(nil)
end
it 'returns everything when view is alone' do
get "#{prefix}/status?view"
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["capacity"]).to_not be(nil)
expect(result["queue"]).to_not be(nil)
expect(result["clone"]).to_not be(nil)
expect(result["boot"]).to_not be(nil)
expect(result["pools"]).to_not be(nil)
expect(result["status"]).to_not be(nil)
end
it 'returns status only when view is empty' do
get "#{prefix}/status?view="
# of course /status doesn't conform to the weird standard everything else uses...
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["capacity"]).to be(nil)
expect(result["queue"]).to be(nil)
expect(result["clone"]).to be(nil)
expect(result["boot"]).to be(nil)
expect(result["pools"]).to be(nil)
expect(result["status"]).to_not be(nil)
end
end
describe 'GET /poolstat' do
it 'returns empty list when pool is not set' do
get "#{prefix}/poolstat"
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result == {})
end
it 'returns empty list when pool is not found' do
get "#{prefix}/poolstat?pool=unknownpool"
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result == {})
end
it 'returns one pool when requesting one with alias' do
get "#{prefix}/poolstat?pool=pool1"
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["pools"].size == 1)
expect(result["pools"]["pool1"]["ready"]).to eq(0)
expect(result["pools"]["pool1"]["max"]).to eq(5)
expect(result["pools"]["pool1"]["alias"]).to eq(['poolone', 'poolun'])
end
it 'returns one pool when requesting one without alias' do
get "#{prefix}/poolstat?pool=pool2"
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["pools"].size == 1)
expect(result["pools"]["pool2"]["ready"]).to eq(0)
expect(result["pools"]["pool2"]["max"]).to eq(10)
expect(result["pools"]["pool2"]["alias"]).to be(nil)
end
it 'returns multiple pools when requesting csv' do
get "#{prefix}/poolstat?pool=pool1,pool2"
expect(last_response.header['Content-Type']).to eq('application/json')
result = JSON.parse(last_response.body)
expect(result["pools"].size == 2)
end
end
describe 'GET /totalrunning' do
it 'returns the number of running VMs' do
get "#{prefix}/totalrunning"
expect(last_response.header['Content-Type']).to eq('application/json')
5.times {|i| create_running_vm("pool1", "vm-#{i}", redis, redis) }
5.times {|i| create_running_vm("pool3", "vm-#{i}", redis, redis) }
result = JSON.parse(last_response.body)
expect(result["running"] == 10)
end
end
end
end

View file

@ -1,352 +0,0 @@
require 'spec_helper'
require 'rack/test'
def has_set_tag?(vm, tag, value)
value == redis.hget("vmpooler__vm__#{vm}", "tag:#{tag}")
end
describe Vmpooler::API::V2 do
include Rack::Test::Methods
def app()
Vmpooler::API
end
# Added to ensure no leakage in rack state from previous tests.
# Removes all routes, filters, middleware and extension hooks from the current class
# https://rubydoc.info/gems/sinatra/Sinatra/Base#reset!-class_method
before(:each) do
app.reset!
end
describe '/vm/:hostname' do
let(:prefix) { '/api/v2' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:config) {
{
config: {
'site_name' => 'test pooler',
'vm_lifetime_auth' => 2,
},
pools: [
{'name' => 'pool1', 'size' => 5},
{'name' => 'pool2', 'size' => 10}
],
alias: { 'poolone' => 'pool1' },
auth: false
}
}
let(:current_time) { Time.now }
before(:each) do
expect(app).to receive(:run!).once
app.execute([:api], config, redis, metrics, nil)
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
end
describe 'PUT /vm/:hostname' do
it 'allows tags to be set' do
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"tags":{"tested_by":"rspec"}}'
expect_json(ok = true, http = 200)
expect has_set_tag?('testhost', 'tested_by', 'rspec')
end
it 'skips empty tags' do
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"tags":{"tested_by":""}}'
expect_json(ok = true, http = 200)
expect !has_set_tag?('testhost', 'tested_by', '')
end
it 'does not set tags if request body format is invalid' do
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"tags":{"tested"}}'
expect_json(ok = false, http = 400)
expect !has_set_tag?('testhost', 'tested', '')
end
context '(allowed_tags configured)' do
it 'fails if specified tag is not in allowed_tags array' do
app.settings.set :config,
{ :config => { 'allowed_tags' => ['created_by', 'project', 'url'] } }
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"tags":{"created_by":"rspec","tested_by":"rspec"}}'
expect_json(ok = false, http = 400)
expect !has_set_tag?('testhost', 'tested_by', 'rspec')
end
end
context '(tagfilter configured)' do
before(:each) do
app.settings.set :config, tagfilter: { 'url' => '(.*)\/' }
end
it 'correctly filters tags' do
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"tags":{"url":"foo.com/something.html"}}'
expect_json(ok = true, http = 200)
expect has_set_tag?('testhost', 'url', 'foo.com')
end
it "doesn't eat tags not matching filter" do
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"tags":{"url":"foo.com"}}'
expect_json(ok = true, http = 200)
expect has_set_tag?('testhost', 'url', 'foo.com')
end
end
context '(auth not configured)' do
before(:each) do
app.settings.set :config, auth: false
end
it 'allows VM lifetime to be modified without a token' do
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"lifetime":"1"}'
expect_json(ok = true, http = 200)
vm = fetch_vm('testhost')
expect(vm['lifetime'].to_i).to eq(1)
end
it 'does not allow a lifetime to be 0' do
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"lifetime":"0"}'
expect_json(ok = false, http = 400)
vm = fetch_vm('testhost')
expect(vm['lifetime']).to be_nil
end
it 'does not enforce a lifetime' do
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"lifetime":"20000"}'
expect_json(ok = true, http = 200)
vm = fetch_vm('testhost')
expect(vm['lifetime']).to eq("20000")
end
it 'does not allow a lifetime to be initially past config max_lifetime_upper_limit' do
app.settings.set :config,
{ :config => { 'max_lifetime_upper_limit' => 168 } }
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"lifetime":"200"}'
expect_json(ok = false, http = 400)
vm = fetch_vm('testhost')
expect(vm['lifetime']).to be_nil
end
# it 'does not allow a lifetime to be extended past config 168' do
# app.settings.set :config,
# { :config => { 'max_lifetime_upper_limit' => 168 } }
# create_vm('testhost', redis)
#
# set_vm_data('testhost', "checkout", (Time.now - (69*60*60)), redis)
# puts redis.hget("vmpooler__vm__testhost", 'checkout')
# put "#{prefix}/vm/testhost", '{"lifetime":"100"}'
# expect_json(ok = false, http = 400)
#
# vm = fetch_vm('testhost')
# expect(vm['lifetime']).to be_nil
# end
end
context '(auth configured)' do
before(:each) do
app.settings.set :config, auth: true
end
it 'allows VM lifetime to be modified with a token' do
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"lifetime":"1"}', {
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
}
expect_json(ok = true, http = 200)
vm = fetch_vm('testhost')
expect(vm['lifetime'].to_i).to eq(1)
end
it 'does not allows VM lifetime to be modified without a token' do
create_vm('testhost', redis)
put "#{prefix}/vm/testhost", '{"lifetime":"1"}'
expect_json(ok = false, http = 401)
end
end
end
describe 'DELETE /vm/:hostname' do
context '(auth not configured)' do
it 'does not delete a non-existant VM' do
delete "#{prefix}/vm/testhost"
expect_json(ok = false, http = 404)
end
it 'deletes an existing VM' do
create_running_vm('pool1', 'testhost', redis)
expect fetch_vm('testhost')
delete "#{prefix}/vm/testhost"
expect_json(ok = true, http = 200)
expect !fetch_vm('testhost')
end
end
context '(auth configured)' do
before(:each) do
app.settings.set :config, auth: true
end
context '(checked-out without token)' do
it 'deletes a VM without supplying a token' do
create_running_vm('pool1', 'testhost', redis)
expect fetch_vm('testhost')
delete "#{prefix}/vm/testhost"
expect_json(ok = true, http = 200)
expect !fetch_vm('testhost')
end
end
context '(checked-out with token)' do
it 'fails to delete a VM without supplying a token' do
create_running_vm('pool1', 'testhost', redis, 'abcdefghijklmnopqrstuvwxyz012345')
expect fetch_vm('testhost')
delete "#{prefix}/vm/testhost"
expect_json(ok = false, http = 401)
expect fetch_vm('testhost')
end
it 'deletes a VM when token is supplied' do
create_running_vm('pool1', 'testhost', redis, 'abcdefghijklmnopqrstuvwxyz012345')
expect fetch_vm('testhost')
delete "#{prefix}/vm/testhost", "", {
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
}
expect_json(ok = true, http = 200)
expect !fetch_vm('testhost')
end
end
end
end
describe 'POST /vm/:hostname/snapshot' do
context '(auth not configured)' do
it 'creates a snapshot' do
create_vm('testhost', redis)
post "#{prefix}/vm/testhost/snapshot"
expect_json(ok = true, http = 202)
expect(JSON.parse(last_response.body)['testhost']['snapshot'].length).to be(32)
end
end
context '(auth configured)' do
before(:each) do
app.settings.set :config, auth: true
end
it 'returns a 401 if not authed' do
post "#{prefix}/vm/testhost/snapshot"
expect_json(ok = false, http = 401)
expect !has_vm_snapshot?('testhost', redis)
end
it 'creates a snapshot if authed' do
create_vm('testhost', redis)
snapshot_vm('testhost', 'testsnapshot', redis)
post "#{prefix}/vm/testhost/snapshot", "", {
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
}
expect_json(ok = true, http = 202)
expect(JSON.parse(last_response.body)['testhost']['snapshot'].length).to be(32)
expect has_vm_snapshot?('testhost', redis)
end
end
end
describe 'POST /vm/:hostname/snapshot/:snapshot' do
context '(auth not configured)' do
it 'reverts to a snapshot' do
create_vm('testhost', redis)
snapshot_vm('testhost', 'testsnapshot', redis)
post "#{prefix}/vm/testhost/snapshot/testsnapshot"
expect_json(ok = true, http = 202)
expect vm_reverted_to_snapshot?('testhost', redis, 'testsnapshot')
end
it 'fails if the specified snapshot does not exist' do
create_vm('testhost', redis)
post "#{prefix}/vm/testhost/snapshot/testsnapshot", "", {
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
}
expect_json(ok = false, http = 404)
expect !vm_reverted_to_snapshot?('testhost', redis, 'testsnapshot')
end
end
context '(auth configured)' do
before(:each) do
app.settings.set :config, auth: true
end
it 'returns a 401 if not authed' do
create_vm('testhost', redis)
snapshot_vm('testhost', 'testsnapshot', redis)
post "#{prefix}/vm/testhost/snapshot/testsnapshot"
expect_json(ok = false, http = 401)
expect !vm_reverted_to_snapshot?('testhost', redis, 'testsnapshot')
end
it 'fails if authed and the specified snapshot does not exist' do
create_vm('testhost', redis)
post "#{prefix}/vm/testhost/snapshot/testsnapshot", "", {
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
}
expect_json(ok = false, http = 404)
expect !vm_reverted_to_snapshot?('testhost', redis, 'testsnapshot')
end
it 'reverts to a snapshot if authed' do
create_vm('testhost', redis)
snapshot_vm('testhost', 'testsnapshot', redis)
post "#{prefix}/vm/testhost/snapshot/testsnapshot", "", {
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
}
expect_json(ok = true, http = 202)
expect vm_reverted_to_snapshot?('testhost', redis, 'testsnapshot')
end
end
end
end
end

View file

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
require 'rack/test' require 'rack/test'
describe Vmpooler::API::V2 do describe Vmpooler::API::V3 do
include Rack::Test::Methods include Rack::Test::Methods
def app() def app()
@ -37,7 +37,7 @@ describe Vmpooler::API::V2 do
} }
describe '/config/pooltemplate' do describe '/config/pooltemplate' do
let(:prefix) { '/api/v2' } let(:prefix) { '/api/v3' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new } let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:current_time) { Time.now } let(:current_time) { Time.now }
@ -388,7 +388,7 @@ describe Vmpooler::API::V2 do
end end
describe 'GET /config' do describe 'GET /config' do
let(:prefix) { '/api/v2' } let(:prefix) { '/api/v3' }
it 'returns pool configuration when set' do it 'returns pool configuration when set' do
get "#{prefix}/config" get "#{prefix}/config"

View file

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
require 'rack/test' require 'rack/test'
describe Vmpooler::API::V1 do describe Vmpooler::API::V3 do
include Rack::Test::Methods include Rack::Test::Methods
def app() def app()
@ -15,7 +15,7 @@ describe Vmpooler::API::V1 do
end end
describe '/ondemandvm' do describe '/ondemandvm' do
let(:prefix) { '/api/v1' } let(:prefix) { '/api/v3' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new } let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:config) { let(:config) {
{ {
@ -28,16 +28,25 @@ describe Vmpooler::API::V1 do
'compute2' => 0 'compute2' => 0
} }
}, },
dns_configs: {
:mock => {
'dns_class' => 'mock',
'domain' => 'example.com'
}
},
pools: [ pools: [
{'name' => 'pool1', 'size' => 0, 'clone_target' => 'compute1'}, {'name' => 'pool1', 'size' => 0, 'clone_target' => 'compute1', 'dns_plugin' => 'mock'},
{'name' => 'pool2', 'size' => 0, 'clone_target' => 'compute2'}, {'name' => 'pool2', 'size' => 0, 'clone_target' => 'compute2', 'dns_plugin' => 'mock'},
{'name' => 'pool3', 'size' => 0, 'clone_target' => 'compute1'} {'name' => 'pool3', 'size' => 0, 'clone_target' => 'compute1', 'dns_plugin' => 'mock'}
], ],
alias: { alias: {
'poolone' => ['pool1'], 'poolone' => ['pool1'],
'pool2' => ['pool1'] 'pool2' => ['pool1']
}, },
pool_names: [ 'pool1', 'pool2', 'pool3', 'poolone' ] pool_names: [ 'pool1', 'pool2', 'pool3', 'poolone' ],
providers: {
:dummy => {},
}
} }
} }
let(:current_time) { Time.now } let(:current_time) { Time.now }
@ -111,24 +120,6 @@ describe Vmpooler::API::V1 do
expect(redis).to receive(:hset).with("vmpooler__odrequest__#{uuid}", 'requested', 'pool2:pool1:1') expect(redis).to receive(:hset).with("vmpooler__odrequest__#{uuid}", 'requested', 'pool2:pool1:1')
post "#{prefix}/ondemandvm", '{"pool2":"1"}' post "#{prefix}/ondemandvm", '{"pool2":"1"}'
end end
context 'with domain set in the config' do
let(:domain) { 'example.com' }
before(:each) do
config[:config]['domain'] = domain
end
it 'should include domain in the return reply' do
post "#{prefix}/ondemandvm", '{"poolone":"1"}'
expect_json(true, 201)
expected = {
"ok": true,
"request_id": uuid,
"domain": domain
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
end end
context 'with a resource request that exceeds the specified limit' do context 'with a resource request that exceeds the specified limit' do
@ -262,35 +253,12 @@ describe Vmpooler::API::V1 do
"ready": true, "ready": true,
"pool1": { "pool1": {
"hostname": [ "hostname": [
vmname "#{vmname}.example.com"
] ]
} }
} }
expect(last_response.body).to eq(JSON.pretty_generate(expected)) expect(last_response.body).to eq(JSON.pretty_generate(expected))
end end
context 'with domain set' do
let(:domain) { 'example.com' }
before(:each) do
config[:config]['domain'] = domain
end
it 'should include the domain in the result' do
get "#{prefix}/ondemandvm/#{uuid}"
expected = {
"ok": true,
"request_id": uuid,
"ready": true,
"pool1": {
"hostname": [
vmname
]
},
"domain": domain
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
end
end
end end
context 'with a deleted request' do context 'with a deleted request' do

View file

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
require 'rack/test' require 'rack/test'
describe Vmpooler::API::V1 do describe Vmpooler::API::V3 do
include Rack::Test::Methods include Rack::Test::Methods
def app() def app()
@ -30,7 +30,7 @@ describe Vmpooler::API::V1 do
} }
describe '/poolreset' do describe '/poolreset' do
let(:prefix) { '/api/v1' } let(:prefix) { '/api/v3' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new } let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:current_time) { Time.now } let(:current_time) { Time.now }

View file

@ -5,7 +5,7 @@ def has_set_tag?(vm, tag, value)
value == redis.hget("vmpooler__vm__#{vm}", "tag:#{tag}") value == redis.hget("vmpooler__vm__#{vm}", "tag:#{tag}")
end end
describe Vmpooler::API::V1 do describe Vmpooler::API::V3 do
include Rack::Test::Methods include Rack::Test::Methods
def app() def app()
@ -20,7 +20,7 @@ describe Vmpooler::API::V1 do
end end
describe 'status and metrics endpoints' do describe 'status and metrics endpoints' do
let(:prefix) { '/api/v1' } let(:prefix) { '/api/v3' }
let(:config) { let(:config) {
{ {

View file

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
require 'rack/test' require 'rack/test'
describe Vmpooler::API::V2 do describe Vmpooler::API::V3 do
include Rack::Test::Methods include Rack::Test::Methods
def app() def app()
@ -16,7 +16,7 @@ describe Vmpooler::API::V2 do
end end
describe '/token' do describe '/token' do
let(:prefix) { '/api/v2' } let(:prefix) { '/api/v3' }
let(:current_time) { Time.now } let(:current_time) { Time.now }
let(:config) { { let(:config) { {
config: {} config: {}
@ -111,7 +111,7 @@ describe Vmpooler::API::V2 do
end end
describe '/token/:token' do describe '/token/:token' do
let(:prefix) { '/api/v2' } let(:prefix) { '/api/v3' }
let(:current_time) { Time.now } let(:current_time) { Time.now }
before(:each) do before(:each) do

View file

@ -5,7 +5,7 @@ def has_set_tag?(vm, tag, value)
value == redis.hget("vmpooler__vm__#{vm}", "tag:#{tag}") value == redis.hget("vmpooler__vm__#{vm}", "tag:#{tag}")
end end
describe Vmpooler::API::V1 do describe Vmpooler::API::V3 do
include Rack::Test::Methods include Rack::Test::Methods
def app() def app()
@ -20,7 +20,7 @@ describe Vmpooler::API::V1 do
end end
describe '/vm/:hostname' do describe '/vm/:hostname' do
let(:prefix) { '/api/v1' } let(:prefix) { '/api/v3' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new } let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:config) { let(:config) {

View file

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
require 'rack/test' require 'rack/test'
describe Vmpooler::API::V2 do describe Vmpooler::API::V3 do
include Rack::Test::Methods include Rack::Test::Methods
def app() def app()
@ -16,7 +16,7 @@ describe Vmpooler::API::V2 do
end end
describe '/vm' do describe '/vm' do
let(:prefix) { '/api/v2' } let(:prefix) { '/api/v3' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new } let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:config) { let(:config) {
{ {
@ -25,14 +25,28 @@ describe Vmpooler::API::V2 do
'vm_lifetime_auth' => 2 'vm_lifetime_auth' => 2
}, },
providers: { providers: {
vsphere: {'domain' => 'one.example.com'}, vsphere: {},
gce: {'domain' => 'two.example.com'}, gce: {},
foo: {'domain' => 'three.example.com'} foo: {}
},
dns_configs: {
:one => {
'dns_class' => 'mock',
'domain' => 'one.example.com'
},
:two => {
'dns_class' => 'mock',
'domain' => 'two.example.com'
},
:three => {
'dns_class' => 'mock',
'domain' => 'three.example.com'
}
}, },
pools: [ pools: [
{'name' => 'pool1', 'size' => 5, 'provider' => 'vsphere'}, {'name' => 'pool1', 'size' => 5, 'provider' => 'vsphere', 'dns_plugin' => 'one'},
{'name' => 'pool2', 'size' => 10, 'provider' => 'gce'}, {'name' => 'pool2', 'size' => 10, 'provider' => 'gce', 'dns_plugin' => 'two'},
{'name' => 'pool3', 'size' => 10, 'provider' => 'foo'} {'name' => 'pool3', 'size' => 10, 'provider' => 'foo', 'dns_plugin' => 'three'}
], ],
statsd: { 'prefix' => 'stats_prefix'}, statsd: { 'prefix' => 'stats_prefix'},
alias: { 'poolone' => ['pool1'] }, alias: { 'poolone' => ['pool1'] },
@ -341,6 +355,28 @@ describe Vmpooler::API::V2 do
expect(pool_has_ready_vm?('pool1', '2abcdefghijklmnop', redis)).to eq(true) expect(pool_has_ready_vm?('pool1', '2abcdefghijklmnop', redis)).to eq(true)
end end
it 'returns the second VM when the first fails to respond' do
create_running_vm 'pool1', vmname, redis
create_ready_vm 'pool1', "2#{vmname}", redis
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).with(vmname, nil).and_raise('mockerror')
allow_any_instance_of(Vmpooler::API::Helpers).to receive(:open_socket).with("2#{vmname}", nil).and_return(socket)
post "#{prefix}/vm", '{"pool1":"1"}'
expect_json(ok = true, http = 200)
expected = {
ok: true,
pool1: {
hostname: "2#{vmname}.one.example.com"
}
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
expect(pool_has_ready_vm?('pool1', vmname, redis)).to be false
end
context '(auth not configured)' do context '(auth not configured)' do
it 'does not extend VM lifetime if auth token is provided' do it 'does not extend VM lifetime if auth token is provided' do
app.settings.set :config, auth: false app.settings.set :config, auth: false

View file

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
require 'rack/test' require 'rack/test'
describe Vmpooler::API::V2 do describe Vmpooler::API::V3 do
include Rack::Test::Methods include Rack::Test::Methods
def app() def app()
@ -16,7 +16,7 @@ describe Vmpooler::API::V2 do
end end
describe '/vm/:template' do describe '/vm/:template' do
let(:prefix) { '/api/v2' } let(:prefix) { '/api/v3' }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new } let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:config) { let(:config) {
{ {
@ -24,11 +24,17 @@ describe Vmpooler::API::V2 do
'site_name' => 'test pooler', 'site_name' => 'test pooler',
'vm_lifetime_auth' => 2, 'vm_lifetime_auth' => 2,
}, },
dns_configs: {
:example => {
'dns_class' => 'mock',
'domain' => 'example.com'
}
},
providers: { vsphere: {} }, providers: { vsphere: {} },
pools: [ pools: [
{'name' => 'pool1', 'size' => 5}, {'name' => 'pool1', 'size' => 5, 'provider' => 'vsphere', 'dns_plugin' => 'example'},
{'name' => 'pool2', 'size' => 10}, {'name' => 'pool2', 'size' => 10, 'provider' => 'vsphere', 'dns_plugin' => 'example'},
{'name' => 'poolone', 'size' => 1} {'name' => 'poolone', 'size' => 1, 'provider' => 'vsphere', 'dns_plugin' => 'example'}
], ],
statsd: { 'prefix' => 'stats_prefix'}, statsd: { 'prefix' => 'stats_prefix'},
alias: { 'poolone' => 'pool1' }, alias: { 'poolone' => 'pool1' },
@ -60,7 +66,7 @@ describe Vmpooler::API::V2 do
expected = { expected = {
ok: true, ok: true,
pool1: { pool1: {
hostname: 'abcdefghijklmnop' hostname: 'abcdefghijklmnop.example.com'
} }
} }
@ -77,7 +83,7 @@ describe Vmpooler::API::V2 do
expected = { expected = {
ok: true, ok: true,
poolone: { poolone: {
hostname: 'abcdefghijklmnop' hostname: 'abcdefghijklmnop.example.com'
} }
} }
expect_json(ok = true, http = 200) expect_json(ok = true, http = 200)
@ -126,10 +132,10 @@ describe Vmpooler::API::V2 do
expected = { expected = {
ok: true, ok: true,
pool1: { pool1: {
hostname: 'abcdefghijklmnop' hostname: 'abcdefghijklmnop.example.com'
}, },
pool2: { pool2: {
hostname: 'qrstuvwxyz012345' hostname: 'qrstuvwxyz012345.example.com'
} }
} }
@ -151,17 +157,17 @@ describe Vmpooler::API::V2 do
expected = { expected = {
ok: true, ok: true,
pool1: { pool1: {
hostname: [ '1abcdefghijklmnop', '2abcdefghijklmnop' ] hostname: [ '1abcdefghijklmnop.example.com', '2abcdefghijklmnop.example.com' ]
}, },
pool2: { pool2: {
hostname: [ '1qrstuvwxyz012345', '2qrstuvwxyz012345', '3qrstuvwxyz012345' ] hostname: [ '1qrstuvwxyz012345.example.com', '2qrstuvwxyz012345.example.com', '3qrstuvwxyz012345.example.com' ]
} }
} }
result = JSON.parse(last_response.body) result = JSON.parse(last_response.body)
expect(result['ok']).to eq(true) expect(result['ok']).to eq(true)
expect(result['pool1']['hostname']).to include('1abcdefghijklmnop', '2abcdefghijklmnop') expect(result['pool1']['hostname']).to include('1abcdefghijklmnop.example.com', '2abcdefghijklmnop.example.com')
expect(result['pool2']['hostname']).to include('1qrstuvwxyz012345', '2qrstuvwxyz012345', '3qrstuvwxyz012345') expect(result['pool2']['hostname']).to include('1qrstuvwxyz012345.example.com', '2qrstuvwxyz012345.example.com', '3qrstuvwxyz012345.example.com')
expect_json(ok = true, http = 200) expect_json(ok = true, http = 200)
end end
@ -265,7 +271,7 @@ describe Vmpooler::API::V2 do
expected = { expected = {
ok: true, ok: true,
pool1: { pool1: {
hostname: 'abcdefghijklmnop' hostname: 'abcdefghijklmnop.example.com'
} }
} }
@ -291,7 +297,7 @@ describe Vmpooler::API::V2 do
expected = { expected = {
ok: true, ok: true,
pool1: { pool1: {
hostname: 'abcdefghijklmnop' hostname: 'abcdefghijklmnop.example.com'
} }
} }
expect(last_response.body).to eq(JSON.pretty_generate(expected)) expect(last_response.body).to eq(JSON.pretty_generate(expected))
@ -311,7 +317,7 @@ describe Vmpooler::API::V2 do
expected = { expected = {
ok: true, ok: true,
pool1: { pool1: {
hostname: 'abcdefghijklmnop' hostname: 'abcdefghijklmnop.example.com'
} }
} }
expect_json(ok = true, http = 200) expect_json(ok = true, http = 200)

View file

@ -16,14 +16,12 @@ describe Vmpooler::API::Helpers do
describe '#hostname_shorten' do describe '#hostname_shorten' do
[ [
['example.com', 'not-example.com', 'example'], ['example.com', 'example'],
['example.com', 'example.com', 'example'], ['sub.example.com', 'sub'],
['sub.example.com', 'example.com', 'sub'], ['adjective-noun.example.com', 'adjective-noun'],
['adjective-noun.example.com', 'example.com', 'adjective-noun'], ['abc123.example.com', 'abc123']
['abc123.example.com', 'example.com', 'abc123'], ].each do |hostname, expected|
['example.com', nil, 'example'] it { expect(subject.hostname_shorten(hostname)).to eq expected }
].each do |hostname, domain, expected|
it { expect(subject.hostname_shorten(hostname, domain)).to eq expected }
end end
end end

172
spec/unit/dns/base_spec.rb Normal file
View file

@ -0,0 +1,172 @@
require 'spec_helper'
require 'vmpooler/dns/base'
# This spec does not really exercise code paths but is merely used
# to enforce that certain methods are defined in the base classes
describe 'Vmpooler::PoolManager::Dns::Base' do
let(:logger) { MockLogger.new }
let(:metrics) { Vmpooler::Metrics::DummyStatsd.new }
let(:config) { {} }
let(:dns_plugin_name) { 'base' }
let(:dns_options) { { 'param' => 'value' } }
let(:fake_vm) {
fake_vm = {}
fake_vm['name'] = 'vm1'
fake_vm['hostname'] = 'vm1'
fake_vm['template'] = 'pool1'
fake_vm['boottime'] = Time.now
fake_vm['powerstate'] = 'PoweredOn'
fake_vm
}
let(:redis_connection_pool) { Vmpooler::PoolManager::GenericConnectionPool.new(
metrics: metrics,
connpool_type: 'redis_connection_pool',
connpool_provider: 'testprovider',
size: 1,
timeout: 5
) { MockRedis.new }
}
subject { Vmpooler::PoolManager::Dns::Base.new(config, logger, metrics, redis_connection_pool, dns_plugin_name, dns_options) }
# Helper attr_reader methods
describe '#logger' do
it 'should come from the provider initialization' do
expect(subject.logger).to be(logger)
end
end
describe '#metrics' do
it 'should come from the provider initialization' do
expect(subject.metrics).to be(metrics)
end
end
describe '#dns_options' do
it 'should come from the provider initialization' do
expect(subject.dns_options).to be(dns_options)
end
end
describe '#pool_config' do
let(:poolname) { 'pool1' }
let(:config) { YAML.load(<<-EOT
---
:pools:
- name: '#{poolname}'
alias: [ 'mockpool' ]
template: 'Templates/pool1'
folder: 'Pooler/pool1'
datastore: 'datastore0'
size: 5
timeout: 10
ready_ttl: 1440
clone_target: 'cluster1'
EOT
)
}
context 'Given a pool that does not exist' do
it 'should return nil' do
expect(subject.pool_config('missing_pool')).to be_nil
end
end
context 'Given a pool that does exist' do
it 'should return the pool\'s configuration' do
result = subject.pool_config(poolname)
expect(result['name']).to eq(poolname)
end
end
end
describe '#dns_config' do
let(:poolname) { 'pool1' }
let(:config) { YAML.load(<<-EOT
---
:dns_configs:
:#{dns_plugin_name}:
option1: 'value1'
EOT
)
}
context 'Given a dns plugin with no configuration' do
let(:config) { YAML.load(<<-EOT
---
:dns_configs:
:bad_dns:
option1: 'value1'
option2: 'value1'
EOT
)
}
it 'should return nil' do
expect(subject.dns_config).to be_nil
end
end
context 'Given a correct dns config name' do
it 'should return the dns\'s configuration' do
result = subject.dns_config
expect(result['option1']).to eq('value1')
end
end
end
describe '#global_config' do
it 'should come from the dns initialization' do
expect(subject.global_config).to be(config)
end
end
describe '#name' do
it "should come from the dns initialization" do
expect(subject.name).to eq(dns_plugin_name)
end
end
describe '#get_ip' do
it 'calls redis hget with vm name and ip' do
redis_connection_pool.with do |redis|
expect(redis).to receive(:hget).with("vmpooler__vm__vm1", 'ip')
end
subject.get_ip(fake_vm['name'])
end
end
describe '#provided_pools' do
let(:config) { YAML.load(<<-EOT
---
:pools:
- name: 'pool1'
dns_config: 'base'
- name: 'pool2'
dns_config: 'base'
- name: 'otherpool'
dns_config: 'other provider'
- name: 'no name'
EOT
)
}
it "should return pools serviced by this provider" do
expect(subject.provided_pools).to eq(['pool1','pool2'])
end
end
describe '#create_or_replace_record' do
it 'should raise error' do
expect{subject.create_or_replace_record('pool')}.to raise_error(/does not implement create_or_replace_record/)
end
end
describe '#delete_record' do
it 'should raise error' do
expect{subject.delete_record('pool')}.to raise_error(/does not implement delete_record/)
end
end
end

62
spec/unit/dns_spec.rb Normal file
View file

@ -0,0 +1,62 @@
require 'spec_helper'
describe 'Vmpooler::Dns' do
let(:dns_class) { 'mock-dnsservice' }
let(:dns_config_name) { 'mock' }
let(:pool) { 'pool1' }
let(:config) { YAML.load(<<~EOT
---
:dns_configs:
:mock:
dns_class: 'mock'
domain: 'example.com'
:pools:
- name: 'pool1'
dns_plugin: 'mock'
EOT
)}
subject { Vmpooler::Dns.new }
describe '.get_dns_plugin_class_by_name' do
it 'returns the plugin class for the specified config' do
result = Vmpooler::Dns.get_dns_plugin_class_by_name(config, dns_config_name)
expect(result).to eq('mock')
end
end
describe '.get_domain_for_pool' do
it 'returns the domain for the specified pool' do
result = Vmpooler::Dns.get_domain_for_pool(config, pool)
expect(result).to eq('example.com')
end
end
describe '.get_dns_plugin_domain_by_name' do
it 'returns the domain for the specified config' do
result = Vmpooler::Dns.get_dns_plugin_domain_by_name(config, dns_config_name)
expect(result).to eq('example.com')
end
end
describe '.get_dns_plugin_config_classes' do
it 'returns the list of dns plugin classes' do
result = Vmpooler::Dns.get_dns_plugin_config_classes(config)
expect(result).to eq(['mock'])
end
end
describe '#load_from_gems' do
let(:gem_name) { 'mock-dnsservice' }
let(:translated_gem_name) { 'mock/dnsservice' }
before(:each) do
allow(subject).to receive(:require).with(gem_name).and_return(true)
end
it 'loads the specified gem' do
expect(subject).to receive(:require).with("vmpooler/dns/#{translated_gem_name}")
result = subject.load_from_gems(gem_name)
expect(result).to eq("vmpooler/dns/#{translated_gem_name}")
end
end
end

View file

@ -22,7 +22,6 @@ describe 'Vmpooler' do
['prefix', test_string, ""], ['prefix', test_string, ""],
['logfile', test_string, nil], ['logfile', test_string, nil],
['site_name', test_string, nil], ['site_name', test_string, nil],
['domain', test_string, nil],
['clone_target', test_string, nil], ['clone_target', test_string, nil],
['create_folders', test_bool, nil], ['create_folders', test_bool, nil],
['create_template_delta_disks', test_bool, nil], ['create_template_delta_disks', test_bool, nil],

View file

@ -20,6 +20,7 @@ describe 'Pool Manager' do
let(:current_time) { Time.now } let(:current_time) { Time.now }
let(:provider_options) { {} } let(:provider_options) { {} }
let(:dns_options) { {} }
let(:redis_connection_pool) { Vmpooler::PoolManager::GenericConnectionPool.new( let(:redis_connection_pool) { Vmpooler::PoolManager::GenericConnectionPool.new(
metrics: metrics, metrics: metrics,
connpool_type: 'redis_connection_pool', connpool_type: 'redis_connection_pool',
@ -30,16 +31,19 @@ describe 'Pool Manager' do
} }
let(:provider) { Vmpooler::PoolManager::Provider::Base.new(config, logger, metrics, redis_connection_pool, 'mock_provider', provider_options) } let(:provider) { Vmpooler::PoolManager::Provider::Base.new(config, logger, metrics, redis_connection_pool, 'mock_provider', provider_options) }
let(:dns_plugin) { MockPoolManagerDnsBase.new }
let(:config) { YAML.load(<<-EOT let(:config) { YAML.load(<<-EOT
--- ---
:config: {} :config: {}
:dns_configs: {}
:providers: :providers:
:mock: :mock:
dns_class: base
:redis: {} :redis: {}
:pools: :pools:
- name: '#{pool}' - name: '#{pool}'
size: 1 size: 1
dns_plugin: 'mock'
EOT EOT
) )
} }
@ -75,6 +79,27 @@ EOT
end end
end end
describe '#load_used_dns_plugins' do
let(:config) { YAML.load(<<-EOT
---
:config:
:dns_configs:
:base:
:pools:
- name: '#{pool}'
size: 1
provider: 'spoof'
EOT
)
}
it do
files = ['vmpooler/dns/base']
expect(subject.load_used_dns_plugins).to eq(files)
end
end
describe '#used_providers' do describe '#used_providers' do
context 'with no named providers' do context 'with no named providers' do
let(:config) { YAML.load(<<-EOT let(:config) { YAML.load(<<-EOT
@ -212,6 +237,8 @@ EOT
describe '#remove_nonexistent_vm' do describe '#remove_nonexistent_vm' do
before do before do
expect(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
expect(dns_plugin).to receive(:delete_record)
expect(subject).not_to be_nil expect(subject).not_to be_nil
end end
@ -855,17 +882,17 @@ EOT
end end
it 'calls _clone_vm' do it 'calls _clone_vm' do
expect(subject).to receive(:_clone_vm).with(pool_object,provider,nil,nil) expect(subject).to receive(:_clone_vm).with(pool_object,provider,dns_plugin,nil,nil)
subject.clone_vm(pool_object,provider) subject.clone_vm(pool_object,provider,dns_plugin)
end end
it 'logs a message if an error is raised' do it 'logs a message if an error is raised' do
allow(logger).to receive(:log) allow(logger).to receive(:log)
expect(logger).to receive(:log).with('s',"[!] [#{pool}] failed while cloning VM with an error: MockError") expect(logger).to receive(:log).with('s',"[!] [#{pool}] failed while cloning VM with an error: MockError")
expect(subject).to receive(:_clone_vm).with(pool,provider,nil,nil).and_raise('MockError') expect(subject).to receive(:_clone_vm).with(pool,provider,dns_plugin,nil,nil).and_raise('MockError')
expect{subject.clone_vm(pool,provider)}.to raise_error(/MockError/) expect{subject.clone_vm(pool,provider,dns_plugin)}.to raise_error(/MockError/)
end end
end end
@ -880,18 +907,23 @@ EOT
let(:config) { let(:config) {
YAML.load(<<-EOT YAML.load(<<-EOT
--- ---
:dns_configs:
:mock:
dns_class: base
:providers: :providers:
:mock_provider: :mock_provider:
skip_dns_check_before_creating_vm: true skip_dns_check_before_creating_vm: true
:pools: :pools:
- name: '#{pool}' - name: '#{pool}'
size: 1 size: 1
dns_config: 'mock'
EOT EOT
) )
} }
it 'should skip the dns check' do it 'should skip the dns check' do
#method is skipped #method is skipped
expect(subject).not_to receive(:check_dns_available) expect(subject).not_to receive(:check_dns_available)
allow(subject).to receive(:get_domain_for_pool).and_return('example.com')
expect(subject.find_unique_hostname(pool)).to eq("spicy-proton") expect(subject.find_unique_hostname(pool)).to eq("spicy-proton")
end end
end end
@ -899,11 +931,15 @@ EOT
let(:config) { let(:config) {
YAML.load(<<-EOT YAML.load(<<-EOT
--- ---
:dns_configs:
:mock:
dns_class: base
:providers: :providers:
:mock_provider: :mock_provider:
:pools: :pools:
- name: '#{pool}' - name: '#{pool}'
size: 1 size: 1
dns_config: 'mock'
EOT EOT
) )
} }
@ -932,10 +968,17 @@ EOT
let(:config) { let(:config) {
YAML.load(<<-EOT YAML.load(<<-EOT
--- ---
:dns_configs:
:mock:
dns_class: base
:config: :config:
prefix: "prefix" prefix: "prefix"
:redis: :redis:
ttl: #{redis_ttl} ttl: #{redis_ttl}
:pools:
- name: 'pool1'
size: 1
dns_config: 'mock'
EOT EOT
) )
} }
@ -945,6 +988,9 @@ EOT
allow(metrics).to receive(:timing) allow(metrics).to receive(:timing)
expect(metrics).to receive(:timing).with(/clone\./,/0/) expect(metrics).to receive(:timing).with(/clone\./,/0/)
expect(provider).to receive(:create_vm).with(pool, String) expect(provider).to receive(:create_vm).with(pool, String)
allow(provider).to receive(:get_vm_ip_address)
allow(subject).to receive(:get_domain_for_pool).and_return('example.com')
allow(subject).to receive(:get_dns_plugin_class_name_for_pool).and_return(dns_plugin)
allow(logger).to receive(:log) allow(logger).to receive(:log)
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
expect(subject).to receive(:find_unique_hostname).with(pool).and_return(vm) expect(subject).to receive(:find_unique_hostname).with(pool).and_return(vm)
@ -955,7 +1001,7 @@ EOT
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
expect(redis.scard("vmpooler__pending__#{pool}")).to eq(0) expect(redis.scard("vmpooler__pending__#{pool}")).to eq(0)
subject._clone_vm(pool,provider) subject._clone_vm(pool,provider,dns_plugin)
expect(redis.scard("vmpooler__pending__#{pool}")).to eq(1) expect(redis.scard("vmpooler__pending__#{pool}")).to eq(1)
expect(redis.hget("vmpooler__vm__#{vm}", 'clone')).to_not be_nil expect(redis.hget("vmpooler__vm__#{vm}", 'clone')).to_not be_nil
@ -970,7 +1016,7 @@ EOT
redis.incr('vmpooler__tasks__clone') redis.incr('vmpooler__tasks__clone')
redis.incr('vmpooler__tasks__clone') redis.incr('vmpooler__tasks__clone')
expect(redis.get('vmpooler__tasks__clone')).to eq('2') expect(redis.get('vmpooler__tasks__clone')).to eq('2')
subject._clone_vm(pool,provider) subject._clone_vm(pool,provider,dns_plugin)
expect(redis.get('vmpooler__tasks__clone')).to eq('1') expect(redis.get('vmpooler__tasks__clone')).to eq('1')
end end
end end
@ -978,19 +1024,20 @@ EOT
it 'should log a message that is being cloned from a template' do it 'should log a message that is being cloned from a template' do
expect(logger).to receive(:log).with('d',/\[ \] \[#{pool}\] Starting to clone '(.+)'/) expect(logger).to receive(:log).with('d',/\[ \] \[#{pool}\] Starting to clone '(.+)'/)
subject._clone_vm(pool,provider) subject._clone_vm(pool,provider,dns_plugin)
end end
it 'should log a message that it completed being cloned' do it 'should log a message that it completed being cloned' do
expect(logger).to receive(:log).with('s',/\[\+\] \[#{pool}\] '(.+)' cloned in [0-9.]+ seconds/) expect(logger).to receive(:log).with('s',/\[\+\] \[#{pool}\] '(.+)' cloned in [0-9.]+ seconds/)
subject._clone_vm(pool,provider) subject._clone_vm(pool,provider,dns_plugin)
end end
end end
context 'with an error during cloning' do context 'with an error during cloning' do
before(:each) do before(:each) do
expect(provider).to receive(:create_vm).with(pool, String).and_raise('MockError') expect(provider).to receive(:create_vm).with(pool, String).and_raise('MockError')
allow(subject).to receive(:get_domain_for_pool)
allow(logger).to receive(:log) allow(logger).to receive(:log)
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
expect(subject).to receive(:find_unique_hostname).with(pool).and_return(vm) expect(subject).to receive(:find_unique_hostname).with(pool).and_return(vm)
@ -1001,7 +1048,7 @@ EOT
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
expect(redis.scard("vmpooler__pending__#{pool}")).to eq(0) expect(redis.scard("vmpooler__pending__#{pool}")).to eq(0)
expect{subject._clone_vm(pool,provider)}.to raise_error(/MockError/) expect{subject._clone_vm(pool,provider,dns_plugin)}.to raise_error(/MockError/)
expect(redis.scard("vmpooler__pending__#{pool}")).to eq(0) expect(redis.scard("vmpooler__pending__#{pool}")).to eq(0)
# Get the new VM Name from the pending pool queue as it should be the only entry # Get the new VM Name from the pending pool queue as it should be the only entry
@ -1015,7 +1062,7 @@ EOT
redis.incr('vmpooler__tasks__clone') redis.incr('vmpooler__tasks__clone')
redis.incr('vmpooler__tasks__clone') redis.incr('vmpooler__tasks__clone')
expect(redis.get('vmpooler__tasks__clone')).to eq('2') expect(redis.get('vmpooler__tasks__clone')).to eq('2')
expect{subject._clone_vm(pool,provider)}.to raise_error(/MockError/) expect{subject._clone_vm(pool,provider,dns_plugin)}.to raise_error(/MockError/)
expect(redis.get('vmpooler__tasks__clone')).to eq('1') expect(redis.get('vmpooler__tasks__clone')).to eq('1')
end end
end end
@ -1025,12 +1072,12 @@ EOT
redis.pipelined do |pipe| redis.pipelined do |pipe|
expect(pipe).to receive(:expire) expect(pipe).to receive(:expire)
end end
expect{subject._clone_vm(pool,provider)}.to raise_error(/MockError/) expect{subject._clone_vm(pool,provider,dns_plugin)}.to raise_error(/MockError/)
end end
end end
it 'should raise the error' do it 'should raise the error' do
expect{subject._clone_vm(pool,provider)}.to raise_error(/MockError/) expect{subject._clone_vm(pool,provider,dns_plugin)}.to raise_error(/MockError/)
end end
end end
@ -1040,6 +1087,9 @@ EOT
allow(metrics).to receive(:timing) allow(metrics).to receive(:timing)
expect(metrics).to receive(:timing).with(/clone\./,/0/) expect(metrics).to receive(:timing).with(/clone\./,/0/)
expect(provider).to receive(:create_vm).with(pool, String) expect(provider).to receive(:create_vm).with(pool, String)
allow(provider).to receive(:get_vm_ip_address).with(vm,pool)
allow(subject).to receive(:get_dns_plugin_class_name_for_pool).and_return(dns_plugin)
expect(dns_plugin).to receive(:create_or_replace_record)
allow(logger).to receive(:log) allow(logger).to receive(:log)
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
expect(subject).to receive(:find_unique_hostname).with(pool).and_return(vm) expect(subject).to receive(:find_unique_hostname).with(pool).and_return(vm)
@ -1048,7 +1098,7 @@ EOT
it 'should set request_id and pool_alias on the vm data' do it 'should set request_id and pool_alias on the vm data' do
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
subject._clone_vm(pool,provider,request_id,pool) subject._clone_vm(pool,provider,dns_plugin,request_id,pool)
expect(redis.hget("vmpooler__vm__#{vm}", 'pool_alias')).to eq(pool) expect(redis.hget("vmpooler__vm__#{vm}", 'pool_alias')).to eq(pool)
expect(redis.hget("vmpooler__vm__#{vm}", 'request_id')).to eq(request_id) expect(redis.hget("vmpooler__vm__#{vm}", 'request_id')).to eq(request_id)
end end
@ -1057,7 +1107,7 @@ EOT
it 'should reduce the clone count' do it 'should reduce the clone count' do
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
expect(redis).to receive(:decr).with('vmpooler__tasks__ondemandclone') expect(redis).to receive(:decr).with('vmpooler__tasks__ondemandclone')
subject._clone_vm(pool,provider,request_id,pool) subject._clone_vm(pool,provider,dns_plugin,request_id,pool)
end end
end end
end end
@ -1065,6 +1115,8 @@ EOT
context 'with #check_dns_available' do context 'with #check_dns_available' do
before(:each) do before(:each) do
allow(logger).to receive(:log) allow(logger).to receive(:log)
allow(provider).to receive(:get_vm_ip_address).and_return(true)
allow(subject).to receive(:get_dns_plugin_class_name_for_pool).and_return(dns_plugin)
end end
it 'should error out if DNS already exists' do it 'should error out if DNS already exists' do
vm_name = "foo" vm_name = "foo"
@ -1072,15 +1124,16 @@ EOT
expect(subject).to receive(:generate_and_check_hostname).exactly(3).times.and_return([vm_name, true]) #skip this, make it available all times expect(subject).to receive(:generate_and_check_hostname).exactly(3).times.and_return([vm_name, true]) #skip this, make it available all times
expect(resolv).to receive(:getaddress).exactly(3).times.and_return("1.2.3.4") expect(resolv).to receive(:getaddress).exactly(3).times.and_return("1.2.3.4")
expect(metrics).to receive(:increment).with("errors.staledns.#{pool}").exactly(3).times expect(metrics).to receive(:increment).with("errors.staledns.#{pool}").exactly(3).times
expect{subject._clone_vm(pool,provider)}.to raise_error(/Unable to generate a unique hostname after/) expect{subject._clone_vm(pool,provider,dns_plugin)}.to raise_error(/Unable to generate a unique hostname after/)
end end
it 'should be successful if DNS does not exist' do it 'should be successful if DNS does not exist' do
vm_name = "foo" vm_name = "foo"
resolv = class_double("Resolv").as_stubbed_const(:transfer_nested_constants => true) resolv = class_double("Resolv").as_stubbed_const(:transfer_nested_constants => true)
expect(subject).to receive(:generate_and_check_hostname).and_return([vm_name, true]) expect(subject).to receive(:generate_and_check_hostname).and_return([vm_name, true])
expect(resolv).to receive(:getaddress).exactly(1).times.and_raise(Resolv::ResolvError) expect(resolv).to receive(:getaddress).exactly(1).times.and_raise(Resolv::ResolvError)
expect(provider).to receive(:get_vm_ip_address)
expect(provider).to receive(:create_vm).with(pool, String) expect(provider).to receive(:create_vm).with(pool, String)
subject._clone_vm(pool,provider) subject._clone_vm(pool,provider,dns_plugin)
end end
end end
end end
@ -1092,17 +1145,17 @@ EOT
end end
it 'calls _destroy_vm' do it 'calls _destroy_vm' do
expect(subject).to receive(:_destroy_vm).with(vm,pool,provider) expect(subject).to receive(:_destroy_vm).with(vm,pool,provider,dns_plugin)
subject.destroy_vm(vm,pool,provider) subject.destroy_vm(vm,pool,provider,dns_plugin)
end end
it 'logs a message if an error is raised' do it 'logs a message if an error is raised' do
allow(logger).to receive(:log) allow(logger).to receive(:log)
expect(logger).to receive(:log).with('d',"[!] [#{pool}] '#{vm}' failed while destroying the VM with an error: MockError") expect(logger).to receive(:log).with('d',"[!] [#{pool}] '#{vm}' failed while destroying the VM with an error: MockError")
expect(subject).to receive(:_destroy_vm).with(vm,pool,provider).and_raise('MockError') expect(subject).to receive(:_destroy_vm).with(vm,pool,provider,dns_plugin).and_raise('MockError')
expect{subject.destroy_vm(vm,pool,provider)}.to raise_error(/MockError/) expect{subject.destroy_vm(vm,pool,provider,dns_plugin)}.to raise_error(/MockError/)
end end
end end
@ -1131,7 +1184,7 @@ EOT
redis.pipelined do |pipe| redis.pipelined do |pipe|
expect(pipe).to receive(:expire).with("vmpooler__vm__#{vm}", 0) expect(pipe).to receive(:expire).with("vmpooler__vm__#{vm}", 0)
end end
subject._destroy_vm(vm,pool,provider) subject._destroy_vm(vm,pool,provider,dns_plugin)
end end
end end
end end
@ -1142,7 +1195,7 @@ EOT
end end
it 'should raise an error' do it 'should raise an error' do
expect{ subject._destroy_vm(vm,pool,provider) }.to raise_error(NoMethodError) expect{ subject._destroy_vm(vm,pool,provider,dns_plugin) }.to raise_error(NoMethodError)
end end
end end
@ -1153,7 +1206,7 @@ EOT
end end
it 'should not raise an error' do it 'should not raise an error' do
subject._destroy_vm(vm,pool,provider) subject._destroy_vm(vm,pool,provider,dns_plugin)
end end
end end
@ -1162,7 +1215,7 @@ EOT
expect(logger).to receive(:log).with('s', /\[-\] \[#{pool}\] '#{vm}' destroyed in [0-9.]+ seconds/) expect(logger).to receive(:log).with('s', /\[-\] \[#{pool}\] '#{vm}' destroyed in [0-9.]+ seconds/)
allow(logger).to receive(:log) allow(logger).to receive(:log)
subject._destroy_vm(vm,pool,provider) subject._destroy_vm(vm,pool,provider,dns_plugin)
end end
it 'should emit a timing metric' do it 'should emit a timing metric' do
@ -1170,13 +1223,13 @@ EOT
allow(metrics).to receive(:timing) allow(metrics).to receive(:timing)
expect(metrics).to receive(:timing).with("destroy.#{pool}", String) expect(metrics).to receive(:timing).with("destroy.#{pool}", String)
subject._destroy_vm(vm,pool,provider) subject._destroy_vm(vm,pool,provider,dns_plugin)
end end
it 'should dereference the mutex' do it 'should dereference the mutex' do
expect(subject).to receive(:dereference_mutex) expect(subject).to receive(:dereference_mutex)
subject._destroy_vm(vm,pool,provider) subject._destroy_vm(vm,pool,provider,dns_plugin)
end end
end end
@ -1190,13 +1243,13 @@ EOT
expect(logger).to receive(:log).with('s', /\[-\] \[#{pool}\] '#{vm}' destroyed in [0-9.]+ seconds/).exactly(0).times expect(logger).to receive(:log).with('s', /\[-\] \[#{pool}\] '#{vm}' destroyed in [0-9.]+ seconds/).exactly(0).times
allow(logger).to receive(:log) allow(logger).to receive(:log)
expect{ subject._destroy_vm(vm,pool,provider) }.to raise_error(/MockError/) expect{ subject._destroy_vm(vm,pool,provider,dns_plugin) }.to raise_error(/MockError/)
end end
it 'should not emit a timing metric' do it 'should not emit a timing metric' do
expect(metrics).to receive(:timing).with("destroy.#{pool}", String).exactly(0).times expect(metrics).to receive(:timing).with("destroy.#{pool}", String).exactly(0).times
expect{ subject._destroy_vm(vm,pool,provider) }.to raise_error(/MockError/) expect{ subject._destroy_vm(vm,pool,provider,dns_plugin) }.to raise_error(/MockError/)
end end
end end
@ -1209,7 +1262,7 @@ EOT
it 'should return' do it 'should return' do
expect(subject).to receive(:vm_mutex).with(vm).and_return(mutex) expect(subject).to receive(:vm_mutex).with(vm).and_return(mutex)
expect(subject._destroy_vm(vm,pool,provider)).to eq(nil) expect(subject._destroy_vm(vm,pool,provider,dns_plugin)).to eq(nil)
end end
end end
end end
@ -1724,6 +1777,51 @@ EOT
end end
end end
describe '#get_dns_plugin_class_name_for_pool' do
let(:config) { YAML.load(<<-EOT
---
:dns_configs:
:mock:
dns_class: base
:pools:
- name: #{pool}
dns_plugin: 'mock'
EOT
)}
before(:each) do
allow(Vmpooler::Dns).to receive(:load_used_dns_plugins).and_return('vmpooler/dns/mock')
end
it 'calls Vmpooler::Dns.get_dns_plugin_class_by_name' do
expect(Vmpooler::Dns).to receive(:get_dns_plugin_class_by_name).with(config, 'mock')
subject.get_dns_plugin_class_name_for_pool(pool)
end
end
describe '#get_dns_plugin_domain_for_pool' do
let(:config) { YAML.load(<<-EOT
---
:dns_configs:
:mock:
dns_class: base
:pools:
- name: #{pool}
dns_plugin: 'mock'
EOT
)}
before(:each) do
allow(Vmpooler::Dns).to receive(:load_used_dns_plugins).and_return('vmpooler/dns/mock')
end
it 'calls Vmpooler::Dns.get_dns_plugin_domain_for_pool' do
expect(Vmpooler::Dns).to receive(:get_dns_plugin_domain_by_name).with(config, 'mock')
subject.get_dns_plugin_domain_for_pool(pool)
end
end
describe '#check_disk_queue' do describe '#check_disk_queue' do
let(:threads) {[]} let(:threads) {[]}
@ -1876,6 +1974,7 @@ EOT
before(:each) do before(:each) do
expect(Thread).to receive(:new).and_yield expect(Thread).to receive(:new).and_yield
allow(Vmpooler::Dns).to receive(:load_used_dns_plugins).and_return('vmpooler/dns/mock')
allow(subject).to receive(:_check_snapshot_queue).with(no_args) allow(subject).to receive(:_check_snapshot_queue).with(no_args)
end end
@ -1921,6 +2020,10 @@ EOT
let(:maxloop) { 5 } let(:maxloop) { 5 }
# Note a maxloop of zero can not be tested as it never terminates # Note a maxloop of zero can not be tested as it never terminates
before(:each) do
allow(subject).to receive(:load_used_dns_plugins).and_return('vmpooler/dns/mock')
end
after(:each) do after(:each) do
# Reset the global variable - Note this is a code smell # Reset the global variable - Note this is a code smell
$threads = nil $threads = nil
@ -2771,6 +2874,23 @@ EOT
end end
context 'on startup' do context 'on startup' do
let(:config) {
YAML.load(<<-EOT
---
:config:
:dns_configs:
:mock:
dns_class: base
:pools:
- name: #{pool}
dns_plugin: 'mock'
EOT
)}
before(:each) do
allow(Vmpooler::Dns).to receive(:get_domain_for_pool)
end
it 'should log a message that VMPooler has started' do it 'should log a message that VMPooler has started' do
expect(logger).to receive(:log).with('d', 'starting vmpooler') expect(logger).to receive(:log).with('d', 'starting vmpooler')
@ -2817,6 +2937,46 @@ EOT
subject.execute!(1,0) subject.execute!(1,0)
end end
context 'creating Dns plugins' do
let(:mock_dns_plugin) { double('mock_dns_plugin') }
let(:config) {
YAML.load(<<-EOT
---
:dns_configs:
:mock:
dns_class: base
:pools:
- name: #{pool}
dns_plugin: 'mock'
- name: 'dummy'
dns_plugin: 'mock'
- name: 'dummy2'
dns_plugin: 'mock'
EOT
)}
it 'should call create_dns_object idempotently' do
# Even though there are two pools using the mock dns plugin, it should only
# create the dns object once.
expect(subject).to receive(:create_dns_object).and_return(mock_dns_plugin)
subject.execute!(1,0)
end
it 'should raise an error if the dns plugin cannot be created' do
expect(subject).to receive(:create_dns_object).and_raise(RuntimeError, "MockError")
expect{ subject.execute!(1,0) }.to raise_error(/MockError/)
end
it 'should log a message if the dns plugin cannot be created' do
expect(subject).to receive(:create_dns_object).and_raise(RuntimeError, "MockError")
expect(logger).to receive(:log).with('s',"Error while creating dns plugin for pool #{pool}: MockError")
expect{ subject.execute!(1,0) }.to raise_error(/MockError/)
end
end
context 'creating Providers' do context 'creating Providers' do
let(:vsphere_provider) { double('vsphere_provider') } let(:vsphere_provider) { double('vsphere_provider') }
let(:config) { let(:config) {
@ -2824,14 +2984,23 @@ EOT
--- ---
:providers: :providers:
:vsphere: {} :vsphere: {}
:dns_configs:
:mock:
dns_class: base
:pools: :pools:
- name: #{pool} - name: #{pool}
provider: 'vsphere' provider: 'vsphere'
dns_plugin: 'mock'
- name: 'dummy' - name: 'dummy'
provider: 'vsphere' provider: 'vsphere'
dns_plugin: 'mock'
EOT EOT
)} )}
before(:each) do
allow(Vmpooler::Dns).to receive(:get_domain_for_pool)
end
it 'should call create_provider_object idempotently' do it 'should call create_provider_object idempotently' do
# Even though there are two pools using the vsphere provider, it should only # Even though there are two pools using the vsphere provider, it should only
# create the provider object once. # create the provider object once.
@ -2859,14 +3028,21 @@ EOT
context 'move vSphere configuration to providers location' do context 'move vSphere configuration to providers location' do
let(:config) { let(:config) {
YAML.load(<<-EOT YAML.load(<<-EOT
:dns_configs:
:mock:
dns_class: base
:vsphere: :vsphere:
server: 'vsphere.example.com' server: 'vsphere.example.com'
username: 'vmpooler' username: 'vmpooler'
password: 'password' password: 'password'
:pools: :pools:
- name: #{pool} - name: #{pool}
dns_plugin: 'mock'
EOT EOT
)} )}
before(:each) do
allow(Vmpooler::Dns).to receive(:get_domain_for_pool)
end
it 'should set providers with no provider to vsphere' do it 'should set providers with no provider to vsphere' do
expect(subject.config[:providers]).to be nil expect(subject.config[:providers]).to be nil
@ -2889,13 +3065,22 @@ EOT
let(:config) { let(:config) {
YAML.load(<<-EOT YAML.load(<<-EOT
--- ---
:dns_configs:
:mock:
dns_class: base
:pools: :pools:
- name: #{pool} - name: #{pool}
dns_plugin: 'mock'
- name: 'dummy' - name: 'dummy'
provider: 'dummy' provider: 'dummy'
dns_plugin: 'mock'
EOT EOT
)} )}
before(:each) do
allow(Vmpooler::Dns).to receive(:get_domain_for_pool)
end
it 'should set providers with no provider to dummy' do it 'should set providers with no provider to dummy' do
expect(subject.config[:pools][0]['provider']).to be_nil expect(subject.config[:pools][0]['provider']).to be_nil
expect(subject.config[:pools][1]['provider']).to eq('dummy') expect(subject.config[:pools][1]['provider']).to eq('dummy')
@ -2916,11 +3101,31 @@ EOT
context 'with dead disk_manager thread' do context 'with dead disk_manager thread' do
let(:disk_manager_thread) { double('thread', :alive? => false) } let(:disk_manager_thread) { double('thread', :alive? => false) }
let(:check_loop_delay_min) { 7 }
let(:check_loop_delay_max) { 20 }
let(:check_loop_delay_decay) { 2.1 }
let(:config) {
YAML.load(<<-EOT
---
:config:
check_loop_delay_min: #{check_loop_delay_min}
check_loop_delay_max: #{check_loop_delay_max}
check_loop_delay_decay: #{check_loop_delay_decay}
:dns_configs:
:mock:
dns_class: base
:pools:
- name: #{pool}
dns_plugin: 'mock'
EOT
)
}
before(:each) do before(:each) do
# Reset the global variable - Note this is a code smell # Reset the global variable - Note this is a code smell
$threads = {} $threads = {}
$threads['disk_manager'] = disk_manager_thread $threads['disk_manager'] = disk_manager_thread
allow(Vmpooler::Dns).to receive(:get_domain_for_pool)
end end
it 'should run the check_disk_queue method and log a message' do it 'should run the check_disk_queue method and log a message' do
@ -2933,10 +3138,30 @@ EOT
context 'with dead snapshot_manager thread' do context 'with dead snapshot_manager thread' do
let(:snapshot_manager_thread) { double('thread', :alive? => false) } let(:snapshot_manager_thread) { double('thread', :alive? => false) }
let(:check_loop_delay_min) { 7 }
let(:check_loop_delay_max) { 20 }
let(:check_loop_delay_decay) { 2.1 }
let(:config) {
YAML.load(<<-EOT
---
:config:
check_loop_delay_min: #{check_loop_delay_min}
check_loop_delay_max: #{check_loop_delay_max}
check_loop_delay_decay: #{check_loop_delay_decay}
:dns_configs:
:mock:
dns_class: base
:pools:
- name: #{pool}
dns_plugin: 'mock'
EOT
)
}
before(:each) do before(:each) do
# Reset the global variable - Note this is a code smell # Reset the global variable - Note this is a code smell
$threads = {} $threads = {}
$threads['snapshot_manager'] = snapshot_manager_thread $threads['snapshot_manager'] = snapshot_manager_thread
allow(Vmpooler::Dns).to receive(:get_domain_for_pool)
end end
it 'should run the check_snapshot_queue method and log a message' do it 'should run the check_snapshot_queue method and log a message' do
@ -2954,10 +3179,27 @@ EOT
let(:default_check_loop_delay_min) { 5 } let(:default_check_loop_delay_min) { 5 }
let(:default_check_loop_delay_max) { 60 } let(:default_check_loop_delay_max) { 60 }
let(:default_check_loop_delay_decay) { 2.0 } let(:default_check_loop_delay_decay) { 2.0 }
let(:check_loop_delay_min) { 7 }
let(:check_loop_delay_max) { 20 }
let(:check_loop_delay_decay) { 2.1 }
let(:config) {
YAML.load(<<-EOT
---
:config:
:dns_configs:
:mock:
dns_class: base
:pools:
- name: #{pool}
dns_plugin: 'mock'
EOT
)
}
before(:each) do before(:each) do
# Reset the global variable - Note this is a code smell # Reset the global variable - Note this is a code smell
$threads = {} $threads = {}
$threads[pool] = pool_thread $threads[pool] = pool_thread
allow(Vmpooler::Dns).to receive(:get_domain_for_pool)
end end
it 'should run the check_pool method and log a message' do it 'should run the check_pool method and log a message' do
@ -2976,6 +3218,19 @@ EOT
let(:default_check_loop_delay_min) { 5 } let(:default_check_loop_delay_min) { 5 }
let(:default_check_loop_delay_max) { 60 } let(:default_check_loop_delay_max) { 60 }
let(:default_check_loop_delay_decay) { 2.0 } let(:default_check_loop_delay_decay) { 2.0 }
let(:config) {
YAML.load(<<-EOT
---
:config:
:dns_configs:
:mock:
dns_class: base
:pools:
- name: #{pool}
dns_plugin: 'mock'
EOT
)
}
before(:each) do before(:each) do
# Reset the global variable - Note this is a code smell # Reset the global variable - Note this is a code smell
$threads = {} $threads = {}
@ -3002,8 +3257,12 @@ EOT
check_loop_delay_min: #{check_loop_delay_min} check_loop_delay_min: #{check_loop_delay_min}
check_loop_delay_max: #{check_loop_delay_max} check_loop_delay_max: #{check_loop_delay_max}
check_loop_delay_decay: #{check_loop_delay_decay} check_loop_delay_decay: #{check_loop_delay_decay}
:dns_configs:
:mock:
dns_class: base
:pools: :pools:
- name: #{pool} - name: #{pool}
dns_plugin: 'mock'
EOT EOT
) )
} }
@ -3011,6 +3270,8 @@ EOT
# Reset the global variable - Note this is a code smell # Reset the global variable - Note this is a code smell
$threads = {} $threads = {}
$threads[pool] = pool_thread $threads[pool] = pool_thread
allow(Vmpooler::Dns).to receive(:get_domain_for_pool)
# allow(subject).to receive(:load_used_dns_plugins).and_return('vmpooler/dns/mock')
end end
it 'should run the check_pool method and log a message' do it 'should run the check_pool method and log a message' do
@ -3028,6 +3289,26 @@ EOT
context 'delays between loops' do context 'delays between loops' do
let(:maxloop) { 2 } let(:maxloop) { 2 }
let(:loop_delay) { 1 } let(:loop_delay) { 1 }
let(:check_loop_delay_min) { 7 }
let(:check_loop_delay_max) { 20 }
let(:check_loop_delay_decay) { 2.1 }
let(:maxloop) { 5 }
let(:config) {
YAML.load(<<-EOT
---
:config:
check_loop_delay_min: #{check_loop_delay_min}
check_loop_delay_max: #{check_loop_delay_max}
check_loop_delay_decay: #{check_loop_delay_decay}
:dns_configs:
:mock:
dns_class: base
:pools:
- name: #{pool}
dns_plugin: 'mock'
EOT
)
}
# Note a maxloop of zero can not be tested as it never terminates # Note a maxloop of zero can not be tested as it never terminates
before(:each) do before(:each) do
@ -3035,6 +3316,7 @@ EOT
allow(subject).to receive(:check_snapshot_queue) allow(subject).to receive(:check_snapshot_queue)
allow(subject).to receive(:check_pool) allow(subject).to receive(:check_pool)
allow(subject).to receive(:check_ondemand_requests) allow(subject).to receive(:check_ondemand_requests)
allow(Vmpooler::Dns).to receive(:get_domain_for_pool)
end end
it 'when a non-default loop delay is specified' do it 'when a non-default loop delay is specified' do
@ -3046,9 +3328,30 @@ EOT
context 'loops specified number of times (5)' do context 'loops specified number of times (5)' do
let(:alive_thread) { double('thread', :alive? => true) } let(:alive_thread) { double('thread', :alive? => true) }
let(:check_loop_delay_min) { 7 }
let(:check_loop_delay_max) { 20 }
let(:check_loop_delay_decay) { 2.1 }
let(:maxloop) { 5 } let(:maxloop) { 5 }
let(:config) {
YAML.load(<<-EOT
---
:config:
check_loop_delay_min: #{check_loop_delay_min}
check_loop_delay_max: #{check_loop_delay_max}
check_loop_delay_decay: #{check_loop_delay_decay}
:dns_configs:
:mock:
dns_class: base
:pools:
- name: #{pool}
dns_plugin: 'mock'
EOT
)
}
# Note a maxloop of zero can not be tested as it never terminates # Note a maxloop of zero can not be tested as it never terminates
before(:each) do before(:each) do
allow(subject).to receive(:get_domain_for_pool)
allow(subject).to receive(:get_dns_plugin_config_classes)
end end
it 'should run startup tasks only once' do it 'should run startup tasks only once' do
@ -4120,6 +4423,10 @@ EOT
} }
} }
before(:each) do
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end
it 'should not call clone_vm when number of VMs is equal to the pool size' do it 'should not call clone_vm when number of VMs is equal to the pool size' do
expect(subject).to receive(:clone_vm).exactly(0).times expect(subject).to receive(:clone_vm).exactly(0).times
@ -4174,6 +4481,9 @@ EOT
end end
context 'when pool is marked as empty' do context 'when pool is marked as empty' do
before(:each) do
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end
before(:each) do before(:each) do
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
@ -4199,6 +4509,9 @@ EOT
end end
context 'when number of VMs is less than the pool size' do context 'when number of VMs is less than the pool size' do
before(:each) do
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end
it 'should return the number of cloned VMs' do it 'should return the number of cloned VMs' do
pool_size = 5 pool_size = 5
@ -4255,6 +4568,10 @@ EOT
end end
context 'export metrics' do context 'export metrics' do
before(:each) do
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end
it 'increments metrics for ready queue' do it 'increments metrics for ready queue' do
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
create_ready_vm(pool,'vm1',redis) create_ready_vm(pool,'vm1',redis)
@ -4345,6 +4662,7 @@ EOT
allow(subject).to receive(:destroy_vm) allow(subject).to receive(:destroy_vm)
allow(subject).to receive(:clone_vm) allow(subject).to receive(:clone_vm)
allow(provider).to receive(:vms_in_pool).with(pool).and_return(new_vm_response) allow(provider).to receive(:vms_in_pool).with(pool).and_return(new_vm_response)
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end end
it 'calls inventory correctly' do it 'calls inventory correctly' do
@ -4449,6 +4767,10 @@ EOT
# RUNNING # RUNNING
context 'when checking running VMs' do context 'when checking running VMs' do
before(:each) do
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end
let(:pool_check_response) { let(:pool_check_response) {
{ {
discovered_vms: 0, discovered_vms: 0,
@ -4471,6 +4793,10 @@ EOT
# READY # READY
context 'when checking ready VMs' do context 'when checking ready VMs' do
before(:each) do
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end
let(:pool_check_response) { let(:pool_check_response) {
{ {
discovered_vms: 0, discovered_vms: 0,
@ -4493,6 +4819,10 @@ EOT
# PENDING # PENDING
context 'when checking ready VMs' do context 'when checking ready VMs' do
before(:each) do
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end
let(:pool_check_response) { let(:pool_check_response) {
{ {
discovered_vms: 0, discovered_vms: 0,
@ -4516,6 +4846,10 @@ EOT
# COMPLETED # COMPLETED
context 'when checking completed VMs' do context 'when checking completed VMs' do
before(:each) do
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end
let(:pool_check_response) { let(:pool_check_response) {
{ {
discovered_vms: 0, discovered_vms: 0,
@ -4538,6 +4872,10 @@ EOT
# DISCOVERED # DISCOVERED
context 'when checking discovered VMs' do context 'when checking discovered VMs' do
before(:each) do
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end
let(:pool_check_response) { let(:pool_check_response) {
{ {
discovered_vms: 0, discovered_vms: 0,
@ -4560,6 +4898,10 @@ EOT
# MIGRATIONS # MIGRATIONS
context 'when checking migrating VMs' do context 'when checking migrating VMs' do
before(:each) do
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end
let(:pool_check_response) { let(:pool_check_response) {
{ {
discovered_vms: 0, discovered_vms: 0,
@ -4589,6 +4931,7 @@ EOT
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
redis.hset('vmpooler__config__poolsize', pool, newpoolsize) redis.hset('vmpooler__config__poolsize', pool, newpoolsize)
end end
allow(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end end
it 'should change the pool size configuration' do it 'should change the pool size configuration' do
@ -4630,6 +4973,7 @@ EOT
expect(redis).to receive(:scard).with("vmpooler__ready__#{pool}").and_return(1) expect(redis).to receive(:scard).with("vmpooler__ready__#{pool}").and_return(1)
expect(redis).to receive(:scard).with("vmpooler__pending__#{pool}").and_return(1) expect(redis).to receive(:scard).with("vmpooler__pending__#{pool}").and_return(1)
end end
expect(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
end end
it 'should call remove_excess_vms' do it 'should call remove_excess_vms' do
@ -4739,6 +5083,7 @@ EOT
before(:each) do before(:each) do
config[:config]['ondemand_clone_limit'] = 10 config[:config]['ondemand_clone_limit'] = 10
expect(subject).to receive(:get_provider_for_pool).and_return(provider) expect(subject).to receive(:get_provider_for_pool).and_return(provider)
expect(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
create_ondemand_creationtask("#{request_string}:#{request_id}", current_time.to_i, redis) create_ondemand_creationtask("#{request_string}:#{request_id}", current_time.to_i, redis)
end end
@ -4746,7 +5091,7 @@ EOT
it 'creates the vm' do it 'creates the vm' do
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
expect(subject).to receive(:clone_vm).with(pool, provider, request_id, pool_alias) expect(subject).to receive(:clone_vm).with(pool, provider, dns_plugin, request_id, pool_alias)
subject.process_ondemand_vms(redis) subject.process_ondemand_vms(redis)
end end
end end
@ -4759,6 +5104,7 @@ EOT
before(:each) do before(:each) do
config[:config]['ondemand_clone_limit'] = 3 config[:config]['ondemand_clone_limit'] = 3
expect(subject).to receive(:get_provider_for_pool).and_return(provider) expect(subject).to receive(:get_provider_for_pool).and_return(provider)
expect(subject).to receive(:get_dns_plugin_class_for_pool).and_return(dns_plugin)
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
create_ondemand_creationtask("#{request_string}:#{request_id}", current_time.to_i, redis) create_ondemand_creationtask("#{request_string}:#{request_id}", current_time.to_i, redis)
end end
@ -4766,7 +5112,7 @@ EOT
it 'should create the maximum number of vms' do it 'should create the maximum number of vms' do
redis_connection_pool.with do |redis| redis_connection_pool.with do |redis|
expect(subject).to receive(:clone_vm).with(pool, provider, request_id, pool_alias).exactly(3).times expect(subject).to receive(:clone_vm).with(pool, provider, dns_plugin, request_id, pool_alias).exactly(3).times
subject.process_ondemand_vms(redis) subject.process_ondemand_vms(redis)
end end
end end

View file

@ -1,104 +1,11 @@
require 'spec_helper' require 'spec_helper'
describe 'Parser' do # The only method previously tested here was '#get_domain_for_pool'
# which was moved to Vmpooler::Dns as the more appropriate class
#
# TODO: Add tests for last remaining method, or move to more appropriate class
describe 'Vmpooler::Parsing' do
let(:pool) { 'pool1' } let(:pool) { 'pool1' }
subject { Vmpooler::Parsing } subject { Vmpooler::Parsing }
describe '.get_domain_for_pool' do
let(:provider_name) { 'mock_provider' }
context 'No provider is set' do
let(:config) { YAML.load(<<~EOT
---
:config:
:providers:
:mock_provider:
:pools:
- name: '#{pool}'
size: 1
EOT
)}
it 'should return nil' do
result = subject.get_domain_for_pool(config, pool)
expect(result).to be_nil
end
end
context 'Provider is vsphere by default' do
let(:config) { YAML.load(<<~EOT
---
:config:
:providers:
:vsphere:
domain: myown.com
:pools:
- name: '#{pool}'
size: 1
EOT
)}
it 'should return the domain set for vsphere' do
result = subject.get_domain_for_pool(config, pool)
expect(result).to eq('myown.com')
end
end
context 'No domain is set' do
let(:config) { YAML.load(<<~EOT
---
:config:
:providers:
:mock_provider:
:pools:
- name: '#{pool}'
size: 1
provider: #{provider_name}
EOT
)}
it 'should return nil' do
result = subject.get_domain_for_pool(config, pool)
expect(result).to be_nil
end
end
context 'Only a global domain is set' do
let(:config) { YAML.load(<<~EOT
---
:config:
domain: example.com
:providers:
:mock_provider:
:pools:
- name: '#{pool}'
size: 1
provider: #{provider_name}
EOT
)}
it 'should return the domain set in the config section' do
result = subject.get_domain_for_pool(config, pool)
expect(result).to_not be_nil
expect(result).to eq('example.com')
end
end
context 'A provider specified a domain to use' do
let(:config) { YAML.load(<<~EOT
---
:config:
:providers:
:mock_provider:
domain: m.example.com
:pools:
- name: '#{pool}'
size: 1
provider: #{provider_name}
EOT
)}
it 'should return the domain set in the config section' do
result = subject.get_domain_for_pool(config, pool)
expect(result).to_not be_nil
expect(result).to eq('m.example.com')
end
end
end
end end

View file

@ -63,12 +63,37 @@ describe 'Vmpooler' do
expect(Vmpooler.config[:providers].keys).to include(:dummy) expect(Vmpooler.config[:providers].keys).to include(:dummy)
expect(Vmpooler.config[:providers].keys).to include(:alice) expect(Vmpooler.config[:providers].keys).to include(:alice)
expect(Vmpooler.config[:providers].keys).to include(:bob) expect(Vmpooler.config[:providers].keys).to include(:bob)
merged_pools = [{"name"=>"pool03", "provider"=>"dummy", "ready_ttl"=>5, "size"=>5}, merged_pools = [{"name"=>"pool03", "provider"=>"dummy", "dns_plugin"=>"example", "ready_ttl"=>5, "size"=>5},
{"name"=>"pool04", "provider"=>"dummy", "ready_ttl"=>5, "size"=>5}, {"name"=>"pool04", "provider"=>"dummy", "dns_plugin"=>"example", "ready_ttl"=>5, "size"=>5},
{"name"=>"pool05", "provider"=>"dummy", "ready_ttl"=>5, "size"=>5}] {"name"=>"pool05", "provider"=>"dummy", "dns_plugin"=>"example", "ready_ttl"=>5, "size"=>5}]
expect(Vmpooler.config[:pools]).to eq(merged_pools) expect(Vmpooler.config[:pools]).to eq(merged_pools)
expect(Vmpooler.config[:config]).not_to be_nil #merge does not deleted existing keys expect(Vmpooler.config[:config]).not_to be_nil #merge does not deleted existing keys
end end
end end
context 'when domain' do
context 'is set as a variable' do
before(:each) do
ENV['VMPOOLER_CONFIG_FILE'] = config_file
ENV['DOMAIN'] = 'example.com'
end
it 'should exit' do
expect { Vmpooler.config }.to raise_error(SystemExit)
end
end
context 'is set in a config file' do
let(:config_file) { File.join(fixtures_dir, 'vmpooler_domain.yaml') }
before(:each) do
ENV['VMPOOLER_CONFIG_FILE'] = config_file
end
it 'should exit' do
expect { Vmpooler.config }.to raise_error(SystemExit)
end
end
end
end end
end end

View file

@ -3,6 +3,12 @@
:dummy: :dummy:
filename: '/tmp/dummy-backing.yaml' filename: '/tmp/dummy-backing.yaml'
:dns_configs:
:example:
dns_class: dynamic-dns
#domain: 'localhost' # Flip these out for local requests
domain: 'example.com'
:redis: :redis:
server: 'localhost' server: 'localhost'
@ -23,8 +29,6 @@
allowed_tags: allowed_tags:
- 'created_by' - 'created_by'
- 'project' - 'project'
domain: 'example.com'
#domain: 'localhost' # Flip these out for local requests
prefix: 'poolvm-' prefix: 'poolvm-'
:pools: :pools:
@ -37,6 +41,7 @@
timeout: 15 timeout: 15
ready_ttl: 1440 ready_ttl: 1440
provider: dummy provider: dummy
dns_plugin: 'example'
- name: 'debian-7-x86_64' - name: 'debian-7-x86_64'
alias: [ 'debian-7-64', 'debian-7-amd64' ] alias: [ 'debian-7-64', 'debian-7-amd64' ]
template: 'Templates/debian-7-x86_64' template: 'Templates/debian-7-x86_64'
@ -46,4 +51,4 @@
timeout: 15 timeout: 15
ready_ttl: 1440 ready_ttl: 1440
provider: dummy provider: dummy
dns_plugin: 'example'

View file

@ -445,12 +445,6 @@
# - allowed_tags # - allowed_tags
# If set, restricts tags to those specified in this array. # If set, restricts tags to those specified in this array.
# #
# - domain
# If set, returns a top-level 'domain' JSON key in POST requests (v1).
# in the v2 API, it concatenates the hostname to include the domain, either set here or at the provider level
# With the introduction of vmpooler-provider-gce, domains can now be set at the provider level. The
# domain key is only retained in v1 for backwards compatibility and will be removed in the future.
#
# - prefix # - prefix
# If set, prefixes all created VMs with this string. This should include # If set, prefixes all created VMs with this string. This should include
# a separator. # a separator.
@ -601,13 +595,35 @@
allowed_tags: allowed_tags:
- 'created_by' - 'created_by'
- 'project' - 'project'
domain: 'example.com'
prefix: 'poolvm-' prefix: 'poolvm-'
experimental_features: true experimental_features: true
backend_weight: backend_weight:
'backend1': 60 'backend1': 60
'backend2': 40 'backend2': 40
# :dns_configs:
#
# This section a list of dns configurations to be referenced by one or more pools.
#
# The currently supported backing services are:
# - dynamic-dns (This assumes that dynamic dns is handling record management and VMPooler does not require interaction)
# - gcp (Google Cloud DNS https://github.com/puppetlabs/vmpooler-dns-gcp)
#
# - dns_class
# Specify one of the supported backing services.
#
# - domain
# The domain expected to make up the FQDN when attempting to resolve VM instances.
#
# See DNS plugin docs for additional options specific to that class.
#
# Example
:dns_configs:
:example:
dns_class: dynamic-dns
domain: 'example.com'
# :pools: # :pools:
# #
# This section contains a list of virtual machine 'pools' for vmpooler to # This section contains a list of virtual machine 'pools' for vmpooler to
@ -638,6 +654,12 @@
# If you have more than one provider, this is where you would choose which # If you have more than one provider, this is where you would choose which
# one to use for this pool # one to use for this pool
# #
# - dns_plugin
# The name of the DNS plugin to use with this pool in order to determine the
# domain and settings specific to a DNS service. This should match
# a name in the :dns_configs: section above. e.g. example
# (required)
#
# - clone_target # - clone_target
# Per-pool option to override the global 'clone_target' cluster. # Per-pool option to override the global 'clone_target' cluster.
# (optional) # (optional)