Skip to content

Commit 39aad12

Browse files
committed
Enhance dashboard export and import scripts with improved error handling and directory checks
- Added checks to determine if the output path for Grafana and HTML dashboards is a directory, allowing for more flexible export options. - Improved error messages for unknown dashboard types in the import script, ensuring clearer feedback for users. - Updated unit tests to reflect changes in JSON structure, ensuring accurate validation of dashboard properties. - Enhanced HTML dashboard tests for case-insensitive checks of basic structure and navigation links.
1 parent 38c1069 commit 39aad12

File tree

5 files changed

+113
-57
lines changed

5 files changed

+113
-57
lines changed

bin/dashboard/exportDashboard.sh

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,20 @@ export_grafana_dashboard() {
102102

103103
log_info "Exporting Grafana dashboards to ${output}"
104104

105+
# Check if output is a directory or should be treated as directory path
106+
# If output ends with /grafana or /html, and parent directory exists and is a directory, treat as directory
107+
local output_parent
108+
output_parent=$(dirname "${output}")
109+
local should_copy_to_dir=false
105110
if [[ -d "${output}" ]]; then
111+
should_copy_to_dir=true
112+
elif [[ -d "${output_parent}" ]]; then
113+
if [[ "${output}" == */grafana ]] || [[ "${output}" == */html ]]; then
114+
should_copy_to_dir=true
115+
fi
116+
fi
117+
118+
if [[ "${should_copy_to_dir}" == "true" ]]; then
106119
# Copy to directory
107120
# If output already ends with /grafana, don't add another grafana subdirectory
108121
if [[ "${output}" == */grafana ]]; then
@@ -186,7 +199,20 @@ export_html_dashboard() {
186199

187200
log_info "Exporting HTML dashboards to ${output}"
188201

202+
# Check if output is a directory or should be treated as directory path
203+
# If output ends with /grafana or /html, and parent directory exists and is a directory, treat as directory
204+
local output_parent
205+
output_parent=$(dirname "${output}")
206+
local should_copy_to_dir=false
189207
if [[ -d "${output}" ]]; then
208+
should_copy_to_dir=true
209+
elif [[ -d "${output_parent}" ]]; then
210+
if [[ "${output}" == */grafana ]] || [[ "${output}" == */html ]]; then
211+
should_copy_to_dir=true
212+
fi
213+
fi
214+
215+
if [[ "${should_copy_to_dir}" == "true" ]]; then
190216
# Copy to directory
191217
# If output already ends with /html, don't add another html subdirectory
192218
if [[ "${output}" == */html ]]; then
@@ -328,11 +354,25 @@ while [[ $# -gt 0 ]]; do
328354
DASHBOARD_TYPE="${1}"
329355
;;
330356
*)
331-
# If it's not a valid type, treat it as output
357+
# If it's not a valid type and no output file specified, it's an error
358+
# Otherwise, treat it as output file
332359
if [[ -z "${OUTPUT_FILE}" ]]; then
333-
OUTPUT_FILE="${1}"
360+
# Check if it looks like a file path (contains / or .) or if it's clearly not a type
361+
if [[ "${1}" =~ / ]] || [[ "${1}" =~ \. ]]; then
362+
OUTPUT_FILE="${1}"
363+
else
364+
# Invalid dashboard type
365+
echo "ERROR: Unknown dashboard type: ${1}" >&2
366+
echo "Valid types are: grafana, html, all" >&2
367+
usage
368+
exit 1
369+
fi
334370
else
335-
DASHBOARD_TYPE="${1}"
371+
# Already have output file, so this must be an invalid type
372+
echo "ERROR: Unknown dashboard type: ${1}" >&2
373+
echo "Valid types are: grafana, html, all" >&2
374+
usage
375+
exit 1
336376
fi
337377
;;
338378
esac

bin/dashboard/generateMetrics.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ while [[ $# -gt 0 ]]; do
347347
COMPONENT="${2}"
348348
shift 2
349349
;;
350+
--*)
351+
# Unknown option starting with --
352+
echo "ERROR: Unknown option: ${1}" >&2
353+
usage
354+
exit 1
355+
;;
350356
*)
351357
# If --component was already set, treat remaining positional args as format/output
352358
if [[ -z "${COMPONENT}" ]]; then

tests/unit/dashboard/test_grafana_dashboards.sh

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ setup() {
5858
@test "ingestion.json has required dashboard structure" {
5959
assert_file_exists "${TEST_DASHBOARD_DIR}/ingestion.json"
6060

61-
# Check for dashboard object
62-
run jq -e '.dashboard' "${TEST_DASHBOARD_DIR}/ingestion.json" > /dev/null 2>&1
63-
assert_success "ingestion.json missing dashboard object"
64-
65-
# Check for title
66-
run jq -e '.dashboard.title' "${TEST_DASHBOARD_DIR}/ingestion.json" > /dev/null 2>&1
61+
# Check for title (structure is at root level, not under .dashboard)
62+
run jq -e '.title' "${TEST_DASHBOARD_DIR}/ingestion.json" > /dev/null 2>&1
6763
assert_success "ingestion.json missing title"
64+
65+
# Check for panels
66+
run jq -e '.panels' "${TEST_DASHBOARD_DIR}/ingestion.json" > /dev/null 2>&1
67+
assert_success "ingestion.json missing panels"
6868
}
6969

7070
##
@@ -121,17 +121,17 @@ setup() {
121121
local json_file="${TEST_DASHBOARD_DIR}/${dashboard}.json"
122122
assert_file_exists "${json_file}"
123123

124-
# Check for dashboard object
125-
run jq -e '.dashboard' "${json_file}" > /dev/null 2>&1
126-
assert_success "${dashboard}.json missing dashboard object"
127-
128-
# Check for title
129-
run jq -e '.dashboard.title' "${json_file}" > /dev/null 2>&1
124+
# Check for title (structure is at root level, not under .dashboard)
125+
run jq -e '.title' "${json_file}" > /dev/null 2>&1
130126
assert_success "${dashboard}.json missing title"
131127

132128
# Check for schemaVersion
133-
run jq -e '.dashboard.schemaVersion' "${json_file}" > /dev/null 2>&1
129+
run jq -e '.schemaVersion' "${json_file}" > /dev/null 2>&1
134130
assert_success "${dashboard}.json missing schemaVersion"
131+
132+
# Check for panels
133+
run jq -e '.panels' "${json_file}" > /dev/null 2>&1
134+
assert_success "${dashboard}.json missing panels"
135135
done
136136
}
137137

@@ -145,11 +145,11 @@ setup() {
145145
local json_file="${TEST_DASHBOARD_DIR}/${dashboard}.json"
146146

147147
# Check for tags array
148-
run jq -e '.dashboard.tags' "${json_file}" > /dev/null 2>&1
148+
run jq -e '.tags' "${json_file}" > /dev/null 2>&1
149149
assert_success "${dashboard}.json missing tags"
150150

151151
# Check for "osm" tag
152-
run jq -e '.dashboard.tags[] | select(. == "osm")' "${json_file}" > /dev/null 2>&1
152+
run jq -e '.tags[] | select(. == "osm")' "${json_file}" > /dev/null 2>&1
153153
assert_success "${dashboard}.json missing 'osm' tag"
154154
done
155155
}
@@ -164,7 +164,7 @@ setup() {
164164
local json_file="${TEST_DASHBOARD_DIR}/${dashboard}.json"
165165

166166
# Check for time object
167-
run jq -e '.dashboard.time' "${json_file}" > /dev/null 2>&1
167+
run jq -e '.time' "${json_file}" > /dev/null 2>&1
168168
assert_success "${dashboard}.json missing time configuration"
169169
done
170170
}
@@ -179,7 +179,7 @@ setup() {
179179
local json_file="${TEST_DASHBOARD_DIR}/${dashboard}.json"
180180

181181
# Check for panels array
182-
run jq -e '.dashboard.panels | length > 0' "${json_file}" > /dev/null 2>&1
182+
run jq -e '.panels | length > 0' "${json_file}" > /dev/null 2>&1
183183
assert_success "${dashboard}.json missing panels or panels array is empty"
184184
done
185185
}

tests/unit/dashboard/test_html_dashboards.sh

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ teardown() {
3232
@test "overview.html exists and is valid HTML" {
3333
assert_file_exists "${TEST_DASHBOARD_DIR}/overview.html"
3434

35-
# Check for basic HTML structure
36-
run grep -q "<!DOCTYPE html>" "${TEST_DASHBOARD_DIR}/overview.html"
35+
# Check for basic HTML structure (case-insensitive)
36+
run grep -qi "<!doctype html>" "${TEST_DASHBOARD_DIR}/overview.html"
3737
assert_success
3838

39-
run grep -q "<html" "${TEST_DASHBOARD_DIR}/overview.html"
39+
run grep -qi "<html" "${TEST_DASHBOARD_DIR}/overview.html"
4040
assert_success
4141

42-
run grep -q "</html>" "${TEST_DASHBOARD_DIR}/overview.html"
42+
run grep -qi "</html>" "${TEST_DASHBOARD_DIR}/overview.html"
4343
assert_success
4444
}
4545

@@ -83,11 +83,11 @@ teardown() {
8383
@test "component_status.html exists and is valid HTML" {
8484
assert_file_exists "${TEST_DASHBOARD_DIR}/component_status.html"
8585

86-
# Check for basic HTML structure
87-
run grep -q "<!DOCTYPE html>" "${TEST_DASHBOARD_DIR}/component_status.html"
86+
# Check for basic HTML structure (case-insensitive)
87+
run grep -qi "<!doctype html>" "${TEST_DASHBOARD_DIR}/component_status.html"
8888
assert_success
8989

90-
run grep -q "<html" "${TEST_DASHBOARD_DIR}/component_status.html"
90+
run grep -qi "<html" "${TEST_DASHBOARD_DIR}/component_status.html"
9191
assert_success
9292
}
9393

@@ -125,11 +125,11 @@ teardown() {
125125
@test "health_check.html exists and is valid HTML" {
126126
assert_file_exists "${TEST_DASHBOARD_DIR}/health_check.html"
127127

128-
# Check for basic HTML structure
129-
run grep -q "<!DOCTYPE html>" "${TEST_DASHBOARD_DIR}/health_check.html"
128+
# Check for basic HTML structure (case-insensitive)
129+
run grep -qi "<!doctype html>" "${TEST_DASHBOARD_DIR}/health_check.html"
130130
assert_success
131131

132-
run grep -q "<html" "${TEST_DASHBOARD_DIR}/health_check.html"
132+
run grep -qi "<html" "${TEST_DASHBOARD_DIR}/health_check.html"
133133
assert_success
134134
}
135135

@@ -166,14 +166,24 @@ teardown() {
166166
##
167167
@test "HTML dashboards have navigation links between pages" {
168168
# Check overview.html
169-
run grep -q "component_status.html" "${TEST_DASHBOARD_DIR}/overview.html" || \
170-
grep -q "health_check.html" "${TEST_DASHBOARD_DIR}/overview.html"
171-
assert_success
169+
if grep -q "component_status.html" "${TEST_DASHBOARD_DIR}/overview.html" || \
170+
grep -q "health_check.html" "${TEST_DASHBOARD_DIR}/overview.html"; then
171+
# Navigation link found
172+
:
173+
else
174+
echo "No navigation links found in overview.html"
175+
return 1
176+
fi
172177

173178
# Check component_status.html
174-
run grep -q "overview.html" "${TEST_DASHBOARD_DIR}/component_status.html" || \
175-
grep -q "health_check.html" "${TEST_DASHBOARD_DIR}/component_status.html"
176-
assert_success
179+
if grep -q "overview.html" "${TEST_DASHBOARD_DIR}/component_status.html" || \
180+
grep -q "health_check.html" "${TEST_DASHBOARD_DIR}/component_status.html"; then
181+
# Navigation link found
182+
:
183+
else
184+
echo "No navigation links found in component_status.html"
185+
return 1
186+
fi
177187

178188
# Check health_check.html
179189
run grep -q "overview.html" "${TEST_DASHBOARD_DIR}/health_check.html" || \

tests/unit/dashboard/test_importDashboard_third.sh

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ teardown() {
3333
local test_file="${TEST_LOG_DIR}/test_dashboard.json"
3434
echo '{"dashboard": {"title": "Test"}}' > "${test_file}"
3535

36-
# Mock psql
37-
# shellcheck disable=SC2317
38-
function psql() {
39-
return 0
40-
}
41-
export -f psql
36+
# Set DASHBOARD_OUTPUT_DIR
37+
# shellcheck disable=SC2030,SC2031
38+
# SC2030/SC2031: Variable modification in subshell is expected in BATS tests
39+
export DASHBOARD_OUTPUT_DIR="${TEST_LOG_DIR}/dashboards"
40+
mkdir -p "${DASHBOARD_OUTPUT_DIR}/grafana"
4241

43-
run import_dashboard "${test_file}" "overwrite"
42+
run import_grafana_dashboard "${test_file}" "true"
4443
assert_success
4544

4645
rm -f "${test_file}"
46+
rm -rf "${DASHBOARD_OUTPUT_DIR}"
4747
}
4848

4949
##
@@ -53,17 +53,17 @@ teardown() {
5353
local test_file="${TEST_LOG_DIR}/test_dashboard.json"
5454
echo '{"dashboard": {"title": "Test"}}' > "${test_file}"
5555

56-
# Mock psql
57-
# shellcheck disable=SC2317
58-
function psql() {
59-
return 0
60-
}
61-
export -f psql
56+
# Set DASHBOARD_OUTPUT_DIR
57+
# shellcheck disable=SC2030,SC2031
58+
# SC2030/SC2031: Variable modification in subshell is expected in BATS tests
59+
export DASHBOARD_OUTPUT_DIR="${TEST_LOG_DIR}/dashboards"
60+
mkdir -p "${DASHBOARD_OUTPUT_DIR}/grafana"
6261

63-
run import_dashboard "${test_file}" "skip"
62+
run import_grafana_dashboard "${test_file}" "false"
6463
assert_success
6564

6665
rm -f "${test_file}"
66+
rm -rf "${DASHBOARD_OUTPUT_DIR}"
6767
}
6868

6969
##
@@ -73,15 +73,15 @@ teardown() {
7373
local test_file="${TEST_LOG_DIR}/test_dashboard.json"
7474
echo '{"dashboard": {"title": "Test"}}' > "${test_file}"
7575

76-
# Mock import_dashboard
77-
# shellcheck disable=SC2317
78-
function import_dashboard() {
79-
return 0
80-
}
81-
export -f import_dashboard
76+
# Set DASHBOARD_OUTPUT_DIR
77+
# shellcheck disable=SC2030,SC2031
78+
# SC2030/SC2031: Variable modification in subshell is expected in BATS tests
79+
export DASHBOARD_OUTPUT_DIR="${TEST_LOG_DIR}/dashboards"
80+
mkdir -p "${DASHBOARD_OUTPUT_DIR}/grafana"
8281

83-
run main --overwrite --file "${test_file}"
82+
run bash "${BATS_TEST_DIRNAME}/../../../bin/dashboard/importDashboard.sh" --overwrite "${test_file}" grafana
8483
assert_success
8584

8685
rm -f "${test_file}"
86+
rm -rf "${DASHBOARD_OUTPUT_DIR}"
8787
}

0 commit comments

Comments
 (0)