-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CLOUD-3200] - querying k8s api to discover generated routes
Signed-off-by: Ricardo Zanini <zanini@redhat.com>
- Loading branch information
1 parent
95d3ccf
commit a47004a
Showing
9 changed files
with
356 additions
and
7 deletions.
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
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,17 @@ | ||
## Keycloak integration bats tests | ||
|
||
These bats tests verify the `keycloak.sh` script use cases. | ||
|
||
### The Mock Server | ||
|
||
The [`server`](server) dir has a bash utility called [shinatra](https://github.com/benrady/shinatra) to mock a real web server for use cases that require Kubernetes API integration. | ||
|
||
The JSON mock responses to test the API calls are in the [`mock_responses`](mock_responses) directory. | ||
|
||
To create a new test case, just create a new JSON file from the API you want to test and save it in the `mock_responses` directory. Check the [OpenShift API reference](https://docs.openshift.com/container-platform/3.11/rest_api/) if you are not sure how to call the internal APIs. | ||
|
||
Then you can use the [`setup_k8s_api`](common.bash) to fire up the server. Just don't forget to kill the pid and all the child processes. Check the usage of this function in the test suite [`hostname-discovery.bats`](hostname-discovery.bats). | ||
|
||
### Hostname Discovery Test Suite | ||
|
||
The tests cases in the [`hostname-discovery.bats`](hostname-discovery.bats) file basically verify if the function `query_routes_from_service` on `keycloak.sh` is working as expected simulating scenarios where the API is not available, there's one, multiple or no routes at all. |
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,32 @@ | ||
echo $BATS_TEST_DIRNAME | ||
load $BATS_TEST_DIRNAME/../../../test-common/log_utils.bash | ||
|
||
export JBOSS_HOME=$BATS_TMPDIR/jboss_home | ||
export K8S_ENV=false | ||
export KUBERNETES_SERVICE_HOST="localhost" | ||
export KUBERNETES_SERVICE_PORT=8080 | ||
export KUBERNETES_SERVICE_PROTOCOL="http" | ||
|
||
if [ -e /var/run/secrets/kubernetes.io/serviceaccount/token ]; then | ||
K8S_ENV=true | ||
fi | ||
|
||
mkdir -p $JBOSS_HOME/bin/launch | ||
mkdir -p $JBOSS_HOME/responses | ||
cp $BATS_TEST_DIRNAME/../../added/keycloak.sh $JBOSS_HOME/bin/launch | ||
cp $BATS_TEST_DIRNAME/../../../test-common/logging.sh $JBOSS_HOME/bin/launch | ||
cp $BATS_TEST_DIRNAME/server/shinatra.sh $JBOSS_HOME/bin/launch | ||
cp $BATS_TEST_DIRNAME/mock_responses/* $JBOSS_HOME/responses | ||
|
||
source $JBOSS_HOME/bin/launch/keycloak.sh | ||
source $JBOSS_HOME/bin/launch/shinatra.sh | ||
|
||
# Configure the Mocked Kubernetes server based on mocked responses | ||
# {1} mock_response | ||
function setup_k8s_api() { | ||
local mock_response=${1} | ||
local data=$(cat $JBOSS_HOME/responses/${mock_response}.json | tr -d \\n) | ||
start_mock_server ${KUBERNETES_SERVICE_PORT} "${data}" >&2 & | ||
local pid=$! | ||
echo ${pid} | ||
} |
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,50 @@ | ||
#!/usr/bin/env bats | ||
|
||
load common | ||
|
||
# Runs the base api test | ||
# {1} mock_response | ||
function run_api_test { | ||
local mock_response=${1} | ||
local server_pid=$(setup_k8s_api ${mock_response}) | ||
K8S_ENV=true | ||
local routes=$(discover_routes) | ||
pkill -P $server_pid | ||
echo $routes | ||
} | ||
|
||
@test "Is nc installed?" { | ||
run nc --version | ||
[ "$status" -eq 0 ] | ||
} | ||
|
||
@test "Kubernetes Route API not available" { | ||
local expected="" | ||
if [ "$K8S_ENV" = true ]; then | ||
skip "This test supposed to be run outside a kubernetes environment" | ||
fi | ||
run discover_routes | ||
[ "$status" -eq 0 ] | ||
[ "${lines[0]}" = "${expected}" ] | ||
} | ||
|
||
@test "Kubernetes Route API found no routes for the pod" { | ||
local expected="" | ||
local mock_response="no-route" | ||
result=$(run_api_test $mock_response) | ||
[ "${result}" = "$expected" ] | ||
} | ||
|
||
@test "Kubernetes Route API found one route for the pod" { | ||
local expected="https://eap-app-bsig-cloud.192.168.99.100.nip.io" | ||
local mock_response="single-route" | ||
result=$(run_api_test $mock_response) | ||
[ "${result}" = "$expected" ] | ||
} | ||
|
||
@test "Kubernetes Route API found multiple routes for the pod" { | ||
local expected="http://bc-authoring-rhpamcentr-bsig-cloud.192.168.99.100.nip.io;https://secure-bc-authoring-rhpamcentr-bsig-cloud.192.168.99.100.nip.io" | ||
local mock_response="multi-route" | ||
result=$(run_api_test $mock_response) | ||
[ "${result}" = "$expected" ] | ||
} |
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,115 @@ | ||
{ | ||
"kind": "RouteList", | ||
"apiVersion": "route.openshift.io/v1", | ||
"metadata": { | ||
"selfLink": "/apis/route.openshift.io/v1/namespaces/bsig-cloud/routes", | ||
"resourceVersion": "1633753" | ||
}, | ||
"items": [ | ||
{ | ||
"metadata": { | ||
"name": "bc-authoring-rhpamcentr", | ||
"namespace": "bsig-cloud", | ||
"selfLink": "/apis/route.openshift.io/v1/namespaces/bsig-cloud/routes/bc-authoring-rhpamcentr", | ||
"uid": "d81990d6-6d27-11e9-b294-080027e8c7bc", | ||
"resourceVersion": "1241102", | ||
"creationTimestamp": "2019-05-02T22:15:50Z", | ||
"labels": { | ||
"app": "rhpam74-authoring", | ||
"application": "bc-authoring", | ||
"rhpam": "1.0", | ||
"service": "bc-authoring-rhpamcentr", | ||
"template": "rhpam74-authoring" | ||
}, | ||
"annotations": { | ||
"description": "Route for Business Central's http service.", | ||
"haproxy.router.openshift.io/timeout": "60s", | ||
"openshift.io/generated-by": "OpenShiftNewApp", | ||
"openshift.io/host.generated": "true" | ||
} | ||
}, | ||
"spec": { | ||
"host": "bc-authoring-rhpamcentr-bsig-cloud.192.168.99.100.nip.io", | ||
"to": { | ||
"kind": "Service", | ||
"name": "bc-authoring-rhpamcentr", | ||
"weight": 100 | ||
}, | ||
"port": { | ||
"targetPort": "http" | ||
}, | ||
"wildcardPolicy": "None" | ||
}, | ||
"status": { | ||
"ingress": [ | ||
{ | ||
"host": "bc-authoring-rhpamcentr-bsig-cloud.192.168.99.100.nip.io", | ||
"routerName": "router", | ||
"conditions": [ | ||
{ | ||
"type": "Admitted", | ||
"status": "True", | ||
"lastTransitionTime": "2019-05-02T22:15:51Z" | ||
} | ||
], | ||
"wildcardPolicy": "None" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"metadata": { | ||
"name": "secure-bc-authoring-rhpamcentr", | ||
"namespace": "bsig-cloud", | ||
"selfLink": "/apis/route.openshift.io/v1/namespaces/bsig-cloud/routes/secure-bc-authoring-rhpamcentr", | ||
"uid": "d8347fb0-6d27-11e9-b294-080027e8c7bc", | ||
"resourceVersion": "1241107", | ||
"creationTimestamp": "2019-05-02T22:15:50Z", | ||
"labels": { | ||
"app": "rhpam74-authoring", | ||
"application": "bc-authoring", | ||
"rhpam": "1.0", | ||
"service": "bc-authoring-rhpamcentr", | ||
"template": "rhpam74-authoring" | ||
}, | ||
"annotations": { | ||
"description": "Route for Business Central's https service.", | ||
"haproxy.router.openshift.io/timeout": "60s", | ||
"openshift.io/generated-by": "OpenShiftNewApp", | ||
"openshift.io/host.generated": "true" | ||
} | ||
}, | ||
"spec": { | ||
"host": "secure-bc-authoring-rhpamcentr-bsig-cloud.192.168.99.100.nip.io", | ||
"to": { | ||
"kind": "Service", | ||
"name": "bc-authoring-rhpamcentr", | ||
"weight": 100 | ||
}, | ||
"port": { | ||
"targetPort": "https" | ||
}, | ||
"tls": { | ||
"termination": "passthrough" | ||
}, | ||
"wildcardPolicy": "None" | ||
}, | ||
"status": { | ||
"ingress": [ | ||
{ | ||
"host": "secure-bc-authoring-rhpamcentr-bsig-cloud.192.168.99.100.nip.io", | ||
"routerName": "router", | ||
"conditions": [ | ||
{ | ||
"type": "Admitted", | ||
"status": "True", | ||
"lastTransitionTime": "2019-05-02T22:15:51Z" | ||
} | ||
], | ||
"wildcardPolicy": "None" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} |
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,9 @@ | ||
{ | ||
"kind": "RouteList", | ||
"apiVersion": "route.openshift.io/v1", | ||
"metadata": { | ||
"selfLink": "/apis/route.openshift.io/v1/namespaces/bsig-cloud/routes", | ||
"resourceVersion": "1633866" | ||
}, | ||
"items": [] | ||
} |
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,60 @@ | ||
{ | ||
"kind": "RouteList", | ||
"apiVersion": "route.openshift.io/v1", | ||
"metadata": { | ||
"selfLink": "/apis/route.openshift.io/v1/namespaces/bsig-cloud/routes", | ||
"resourceVersion": "1633112" | ||
}, | ||
"items": [ | ||
{ | ||
"metadata": { | ||
"name": "eap-app", | ||
"namespace": "bsig-cloud", | ||
"selfLink": "/apis/route.openshift.io/v1/namespaces/bsig-cloud/routes/eap-app", | ||
"uid": "eb9ed521-6d0a-11e9-8682-080027e8c7bc", | ||
"resourceVersion": "1194535", | ||
"creationTimestamp": "2019-05-02T18:48:48Z", | ||
"labels": { | ||
"app": "eap72-basic-s2i", | ||
"application": "eap-app", | ||
"template": "eap72-basic-s2i", | ||
"xpaas": "1.0.0" | ||
}, | ||
"annotations": { | ||
"description": "Route for application's https service.", | ||
"openshift.io/generated-by": "OpenShiftNewApp", | ||
"openshift.io/host.generated": "true" | ||
} | ||
}, | ||
"spec": { | ||
"host": "eap-app-bsig-cloud.192.168.99.100.nip.io", | ||
"to": { | ||
"kind": "Service", | ||
"name": "eap-app", | ||
"weight": 100 | ||
}, | ||
"tls": { | ||
"termination": "edge", | ||
"insecureEdgeTerminationPolicy": "Redirect" | ||
}, | ||
"wildcardPolicy": "None" | ||
}, | ||
"status": { | ||
"ingress": [ | ||
{ | ||
"host": "eap-app-bsig-cloud.192.168.99.100.nip.io", | ||
"routerName": "router", | ||
"conditions": [ | ||
{ | ||
"type": "Admitted", | ||
"status": "True", | ||
"lastTransitionTime": "2019-05-02T18:48:48Z" | ||
} | ||
], | ||
"wildcardPolicy": "None" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} |
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,2 @@ | ||
Based on: | ||
https://github.com/benrady/shinatra |
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,12 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Starts the server | ||
# {1} port | ||
# {2} response | ||
function start_mock_server() { | ||
RESPONSE="HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n${2:-"OK"}\r\n" | ||
while { echo -en "$RESPONSE"; } | nc -l "${1:-8080}"; do | ||
echo "===============================" | ||
done | ||
} | ||
|