-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest-e2e.sh
executable file
·144 lines (124 loc) · 3.38 KB
/
test-e2e.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
#!/usr/bin/env bash
# Test cluster
CLUSTER_NAME=""
KUBECONFIG=kubeconfig
# Values file to pass to Helm
VALUES_FILE=values.yaml
# New Relic account (production) details
ACCOUNT_ID=""
API_KEY=""
LICENSE_KEY=""
# Unset if you only want to setup a test cluster with E2E specifications
# Set to true if you additionally want to run tests
RUN_TESTS=""
function main() {
parse_args "$@"
create_cluster
if [[ "$RUN_TESTS" == "true" ]]; then
run_tests
teardown
fi
}
function parse_args() {
totalArgs=$#
# Arguments are passed by value, so other functions
# are not affected by destructive processing
while [[ $# -gt 0 ]]; do
case $1 in
--account_id)
shift
ACCOUNT_ID="$1"
;;
--api_key)
shift
API_KEY="$1"
;;
--help)
help
exit 0
;;
--license_key)
shift
LICENSE_KEY="$1"
;;
--run_tests)
RUN_TESTS="true"
;;
-*|--*|*)
echo "Unknown field: $1"
exit 1
;;
esac
shift
done
if [[ totalArgs -lt 6 ]]; then
help
fi
}
function help() {
cat <<END
Usage:
${0##*/} --account_id <new_relic_prod> --api_key <api_key>
--license_key <license_key> [--run_tests]
--account_id: New Relic account in production
--api_key: key type 'USER'
--license_key: key type 'INGEST - LICENSE'
--run_tests: if unset, create a cluster with specifications matching E2E tests
otherwise run tests in addition to setting up cluster
END
}
function create_cluster() {
echo "🔄 Setup"
cleanup
now=$( date "+%Y-%m-%d-%H-%M-%S" )
CLUSTER_NAME=${now}-e2e-tests
touch ${KUBECONFIG} > /dev/null
chmod go-r ${KUBECONFIG}
export KUBECONFIG=${KUBECONFIG}
echo "🔄 Creating cluster"
make kind-up > /dev/null 2>&1
echo "🔄 Adding Helm repositories"
helm repo add newrelic-k8s-metrics-adapter https://newrelic.github.io/newrelic-k8s-metrics-adapter > /dev/null
helm repo update > /dev/null
echo "🔄 Installing newrelic-k8s-metrics-adapter"
cat << EOF > ${VALUES_FILE}
global:
licenseKey: ${LICENSE_KEY}
cluster: ${CLUSTER_NAME}
nrStaging: false
personalAPIKey: ${API_KEY}
config:
accountID: ${ACCOUNT_ID}
externalMetrics:
e2e:
query: "SELECT latest(attributeName) FROM (SELECT 0.123 AS 'attributeName' FROM NrUsage)"
removeClusterFilter: true
EOF
helm install --create-namespace \
--namespace newrelic-k8s-metrics-adapter \
newrelic-k8s-metrics-adapter/newrelic-k8s-metrics-adapter \
--generate-name \
--values ${VALUES_FILE} > /dev/null
}
function run_tests() {
echo "🔄 Waiting for metrics adapter to settle"
sleep 30
echo "🔄 Starting E2E tests"
export NEWRELIC_ACCOUNT_ID=${ACCOUNT_ID}
export NEWRELIC_API_KEY=${API_KEY}
export NEWRELIC_CLUSTER_NAME=${CLUSTER_NAME}
export NEWRELIC_REGION=US
make test-e2e
}
function teardown() {
echo "🔄 Teardown"
cleanup
}
function cleanup() {
go clean -testcache > /dev/null
make kind-down > /dev/null 2>&1
docker stop kind-registry > /dev/null 2>&1
docker rm kind-registry > /dev/null 2>&1
rm -f ${KUBECONFIG} ${VALUES_FILE} > /dev/null
}
main "$@"