Subversion Repositories havirt

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
47 rodolico 1
#!/bin/bash
2
 
3
# test_havirt.sh
4
# Comprehensive test script to exercise havirt functionality
5
# This script tests various havirt commands in dry-run mode to ensure they work properly
6
# 
7
# Usage: ./test_havirt.sh [--verbose]
8
#
9
# Copyright 2026 Daily Data, Inc.
10
 
11
# Don't exit on error - we want to catch and report errors
12
set +e
13
 
14
# Colors for output
15
RED='\033[0;31m'
16
GREEN='\033[0;32m'
17
YELLOW='\033[1;33m'
18
BLUE='\033[0;34m'
19
NC='\033[0m' # No Color
20
 
21
# Get script directory (tests/) and parent directory (havirt/)
22
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
23
PARENT_DIR="$( cd "$SCRIPT_DIR/.." && pwd )"
24
HAVIRT="$PARENT_DIR/havirt"
25
 
26
# Test counter
27
TESTS_RUN=0
28
TESTS_PASSED=0
29
TESTS_FAILED=0
30
 
31
# Verbose flag
32
VERBOSE=0
33
if [[ "$1" == "--verbose" || "$1" == "-v" ]]; then
34
    VERBOSE=1
35
fi
36
 
37
# Log function
38
log() {
39
    echo -e "${BLUE}[INFO]${NC} $*"
40
}
41
 
42
log_success() {
43
    echo -e "${GREEN}[PASS]${NC} $*"
44
    ((TESTS_PASSED++))
45
}
46
 
47
log_failure() {
48
    echo -e "${RED}[FAIL]${NC} $*"
49
    ((TESTS_FAILED++))
50
}
51
 
52
log_test() {
53
    echo -e "${YELLOW}[TEST]${NC} $*"
54
    ((TESTS_RUN++))
55
}
56
 
57
# Function to run a test command
58
run_test() {
59
    local test_name="$1"
60
    shift
61
    local cmd="$*"
62
 
63
    log_test "$test_name"
64
 
65
    if [ $VERBOSE -eq 1 ]; then
66
        echo "  Command: $cmd"
67
    fi
68
 
69
    # Run command from parent directory so havirt can find its modules
70
    if output=$(cd "$PARENT_DIR" && eval "$cmd" 2>&1); then
71
        if [ $VERBOSE -eq 1 ]; then
72
            echo "  Output: $output"
73
        fi
74
        log_success "$test_name"
75
        return 0
76
    else
77
        log_failure "$test_name"
78
        echo "  Error output: $output"
79
        return 1
80
    fi
81
}
82
 
83
# Function to test that a command produces output
84
run_test_with_output() {
85
    local test_name="$1"
86
    shift
87
    local cmd="$*"
88
 
89
    log_test "$test_name"
90
 
91
    if [ $VERBOSE -eq 1 ]; then
92
        echo "  Command: $cmd"
93
    fi
94
 
95
    # Run command from parent directory so havirt can find its modules
96
    if output=$(cd "$PARENT_DIR" && eval "$cmd" 2>&1); then
97
        if [ -n "$output" ]; then
98
            if [ $VERBOSE -eq 1 ]; then
99
                echo "  Output: $output"
100
            fi
101
            log_success "$test_name"
102
            return 0
103
        else
104
            log_failure "$test_name (no output produced)"
105
            return 1
106
        fi
107
    else
108
        log_failure "$test_name"
109
        echo "  Error output: $output"
110
        return 1
111
    fi
112
}
113
 
114
echo "=========================================="
115
echo "  havirt Test Suite"
116
echo "=========================================="
117
echo ""
118
 
119
# Check if havirt exists
120
if [ ! -f "$HAVIRT" ]; then
121
    log_failure "havirt executable not found at $HAVIRT"
122
    exit 1
123
fi
124
 
125
log "Using havirt at: $HAVIRT"
126
echo ""
127
 
128
# Section 1: Basic Command Tests
129
echo "=========================================="
130
echo "Section 1: Basic Commands"
131
echo "=========================================="
132
echo ""
133
 
134
run_test "Version flag" "$HAVIRT --version"
135
run_test "Help flag" "$HAVIRT --help"
136
run_test "Help command" "$HAVIRT help"
137
 
138
echo ""
139
 
140
# Section 2: Domain Module Tests
141
echo "=========================================="
142
echo "Section 2: Domain Module"
143
echo "=========================================="
144
echo ""
145
 
146
run_test "Domain help" "$HAVIRT domain help"
147
run_test_with_output "Domain list (screen format)" "$HAVIRT domain list --dryrun"
148
run_test_with_output "Domain list (TSV format)" "$HAVIRT domain list --format tsv --dryrun"
149
run_test "Domain update (dry-run)" "$HAVIRT domain update --dryrun"
150
run_test "Domain update with verbose" "$HAVIRT domain update --dryrun --verbose"
151
 
152
# Test shortcut commands (these should work even without existing domains in dry-run mode)
153
if run_test_with_output "Get domain list" "$HAVIRT domain list --dryrun"; then
154
    # Extract a domain name if one exists
155
    DOMAIN_NAME=$(eval "$HAVIRT domain list --dryrun" 2>/dev/null | awk 'NR>1 && NF>0 {print $1; exit}')
156
 
157
    if [ -n "$DOMAIN_NAME" ]; then
158
        log "Found test domain: $DOMAIN_NAME"
159
        run_test "Domain start (shortcut)" "$HAVIRT start $DOMAIN_NAME --dryrun"
160
        run_test "Domain shutdown (shortcut)" "$HAVIRT shutdown $DOMAIN_NAME --dryrun"
161
        run_test "Domain start (explicit)" "$HAVIRT domain start $DOMAIN_NAME --dryrun"
