Skip to content

Commit

Permalink
Added spock test cases for Repose
Browse files Browse the repository at this point in the history
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
richarxt committed Apr 24, 2024
1 parent 6557fd4 commit 34ad04d
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 1 deletion.
81 changes: 81 additions & 0 deletions blueflood-spock-tests/pom.xml
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>
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
}

}
32 changes: 31 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>blueflood-cloudfiles</module>
<module>blueflood-rollupTools</module>
<module>blueflood-integration-tests</module>
<module>blueflood-spock-tests</module>
<module>blueflood-all</module>
</modules>

Expand Down Expand Up @@ -101,6 +102,28 @@
<artifactId>netty-all</artifactId>
<version>4.0.44.Final</version>
</dependency>

<!-- Spock Framework -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>2.4-M4-groovy-4.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-junit4</artifactId>
<version>2.4-M4-groovy-4.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>4.0.16</version>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -207,13 +230,20 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<version>3.2.5</version>
<configuration>
<!-- Skips unit tests if the value of skip.unit.tests property is true -->
<skipTests>${skip.unit.tests}</skipTests>
<includes>
<include>**/*Test</include>
<!-- Run all spock test -->
<include>**/*Spec</include>
</includes>
<!-- Excludes repose spock tests when unit tests are run. -->
<!-- Excludes integration tests when unit tests are run. -->
<!-- This is only necessary because the failsafe plugin's test naming conventions are overridden. -->
<excludes>
<exclude>**/*Repose*Spec</exclude>
<exclude>**/*Integration*.java</exclude>
</excludes>
</configuration>
Expand Down

0 comments on commit 34ad04d

Please sign in to comment.