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_safe.sh
4
# Safe test script that exercises havirt in read-only mode
5
# This script only tests commands that don't require SSH connections
6
# 
7
# Usage: ./test_havirt_safe.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
log_skip() {
58
    echo -e "${BLUE}[SKIP]${NC} $*"
59
}
60
 
61
# Function to run a test command
62
run_test() {
63
    local test_name="$1"
64
    shift
65
    local cmd="$*"
66
 
67
    log_test "$test_name"
68
 
69
    if [ $VERBOSE -eq 1 ]; then
70
        echo "  Command: $cmd"
71
    fi
72
 
73
    # Run command from parent directory so havirt can find its modules
74
    if output=$(cd "$PARENT_DIR" && eval "$cmd" 2>&1); then
75
        if [ $VERBOSE -eq 1 ]; then
76
            echo "  Output: $output"
77
        fi
78
        log_success "$test_name"
79
        return 0
80
    else
81
        log_failure "$test_name"
82
        echo "  Error output: $output"
83
        return 1
84
    fi
85
}
86
 
87
# Function to test that a command produces output
88
run_test_with_output() {
89
    local test_name="$1"
90
    shift
91
    local cmd="$*"
92
 
93
    log_test "$test_name"
94
 
95
    if [ $VERBOSE -eq 1 ]; then
96
        echo "  Command: $cmd"
97
    fi
98
 
99
    # Run command from parent directory so havirt can find its modules
100
    if output=$(cd "$PARENT_DIR" && eval "$cmd" 2>&1); then
101
        if [ -n "$output" ]; then
102
            if [ $VERBOSE -eq 1 ]; then
103
                echo "  Output: $output"
104
            fi
105
            log_success "$test_name"
106
            return 0
107
        else
108
            log_failure "$test_name (no output produced)"
109
            return 1
110
        fi
111
    else
112
        log_failure "$test_name"
113
        echo "  Error output: $output"
114
        return 1
115
    fi
116
}
117
 
118
echo "=========================================="
119
echo "  havirt Safe Test Suite"
120
echo "  (Read-only operations)"
121
echo "=========================================="
122
echo ""
123
 
124
# Check if havirt exists
125
if [ ! -f "$HAVIRT" ]; then
126
    log_failure "havirt executable not found at $HAVIRT"
127
    exit 1
128
fi
129
 
130
log "Using havirt at: $HAVIRT"
131
echo ""
132
 
133
# Section 1: Basic Command Tests
134
echo "=========================================="
135
echo "Section 1: Basic Commands"
136
echo "=========================================="
137
echo ""
138
 
139
run_test "Version flag (--version)" "$HAVIRT --version"
140
run_test "Version flag (-V)" "$HAVIRT -V"
141
run_test "Help flag (--help)" "$HAVIRT --help"
142
run_test "Help flag (-h)" "$HAVIRT -h"
143
run_test "Help command" "$HAVIRT help"
144
 
145
echo ""
146
 
147
# Section 2: Domain Module Tests (Read-only)
148
echo "=========================================="
149
echo "Section 2: Domain Module (Read-only)"
150
echo "=========================================="
151
echo ""
152
 
153
run_test "Domain help" "$HAVIRT domain help"
154
run_test "Domain with no action (should show help)" "$HAVIRT domain"
155
run_test_with_output "Domain list (screen format)" "$HAVIRT domain list --testing"
156
run_test_with_output "Domain list (TSV format)" "$HAVIRT domain list --format tsv --testing"
157
run_test_with_output "Domain list (TSV shorthand)" "$HAVIRT domain list -f tsv --testing"
158
run_test "Domain list with quiet flag" "$HAVIRT domain list --quiet --testing"
159
run_test "Domain list with verbose" "$HAVIRT domain list --verbose --testing"
160
run_test "Domain list with multiple verbose" "$HAVIRT domain list -vv --testing"
161
 
162
echo ""
163
 
164
# Section 3: Node Module Tests (Read-only)
165
echo "=========================================="
166
echo "Section 3: Node Module (Read-only)"
167
echo "=========================================="
168
echo ""
169
 
170
run_test "Node help" "$HAVIRT node help"
171
run_test "Node with no action (should show help)" "$HAVIRT node"
172
run_test_with_output "Node list (screen format)" "$HAVIRT node list --testing"
173
run_test_with_output "Node list (TSV format)" "$HAVIRT node list --format tsv --testing"
174
run_test_with_output "Node list (TSV shorthand)" "$HAVIRT node list -f tsv --testing"
175
run_test "Node list with quiet flag" "$HAVIRT node list --quiet --testing"
176
run_test "Node list with verbose" "$HAVIRT node list --verbose --testing"
177
run_test "Node list with bundled flags" "$HAVIRT node list -vv --testing"
178
run_test "Node scan with testing flag" "$HAVIRT node scan --testing"
179
 
180
echo ""
181
 
182
# Section 4: Cluster Module Tests (Read-only)
183
echo "=========================================="
184
echo "Section 4: Cluster Module (Read-only)"
185
echo "=========================================="
186
echo ""
187
 
188
run_test "Cluster help" "$HAVIRT cluster help"
189
run_test "Cluster with no action (should show help)" "$HAVIRT cluster"
190
run_test_with_output "Cluster status (screen)" "$HAVIRT cluster status --testing"
191
run_test_with_output "Cluster status (TSV)" "$HAVIRT cluster status --format tsv --testing"
192
run_test_with_output "Cluster status (TSV shorthand)" "$HAVIRT cluster status -f tsv --testing"
193
run_test "Cluster status with verbose" "$HAVIRT cluster status --verbose --testing"
194
 
195
echo ""
196
 
197
# Section 5: Flag Tests (various combinations)
198
echo "=========================================="
199
echo "Section 5: Flag Combinations"
200
echo "=========================================="
201
echo ""
202
 
203
run_test "Bundled short flags (-vvf)" "$HAVIRT domain list -vvf tsv --testing"
204
run_test "Mixed long and short flags" "$HAVIRT --verbose node list -f tsv --testing"
205
run_test "Flags before command" "$HAVIRT --format tsv domain list --testing"
206
run_test "Flags after action" "$HAVIRT domain list --format tsv --testing"
207
run_test "Testing flag" "$HAVIRT domain list --testing"
208
run_test "Debug flag level 1" "$HAVIRT domain list --debug --testing"
209
run_test "Multiple debug flags" "$HAVIRT domain list -dd --testing"
210
 
211
echo ""
212
 
213
# Section 6: Config and File Tests
214
echo "=========================================="
215
echo "Section 6: Configuration & Files"
216
echo "=========================================="
217
echo ""
218
 
219
if [ -f "$PARENT_DIR/config.yaml" ]; then
220
    log_success "Config file exists: config.yaml"
221
    ((TESTS_PASSED++))
222
    ((TESTS_RUN++))
223
 
224
    if [ $VERBOSE -eq 1 ]; then
225
        echo "  Location: $PARENT_DIR/config.yaml"
226
        echo "  Size: $(stat -f%z "$PARENT_DIR/config.yaml" 2>/dev/null || stat -c%s "$PARENT_DIR/config.yaml" 2>/dev/null) bytes"
227
    fi
228
else
229
    log "Config file not found (will be auto-generated)"
230
fi
231
 
232
if [ -d "$PARENT_DIR/conf" ]; then
233
    CONF_COUNT=$(find "$PARENT_DIR/conf" -name "*.xml" -type f 2>/dev/null | wc -l)
234
    log_success "Domain config directory exists with $CONF_COUNT XML files"
235
    ((TESTS_PASSED++))
236
    ((TESTS_RUN++))
237
else
238
    log "Domain config directory not found"
239
fi
240
 
241
if [ -d "$PARENT_DIR/var" ]; then
242
    log_success "Var directory exists"
243
    ((TESTS_PASSED++))
244
    ((TESTS_RUN++))
245
 
246
    if [ -f "$PARENT_DIR/var/status.yaml" ]; then
247
        log_success "Status database exists"
248
        ((TESTS_PASSED++))
249
        ((TESTS_RUN++))
250
 
251
        if [ $VERBOSE -eq 1 ]; then
252
            echo "  Location: $PARENT_DIR/var/status.yaml"
253
            echo "  Size: $(stat -f%z "$PARENT_DIR/var/status.yaml" 2>/dev/null || stat -c%s "$PARENT_DIR/var/status.yaml" 2>/dev/null) bytes"
254
        fi
255
    else
256
        log "Status database not found"
257
    fi
258
else
259
    log "Var directory not found"
260
fi
261
 
262
echo ""
263
 
264
# Section 7: Module File Tests
265
echo "=========================================="
266
echo "Section 7: Module Files"
267
echo "=========================================="
268
echo ""
269
 
270
for module in havirt domain node cluster; do
271
    if [ -f "$PARENT_DIR/$module.pm" ]; then
272
        log_success "Module $module.pm exists"
273
        ((TESTS_PASSED++))
274
        ((TESTS_RUN++))
275
 
276
        # Check if module is syntactically valid
277
        if perl -c "$PARENT_DIR/$module.pm" >/dev/null 2>&1; then
278
            log_success "Module $module.pm syntax valid"
279
            ((TESTS_PASSED++))
280
            ((TESTS_RUN++))
281
        else
282
            log_failure "Module $module.pm has syntax errors"
283
            ((TESTS_FAILED++))
284
            ((TESTS_RUN++))
285
        fi
286
    else
287
        log_failure "Module $module.pm not found"
288
        ((TESTS_FAILED++))
289
        ((TESTS_RUN++))
290
    fi
291
done
292
 
293
echo ""
294
 
295
# Section 8: Error Handling Tests
296
echo "=========================================="
297
echo "Section 8: Error Handling"
298
echo "=========================================="
299
echo ""
300
 
301
# These should fail gracefully
302
log_test "Invalid command"
303
((TESTS_RUN++))
304
if output=$(cd "$PARENT_DIR" && "$HAVIRT" invalidcommand 2>&1); then
305
    log_failure "Invalid command (should have failed)"
306
    ((TESTS_FAILED++))
307
else
308
    if echo "$output" | grep -q "Error"; then
309
        log_success "Invalid command (failed gracefully with error message)"
310
        ((TESTS_PASSED++))
311
    else
312
        log_failure "Invalid command (failed but no error message)"
313
        ((TESTS_FAILED++))
314
    fi
315
fi
316
 
317
log_test "Invalid action"
318
((TESTS_RUN++))
319
if output=$(cd "$PARENT_DIR" && "$HAVIRT" domain invalidaction 2>&1); then
320
    log_failure "Invalid action (should have failed)"
321
    ((TESTS_FAILED++))
322
else
323
    if echo "$output" | grep -q "Error"; then
324
        log_success "Invalid action (failed gracefully with error message)"
325
        ((TESTS_PASSED++))
326
    else
327
        log_failure "Invalid action (failed but no error message)"
328
        ((TESTS_FAILED++))
329
    fi
330
fi
331
 
332
echo ""
333
 
334
# Section 9: Skipped Tests (require live connections)
335
echo "=========================================="
336
echo "Section 9: Skipped Tests"
337
echo "=========================================="
338
echo ""
339
 
340
log_skip "Domain start operations (requires SSH connections)"
341
log_skip "Domain shutdown operations (requires SSH connections)"
342
log_skip "Domain migrate operations (requires SSH connections)"
343
log_skip "Node scan operations (requires SSH connections)"
344
log_skip "Cluster balance operations (requires SSH connections)"
345
log_skip "Run test_havirt.sh with actual nodes configured for full tests"
346
 
347
echo ""
348
 
349
# Final Summary
350
echo "=========================================="
351
echo "  Test Summary"
352
echo "=========================================="
353
echo ""
354
echo "Total tests run:    $TESTS_RUN"
355
echo -e "Tests passed:       ${GREEN}$TESTS_PASSED${NC}"
356
echo -e "Tests failed:       ${RED}$TESTS_FAILED${NC}"
357
 
358
if [ $TESTS_RUN -gt 0 ]; then
359
    PASS_PERCENT=$((TESTS_PASSED * 100 / TESTS_RUN))
360
    echo "Success rate:       ${PASS_PERCENT}%"
361
fi
362
 
363
echo ""
364
 
365
if [ $TESTS_FAILED -eq 0 ]; then
366
    echo -e "${GREEN}✓ All tests passed!${NC}"
367
    exit 0
368
else
369
    echo -e "${RED}✗ Some tests failed.${NC}"
370
    exit 1
371
fi