forked from splunk/splunk-app-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cicd_runner.sh
executable file
·161 lines (137 loc) · 5.22 KB
/
cicd_runner.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
#set -x
if [ $# -lt 2 ]; then
echo "Usage: cicd_runner.sh [Splunk Image] [Cypress Image]"
echo "Image: "
echo " registry/image:tag"
exit 1
fi
MAX_WAIT_SECONDS=120
version=$1
cypress_image=$2
container="splunk"
APP_ROOT="testing_app"
APPS_DIR="/opt/splunk/etc/apps"
USER="admin"
PASSWORD="newPassword"
CI_PROJECT_DIR=${CI_PROJECT_DIR:-`pwd`}
SPLUNK_HOME="/opt/splunk"
SPLUNK_ETC="/opt/splunk/etc"
SPLUNK_START_ARGS="--accept-license"
SPLUNK_ENABLE_LISTEN="9997"
SPLUNK_ADD="tcp 1514"
SPLUNK_PASSWORD="newPassword"
SPLUNK_HOSTNAME="idx-example.splunkcloud.com"
mkdir -p build
echo "Running image: ${version}..."
# The Docker network allows the Splunk and testing containers to communicate
echo "Create a bridge network for the containers to communicate"
docker network create testingnet
# Create and start the Splunk container
echo "Starting splunk image ${version} as ${container}..."
docker run -d \
--name $container \
--network testingnet \
--hostname "$SPLUNK_HOSTNAME" \
--env SPLUNK_HOME="$SPLUNK_HOME" \
--env SPLUNK_ETC="$SPLUNK_ETC" \
--env SPLUNK_START_ARGS="$SPLUNK_START_ARGS" \
--env SPLUNK_ENABLE_LISTEN="$SPLUNK_ENABLE_LISTEN" \
--env SPLUNK_ADD="$SPLUNK_ADD" \
--env SPLUNK_PASSWORD="$SPLUNK_PASSWORD" \
--user root \
-p 8000:8000 \
-p 8089:8089 \
$version
echo "Copying data into container..."
docker exec $container bash -c "mkdir -p -m 777 /opt/splunk/etc/apps"
docker cp $CI_PROJECT_DIR/cicd/config/passwd $container:$SPLUNK_ETC/passwd
# Copy in the sample app
docker cp $CI_PROJECT_DIR/$APP_ROOT $container:$APPS_DIR/$APP_ROOT
# Copy in the generated data
docker cp $CI_PROJECT_DIR/output $container:/
# Prevent splunk from prompting for password reset
docker exec $container bash -c "touch /opt/splunk/etc/.ui_login"
# Wait for instance to be available
# Waiting for 2 and a half minutes.
loopCounter=0
health="starting"
# check to see if container has a health check and if so, wait for it to be healthy
while [[ $loopCounter -lt $MAX_WAIT_SECONDS && $health =~ "starting" ]]; do
health=`docker ps --filter "name=${container}" --format "{{.Status}}"`
echo -ne "\rWaiting for Splunk to be available...$((MAX_WAIT_SECONDS - loopCounter)) "
((loopCounter++))
sleep 1
done
# validate container is running
health=`docker ps --filter "name=${container}" --format "{{.Status}}"`
# if there was a problem, print some debugging information
if [[ $health == "" ]]; then
echo "Health:\n${health}\n" &> Errors.txt
echo "--------------------------------" &> Errors.txt
docker ps -a &> Errors.txt
echo "--------------------------------" &> Errors.txt
docker inspect $container &> Errors.txt
echo "--------------------------------" &> Errors.txt
docker logs $container &> Errors.txt
echo "--------------------------------" &> Errors.txt
echo "Container is no longer running!"
echo "See Errors.txt for more information."
exit 1
else
echo -e "\n\033[0;32m\xE2\x9C\x94\033[0m Splunk Available!"
fi
# if the container is healthy, or it's an old container without a health check, use the saved search to validate data is loaded
loopCounter=0
splunkReady=0
while [[ $loopCounter -lt $MAX_WAIT_SECONDS && $splunkReady -lt 1 ]]; do
echo -ne "container running, checking indexed data count...$((MAX_WAIT_SECONDS - loopCounter)) \r"
eventCount=`docker exec $container bash -c "SPLUNK_USERNAME=admin SPLUNK_PASSWORD=newPassword /opt/splunk/bin/splunk search 'index=main source=/output/access.log | stats count' -app testing_app"`
if [[ $eventCount =~ "1559" ]]; then
echo -e "\n\033[0;32m\xE2\x9C\x94\033[0m Data full indexed!"
splunkReady=1
fi
((loopCounter+=5))
sleep 5
done
# timeout error message if the data was not fully indexed
if [[ $splunkReady != 1 ]]; then
echo "Timeout waiting for data to be ingested into Splunk!"
echo "See build/Errors.txt for more information."
docker exec $container bash -c "ls -l /output" &> build/Errors.txt
docker logs $container &> Errors.txt
exit 1
fi
echo "Setting up test environment..."
# Run btool on the Splunk container
echo -n "Running btool checks..."
docker exec $container bash -c "/opt/splunk/bin/splunk btool check --debug" &> build/btool_output.txt
if [ $? -eq 0 ]
then
echo -e "\033[0;32m\xE2\x9C\x94\033[0m"
else
echo "Failed!"
echo "See build/btool_output.txt for more information"
fi
echo "Executing Cypress test specs..."
# Create Cypress container but do not start it yet
docker container create --name cypress_runner \
--network testingnet \
-w /e2e \
-e CYPRESS_baseUrl=http://$container:8000 \
-e CYPRESS_base_api=https://$container:8089 \
-e CYPRESS_headless=true \
$cypress_image
# Copy in configuration and tests
docker cp cicd/test/cypress cypress_runner:/e2e/cypress
docker cp cicd/test/cypress.json cypress_runner:/e2e/cypress.json
# Start Cypress container, which runs the tests
docker start -a cypress_runner || status=$?
# Copy out the test results (Cypress videos)
docker cp cypress_runner:/e2e/cypress/videos $CI_PROJECT_DIR/cicd/test/cypress/videos
# clean up from the run
docker stop $container || true
docker container rm $container || true
docker container rm cypress_runner || true
docker network rm testingnet || true
exit ${status:-0}