-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
While upgrading repose, we found out that no test case exist for Repose and we decided to write it using spock framework.
- Loading branch information
Showing
3 changed files
with
258 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<parent> | ||
<artifactId>blueflood</artifactId> | ||
<groupId>com.rackspacecloud</groupId> | ||
<version>2.1.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>blueflood-spock-tests</artifactId> | ||
<name>Blueflood Spock Tests</name> | ||
<url>http://blueflood.io</url> | ||
|
||
<dependencies> | ||
<!-- Spock Framework --> | ||
<dependency> | ||
<groupId>org.spockframework</groupId> | ||
<artifactId>spock-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.spockframework</groupId> | ||
<artifactId>spock-junit4</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.groovy</groupId> | ||
<artifactId>groovy-all</artifactId> | ||
</dependency> | ||
|
||
<!-- Apache HttpClient --> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.13</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin, | ||
visit https://github.com/groovy/GMavenPlus/wiki --> | ||
<groupId>org.codehaus.gmavenplus</groupId> | ||
<artifactId>gmavenplus-plugin</artifactId> | ||
<version>3.0.2</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>compileTests</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<profiles> | ||
<profile> | ||
<id>repose-tests</id> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>3.2.5</version> | ||
<configuration combine.self="override"> | ||
<includes> | ||
<include>**/*Repose*Spec</include> <!-- Include Repose test classes --> | ||
</includes> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
|
||
</project> |
146 changes: 146 additions & 0 deletions
146
blueflood-spock-tests/src/test/groovy/com/rackspacecloud/blueflood/repose/ReposeSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright 2024 Rackspace US, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.rackspacecloud.blueflood.repose | ||
|
||
import groovy.json.JsonBuilder | ||
import org.apache.http.client.methods.HttpGet | ||
import org.apache.http.client.methods.HttpPost | ||
import org.apache.http.entity.StringEntity | ||
import org.apache.http.impl.client.HttpClients | ||
import spock.lang.IgnoreIf | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
/* | ||
* Repose upgrade activity needs to validate various Repose filters features | ||
* This test can be run against local vagrant or stage or prod blueflood endpoints | ||
* | ||
* Vagrant (local) allows us to configure repose filters to tweak as needed. | ||
* Example: Rate limits, authorization needs | ||
* | ||
* Pre-requisites for running: | ||
* Environment variables: | ||
* - TENANT_ID | ||
* - X-Auth-Token - Rackspace identity token to work with keystone-v2 filter config | ||
* - RATE_LIMIT_COUNT (Optional) - If this is set, we will be hitting request to this no of times to verify we reached the threshold of no of request in a minute. | ||
* This is used to test against vagrant or staging with configurable rate limit filters | ||
* - ENV_TO_RUN (local/stage/prod) defaults to stage | ||
*/ | ||
class ReposeSpec extends Specification { | ||
|
||
final String X_AUTH_TOKEN_HEADER = "X-Auth-Token" | ||
|
||
@Shared | ||
final String tenantId = System.getenv("TENANT_ID") | ||
@Shared | ||
final String authToken = System.getenv("AUTH_TOKEN") | ||
@Shared | ||
final String envToRun = System.getenv("ENV_TO_RUN")?.toLowerCase() ?: "stage" | ||
// This need to be static because Only @Shared and static fields may be accessed from where | ||
@Shared | ||
static final def defaultRateLimit = Integer.parseInt(System.getenv("RATE_LIMIT_COUNT") ?: "0") | ||
|
||
def httpClient = HttpClients.createDefault() | ||
@Shared | ||
String ingestEndpointUrl | ||
@Shared | ||
String queryEndpointUrl | ||
|
||
def setupSpec() { | ||
assert tenantId != null: envError("TENANT_ID") | ||
assert authToken != null: envError("AUTH_TOKEN") | ||
if (envToRun == "local") { | ||
ingestEndpointUrl = "http://localhost:9001/v2.0/${tenantId}" | ||
queryEndpointUrl = "http://localhost:9011/v2.0/${tenantId}" | ||
} else { | ||
def prefix = envToRun == "stage" ? "staging" : "global" | ||
ingestEndpointUrl = "https://${prefix}.metrics-ingest.api.rackspacecloud.com/v2.0/${tenantId}" | ||
queryEndpointUrl = "https://${prefix}.metrics.api.rackspacecloud.com/v2.0/${tenantId}" | ||
} | ||
} | ||
|
||
def envError(String variableName) { | ||
"${variableName} environment variable is missing" | ||
} | ||
|
||
def "invalid auth token returns 401"() { | ||
given: | ||
def request = getIngestHttpPostRequest() | ||
request.setHeader(X_AUTH_TOKEN_HEADER, "fooToken") | ||
|
||
when: | ||
def response = httpClient.execute(request) | ||
|
||
then: | ||
response.getStatusLine().getStatusCode() == 401 | ||
} | ||
|
||
def "/limits returns expected static limits as configured in rate-limit filter"() { | ||
given: | ||
def url = ingestEndpointUrl + "/limits" | ||
def request = new HttpGet(url) | ||
request.addHeader(X_AUTH_TOKEN_HEADER, authToken) | ||
|
||
when: | ||
def response = httpClient.execute(request) | ||
|
||
then: | ||
response.getStatusLine().getStatusCode() == 200 | ||
} | ||
|
||
def "ingesting valid metric payload succeeds"() { | ||
given: | ||
def request = getIngestHttpPostRequest() | ||
|
||
when: | ||
def response = httpClient.execute(request) | ||
|
||
then: | ||
response.getStatusLine().getStatusCode() == 200 | ||
} | ||
|
||
@IgnoreIf({ defaultRateLimit == 0 }) | ||
def "rate limited requests get rejected and return 429"() { | ||
when: | ||
def request = getIngestHttpPostRequest() | ||
def response = httpClient.execute(request) | ||
|
||
then: | ||
response.getStatusLine().getStatusCode() == (runIndex == defaultRateLimit ? 429 : 200) | ||
|
||
where: | ||
runIndex << (1..defaultRateLimit) | ||
} | ||
|
||
private HttpPost getIngestHttpPostRequest() { | ||
def url = ingestEndpointUrl + "/ingest" | ||
def request = new HttpPost(url) | ||
request.addHeader(X_AUTH_TOKEN_HEADER, authToken) | ||
request.addHeader("Content-Type", "application/json") | ||
def jsonBody = [ | ||
[ | ||
"collectionTime": new Date().getTime(), | ||
"ttlInSeconds" : 172800, | ||
"metricValue" : new Random().nextInt(100), | ||
"metricName" : "repose.test.metric" | ||
] | ||
] | ||
request.setEntity(new StringEntity(new JsonBuilder(jsonBody).toString())) | ||
return request | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters