Skip to content

Commit a59dd9a

Browse files
Merge pull request #458 from Makdaam/SREP-222
SREP-222 Adding local testing environment
2 parents 7e13af9 + 02f4186 commit a59dd9a

File tree

5 files changed

+167
-2
lines changed

5 files changed

+167
-2
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ dist
77
.vscode
88
cad_testing
99
e2e-suite.test
10-
payload
10+
payload
11+
test/testinfra/*.log
12+
test/testinfra/*.pem

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ They are initialized for you and passed to the investigation via investigation.R
8989

9090
## Testing locally
9191

92-
Requires an existing cluster.
92+
### Against upstream stage OCM Backplane
93+
Requires an existing cluster. Requires that the metadata.yaml is commited to the main branch of the upstream repo (see below for testing against a local metadata.yaml).
9394

9495
1. Create a test incident and payload file for your cluster
9596

@@ -112,6 +113,45 @@ Requires an existing cluster.
112113
./bin/cadctl investigate --payload-path payload
113114
```
114115

116+
### Against local OCM Backplane
117+
Requires existing cluster, same as above.
118+
The requests to /backplane/remediate and /backplane/remediation OCM Backplane endpoints are redirected to the local instance of OCM Backplane.
119+
This means the metadata.yaml commited to the main branch on your local disk is used to grant permissions (an alternate branch will be available after SREP-636 is complete).
120+
121+
Make sure to install the dependencies first with
122+
```
123+
dnf install jq openssl tinyproxy haproxy proxytunnel
124+
```
125+
It will run services on the following local ports:8001 8091 8443 8888
126+
127+
1. Create a test incident and payload file for your cluster
128+
129+
```bash
130+
./test/generate_incident.sh <alertname> <clusterid>
131+
```
132+
133+
2. In a separate terminal start the local infrastructure
134+
> **Note:** You need to clone the backplane-api code repository to a local directory and copy ocm.json from a staging cluster to its ./configs dir.
135+
```
136+
OCM_BACKPLANE_REPO_PATH=/home/me/backplane-api ./test/launch_local_env.sh
137+
```
138+
139+
140+
3. Export the required env variables from vault
141+
> **Note:** For information on the envs see [required env variables](#required-env-variables).
142+
143+
```
144+
source test/set_stage_env.sh
145+
```
146+
147+
4. `make build`
148+
5. Run `cadctl` with the payload file created by `test/generate_incident.sh` and proxy as well as the backplane URL set to localhost
149+
150+
```bash
151+
BACKPLANE_URL=https://localhost:8443 HTTP_PROXY=http://127.0.0.1:8888 HTTPS_PROXY=http://127.0.0.1:8888 BACKPLANE_PROXY=http://127.0.0.1:8888 ./bin/cadctl investigate --payload-path ./payload --log-level debug"
152+
```
153+
6. Close the local infrastructure when done by sending SIGINT (Ctrl+C) to the launch_local_env.sh
154+
115155
### Logging levels
116156
117157
CAD allows for different logging levels (debug, info, warn, error, fatal, panic). The log level is determind through a hierarchy, where the cli flag `log-level`

test/launch_local_env.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
#assuming we're launched from inside the configuration-anomaly-detection repository
5+
CAD_REPO_PATH=$(git rev-parse --show-toplevel)
6+
echo "Assuming CAD repository root is ${CAD_REPO_PATH}"
7+
8+
#check presence of binary, assume the dnf package name is the same
9+
check_presence () {
10+
# $1 - name of the binary
11+
echo -n "Checking presence of $1..."
12+
if ! which $1 2>/dev/null >/dev/null; then
13+
echo "Not Found"
14+
echo "Try 'dnf install $1' on Fedora"
15+
exit -1
16+
else
17+
echo "Found"
18+
fi
19+
}
20+
21+
# clean up child processes on SIGINT
22+
trap "kill -- -$$" EXIT
23+
24+
check_presence "jq"
25+
check_presence "openssl"
26+
check_presence "tinyproxy"
27+
check_presence "haproxy"
28+
check_presence "proxytunnel"
29+
30+
#loading env vars
31+
. ${CAD_REPO_PATH}/test/set_stage_env.sh
32+
33+
#checking env vars
34+
set +u
35+
if [[ -z "${OCM_BACKPLANE_REPO_PATH}" ]]; then
36+
echo "Please set OCM_BACKPLANE_REPO_PATH variable to the path of the OCM Backplane code repository"
37+
exit -1
38+
fi
39+
set -u
40+
41+
if ! [ $(cat ${OCM_BACKPLANE_REPO_PATH}/configs/ocm.json | jq -r .client_id) = "ocm-backplane-staging" ]; then
42+
echo "OCM Backplane ocm.json (${OCM_BACKPLANE_REPO_PATH}/configs/ocm.json) isn't the ocm-backplane-staging config."
43+
echo "Please get the config from a backplane pod on a staging backplanes0* cluster (in /ocm inside the pod)"
44+
echo "and place it in the configs subdirectory of the backplane-api repo."
45+
exit -1
46+
fi
47+
48+
#checking certificate validity
49+
if ! openssl verify ${OCM_BACKPLANE_REPO_PATH}/localhost.crt; then
50+
echo "Certificate ${OCM_BACKPLANE_REPO_PATH}/localhost.crt not valid, please run make dev-certs in the OCM Backplane directory as root to generate and trust the localhost certificates"
51+
exit -1
52+
fi
53+
54+
#creating certificate file for the HAProxy
55+
cat ${OCM_BACKPLANE_REPO_PATH}/localhost.crt ${OCM_BACKPLANE_REPO_PATH}/localhost.key > ${CAD_REPO_PATH}/test/testinfra/localhost.pem
56+
57+
#checking BACKPLANE_PROXY reachability reachability
58+
echo "Checking Proxy reachability"
59+
if ! curl ${BACKPLANE_PROXY} -o /dev/null; then
60+
echo "Proxy ${BACKPLANE_PROXY} not reachable, check VPN connection"
61+
exit -1
62+
fi
63+
64+
#run the env
65+
echo "Starting tinyproxy on port 8888"
66+
tinyproxy -d -c ${CAD_REPO_PATH}/test/testinfra/tinyproxy.conf > ${CAD_REPO_PATH}/test/testinfra/tinyproxy.log 2> ${CAD_REPO_PATH}/test/testinfra/tinyproxy.error.log&
67+
68+
echo "Starting proxytunnel on port 8091"
69+
proxytunnel -v -p squid.corp.redhat.com:3128 -d api.stage.backplane.openshift.com:443 -a 8091 > ${CAD_REPO_PATH}/test/testinfra/proxytunnel.log 2> ${CAD_REPO_PATH}/test/testinfra/proxytunnel.error.log &
70+
71+
echo "Starting haproxy on port 8443"
72+
pushd ${CAD_REPO_PATH}/test/testinfra/
73+
haproxy -f haproxy.cfg > ${CAD_REPO_PATH}/test/testinfra/haproxy.log 2> ${CAD_REPO_PATH}/test/testinfra/haproxy.error.log &
74+
popd
75+
76+
echo "Starting backplane-api on port 8001"
77+
pushd $OCM_BACKPLANE_REPO_PATH
78+
GIT_REPO=${CAD_REPO_PATH} make run-local-with-testremediation > ${CAD_REPO_PATH}/test/testinfra/backplan-api.log 2> ${CAD_REPO_PATH}/test/testinfra/backplan-api.error.log &
79+
popd
80+
81+
echo "Environment started. Check ${CAD_REPO_PATH}/test/testinfra/ directory for logs"
82+
echo "Run cadctl with the following command to test against the local backplane-api for remediations"
83+
echo ""
84+
echo "BACKPLANE_URL=https://localhost:8443 HTTP_PROXY=http://127.0.0.1:8888 HTTPS_PROXY=http://127.0.0.1:8888 BACKPLANE_PROXY=http://127.0.0.1:8888 ./bin/cadctl investigate --payload-path ./payload --log-level debug"
85+
echo ""
86+
echo "Send SIGINT (Ctrl+C) to terminate the local infrastructure"
87+
#keep the script alive until all child processes are cleaned up
88+
wait

test/testinfra/haproxy.cfg

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
global
2+
log stderr format iso local7
3+
defaults
4+
log global
5+
mode http
6+
timeout connect 5000ms
7+
timeout client 50000ms
8+
timeout server 50000ms
9+
10+
frontend https-in
11+
option httplog
12+
bind *:8443 ssl crt ./localhost.pem
13+
redirect scheme https code 301 if !{ ssl_fc }
14+
use_backend local-ocmb if { path_beg /backplane/remediat }
15+
default_backend upstream-ocmb
16+
17+
backend upstream-ocmb
18+
http-request set-header Host api.stage.backplane.openshift.com
19+
server upstream 127.0.0.1:8091 ssl verify none
20+
21+
backend local-ocmb
22+
server local 127.0.0.1:8001 ssl verify none

test/testinfra/tinyproxy.conf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Port 8888
2+
Listen 127.0.0.1
3+
Timeout 600
4+
DefaultErrorFile "/usr/share/tinyproxy/default.html"
5+
StatFile "/usr/share/tinyproxy/stats.html"
6+
LogLevel Info
7+
upstream http squid.corp.redhat.com:3128 ".com"
8+
upstream none "localhost"
9+
MaxClients 100
10+
Allow 127.0.0.1
11+
Allow ::1
12+
ViaProxyName "tinyproxy"
13+

0 commit comments

Comments
 (0)