#!/bin/bash # End-to-end tests for basic_graph example # Tests CLI vs Service build consistency set -euo pipefail # Get the directory of this script SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Source utilities source "$SCRIPT_DIR/lib/test_utils.sh" source "$SCRIPT_DIR/lib/db_utils.sh" source "$SCRIPT_DIR/lib/service_utils.sh" # Test configuration TEST_NAME="basic_graph_e2e" CLI_BUILD_BINARY="${1:-}" SERVICE_BINARY="${2:-}" # Validate inputs if [[ -z "$CLI_BUILD_BINARY" ]]; then test_fail "CLI build binary path required as first argument" fi if [[ -z "$SERVICE_BINARY" ]]; then test_fail "Service binary path required as second argument" fi if [[ ! -x "$CLI_BUILD_BINARY" ]]; then test_fail "CLI build binary not found or not executable: $CLI_BUILD_BINARY" fi if [[ ! -x "$SERVICE_BINARY" ]]; then test_fail "Service binary not found or not executable: $SERVICE_BINARY" fi # Setup test environment TEST_DIR=$(setup_test_env "$TEST_NAME") CLI_DB_PATH=$(create_test_database "cli_test_db") SERVICE_DB_PATH=$(create_test_database "service_test_db") # Cleanup function cleanup() { if [[ -n "${SERVICE_INFO:-}" ]]; then stop_test_service "$SERVICE_INFO" || true fi cleanup_test_dir "$TEST_DIR" || true } trap cleanup EXIT log_info "Starting Basic Graph end-to-end tests" log_info "CLI Binary: $CLI_BUILD_BINARY" log_info "Service Binary: $SERVICE_BINARY" log_info "Test Directory: $TEST_DIR" # Test 1: Single Partition Build test_single_partition() { log_info "=== Test 1: Single Partition Build ===" local partition="generated_number/pippin" local cli_output="$TEST_DIR/cli_single.out" local service_output="$TEST_DIR/service_single.out" # CLI Build log_info "Running CLI build for partition: $partition" export DATABUILD_BUILD_EVENT_LOG="sqlite:///$CLI_DB_PATH" if ! run_with_timeout 60 "$CLI_BUILD_BINARY" "$partition" > "$cli_output" 2>&1; then log_error "CLI build failed for partition: $partition" cat "$cli_output" return 1 fi # Service Build log_info "Running Service build for partition: $partition" SERVICE_INFO=$(start_test_service "$SERVICE_BINARY" "$SERVICE_DB_PATH") if ! execute_service_build "$SERVICE_INFO" "[\"$partition\"]" 60; then log_error "Service build failed for partition: $partition" return 1 fi stop_test_service "$SERVICE_INFO" unset SERVICE_INFO # Compare results log_info "Comparing CLI and Service build results" # Check that both databases have events local cli_events=$(count_build_events "$CLI_DB_PATH") local service_events=$(count_build_events "$SERVICE_DB_PATH") if [[ "$cli_events" -eq 0 ]]; then log_error "No CLI build events found" return 1 fi if [[ "$service_events" -eq 0 ]]; then log_error "No Service build events found" return 1 fi # Check that partition was built in both if ! is_partition_built "$CLI_DB_PATH" "$partition"; then log_error "Partition $partition was not built via CLI" return 1 fi if ! is_partition_built "$SERVICE_DB_PATH" "$partition"; then log_error "Partition $partition was not built via Service" return 1 fi test_pass "Single partition build test" } # Test 2: Multiple Partition Build test_multiple_partitions() { log_info "=== Test 2: Multiple Partition Build ===" local partitions=("generated_number/pippin" "generated_number/salem" "generated_number/sadie") local partitions_json='["generated_number/pippin", "generated_number/salem", "generated_number/sadie"]' local cli_output="$TEST_DIR/cli_multiple.out" local service_output="$TEST_DIR/service_multiple.out" # Clear previous events clear_build_events "$CLI_DB_PATH" clear_build_events "$SERVICE_DB_PATH" # CLI Build log_info "Running CLI build for multiple partitions: ${partitions[*]}" export DATABUILD_BUILD_EVENT_LOG="sqlite:///$CLI_DB_PATH" if ! run_with_timeout 120 "$CLI_BUILD_BINARY" "${partitions[@]}" > "$cli_output" 2>&1; then log_error "CLI build failed for multiple partitions" cat "$cli_output" return 1 fi # Service Build log_info "Running Service build for multiple partitions" SERVICE_INFO=$(start_test_service "$SERVICE_BINARY" "$SERVICE_DB_PATH") if ! execute_service_build "$SERVICE_INFO" "$partitions_json" 120; then log_error "Service build failed for multiple partitions" return 1 fi stop_test_service "$SERVICE_INFO" unset SERVICE_INFO # Compare results log_info "Comparing CLI and Service build results for multiple partitions" # Check that all partitions were built in both for partition in "${partitions[@]}"; do if ! is_partition_built "$CLI_DB_PATH" "$partition"; then log_error "Partition $partition was not built via CLI" return 1 fi if ! is_partition_built "$SERVICE_DB_PATH" "$partition"; then log_error "Partition $partition was not built via Service" return 1 fi done # Check event counts are similar (within reasonable range) local cli_events=$(count_build_events "$CLI_DB_PATH") local service_events=$(count_build_events "$SERVICE_DB_PATH") if [[ $((cli_events - service_events)) -gt 2 ]] || [[ $((service_events - cli_events)) -gt 2 ]]; then log_warn "Event counts differ significantly: CLI=$cli_events, Service=$service_events" fi test_pass "Multiple partition build test" } # Test 3: Sum Partition Build (with dependencies) test_sum_partition() { log_info "=== Test 3: Sum Partition Build (with dependencies) ===" local sum_partition="sum/pippin_salem_sadie" local cli_output="$TEST_DIR/cli_sum.out" local service_output="$TEST_DIR/service_sum.out" # Clear previous events clear_build_events "$CLI_DB_PATH" clear_build_events "$SERVICE_DB_PATH" # CLI Build log_info "Running CLI build for sum partition: $sum_partition" export DATABUILD_BUILD_EVENT_LOG="sqlite:///$CLI_DB_PATH" if ! run_with_timeout 180 "$CLI_BUILD_BINARY" "$sum_partition" > "$cli_output" 2>&1; then log_error "CLI build failed for sum partition" cat "$cli_output" return 1 fi # Service Build log_info "Running Service build for sum partition" SERVICE_INFO=$(start_test_service "$SERVICE_BINARY" "$SERVICE_DB_PATH") if ! execute_service_build "$SERVICE_INFO" "[\"$sum_partition\"]" 180; then log_error "Service build failed for sum partition" return 1 fi stop_test_service "$SERVICE_INFO" unset SERVICE_INFO # Compare results log_info "Comparing CLI and Service build results for sum partition" # Check that sum partition was built if ! is_partition_built "$CLI_DB_PATH" "$sum_partition"; then log_error "Sum partition $sum_partition was not built via CLI" return 1 fi if ! is_partition_built "$SERVICE_DB_PATH" "$sum_partition"; then log_error "Sum partition $sum_partition was not built via Service" return 1 fi # Check that dependencies were also built local dependencies=("generated_number/pippin" "generated_number/salem" "generated_number/sadie") for dep in "${dependencies[@]}"; do if ! is_partition_built "$CLI_DB_PATH" "$dep"; then log_error "Dependency partition $dep was not built via CLI" return 1 fi if ! is_partition_built "$SERVICE_DB_PATH" "$dep"; then log_error "Dependency partition $dep was not built via Service" return 1 fi done test_pass "Sum partition build test" } # Test 4: Event Comparison test_event_comparison() { log_info "=== Test 4: Build Event Comparison ===" # Use fresh databases for this test local cli_db_events="$(create_test_database "cli_events_test")" local service_db_events="$(create_test_database "service_events_test")" local partition="generated_number/pippin" local cli_output="$TEST_DIR/cli_events.out" # CLI Build export DATABUILD_BUILD_EVENT_LOG="sqlite:///$cli_db_events" if ! run_with_timeout 60 "$CLI_BUILD_BINARY" "$partition" > "$cli_output" 2>&1; then log_error "CLI build failed for event comparison test" return 1 fi # Service Build SERVICE_INFO=$(start_test_service "$SERVICE_BINARY" "$service_db_events") if ! execute_service_build "$SERVICE_INFO" "[\"$partition\"]" 60; then log_error "Service build failed for event comparison test" return 1 fi stop_test_service "$SERVICE_INFO" unset SERVICE_INFO # Extract and compare events local cli_events_file="$TEST_DIR/cli_events.json" local service_events_file="$TEST_DIR/service_events.json" get_partition_events "$cli_db_events" "$partition" "$cli_events_file" get_partition_events "$service_db_events" "$partition" "$service_events_file" # Basic validation - both should have some events local cli_event_count=$(count_lines "$cli_events_file") local service_event_count=$(count_lines "$service_events_file") if [[ "$cli_event_count" -eq 0 ]]; then log_error "No CLI events found for partition $partition" return 1 fi if [[ "$service_event_count" -eq 0 ]]; then log_error "No Service events found for partition $partition" return 1 fi # Detailed event count validation (matching simple_test.sh approach) log_info "Performing detailed event count validation..." local cli_total_events=$(count_build_events "$cli_db_path") local service_total_events=$(count_build_events "$service_db_path") log_info "Total events: CLI=$cli_total_events, Service=$service_total_events" # Count events by type using the same approach as simple_test.sh local cli_job_events=$(sqlite3 "$cli_db_path" "SELECT COUNT(*) FROM build_events WHERE event_type = 'job';" 2>/dev/null || echo "0") local cli_partition_events=$(sqlite3 "$cli_db_path" "SELECT COUNT(*) FROM build_events WHERE event_type = 'partition';" 2>/dev/null || echo "0") local cli_request_events=$(sqlite3 "$cli_db_path" "SELECT COUNT(*) FROM build_events WHERE event_type = 'build_request';" 2>/dev/null || echo "0") local service_job_events=$(sqlite3 "$service_db_path" "SELECT COUNT(*) FROM build_events WHERE event_type = 'job';" 2>/dev/null || echo "0") local service_partition_events=$(sqlite3 "$service_db_path" "SELECT COUNT(*) FROM build_events WHERE event_type = 'partition';" 2>/dev/null || echo "0") local service_request_events=$(sqlite3 "$service_db_path" "SELECT COUNT(*) FROM build_events WHERE event_type = 'build_request';" 2>/dev/null || echo "0") log_info "Event breakdown:" log_info " Job events: CLI=$cli_job_events, Service=$service_job_events" log_info " Partition events: CLI=$cli_partition_events, Service=$service_partition_events" log_info " Request events: CLI=$cli_request_events, Service=$service_request_events" # Validate core events are identical (job, partition, and request events should all match now) if [[ "$cli_job_events" -eq "$service_job_events" ]] && [[ "$cli_partition_events" -eq "$service_partition_events" ]] && [[ "$cli_request_events" -eq "$service_request_events" ]]; then log_info "✅ All build events (job, partition, and request) are identical" else log_error "❌ Build events differ between CLI and Service - this indicates a problem" log_error "Expected CLI and Service to emit identical event counts after alignment" return 1 fi # Validate total event counts are identical if [[ "$cli_total_events" -eq "$service_total_events" ]]; then log_info "✅ Total event counts are identical: $cli_total_events events each" else log_error "❌ Total event counts differ: CLI=$cli_total_events, Service=$service_total_events" return 1 fi test_pass "Event comparison test" } # Run all tests main() { log_info "Starting Basic Graph End-to-End Tests" test_single_partition test_multiple_partitions test_sum_partition test_event_comparison log_info "All Basic Graph tests completed successfully!" } # Execute main function main "$@"