-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathtask.bash
More file actions
executable file
·202 lines (161 loc) · 4.26 KB
/
task.bash
File metadata and controls
executable file
·202 lines (161 loc) · 4.26 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env bash
# shellcheck disable=SC2317
set -euo pipefail
declare -A PORTS
PORTS["mock-rocketchat"]=8080
PORTS["cluster-rocketchat"]=9080
PORTS["mock-monitoring"]=8082
PORTS["cluster-monitoring"]=9082
function _error() {
for line in "${@}"; do
echo -e "[ERROR]: ${line}" >&2
done
return 1
}
function _warn() {
for line in "${@}"; do
echo -e "[WARN]: ${line}" >&2
done
}
function _info() {
for line in "${@}"; do
echo -e "[INFO]: ${line}"
done
}
function submodules() {
git submodule update --init --recursive
}
function mock.run() {
KUBECONFIG_FILE="${KUBECONFIG_FILE:-$(mktemp)}"
export KWOK_PORT
KWOK_PORT="${PORTS[${PROJECT_NAME}]}"
sed "s/8080/${KWOK_PORT}/g" mock/kubeconfig.yaml >"${KUBECONFIG_FILE}"
# shellcheck disable=SC2329
function _compose() {
docker compose \
--file mock/compose.yaml \
--project-name "${PROJECT_NAME}" \
"$@"
}
# shellcheck disable=SC2329
function create() {
_compose up -d
}
# shellcheck disable=SC2329
function delete() {
_compose down --volumes --remove-orphans
}
"${@}"
}
cluster.run() {
PROJECT_NAME="${PROJECT_NAME:-${1}}"
PORT="${PORTS[${PROJECT_NAME}]}"
KUBECONFIG_FILE="${KUBECONFIG_FILE:-$(mktemp)}"
local cluster_exists="false"
# shellcheck disable=SC2329
function create() {
k3d cluster create \
--api-port "${PORT}" \
--image rancher/k3s:v1.33.2-k3s1 \
--kubeconfig-switch-context=false \
--kubeconfig-update-default=false \
--no-lb \
"${PROJECT_NAME}" || cluster_exists="true"
if [[ "${IGNORE_CLEANUP:-}" == "true" && "${cluster_exists}" == "true" ]]; then
_warn "Cluster ${PROJECT_NAME} already exists, skipping creation."
elif [[ "${cluster_exists}" == "true" ]]; then
_error "Failed to create cluster ${PROJECT_NAME}."
fi
get_kubeconfig >"${KUBECONFIG_FILE}"
}
# shellcheck disable=SC2329
function get_kubeconfig() {
k3d kubeconfig get "${PROJECT_NAME}"
}
# shellcheck disable=SC2329
function delete() {
k3d cluster delete "${PROJECT_NAME}"
}
"${@}"
}
function mock() {
export KUBECONFIG_FILE
export PROJECT_NAME
export KUBECONFIG
KUBECONFIG_FILE="$(mktemp)"
KUBECONFIG="${KUBECONFIG_FILE}"
args="$*"
PROJECT_NAME="mock-${1}"
echo "${PROJECT_NAME}"
_info \
"Using project name: ${PROJECT_NAME}" \
"Using kubeconfig file: ${KUBECONFIG_FILE}"
[[ -z "${IGNORE_CLEANUP:-}" ]] &&
trap 'mock.run delete' EXIT
mock.run create
"$@"
}
function cluster() {
export KUBECONFIG_FILE
export PROJECT_NAME
export KUBECONFIG
KUBECONFIG_FILE="$(mktemp)"
KUBECONFIG="${KUBECONFIG_FILE}"
args="${*}"
PROJECT_NAME="cluster-${1}"
_info \
"Running tests for ${1} mode" \
"Using project name: ${PROJECT_NAME}" \
"Using kubeconfig file: ${KUBECONFIG_FILE}"
[[ -z "${IGNORE_CLEANUP:-}" ]] &&
trap 'cluster.run delete' EXIT
cluster.run create
"$@"
}
function rocketchat() {
./rocketchat/tests/run.bash "$@"
}
function monitoring() {
./bats/core/bin/bats ./monitoring/tests/tests.bats "$@"
}
function clean() {
find . -name "*.tgz" -delete
for mode in "microservices" "monolith"; do
PROJECT_NAME="mock-rocketchat-${mode}" \
mock.run delete || true
PROJECT_NAME="cluster-rocketchat-${mode}" \
cluster.run delete || true
done
PROJECT_NAME="mock-monitoring" \
mock.run delete || true
PROJECT_NAME="cluster-monitoring" \
cluster.run delete || true
"$@"
}
function keep() {
export IGNORE_CLEANUP="true"
_info "---"
_info "Resources will be kept after running tests"
_info "---"
"$@"
}
function usage() {
echo "Usage: $0 <command> [args]"
echo "Available commands:"
echo " submodules - Initialize git submodules"
echo " mock - Run mock tests"
echo " cluster - Run cluster tests"
echo " rocketchat - Run Rocket.Chat tests"
echo " monitoring - Run monitoring tests"
echo " clean - Clean up generated files and resources"
echo " keep - Keep resources after running tests"
echo " help - Show this help message"
echo "Example usage:"
echo " $0 [keep] mock rocketchat"
echo " $0 [keep] cluster monitoring"
}
if [[ -z "${1:-}" ]]; then
_error "$(usage)"
exit 1
fi
"$@"