162
        run_test "Domain shutdown (explicit)" "$HAVIRT domain shutdown $DOMAIN_NAME --dryrun"
163
    else
164
        log "No domains found for shortcut tests (skipping)"
165
    fi
166
fi
167
 
168
echo ""
169
 
170
# Section 3: Node Module Tests
171
echo "=========================================="
172
echo "Section 3: Node Module"
173
echo "=========================================="
174
echo ""
175
 
176
run_test "Node help" "$HAVIRT node help"
177
run_test_with_output "Node list (screen format)" "$HAVIRT node list --dryrun"
178
run_test_with_output "Node list (TSV format)" "$HAVIRT node list --format tsv --dryrun"
179
run_test "Node update" "$HAVIRT node update --dryrun"
180
run_test "Node scan (dry-run)" "$HAVIRT node scan --dryrun"
181
run_test "Node scan with force" "$HAVIRT node scan --force --dryrun"
182
run_test "Node scan with verbose" "$HAVIRT node scan --verbose --dryrun"
183
 
184
# Get a node name for targeted tests
185
if run_test_with_output "Get node list" "$HAVIRT node list --dryrun"; then
186
    NODE_NAME=$(eval "$HAVIRT node list --dryrun" 2>/dev/null | awk 'NR>1 && NF>0 {print $1; exit}')
187
 
188
    if [ -n "$NODE_NAME" ]; then
189
        log "Found test node: $NODE_NAME"
190
        run_test "Node scan specific node" "$HAVIRT node scan --target $NODE_NAME --dryrun"
191
    else
192
        log "No nodes found for targeted tests (skipping)"
193
    fi
194
fi
195
 
196
echo ""
197
 
198
# Section 4: Cluster Module Tests
199
echo "=========================================="
200
echo "Section 4: Cluster Module"
201
echo "=========================================="
202
echo ""
203
 
204
run_test "Cluster help" "$HAVIRT cluster help"
205
run_test_with_output "Cluster status" "$HAVIRT cluster status --dryrun"
206
run_test_with_output "Cluster status (TSV)" "$HAVIRT cluster status --format tsv --dryrun"
207
run_test "Cluster balance (dry-run)" "$HAVIRT cluster balance --dryrun"
208
run_test "Cluster balance with variance" "$HAVIRT cluster balance --variance 0.15 --dryrun"
209
run_test "Cluster iscsi" "$HAVIRT cluster iscsi --dryrun"
210
 
211
echo ""
212
 
213
# Section 5: Flag Combinations
214
echo "=========================================="
215
echo "Section 5: Flag Combinations"
216
echo "=========================================="
217
echo ""
218
 
219
run_test "Multiple verbose flags" "$HAVIRT node list -vv --dryrun"
220
run_test "Debug flag" "$HAVIRT domain list --debug --dryrun"
221
run_test "Quiet flag" "$HAVIRT node list --quiet --dryrun"
222
run_test "Testing flag" "$HAVIRT domain list --testing"
223
run_test "Force flag" "$HAVIRT node scan --force --dryrun"
224
run_test "Bundled flags" "$HAVIRT node list -vvn"
225
 
226
echo ""
227
 
228
# Section 6: Config File Tests
229
echo "=========================================="
230
echo "Section 6: Configuration"
231
echo "=========================================="
232
echo ""
233
 
234
if [ -f "$PARENT_DIR/config.yaml" ]; then
235
    log_success "Config file exists"
236
 
237
    if [ $VERBOSE -eq 1 ]; then
238
        echo "  Config file location: $PARENT_DIR/config.yaml"
239
    fi
240
else
241
    log "Config file not found (will be auto-generated on first run)"
242
fi
243
 
244
if [ -d "$PARENT_DIR/conf" ]; then
245
    CONF_COUNT=$(find "$PARENT_DIR/conf" -name "*.xml" -type f | wc -l)
246
    log_success "Domain config directory exists with $CONF_COUNT XML files"
247
else
248
    log "Domain config directory not found"
249
fi
250
 
251
if [ -f "$PARENT_DIR/var/status.yaml" ]; then
252
    log_success "Status database exists"
253
else
254
    log "Status database not found (will be created on first scan)"
255
fi
256
 
257
echo ""
258
 
259
# Section 7: Error Handling Tests
260
echo "=========================================="
261
echo "Section 7: Error Handling"
262
echo "=========================================="
263
echo ""
264
 
265
# These should fail gracefully
266
log_test "Invalid command"
267
((TESTS_RUN++))
268
if output=$("$HAVIRT" invalidcommand 2>&1); then
269
    log_failure "Invalid command (should have failed)"
270
else
271
    log_success "Invalid command (failed as expected)"
272
fi
273
 
274
log_test "Invalid action"
275
((TESTS_RUN++))
276
if output=$("$HAVIRT" domain invalidaction --dryrun 2>&1); then
277
    log_failure "Invalid action (should have failed)"
278
else
279
    log_success "Invalid action (failed as expected)"
280
fi
281
 
282
echo ""
283
 
284
# Final Summary
285
echo "=========================================="
286
echo "  Test Summary"
287
echo "=========================================="
288
echo ""
289
echo "Total tests run:    $TESTS_RUN"
290
echo -e "Tests passed:       ${GREEN}$TESTS_PASSED${NC}"
291
echo -e "Tests failed:       ${RED}$TESTS_FAILED${NC}"
292
echo ""
293
 
294
if [ $TESTS_FAILED -eq 0 ]; then
295
    echo -e "${GREEN}All tests passed!${NC}"
296
    exit 0
297
else
298
    echo -e "${RED}Some tests failed.${NC}"
299
    exit 1
300
fi