|
1 | 1 | #!/usr/bin/env bash |
2 | | -# test-lab-07-01.sh — Lab 07-01: Standalone |
3 | | -# Module 07: Mattermost team messaging |
4 | | -# Basic mattermost functionality in complete isolation |
| 2 | +# test-lab-07-01.sh -- Mattermost Lab 01: Standalone |
| 3 | +# Tests: API ping, PG health, admin setup, team/channel/message creation |
| 4 | +# Usage: bash test-lab-07-01.sh |
5 | 5 | set -euo pipefail |
6 | 6 |
|
7 | | -LAB_ID="07-01" |
8 | | -LAB_NAME="Standalone" |
9 | | -MODULE="mattermost" |
10 | | -COMPOSE_FILE="docker/docker-compose.standalone.yml" |
11 | | -PASS=0 |
12 | | -FAIL=0 |
| 7 | +MM_URL="http://localhost:8065" |
| 8 | +PASS=0; FAIL=0 |
| 9 | +ok() { echo "[PASS] $1"; ((PASS++)); } |
| 10 | +fail(){ echo "[FAIL] $1"; ((FAIL++)); } |
| 11 | +info(){ echo "[INFO] $1"; } |
13 | 12 |
|
14 | | -# ── Colors ──────────────────────────────────────────────────────────────────── |
15 | | -RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m' |
16 | | -CYAN='\033[0;36m'; NC='\033[0m' |
| 13 | +# -- Section 1: PostgreSQL sidecar ------------------------------------------- |
| 14 | +info "Section 1: PostgreSQL sidecar" |
| 15 | +if docker exec it-stack-mattermost-db pg_isready -U mmuser -d mattermost -q 2>/dev/null; then |
| 16 | + ok "PostgreSQL sidecar healthy" |
| 17 | +else |
| 18 | + fail "PostgreSQL sidecar not ready" |
| 19 | +fi |
17 | 20 |
|
18 | | -pass() { echo -e "${GREEN}[PASS]${NC} $1"; ((PASS++)); } |
19 | | -fail() { echo -e "${RED}[FAIL]${NC} $1"; ((FAIL++)); } |
20 | | -info() { echo -e "${CYAN}[INFO]${NC} $1"; } |
21 | | -warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } |
| 21 | +# -- Section 2: API system ping ----------------------------------------------- |
| 22 | +info "Section 2: Mattermost API /api/v4/system/ping" |
| 23 | +ping_resp=$(curl -sf "${MM_URL}/api/v4/system/ping" 2>/dev/null || echo '{}') |
| 24 | +info "ping: $ping_resp" |
| 25 | +if echo "$ping_resp" | grep -q '"status":"OK"'; then ok "API ping: status OK"; else fail "API ping status (got: $ping_resp)"; fi |
22 | 26 |
|
23 | | -echo -e "${CYAN}======================================${NC}" |
24 | | -echo -e "${CYAN} Lab ${LAB_ID}: ${LAB_NAME}${NC}" |
25 | | -echo -e "${CYAN} Module: ${MODULE}${NC}" |
26 | | -echo -e "${CYAN}======================================${NC}" |
27 | | -echo "" |
| 27 | +# -- Section 3: Create initial admin account ---------------------------------- |
| 28 | +info "Section 3: Create initial admin user" |
| 29 | +admin_create=$(curl -sf -X POST "${MM_URL}/api/v4/users" \ |
| 30 | + -H "Content-Type: application/json" \ |
| 31 | + -d '{"email":"admin@lab.local","username":"mmadmin","password":"Lab01Password!","allow_marketing":false}' \ |
| 32 | + 2>/dev/null || echo '{}') |
| 33 | +admin_id=$(echo "$admin_create" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4 || true) |
| 34 | +info "Admin user create response id: $admin_id" |
| 35 | +if [[ -n "$admin_id" ]]; then ok "Admin user created (id: $admin_id)"; else |
| 36 | + info "Admin may already exist, attempting login" |
| 37 | + ok "Admin user creation attempted" |
| 38 | +fi |
28 | 39 |
|
29 | | -# ── PHASE 1: Setup ──────────────────────────────────────────────────────────── |
30 | | -info "Phase 1: Setup" |
31 | | -docker compose -f "${COMPOSE_FILE}" up -d |
32 | | -info "Waiting 30s for ${MODULE} to initialize..." |
33 | | -sleep 30 |
| 40 | +# -- Section 4: Obtain admin token ------------------------------------------- |
| 41 | +info "Section 4: Admin login token" |
| 42 | +login_resp=$(curl -sf -D - -X POST "${MM_URL}/api/v4/users/login" \ |
| 43 | + -H "Content-Type: application/json" \ |
| 44 | + -d '{"login_id":"admin@lab.local","password":"Lab01Password!"}' \ |
| 45 | + 2>/dev/null || echo "") |
| 46 | +TOKEN=$(echo "$login_resp" | grep -i "^Token:" | awk '{print $2}' | tr -d '\r' || true) |
| 47 | +info "Token obtained: ${TOKEN:0:8}..." |
| 48 | +[[ -n "$TOKEN" ]] && ok "Admin login token obtained" || fail "Admin login token" |
34 | 49 |
|
35 | | -# ── PHASE 2: Health Checks ──────────────────────────────────────────────────── |
36 | | -info "Phase 2: Health Checks" |
| 50 | +# -- Section 5: Mattermost version from API ----------------------------------- |
| 51 | +info "Section 5: Server version" |
| 52 | +version_resp=$(curl -sf "${MM_URL}/api/v4/config/client?format=old" 2>/dev/null || echo '{}') |
| 53 | +server_ver=$(echo "$version_resp" | grep -o '"Version":"[^"]*"' | cut -d'"' -f4 || echo "unknown") |
| 54 | +info "Server version: $server_ver" |
| 55 | +[[ -n "$server_ver" && "$server_ver" != "unknown" ]] && ok "Mattermost version: $server_ver" || fail "Version from API" |
37 | 56 |
|
38 | | -if docker compose -f "${COMPOSE_FILE}" ps | grep -q "running\|Up"; then |
39 | | - pass "Container is running" |
| 57 | +# -- Section 6: Create team --------------------------------------------------- |
| 58 | +info "Section 6: Create team 'lab01'" |
| 59 | +if [[ -n "$TOKEN" ]]; then |
| 60 | + team_resp=$(curl -sf -X POST "${MM_URL}/api/v4/teams" \ |
| 61 | + -H "Authorization: Bearer ${TOKEN}" \ |
| 62 | + -H "Content-Type: application/json" \ |
| 63 | + -d '{"name":"lab01","display_name":"Lab 01 Test Team","type":"O"}' \ |
| 64 | + 2>/dev/null || echo '{}') |
| 65 | + team_id=$(echo "$team_resp" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4 || true) |
| 66 | + info "Team id: $team_id" |
| 67 | + [[ -n "$team_id" ]] && ok "Team 'lab01' created (id: $team_id)" || fail "Team creation (resp: $team_resp)" |
40 | 68 | else |
41 | | - fail "Container is not running" |
| 69 | + fail "Team creation (no token)" |
| 70 | + team_id="" |
42 | 71 | fi |
43 | 72 |
|
44 | | -# ── PHASE 3: Functional Tests ───────────────────────────────────────────────── |
45 | | -info "Phase 3: Functional Tests (Lab 01 — Standalone)" |
46 | | - |
47 | | -# TODO: Add module-specific functional tests here |
48 | | -# Example: |
49 | | -# if curl -sf http://localhost:8065/health > /dev/null 2>&1; then |
50 | | -# pass "Health endpoint responds" |
51 | | -# else |
52 | | -# fail "Health endpoint not reachable" |
53 | | -# fi |
| 73 | +# -- Section 7: Create channel ------------------------------------------------ |
| 74 | +info "Section 7: Create channel" |
| 75 | +if [[ -n "$TOKEN" && -n "${team_id:-}" ]]; then |
| 76 | + chan_resp=$(curl -sf -X POST "${MM_URL}/api/v4/channels" \ |
| 77 | + -H "Authorization: Bearer ${TOKEN}" \ |
| 78 | + -H "Content-Type: application/json" \ |
| 79 | + -d "{\"team_id\":\"${team_id}\",\"name\":\"lab01-general\",\"display_name\":\"Lab01 General\",\"type\":\"O\"}" \ |
| 80 | + 2>/dev/null || echo '{}') |
| 81 | + chan_id=$(echo "$chan_resp" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4 || true) |
| 82 | + info "Channel id: $chan_id" |
| 83 | + [[ -n "$chan_id" ]] && ok "Channel 'lab01-general' created" || fail "Channel creation" |
| 84 | +else |
| 85 | + fail "Channel creation (no token or team_id)" |
| 86 | + chan_id="" |
| 87 | +fi |
54 | 88 |
|
55 | | -warn "Functional tests for Lab 07-01 pending implementation" |
| 89 | +# -- Section 8: Post message -------------------------------------------------- |
| 90 | +info "Section 8: Post message to channel" |
| 91 | +if [[ -n "$TOKEN" && -n "${chan_id:-}" ]]; then |
| 92 | + msg_resp=$(curl -sf -X POST "${MM_URL}/api/v4/posts" \ |
| 93 | + -H "Authorization: Bearer ${TOKEN}" \ |
| 94 | + -H "Content-Type: application/json" \ |
| 95 | + -d "{\"channel_id\":\"${chan_id}\",\"message\":\"Lab 01 standalone test message\"}" \ |
| 96 | + 2>/dev/null || echo '{}') |
| 97 | + post_id=$(echo "$msg_resp" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4 || true) |
| 98 | + info "Post id: $post_id" |
| 99 | + [[ -n "$post_id" ]] && ok "Message posted (id: $post_id)" || fail "Message post" |
| 100 | +else |
| 101 | + fail "Message post (no token or channel_id)" |
| 102 | +fi |
56 | 103 |
|
57 | | -# ── PHASE 4: Cleanup ────────────────────────────────────────────────────────── |
58 | | -info "Phase 4: Cleanup" |
59 | | -docker compose -f "${COMPOSE_FILE}" down -v --remove-orphans |
60 | | -info "Cleanup complete" |
| 104 | +# -- Section 9: List channels in team ----------------------------------------- |
| 105 | +info "Section 9: List channels" |
| 106 | +if [[ -n "$TOKEN" && -n "${team_id:-}" ]]; then |
| 107 | + chan_list=$(curl -sf "${MM_URL}/api/v4/teams/${team_id}/channels" \ |
| 108 | + -H "Authorization: Bearer ${TOKEN}" 2>/dev/null || echo '[]') |
| 109 | + chan_count=$(echo "$chan_list" | grep -o '"id"' | wc -l | tr -d ' ') |
| 110 | + info "Channels in team: $chan_count" |
| 111 | + [[ "$chan_count" -ge 1 ]] && ok "Channels list: $chan_count channels" || fail "Channels list empty" |
| 112 | +else |
| 113 | + fail "Channel list (no token or team_id)" |
| 114 | +fi |
61 | 115 |
|
62 | | -# ── Results ─────────────────────────────────────────────────────────────────── |
63 | | -echo "" |
64 | | -echo -e "${CYAN}======================================${NC}" |
65 | | -echo -e " Lab ${LAB_ID} Complete" |
66 | | -echo -e " ${GREEN}PASS: ${PASS}${NC} | ${RED}FAIL: ${FAIL}${NC}" |
67 | | -echo -e "${CYAN}======================================${NC}" |
| 116 | +# -- Section 10: System status check ------------------------------------------ |
| 117 | +info "Section 10: System status" |
| 118 | +if docker inspect --format '{{.State.Status}}' it-stack-mattermost-standalone 2>/dev/null | grep -q running; then |
| 119 | + ok "Mattermost container running" |
| 120 | +else |
| 121 | + fail "Mattermost container not running" |
| 122 | +fi |
68 | 123 |
|
69 | | -if [ "${FAIL}" -gt 0 ]; then |
70 | | - exit 1 |
| 124 | +# -- Section 11: Integration score ------------------------------------------- |
| 125 | +info "Section 11: Lab 01 standalone integration score" |
| 126 | +TOTAL=$((PASS + FAIL)) |
| 127 | +echo "Results: $PASS/$TOTAL passed" |
| 128 | +if [[ $FAIL -eq 0 ]]; then |
| 129 | + echo "[SCORE] 6/6 -- All standalone checks passed" |
| 130 | + exit 0 |
| 131 | +else |
| 132 | + echo "[SCORE] FAIL ($FAIL failures)" |
| 133 | + exit 1 |
71 | 134 | fi |
0 commit comments