Community Plugin: This plugin is developed, tested, and maintained by a third-party contributor.
This plugin will enable mocking virtual API request and response pairs through Kong Gateway.
Overview
This plugin will enable mocking virtual API request and response pairs through Kong Gateway.
Explanation
kong-service-virtualization
schema virtual_tests
arguments:
[
{
"name": "TestCase1",
"requestHttpMethod": "POST",
"requestHash": "0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4",
"responseHttpStatus": "200",
"responseContentType": "application/json",
"response": "eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="
},
{
"name": "TestCase2",
"requestHttpMethod": "GET",
"requestHash": "e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32",
"responseHttpStatus": "200",
"responseContentType": "application/json",
"response": "eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="
}
]
Where TestCase1
and TestCase2
are the names of the virtual test cases and must be passed in as a header value:
X-VirtualRequest: TestCase1
or
X-VirtualRequest: TestCase2
The requestHash
arg is a Sha256(HTTP Request as query parameters or HTTP Body).
The response
arg is a Base64 encoded format of the response HTTP Body.
So the above plugin equates to these pseudo requests:
https://gateway.company.com/virtualtest
POST:
{
"virtual": "test"
}
Response : {"message": "A Big Success!"} as base64 encoded in plugin
GET:
hello=world&service=virtualized
Response : {"message": "Another Success!"} as base4 encoded in plugin
In the event you do not successfully match on request you will receive a Sha256 comparison for you own personal debugging:
Status Code: 404 Not Found
Content-Length: 207
Content-Type: application/json; charset=utf-8
{"message":"No virtual request match found, your request yielded: 46c4b4caf0cc3a5a589cbc4e0f3cd0492985d5b889f19ebc11e5a5bd6454d20f expected 0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4"}
If the test case specified in the header does not match anything found stored within the plugin your error would be like so:
Passing X-VirtualRequest: TestCase3
in header yields:
Status Code: 404 Not Found
Content-Length: 49
Content-Type: application/json; charset=utf-8
{"message":"No matching virtual request found!"}
Installation
Recommended:
$ luarocks install kong-service-virtualization
Optional:
$ git clone https://github.com/Optum/kong-service-virtualization
$ cd /path/to/kong/plugins/kong-service-virtualization
$ luarocks make *.rockspec
Maintainers
jeremyjpj0916
rsbrisci
Feel free to open issues, or refer to our Contribution Guidelines if you have any questions.
Configuration Reference
This plugin is compatible with DB-less mode.
Example plugin configuration
Enable on a service
Enable on a route
Enable globally
The following examples provide some typical configurations for enabling
the kong-service-virtualization
plugin on a
service.
Admin API
Kubernetes
Declarative (YAML)
Make the following request:
curl -X POST http://localhost:8001/services/SERVICE_NAME|SERVICE_ID/plugins \
--data "name=kong-service-virtualization" \
--data "config.virtual_tests={"name"=>"TestCase1", "requestHttpMethod"=>"POST", "requestHash"=>"0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="}" \
--data "config.virtual_tests={"name"=>"TestCase2", "requestHttpMethod"=>"GET", "requestHash"=>"e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="}"
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: kong-service-virtualization-example
config:
virtual_tests:
- {"name"=>"TestCase1", "requestHttpMethod"=>"POST", "requestHash"=>"0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="}
- {"name"=>"TestCase2", "requestHttpMethod"=>"GET", "requestHash"=>"e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="}
plugin: kong-service-virtualization
Next, apply the KongPlugin resource to a
service by annotating the
service as follows:
apiVersion: v1
kind: Service
metadata:
name: SERVICE_NAME|SERVICE_ID
labels:
app: SERVICE_NAME|SERVICE_ID
annotations:
konghq.com/plugins: kong-service-virtualization-example
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: SERVICE_NAME|SERVICE_ID
selector:
app: SERVICE_NAME|SERVICE_ID
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: kong-service-virtualization
service: SERVICE_NAME|SERVICE_ID
config:
virtual_tests:
- {"name"=>"TestCase1", "requestHttpMethod"=>"POST", "requestHash"=>"0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="}
- {"name"=>"TestCase2", "requestHttpMethod"=>"GET", "requestHash"=>"e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="}
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
The following examples provide some typical configurations for enabling
the kong-service-virtualization
plugin on a
route.
Admin API
Kubernetes
Declarative (YAML)
Make the following request:
curl -X POST http://localhost:8001/routes/ROUTE_NAME|ROUTE_ID/plugins \
--data "name=kong-service-virtualization" \
--data "config.virtual_tests={"name"=>"TestCase1", "requestHttpMethod"=>"POST", "requestHash"=>"0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="}" \
--data "config.virtual_tests={"name"=>"TestCase2", "requestHttpMethod"=>"GET", "requestHash"=>"e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="}"
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: kong-service-virtualization-example
config:
virtual_tests:
- {"name"=>"TestCase1", "requestHttpMethod"=>"POST", "requestHash"=>"0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="}
- {"name"=>"TestCase2", "requestHttpMethod"=>"GET", "requestHash"=>"e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="}
plugin: kong-service-virtualization
Then, apply it to an ingress (route or routes)
by annotating the ingress as follows:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ROUTE_NAME|ROUTE_ID
annotations:
kubernetes.io/ingress.class: kong
konghq.com/plugins: kong-service-virtualization-example
spec:
rules:
- host: examplehostname.com
http:
paths:
- path: /bar
backend:
service:
name: echo
port:
number: 80
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: kong-service-virtualization
route: ROUTE_NAME
config:
virtual_tests:
- {"name"=>"TestCase1", "requestHttpMethod"=>"POST", "requestHash"=>"0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="}
- {"name"=>"TestCase2", "requestHttpMethod"=>"GET", "requestHash"=>"e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="}
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration
will target.
A plugin which is not associated to any service, route, or consumer is
considered global, and will be run on every request. Read the
Plugin Reference and the Plugin Precedence
sections for more information.
The following examples provide some typical configurations for enabling
the kong-service-virtualization
plugin globally.
Admin API
Kubernetes
Declarative (YAML)
Make the following request:
curl -X POST http://localhost:8001/plugins/ \
--data "name=kong-service-virtualization" \
--data "config.virtual_tests={"name"=>"TestCase1", "requestHttpMethod"=>"POST", "requestHash"=>"0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="}" \
--data "config.virtual_tests={"name"=>"TestCase2", "requestHttpMethod"=>"GET", "requestHash"=>"e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="}"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-kong-service-virtualization>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
virtual_tests:
- {"name"=>"TestCase1", "requestHttpMethod"=>"POST", "requestHash"=>"0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="}
- {"name"=>"TestCase2", "requestHttpMethod"=>"GET", "requestHash"=>"e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="}
plugin: kong-service-virtualization
Add a plugins
entry in the declarative
configuration file:
plugins:
- name: kong-service-virtualization
config:
virtual_tests:
- {"name"=>"TestCase1", "requestHttpMethod"=>"POST", "requestHash"=>"0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="}
- {"name"=>"TestCase2", "requestHttpMethod"=>"GET", "requestHash"=>"e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="}
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
Form Parameter |
Description |
name
required
Type: string |
The name of the plugin, in this case kong-service-virtualization . |
service.name or service.id
Type: string |
The name or ID of the service the plugin targets.
Set one of these parameters if adding the plugin to a service through the top-level /plugins endpoint.
Not required if using /services/SERVICE_NAME|SERVICE_ID/plugins . |
route.name or route.id
Type: string |
The name or ID of the route the plugin targets.
Set one of these parameters if adding the plugin to a route through the top-level /plugins endpoint.
Not required if using /routes/ROUTE_NAME|ROUTE_ID/plugins . |
enabled
Type: boolean
Default value: true |
Whether this plugin will be applied. |
api_id
Type: string |
The ID of the API the plugin targets.
Note: The API Entity is deprecated in favor of Services since CE 0.13.0 and EE 0.32. |
config.virtual_tests
required
|
A JSON array as string representation of the plugin’s configurable fields.
|
name
JSON represents a human readable test case name
requestHttpMethod
JSON represents the HTTP method associated to the virtual request
requestHash
JSON represents the Sha256 of the HTTP Body or QUERY Parameters of your request
responseHttpStatus
JSON represents the HTTP response to send consumers after successful virtual match
responseContentType
JSON represents the Content-Type of the HTTP response after successful virtual request match
response
JSON represents the Base64 encoded virtual response to send after successful virtual request match