Skip to content

Commit 91dab26

Browse files
tip-dtellerclaude
andcommitted
fix: resolve test script issues and improve error handling
- Fix cluster-lifecycle.sh test by removing invalid --preserve-existing flag from destroy command * infra destroy doesn't support --preserve-existing (only infra apply does) * Update documentation text to clarify actual safety mechanism * Default behavior already only destroys resources defined in YAML configurations - Fix database-operations.sh unbound variable errors and improve authentication * Resolve $1 unbound variable error by using ${1:-all} pattern * Add user existence validation before attempting username/password authentication * Improve error messages and user feedback for missing manual credentials * Enhanced robustness when manual database users aren't configured Both scripts now run successfully with source .env from project root directory. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 96cc888 commit 91dab26

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

scripts/test/cluster-lifecycle.sh

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ test_yaml_cluster_lifecycle() {
309309
local config_file="$TEST_REPORTS_DIR/yaml-cluster-config.yaml"
310310

311311
print_info "Testing YAML cluster lifecycle with cluster: $cluster_name"
312-
print_success "✓ SAFE MODE: Using --preserve-existing to protect existing clusters"
312+
print_success "✓ SAFE MODE: Only managing resources defined in YAML configurations"
313313

314314
# Get project name for proper YAML configuration
315315
local project_name
@@ -470,11 +470,10 @@ EOF
470470
sleep 3
471471

472472
print_info "Destroying cluster and users..."
473-
print_info "Using --preserve-existing to protect any existing clusters"
473+
print_info "Only destroying resources defined in YAML configuration (existing clusters protected)"
474474

475475
if "$PROJECT_ROOT/matlas" infra destroy -f "$config_file" \
476476
--project-id "$ATLAS_PROJECT_ID" \
477-
--preserve-existing \
478477
--auto-approve; then
479478

480479
print_success "Infrastructure destroy initiated"
@@ -1263,7 +1262,7 @@ run_cluster_lifecycle_tests() {
12631262

12641263
print_header "MongoDB Atlas Cluster Lifecycle Tests"
12651264
print_warning "⚠️ WARNING: These tests create real Atlas clusters and may incur costs!"
1266-
print_success "✓ SAFE MODE: Tests now use --preserve-existing to protect existing clusters"
1265+
print_success "✓ SAFE MODE: Tests only manage resources defined in configurations"
12671266
print_info "Tests only manage resources they create - existing resources are preserved"
12681267
echo
12691268

@@ -1337,7 +1336,7 @@ show_usage() {
13371336
echo
13381337
echo "Commands:"
13391338
echo " cli Run CLI-based cluster lifecycle tests only"
1340-
echo " yaml Run YAML-based cluster lifecycle tests (now SAFE with --preserve-existing)"
1339+
echo " yaml Run YAML-based cluster lifecycle tests (SAFE - only manages defined resources)"
13411340
echo " yaml-existing Run YAML test with existing clusters (safe individual resources)"
13421341
echo " yaml-multi Run YAML multi-cluster test (clean project scenario)"
13431342
echo " yaml-partial Run YAML partial removal test (add 2, remove 1)"
@@ -1360,14 +1359,14 @@ show_usage() {
13601359
echo " ATLAS_ORG_ID Atlas organization ID"
13611360
echo
13621361
echo "Examples:"
1363-
echo " $0 # Run basic safe tests (CLI + YAML with --preserve-existing)"
1362+
echo " $0 # Run basic safe tests (CLI + YAML - only manages defined resources)"
13641363
echo " $0 comprehensive # Run all test scenarios (recommended)"
13651364
echo " $0 yaml-existing # Test YAML with existing clusters (safe)"
13661365
echo " $0 yaml-multi # Test YAML multi-cluster management"
13671366
echo " $0 yaml-partial # Test YAML partial removal"
13681367
echo " $0 cli # Test CLI approach only"
13691368
echo ""
1370-
echo "SAFETY: All YAML tests now use --preserve-existing to protect existing resources"
1369+
echo "SAFETY: All tests only manage resources they define - existing resources are protected"
13711370
}
13721371

13731372
# Main execution

scripts/test/database-operations.sh

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,22 @@ test_all_authentication_methods() {
148148

149149
# Test 2: --username/--password authentication (if available)
150150
if [[ -n "$TEST_DATABASE_USER" && -n "$TEST_DATABASE_PASSWORD" ]]; then
151+
print_info "Found manual database credentials - testing username/password authentication"
151152
print_subheader "Test 2: Database creation with --username/--password"
152153
local test_db2="${test_db_base}-userpass"
153154

154155
print_info "Testing database creation with user credentials: $TEST_DATABASE_USER"
155156

156-
if "$PROJECT_ROOT/matlas" database create "$test_db2" \
157+
# First verify the database user exists and has permissions
158+
print_info "Verifying manual database user exists and has permissions..."
159+
if ! "$PROJECT_ROOT/matlas" atlas users list --project-id "$ATLAS_PROJECT_ID" 2>/dev/null | grep -q "$TEST_DATABASE_USER"; then
160+
print_warning "Manual database user '$TEST_DATABASE_USER' not found in Atlas project"
161+
print_info "Skipping username/password authentication test"
162+
print_info "To test this method, create a database user in Atlas with readWriteAnyDatabase permissions"
163+
else
164+
print_success "Manual database user found in Atlas project"
165+
166+
if "$PROJECT_ROOT/matlas" database create "$test_db2" \
157167
--cluster "$TEST_CLUSTER_NAME" \
158168
--project-id "$ATLAS_PROJECT_ID" \
159169
--collection "$test_collection" \
@@ -175,9 +185,10 @@ test_all_authentication_methods() {
175185
else
176186
print_warning "Database not immediately visible (propagation delay expected)"
177187
fi
178-
else
179-
print_error "Failed to create database with --username/--password"
180-
((auth_failures++))
188+
else
189+
print_error "Failed to create database with --username/--password"
190+
((auth_failures++))
191+
fi
181192
fi
182193
else
183194
print_info "Skipping --username/--password test (no manual credentials provided)"
@@ -964,14 +975,14 @@ show_usage() {
964975
main() {
965976
case "${1:-all}" in
966977
"auth"|"failures"|"databases"|"collections"|"indexes"|"yaml"|"workflow"|"comprehensive"|"all")
967-
run_database_operations_tests "$1"
978+
run_database_operations_tests "${1:-all}"
968979
;;
969980
"-h"|"--help"|"help")
970981
show_usage
971982
exit 0
972983
;;
973984
*)
974-
echo "Unknown command: $1"
985+
echo "Unknown command: ${1:-}"
975986
echo
976987
show_usage
977988
exit 1

0 commit comments

Comments
 (0)