-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo
executable file
·117 lines (98 loc) · 3.13 KB
/
go
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
#!/bin/bash
set -e
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "${SCRIPT_DIR}"
function error {
echo -e >&2 "\033[31m${1}\033[0m";
exit 1;
}
function notice {
echo $2 -e >&2 "\033[33m${1}\033[0m";
}
function ensure_env {
command -v node >/dev/null 2>&1 || error "Please install nodejs"
command -v npm >/dev/null 2>&1 || error "Please install npm"
if [[ ! -d "./node_modules" ]]; then
npm install
fi
}
function cmd-cli {
./node_modules/.bin/tsc
node dist/src/cli.js "$@"
}
function cmd-docker-start {
command -v docker >/dev/null 2>&1 || error "Please install docker"
command -v docker-compose >/dev/null 2>&1 || error "Please install docker-compose"
(cd docker && docker-compose up -d) || error "Failed to start docker test environment"
}
function cmd-docker-create-api-token {
# TODO: error handling
curl -sS -X POST -H "Content-Type: application/json" -d "{\"name\":\"key$(date +%s)\", \"role\": \"Admin\"}" http://admin:password@${GRAFANA_HOST:-localhost:3000}/api/auth/keys | jq -r .key
}
function cmd-docker-create-datasource {
# TODO: error handling
curl -sS -X POST -H "Content-Type: application/json" -d '{"name": "Yay Prometheus", "type": "prometheus", "access": "proxy", "url": "http://prometheus:9090"}' http://admin:password@${GRAFANA_HOST:-localhost:3000}/api/datasources 2>&1 1>/dev/null
}
function run-grafana-tests {
for file in $(ls dist/dashboards/0*.js); do
run-single-test "$file"
done
run-single-test "dist/dashboards/outside-context-test.js" environment=preview
}
function run-single-test {
local file="$1"
shift
local additionalArguments="$@"
notice "Deploying '$file' " -n
result="$(node dist/src/cli.js --host "${GRAFANA_HOST}" --no-ssl $file $additionalArguments)"
status=$(echo "$result" | jq .status)
if [[ "$status" == "\"success\"" ]]; then
echo "✅"
else
echo "❌"
echo $result
exit 1
fi
}
function cmd-docker-test {
cmd-docker-start
rm -rf dist/
tsc
export GRAFANA_HOST="localhost:3000"
export GRAFANA_API_TOKEN="$(cmd-docker-create-api-token)"
cmd-docker-create-datasource || true
run-grafana-tests
}
function cmd-ci-test {
npm install
npm run build
npm run test
export GRAFANA_HOST="grafana:3000"
export GRAFANA_API_TOKEN="$(cmd-docker-create-api-token)"
run-grafana-tests
}
function cmd-usage {
cat <<-eof
./go [command] [options]
cli - starts the TypedGrafana CLI (-h for help)
docker-start - starts a Grafana / Prometheus test environment
docker-test - starts the integration tests that run against the test environment
docker-create-api-token
docker-create-datasource
eof
}
ensure_env
command=""
if (( $# > 0 )); then
command="${1}"
shift
fi
case "${command}" in
docker-start) cmd-docker-start ;;
docker-test) cmd-docker-test ;;
docker-create-api-token) cmd-docker-create-api-token ;;
docker-create-datasource) cmd-docker-create-datasource ;;
ci-test) cmd-ci-test ;;
cli) cmd-cli "${command}" "$@" ;;
*) cmd-usage ;;
esac