summaryrefslogtreecommitdiff
path: root/apps/test-framework/helpers/test_common.sh
blob: 67193cacbb94adef8296057710f1e46fde8798bc (plain)
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
#!/usr/bin/env bash

# AzerothCore Test Common Utilities
# Shared functions and setup for all BATS tests

export AC_TEST_FRAMEWORK_VERSION="1.0.0"

# Get paths
AC_TEST_FRAMEWORK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
AC_PROJECT_ROOT="$(cd "$AC_TEST_FRAMEWORK_DIR/../.." && pwd)"

# Common test environment setup
setup_test_env() {
    export TEST_DIR="$(mktemp -d)"
    export AC_TEST_ROOT="$AC_PROJECT_ROOT"
    export AC_TEST_APPS="$AC_TEST_ROOT/apps"
    
    # Create standard test directory structure
    mkdir -p "$TEST_DIR"/{bin,etc,logs,data,crashes,build}
    
    # Set up test-specific environment variables
    export ORIGINAL_PATH="$PATH"
    export PATH="$TEST_DIR/bin:$PATH"
    
    # Common environment variables for AzerothCore
    export BUILDPATH="$TEST_DIR/build"
    export SRCPATH="$AC_TEST_ROOT"
    export BINPATH="$TEST_DIR/bin"
    export LOGS_PATH="$TEST_DIR/logs"
}

cleanup_test_env() {
    if [[ -n "$TEST_DIR" && -d "$TEST_DIR" ]]; then
        rm -rf "$TEST_DIR"
    fi
    if [[ -n "$ORIGINAL_PATH" ]]; then
        export PATH="$ORIGINAL_PATH"
    fi
}

# Create standard test binary
create_test_binary() {
    local binary_name="$1"
    local exit_code="${2:-0}"
    local runtime="${3:-2}"
    local extra_output="${4:-""}"
    
    cat > "$TEST_DIR/bin/$binary_name" << EOF
#!/usr/bin/env bash
echo "$binary_name starting with config: \$2"
echo "$binary_name running for $runtime seconds..."
if [[ -n "$extra_output" ]]; then
    echo "$extra_output"
fi
sleep $runtime
echo "$binary_name exiting with code $exit_code"
exit $exit_code
EOF
    chmod +x "$TEST_DIR/bin/$binary_name"
}

# Create test configuration file
create_test_config() {
    local config_name="$1"
    local content="$2"
    
    cat > "$TEST_DIR/etc/$config_name" << EOF
# Test configuration file: $config_name
# Generated by AzerothCore test framework
$content
EOF
}

# Create AzerothCore specific test binaries
create_acore_binaries() {
    create_test_binary "authserver" 0 1 "AuthServer initialized"
    create_test_binary "worldserver" 0 2 "WorldServer initialized"
    create_test_binary "cmake" 0 1 "CMake configured"
    create_test_binary "make" 0 2 "Build completed"
    create_test_binary "mapextractor" 0 3 "Map extraction completed"
    create_test_binary "vmap4extractor" 0 2 "VMap extraction completed"
    create_test_binary "vmap4assembler" 0 1 "VMap assembly completed"
    create_test_binary "mmaps_generator" 0 5 "MMap generation completed"
}

# Create AzerothCore specific test configs
create_acore_configs() {
    create_test_config "authserver.conf" 'Database.Info = "127.0.0.1;3306;acore;acore;acore_auth"
LoginDatabaseInfo = "127.0.0.1;3306;acore;acore;acore_auth"'
    
    create_test_config "worldserver.conf" 'Database.Info = "127.0.0.1;3306;acore;acore;acore_world"
LoginDatabaseInfo = "127.0.0.1;3306;acore;acore;acore_auth"
CharacterDatabaseInfo = "127.0.0.1;3306;acore;acore;acore_characters"'
    
    create_test_config "config.sh" "export BUILDPATH=\"$TEST_DIR/build\"
export SRCPATH=\"$AC_TEST_ROOT\"
export BINPATH=\"$TEST_DIR/bin\"
export LOGS_PATH=\"$TEST_DIR/logs\""
}

# Create a test script configuration (for startup scripts)
create_test_script_config() {
    local script_name="$1"
    local binary_name="${2:-authserver}"
    
    cat > "$TEST_DIR/conf-$script_name.sh" << EOF
export BINPATH="$TEST_DIR/bin"
export SERVERBIN="$binary_name"
export CONFIG="$TEST_DIR/etc/$binary_name.conf"
export LOGS_PATH="$TEST_DIR/logs"
export LOG_PREFIX_NAME="$script_name"
export SCREEN_NAME="AC-$script_name"
export GDB_ENABLED=0
export WITH_CONSOLE=1
EOF
}

# Debug helper function
debug_on_failure() {
    if [[ "$status" -ne 0 ]]; then
        echo "Command failed with status: $status" >&3
        echo "Output was:" >&3
        echo "$output" >&3
        if [[ -n "$TEST_DIR" ]]; then
            echo "Test directory contents:" >&3
            ls -la "$TEST_DIR" >&3 2>/dev/null || true
        fi
    fi
}

# Print test environment info
print_test_env() {
    echo "Test Environment:" >&3
    echo "  TEST_DIR: $TEST_DIR" >&3
    echo "  AC_TEST_ROOT: $AC_TEST_ROOT" >&3
    echo "  AC_TEST_APPS: $AC_TEST_APPS" >&3
    echo "  PATH: $PATH" >&3
}

# Check if running in test mode
is_test_mode() {
    [[ -n "$BATS_TEST_FILENAME" ]] || [[ -n "$TEST_DIR" ]]
}