forked from mtusi2026/mtusi2026lab2
test: add resilience and benchmark verification
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type apiClient struct {
|
||||
baseURL string
|
||||
client *http.Client
|
||||
token string
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("PASS: PostgreSQL and Valkey outage recovery")
|
||||
}
|
||||
|
||||
func run() error {
|
||||
baseURL := flag.String("base-url", "http://localhost:8080", "API base URL")
|
||||
flag.Parse()
|
||||
client := &apiClient{baseURL: *baseURL, client: &http.Client{Timeout: 10 * time.Second}}
|
||||
if err := client.registerAndLogin(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := client.expect(http.MethodGet, "/api/v1/users/me", http.StatusOK, true); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := verifyOutage(client, "valkey"); err != nil {
|
||||
return err
|
||||
}
|
||||
return verifyOutage(client, "postgres")
|
||||
}
|
||||
|
||||
func (c *apiClient) registerAndLogin() error {
|
||||
username := "dep_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:16]
|
||||
password := "T3st-" + uuid.NewString()
|
||||
body := map[string]string{"username": username, "display_name": "Dependency check", "password": password}
|
||||
payload, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request, err := http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/users", bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
response, err := c.client.Do(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("register: %w", err)
|
||||
}
|
||||
if err := closeResponse(response, http.StatusCreated); err != nil {
|
||||
return fmt.Errorf("register: %w", err)
|
||||
}
|
||||
|
||||
request, err = http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/auth/sessions", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.SetBasicAuth(username, password)
|
||||
response, err = c.client.Do(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("login: %w", err)
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return fmt.Errorf("login status = %d", response.StatusCode)
|
||||
}
|
||||
var session struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&session); err != nil {
|
||||
return fmt.Errorf("decode session: %w", err)
|
||||
}
|
||||
if session.Token == "" {
|
||||
return fmt.Errorf("login returned an empty token")
|
||||
}
|
||||
c.token = session.Token
|
||||
return nil
|
||||
}
|
||||
|
||||
func verifyOutage(client *apiClient, service string) (returnErr error) {
|
||||
if err := compose("stop", service); err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err := compose("up", "-d", "--wait", "--wait-timeout", "90", service); returnErr == nil && err != nil {
|
||||
returnErr = err
|
||||
}
|
||||
}()
|
||||
if err := client.expectEventually("/health/ready", http.StatusServiceUnavailable, false); err != nil {
|
||||
return fmt.Errorf("%s readiness outage: %w", service, err)
|
||||
}
|
||||
if err := client.expectEventually("/api/v1/users/me", http.StatusServiceUnavailable, true); err != nil {
|
||||
return fmt.Errorf("%s business outage: %w", service, err)
|
||||
}
|
||||
if err := compose("up", "-d", "--wait", "--wait-timeout", "90", service); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := client.expectEventually("/health/ready", http.StatusOK, false); err != nil {
|
||||
return fmt.Errorf("%s readiness recovery: %w", service, err)
|
||||
}
|
||||
if err := client.expectEventually("/api/v1/users/me", http.StatusOK, true); err != nil {
|
||||
return fmt.Errorf("%s session recovery: %w", service, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func compose(arguments ...string) error {
|
||||
command := exec.Command("docker", append([]string{"compose"}, arguments...)...)
|
||||
if output, err := command.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("docker compose %v: %w: %s", arguments, err, bytes.TrimSpace(output))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *apiClient) expectEventually(path string, status int, authenticated bool) error {
|
||||
deadline := time.Now().Add(20 * time.Second)
|
||||
var lastErr error
|
||||
for time.Now().Before(deadline) {
|
||||
lastErr = c.expect(http.MethodGet, path, status, authenticated)
|
||||
if lastErr == nil {
|
||||
return nil
|
||||
}
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
return lastErr
|
||||
}
|
||||
|
||||
func (c *apiClient) expect(method, path string, status int, authenticated bool) error {
|
||||
request, err := http.NewRequest(method, c.baseURL+path, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if authenticated {
|
||||
request.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
response, err := c.client.Do(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return closeResponse(response, status)
|
||||
}
|
||||
|
||||
func closeResponse(response *http.Response, status int) error {
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
_, _ = io.Copy(io.Discard, io.LimitReader(response.Body, 1<<20))
|
||||
if response.StatusCode != status {
|
||||
return fmt.Errorf("status = %d, want %d", response.StatusCode, status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
const experimentSchema = "lab2_experiment"
|
||||
|
||||
type report struct {
|
||||
GeneratedAt string `json:"generated_at"`
|
||||
GoVersion string `json:"go_version"`
|
||||
Platform string `json:"platform"`
|
||||
CPUs int `json:"cpus"`
|
||||
Messages int `json:"messages"`
|
||||
Distribution []int `json:"chat_distribution"`
|
||||
SeedDuration float64 `json:"seed_duration_seconds"`
|
||||
Plans []planResult `json:"plans"`
|
||||
Writes []writeResult `json:"writes"`
|
||||
}
|
||||
|
||||
type planResult struct {
|
||||
Name string `json:"name"`
|
||||
DDL string `json:"ddl"`
|
||||
Plan json.RawMessage `json:"plan"`
|
||||
}
|
||||
|
||||
type writeResult struct {
|
||||
Name string `json:"name"`
|
||||
Rows int `json:"rows"`
|
||||
SamplesSeconds []float64 `json:"samples_seconds"`
|
||||
MedianSeconds float64 `json:"median_seconds"`
|
||||
RowsPerSec float64 `json:"rows_per_second"`
|
||||
IndexBytes int64 `json:"index_bytes"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
urlFile := flag.String("database-url-file", "secrets/database_url", "file containing the PostgreSQL URL")
|
||||
output := flag.String("output", "evidence/lab2-track2.json", "result JSON path")
|
||||
messages := flag.Int("messages", 100000, "number of messages")
|
||||
writeRows := flag.Int("write-rows", 10000, "rows per write test")
|
||||
flag.Parse()
|
||||
if *messages < 100000 {
|
||||
return fmt.Errorf("messages must be at least 100000")
|
||||
}
|
||||
if *writeRows < 1000 {
|
||||
return fmt.Errorf("write-rows must be at least 1000")
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(*urlFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read database URL: %w", err)
|
||||
}
|
||||
pool, err := pgxpool.New(context.Background(), strings.TrimSpace(string(data)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("open database: %w", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
distribution := messageDistribution(*messages, 20)
|
||||
seedStarted := time.Now()
|
||||
chatIDs, err := seed(context.Background(), pool, distribution)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result := report{
|
||||
GeneratedAt: time.Now().UTC().Format(time.RFC3339),
|
||||
GoVersion: runtime.Version(), Platform: runtime.GOOS + "/" + runtime.GOARCH,
|
||||
CPUs: runtime.NumCPU(), Messages: *messages, Distribution: distribution,
|
||||
SeedDuration: time.Since(seedStarted).Seconds(),
|
||||
}
|
||||
result.Plans, err = comparePlans(context.Background(), pool, chatIDs[len(chatIDs)-1], distribution[len(distribution)-1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result.Writes, err = compareWrites(context.Background(), pool, *writeRows)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
payload, err := json.MarshalIndent(result, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode report: %w", err)
|
||||
}
|
||||
if err := os.WriteFile(*output, append(payload, '\n'), 0o644); err != nil {
|
||||
return fmt.Errorf("write report: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func messageDistribution(total, chats int) []int {
|
||||
distribution := make([]int, chats)
|
||||
distribution[0] = total * 60 / 100
|
||||
distribution[1] = total * 20 / 100
|
||||
distribution[2] = total * 10 / 100
|
||||
remaining := total - distribution[0] - distribution[1] - distribution[2]
|
||||
for i := 3; i < chats; i++ {
|
||||
distribution[i] = remaining / (chats - 3)
|
||||
}
|
||||
for assigned := sum(distribution); assigned < total; assigned++ {
|
||||
distribution[chats-1]++
|
||||
}
|
||||
return distribution
|
||||
}
|
||||
|
||||
func sum(values []int) int {
|
||||
var result int
|
||||
for _, value := range values {
|
||||
result += value
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func seed(ctx context.Context, pool *pgxpool.Pool, distribution []int) ([]uuid.UUID, error) {
|
||||
if _, err := pool.Exec(ctx, `DROP SCHEMA IF EXISTS `+experimentSchema+` CASCADE; CREATE SCHEMA `+experimentSchema+`; CREATE TABLE `+experimentSchema+`.messages (id uuid PRIMARY KEY, chat_id uuid NOT NULL, created_at timestamptz NOT NULL, text text NOT NULL)`); err != nil {
|
||||
return nil, fmt.Errorf("create experiment table: %w", err)
|
||||
}
|
||||
chatIDs := make([]uuid.UUID, len(distribution))
|
||||
for i := range chatIDs {
|
||||
chatIDs[i] = uuid.NewSHA1(uuid.NameSpaceOID, []byte("chat-"+strconv.Itoa(i)))
|
||||
}
|
||||
base := time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC)
|
||||
rows := make([][]any, 0, sum(distribution))
|
||||
sequence := 0
|
||||
for chatIndex, count := range distribution {
|
||||
for i := range count {
|
||||
id := uuid.NewSHA1(chatIDs[chatIndex], []byte(strconv.Itoa(i)))
|
||||
text := strings.Repeat("m", 32+(i%16)*32)
|
||||
sequence = i*len(distribution) + chatIndex
|
||||
rows = append(rows, []any{id, chatIDs[chatIndex], base.Add(time.Duration(sequence) * time.Microsecond), text})
|
||||
}
|
||||
}
|
||||
if _, err := pool.CopyFrom(ctx, pgx.Identifier{experimentSchema, "messages"}, []string{"id", "chat_id", "created_at", "text"}, pgx.CopyFromRows(rows)); err != nil {
|
||||
return nil, fmt.Errorf("seed messages: %w", err)
|
||||
}
|
||||
return chatIDs, nil
|
||||
}
|
||||
|
||||
func comparePlans(ctx context.Context, pool *pgxpool.Pool, chatID uuid.UUID, count int) ([]planResult, error) {
|
||||
base := time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC)
|
||||
middle := count / 2
|
||||
cursorTime := base.Add(time.Duration(middle*20+19) * time.Microsecond)
|
||||
cursorID := uuid.NewSHA1(chatID, []byte(strconv.Itoa(middle)))
|
||||
candidates := []struct {
|
||||
name string
|
||||
ddl string
|
||||
}{
|
||||
{"history_composite", `CREATE INDEX experiment_history_idx ON ` + experimentSchema + `.messages (chat_id, created_at, id)`},
|
||||
{"global_time", `CREATE INDEX experiment_history_idx ON ` + experimentSchema + `.messages (created_at, id)`},
|
||||
}
|
||||
results := make([]planResult, 0, len(candidates))
|
||||
for _, candidate := range candidates {
|
||||
if _, err := pool.Exec(ctx, `DROP INDEX IF EXISTS `+experimentSchema+`.experiment_history_idx; `+candidate.ddl+`; ANALYZE `+experimentSchema+`.messages`); err != nil {
|
||||
return nil, fmt.Errorf("prepare %s index: %w", candidate.name, err)
|
||||
}
|
||||
const explain = `EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
|
||||
SELECT id, chat_id, created_at, text
|
||||
FROM lab2_experiment.messages
|
||||
WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 100`
|
||||
var raw []byte
|
||||
if err := pool.QueryRow(ctx, explain, chatID, cursorTime, cursorID).Scan(&raw); err != nil {
|
||||
return nil, fmt.Errorf("explain %s: %w", candidate.name, err)
|
||||
}
|
||||
results = append(results, planResult{Name: candidate.name, DDL: candidate.ddl, Plan: json.RawMessage(raw)})
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func compareWrites(ctx context.Context, pool *pgxpool.Pool, rows int) ([]writeResult, error) {
|
||||
const repetitions = 5
|
||||
candidates := []struct {
|
||||
name string
|
||||
ddl string
|
||||
}{
|
||||
{"no_secondary_index", ""},
|
||||
{"history_composite", `CREATE INDEX write_history_idx ON ` + experimentSchema + `.write_messages (chat_id, created_at, id)`},
|
||||
{"global_time", `CREATE INDEX write_history_idx ON ` + experimentSchema + `.write_messages (created_at, id)`},
|
||||
}
|
||||
results := make([]writeResult, 0, len(candidates))
|
||||
for _, candidate := range candidates {
|
||||
samples := make([]float64, 0, repetitions)
|
||||
var indexBytes int64
|
||||
for repetition := range repetitions {
|
||||
if _, err := pool.Exec(ctx, `DROP TABLE IF EXISTS `+experimentSchema+`.write_messages; CREATE TABLE `+experimentSchema+`.write_messages (id uuid PRIMARY KEY, chat_id uuid NOT NULL, created_at timestamptz NOT NULL, text text NOT NULL); `+candidate.ddl); err != nil {
|
||||
return nil, fmt.Errorf("prepare write test %s: %w", candidate.name, err)
|
||||
}
|
||||
values := make([][]any, rows)
|
||||
base := time.Date(2026, time.January, 1, 0, 0, repetition, 0, time.UTC)
|
||||
for i := range rows {
|
||||
values[i] = []any{
|
||||
uuid.NewSHA1(uuid.NameSpaceOID, []byte(strconv.Itoa(i))),
|
||||
uuid.NewSHA1(uuid.NameSpaceURL, []byte(strconv.Itoa(i%20))),
|
||||
base.Add(time.Duration(i) * time.Microsecond),
|
||||
"message",
|
||||
}
|
||||
}
|
||||
started := time.Now()
|
||||
if _, err := pool.CopyFrom(ctx, pgx.Identifier{experimentSchema, "write_messages"}, []string{"id", "chat_id", "created_at", "text"}, pgx.CopyFromRows(values)); err != nil {
|
||||
return nil, fmt.Errorf("write test %s: %w", candidate.name, err)
|
||||
}
|
||||
samples = append(samples, time.Since(started).Seconds())
|
||||
if candidate.ddl != "" {
|
||||
if err := pool.QueryRow(ctx, `SELECT pg_relation_size($1::regclass)`, experimentSchema+".write_history_idx").Scan(&indexBytes); err != nil {
|
||||
return nil, fmt.Errorf("read %s index size: %w", candidate.name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Float64s(samples)
|
||||
median := samples[len(samples)/2]
|
||||
results = append(results, writeResult{
|
||||
Name: candidate.name, Rows: rows, SamplesSeconds: samples,
|
||||
MedianSeconds: median, RowsPerSec: float64(rows) / median, IndexBytes: indexBytes,
|
||||
})
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRunRequiresAddress(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
host string
|
||||
port string
|
||||
want string
|
||||
}{
|
||||
{name: "missing host", port: "8080", want: "APP_HOST is required"},
|
||||
{name: "missing port", host: "127.0.0.1", want: "APP_PORT is required"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Setenv("APP_HOST", tt.host)
|
||||
t.Setenv("APP_PORT", tt.port)
|
||||
if err := run(nil); err == nil || !strings.Contains(err.Error(), tt.want) {
|
||||
t.Fatalf("run() error = %v, want %q", err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
backup_format=PostgreSQL custom
|
||||
restore_database=messenger_restore
|
||||
users=35
|
||||
chats=9
|
||||
chat_members=17
|
||||
messages=8
|
||||
message_sample_digest_match=true
|
||||
recovery_seconds=1
|
||||
rpo=backup snapshot; commits after pg_dump snapshot are outside the restore point
|
||||
@@ -0,0 +1,234 @@
|
||||
{
|
||||
"generated_at": "2026-09-24T06:31:48Z",
|
||||
"go_version": "go1.26.5",
|
||||
"platform": "linux/arm64",
|
||||
"cpus": 2,
|
||||
"messages": 100000,
|
||||
"chat_distribution": [
|
||||
60000,
|
||||
20000,
|
||||
10000,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
588,
|
||||
592
|
||||
],
|
||||
"seed_duration_seconds": 0.297102214,
|
||||
"plans": [
|
||||
{
|
||||
"name": "history_composite",
|
||||
"ddl": "CREATE INDEX experiment_history_idx ON lab2_experiment.messages (chat_id, created_at, id)",
|
||||
"plan": [
|
||||
{
|
||||
"Plan": {
|
||||
"Node Type": "Limit",
|
||||
"Parallel Aware": false,
|
||||
"Async Capable": false,
|
||||
"Startup Cost": 0.42,
|
||||
"Total Cost": 271.86,
|
||||
"Plan Rows": 100,
|
||||
"Plan Width": 315,
|
||||
"Actual Startup Time": 0.020,
|
||||
"Actual Total Time": 0.042,
|
||||
"Actual Rows": 100,
|
||||
"Actual Loops": 1,
|
||||
"Shared Hit Blocks": 6,
|
||||
"Shared Read Blocks": 4,
|
||||
"Shared Dirtied Blocks": 0,
|
||||
"Shared Written Blocks": 0,
|
||||
"Local Hit Blocks": 0,
|
||||
"Local Read Blocks": 0,
|
||||
"Local Dirtied Blocks": 0,
|
||||
"Local Written Blocks": 0,
|
||||
"Temp Read Blocks": 0,
|
||||
"Temp Written Blocks": 0,
|
||||
"Plans": [
|
||||
{
|
||||
"Node Type": "Index Scan",
|
||||
"Parent Relationship": "Outer",
|
||||
"Parallel Aware": false,
|
||||
"Async Capable": false,
|
||||
"Scan Direction": "Forward",
|
||||
"Index Name": "experiment_history_idx",
|
||||
"Relation Name": "messages",
|
||||
"Alias": "messages",
|
||||
"Startup Cost": 0.42,
|
||||
"Total Cost": 1411.91,
|
||||
"Plan Rows": 520,
|
||||
"Plan Width": 315,
|
||||
"Actual Startup Time": 0.019,
|
||||
"Actual Total Time": 0.037,
|
||||
"Actual Rows": 100,
|
||||
"Actual Loops": 1,
|
||||
"Index Cond": "((chat_id = '62ec06f6-c3c9-5e9b-a213-28604e4f69cd'::uuid) AND (ROW(created_at, id) \u003e ROW('2026-01-01 00:00:00.005939+00'::timestamp with time zone, 'd160edac-1f4b-57f8-a01f-cbbfd7250741'::uuid)))",
|
||||
"Rows Removed by Index Recheck": 0,
|
||||
"Shared Hit Blocks": 6,
|
||||
"Shared Read Blocks": 4,
|
||||
"Shared Dirtied Blocks": 0,
|
||||
"Shared Written Blocks": 0,
|
||||
"Local Hit Blocks": 0,
|
||||
"Local Read Blocks": 0,
|
||||
"Local Dirtied Blocks": 0,
|
||||
"Local Written Blocks": 0,
|
||||
"Temp Read Blocks": 0,
|
||||
"Temp Written Blocks": 0
|
||||
}
|
||||
]
|
||||
},
|
||||
"Planning": {
|
||||
"Shared Hit Blocks": 51,
|
||||
"Shared Read Blocks": 1,
|
||||
"Shared Dirtied Blocks": 0,
|
||||
"Shared Written Blocks": 0,
|
||||
"Local Hit Blocks": 0,
|
||||
"Local Read Blocks": 0,
|
||||
"Local Dirtied Blocks": 0,
|
||||
"Local Written Blocks": 0,
|
||||
"Temp Read Blocks": 0,
|
||||
"Temp Written Blocks": 0
|
||||
},
|
||||
"Planning Time": 0.222,
|
||||
"Triggers": [],
|
||||
"Execution Time": 0.069
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "global_time",
|
||||
"ddl": "CREATE INDEX experiment_history_idx ON lab2_experiment.messages (created_at, id)",
|
||||
"plan": [
|
||||
{
|
||||
"Plan": {
|
||||
"Node Type": "Limit",
|
||||
"Parallel Aware": false,
|
||||
"Async Capable": false,
|
||||
"Startup Cost": 0.42,
|
||||
"Total Cost": 3533.04,
|
||||
"Plan Rows": 100,
|
||||
"Plan Width": 315,
|
||||
"Actual Startup Time": 0.055,
|
||||
"Actual Total Time": 0.584,
|
||||
"Actual Rows": 100,
|
||||
"Actual Loops": 1,
|
||||
"Shared Hit Blocks": 1994,
|
||||
"Shared Read Blocks": 19,
|
||||
"Shared Dirtied Blocks": 0,
|
||||
"Shared Written Blocks": 0,
|
||||
"Local Hit Blocks": 0,
|
||||
"Local Read Blocks": 0,
|
||||
"Local Dirtied Blocks": 0,
|
||||
"Local Written Blocks": 0,
|
||||
"Temp Read Blocks": 0,
|
||||
"Temp Written Blocks": 0,
|
||||
"Plans": [
|
||||
{
|
||||
"Node Type": "Index Scan",
|
||||
"Parent Relationship": "Outer",
|
||||
"Parallel Aware": false,
|
||||
"Async Capable": false,
|
||||
"Scan Direction": "Forward",
|
||||
"Index Name": "experiment_history_idx",
|
||||
"Relation Name": "messages",
|
||||
"Alias": "messages",
|
||||
"Startup Cost": 0.42,
|
||||
"Total Cost": 20524.95,
|
||||
"Plan Rows": 581,
|
||||
"Plan Width": 315,
|
||||
"Actual Startup Time": 0.054,
|
||||
"Actual Total Time": 0.578,
|
||||
"Actual Rows": 100,
|
||||
"Actual Loops": 1,
|
||||
"Index Cond": "(ROW(created_at, id) \u003e ROW('2026-01-01 00:00:00.005939+00'::timestamp with time zone, 'd160edac-1f4b-57f8-a01f-cbbfd7250741'::uuid))",
|
||||
"Rows Removed by Index Recheck": 0,
|
||||
"Filter": "(chat_id = '62ec06f6-c3c9-5e9b-a213-28604e4f69cd'::uuid)",
|
||||
"Rows Removed by Filter": 1900,
|
||||
"Shared Hit Blocks": 1994,
|
||||
"Shared Read Blocks": 19,
|
||||
"Shared Dirtied Blocks": 0,
|
||||
"Shared Written Blocks": 0,
|
||||
"Local Hit Blocks": 0,
|
||||
"Local Read Blocks": 0,
|
||||
"Local Dirtied Blocks": 0,
|
||||
"Local Written Blocks": 0,
|
||||
"Temp Read Blocks": 0,
|
||||
"Temp Written Blocks": 0
|
||||
}
|
||||
]
|
||||
},
|
||||
"Planning": {
|
||||
"Shared Hit Blocks": 33,
|
||||
"Shared Read Blocks": 1,
|
||||
"Shared Dirtied Blocks": 0,
|
||||
"Shared Written Blocks": 0,
|
||||
"Local Hit Blocks": 0,
|
||||
"Local Read Blocks": 0,
|
||||
"Local Dirtied Blocks": 0,
|
||||
"Local Written Blocks": 0,
|
||||
"Temp Read Blocks": 0,
|
||||
"Temp Written Blocks": 0
|
||||
},
|
||||
"Planning Time": 0.161,
|
||||
"Triggers": [],
|
||||
"Execution Time": 0.597
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"writes": [
|
||||
{
|
||||
"name": "no_secondary_index",
|
||||
"rows": 10000,
|
||||
"samples_seconds": [
|
||||
0.029270334,
|
||||
0.031755962,
|
||||
0.032344456,
|
||||
0.032778799,
|
||||
0.033289467
|
||||
],
|
||||
"median_seconds": 0.032344456,
|
||||
"rows_per_second": 309171.9953490638,
|
||||
"index_bytes": 0
|
||||
},
|
||||
{
|
||||
"name": "history_composite",
|
||||
"rows": 10000,
|
||||
"samples_seconds": [
|
||||
0.032376473,
|
||||
0.032606465,
|
||||
0.03496374,
|
||||
0.039391471,
|
||||
0.05066359
|
||||
],
|
||||
"median_seconds": 0.03496374,
|
||||
"rows_per_second": 286010.5926883108,
|
||||
"index_bytes": 991232
|
||||
},
|
||||
{
|
||||
"name": "global_time",
|
||||
"rows": 10000,
|
||||
"samples_seconds": [
|
||||
0.030691535,
|
||||
0.032534157,
|
||||
0.035047577,
|
||||
0.035801915,
|
||||
0.043506137
|
||||
],
|
||||
"median_seconds": 0.035047577,
|
||||
"rows_per_second": 285326.4292707025,
|
||||
"index_bytes": 425984
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
date=2026-09-24
|
||||
environment=linux/arm64 containers, API limit 2 CPU and 512 MiB
|
||||
make_check=PASS
|
||||
openapi_validation=PASS
|
||||
golangci_lint_2.13.2=0 issues
|
||||
go_test_race_all=PASS
|
||||
go_vet_all=PASS
|
||||
go_build_messenger=PASS
|
||||
compose_config=PASS
|
||||
public_smoke=PASS: lab2 public API scenario
|
||||
postgres_valkey_integration=PASS
|
||||
dependency_outage=PASS: PostgreSQL and Valkey outage recovery
|
||||
persistence=PASS: confirmed data and existing session preserved
|
||||
migrations=PASS: repeated API restarts reused schema migration checksum without duplicate data
|
||||
@@ -0,0 +1,173 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/model"
|
||||
)
|
||||
|
||||
var errDependency = errors.New("dependency unavailable")
|
||||
|
||||
type repositoryStub struct {
|
||||
pingErr error
|
||||
userByID model.User
|
||||
userByIDErr error
|
||||
credentials model.UserCredentials
|
||||
credentialsErr error
|
||||
}
|
||||
|
||||
func (s repositoryStub) Ping(context.Context) error { return s.pingErr }
|
||||
func (repositoryStub) CreateUser(context.Context, string, string, string) (model.User, error) {
|
||||
return model.User{}, nil
|
||||
}
|
||||
func (s repositoryStub) UserByID(context.Context, string) (model.User, error) {
|
||||
return s.userByID, s.userByIDErr
|
||||
}
|
||||
func (s repositoryStub) UserByUsername(context.Context, string) (model.UserCredentials, error) {
|
||||
return s.credentials, s.credentialsErr
|
||||
}
|
||||
func (repositoryStub) CreateChat(context.Context, string, []string, string) (model.Chat, error) {
|
||||
return model.Chat{}, nil
|
||||
}
|
||||
func (repositoryStub) ChatForUser(context.Context, string, string) (model.Chat, error) {
|
||||
return model.Chat{}, nil
|
||||
}
|
||||
func (repositoryStub) ListChats(context.Context, string, *model.Position, int) ([]model.Chat, bool, error) {
|
||||
return nil, false, nil
|
||||
}
|
||||
func (repositoryStub) CreateMessage(context.Context, string, string, string) (model.Message, error) {
|
||||
return model.Message{}, nil
|
||||
}
|
||||
func (repositoryStub) ListMessages(context.Context, string, string, *model.Position, int) ([]model.Message, bool, error) {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
type sessionStub struct {
|
||||
pingErr error
|
||||
userID string
|
||||
userIDErr error
|
||||
}
|
||||
|
||||
func (s sessionStub) Ping(context.Context) error { return s.pingErr }
|
||||
func (sessionStub) Create(context.Context, string, time.Duration) (string, time.Time, error) {
|
||||
return "token", time.Now().Add(time.Hour), nil
|
||||
}
|
||||
func (s sessionStub) UserID(context.Context, string) (string, error) { return s.userID, s.userIDErr }
|
||||
func (sessionStub) Delete(context.Context, string) error { return nil }
|
||||
|
||||
type passwordsStub struct {
|
||||
verified bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (passwordsStub) Hash(string) (string, error) { return "hash", nil }
|
||||
func (passwordsStub) DummyHash() string { return "hash" }
|
||||
func (s passwordsStub) Verify(string, string) (bool, error) {
|
||||
return s.verified, s.err
|
||||
}
|
||||
|
||||
func TestReadinessReturnsUnavailableForDependencyFailure(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
repo repositoryStub
|
||||
sessions sessionStub
|
||||
}{
|
||||
{name: "postgres", repo: repositoryStub{pingErr: errDependency}},
|
||||
{name: "valkey", sessions: sessionStub{pingErr: errDependency}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := newTestServer(tt.repo, tt.sessions, passwordsStub{})
|
||||
response := serveRequest(server, http.MethodGet, "/health/ready", "")
|
||||
if response.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusServiceUnavailable)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBusinessRequestReturnsUnavailableForDependencyFailure(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
repo repositoryStub
|
||||
sessions sessionStub
|
||||
}{
|
||||
{name: "valkey", sessions: sessionStub{userIDErr: errDependency}},
|
||||
{name: "postgres", repo: repositoryStub{userByIDErr: errDependency}, sessions: sessionStub{userID: "user-id"}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := newTestServer(tt.repo, tt.sessions, passwordsStub{})
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil)
|
||||
request.Header.Set("Authorization", "Bearer valid-token")
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
if response.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusServiceUnavailable)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthenticationChallenges(t *testing.T) {
|
||||
server := newTestServer(repositoryStub{}, sessionStub{}, passwordsStub{})
|
||||
bearer := serveRequest(server, http.MethodGet, "/api/v1/users/me", "")
|
||||
if bearer.Code != http.StatusUnauthorized || bearer.Header().Get("WWW-Authenticate") != `Bearer realm="messenger"` {
|
||||
t.Fatalf("business challenge = %d %q", bearer.Code, bearer.Header().Get("WWW-Authenticate"))
|
||||
}
|
||||
|
||||
repository := repositoryStub{credentials: model.UserCredentials{PasswordHash: "hash"}}
|
||||
server = newTestServer(repository, sessionStub{}, passwordsStub{verified: false})
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", nil)
|
||||
request.SetBasicAuth("user", "wrong-password")
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
if response.Code != http.StatusUnauthorized || response.Header().Get("WWW-Authenticate") != `Basic realm="messenger", charset="UTF-8"` {
|
||||
t.Fatalf("session challenge = %d %q", response.Code, response.Header().Get("WWW-Authenticate"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionCreationRejectsChunkedBody(t *testing.T) {
|
||||
server := newTestServer(repositoryStub{}, sessionStub{}, passwordsStub{})
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", strings.NewReader("{}"))
|
||||
request.ContentLength = -1
|
||||
request.SetBasicAuth("user", "password")
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistrationRejectsWhitespacePassword(t *testing.T) {
|
||||
server := newTestServer(repositoryStub{}, sessionStub{}, passwordsStub{})
|
||||
response := serveRequest(server, http.MethodPost, "/api/v1/users", `{"username":"student","display_name":"Student","password":" "}`)
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func newTestServer(repository Repository, sessions SessionStore, passwords Passwords) *Server {
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
return NewServer(logger, repository, sessions, passwords, time.Hour, time.Second, "8081")
|
||||
}
|
||||
|
||||
func serveRequest(server *Server, method, path, body string) *httptest.ResponseRecorder {
|
||||
request := httptest.NewRequest(method, path, strings.NewReader(body))
|
||||
if body != "" {
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
return response
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestUserTimestampHasFixedMicroseconds(t *testing.T) {
|
||||
createdAt := time.Date(2026, time.September, 23, 21, 28, 15, 423030000, time.UTC)
|
||||
data, err := json.Marshal(User{ID: "id", Username: "user", DisplayName: "User", CreatedAt: createdAt})
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal() error = %v", err)
|
||||
}
|
||||
if string(data) != `{"id":"id","username":"user","display_name":"User","created_at":"2026-09-23T21:28:15.423030Z"}` {
|
||||
t.Fatalf("Marshal() = %s", data)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package password
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHasher(t *testing.T) {
|
||||
hasher := Hasher{}
|
||||
hash, err := hasher.Hash("T3st-password-123")
|
||||
if err != nil {
|
||||
t.Fatalf("Hash() error = %v", err)
|
||||
}
|
||||
|
||||
verified, err := hasher.Verify("T3st-password-123", hash)
|
||||
if err != nil {
|
||||
t.Fatalf("Verify() error = %v", err)
|
||||
}
|
||||
if !verified {
|
||||
t.Fatal("Verify() = false for the original password")
|
||||
}
|
||||
|
||||
verified, err = hasher.Verify("wrong-password", hash)
|
||||
if err != nil {
|
||||
t.Fatalf("Verify() error = %v", err)
|
||||
}
|
||||
if verified {
|
||||
t.Fatal("Verify() = true for a different password")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyRejectsMalformedHash(t *testing.T) {
|
||||
if _, err := (Hasher{}).Verify("password", "invalid"); err == nil {
|
||||
t.Fatal("Verify() error = nil for malformed hash")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/database"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/postgres"
|
||||
)
|
||||
|
||||
func TestConcurrentUsernameCreation(t *testing.T) {
|
||||
pool := openTestDatabase(t)
|
||||
store := postgres.New(pool)
|
||||
username := "race_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:16]
|
||||
|
||||
const workers = 8
|
||||
results := make(chan error, workers)
|
||||
var group sync.WaitGroup
|
||||
for range workers {
|
||||
group.Add(1)
|
||||
go func() {
|
||||
defer group.Done()
|
||||
_, err := store.CreateUser(context.Background(), username, "Race user", "password-hash")
|
||||
results <- err
|
||||
}()
|
||||
}
|
||||
group.Wait()
|
||||
close(results)
|
||||
|
||||
var created, conflicts int
|
||||
for err := range results {
|
||||
switch {
|
||||
case err == nil:
|
||||
created++
|
||||
case errors.Is(err, postgres.ErrConflict):
|
||||
conflicts++
|
||||
default:
|
||||
t.Fatalf("CreateUser() error = %v", err)
|
||||
}
|
||||
}
|
||||
if created != 1 || conflicts != workers-1 {
|
||||
t.Fatalf("created = %d, conflicts = %d", created, conflicts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatRollsBackForUnknownMember(t *testing.T) {
|
||||
pool := openTestDatabase(t)
|
||||
store := postgres.New(pool)
|
||||
owner, err := store.CreateUser(context.Background(), uniqueUsername(), "Owner", "password-hash")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser() error = %v", err)
|
||||
}
|
||||
|
||||
before := chatCount(t, pool)
|
||||
_, err = store.CreateChat(context.Background(), "Invalid chat", []string{owner.ID, uuid.NewString()}, owner.ID)
|
||||
if !errors.Is(err, postgres.ErrInvalidMembers) {
|
||||
t.Fatalf("CreateChat() error = %v, want ErrInvalidMembers", err)
|
||||
}
|
||||
if after := chatCount(t, pool); after != before {
|
||||
t.Fatalf("chat count changed from %d to %d", before, after)
|
||||
}
|
||||
}
|
||||
|
||||
func openTestDatabase(t *testing.T) *pgxpool.Pool {
|
||||
t.Helper()
|
||||
url := testSecret(t, "TEST_DATABASE_URL", "TEST_DATABASE_URL_FILE")
|
||||
pool, err := database.Open(context.Background(), database.Config{
|
||||
URL: url, MinConnections: 0, MaxConnections: 10,
|
||||
MaxConnectionAge: time.Minute, MaxConnectionIdle: time.Minute,
|
||||
HealthCheckPeriod: time.Minute, ConnectTimeout: 5 * time.Second,
|
||||
MigrationTimeout: 30 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("database.Open() error = %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
return pool
|
||||
}
|
||||
|
||||
func testSecret(t *testing.T, valueName, fileName string) string {
|
||||
t.Helper()
|
||||
if value := strings.TrimSpace(os.Getenv(valueName)); value != "" {
|
||||
return value
|
||||
}
|
||||
path := strings.TrimSpace(os.Getenv(fileName))
|
||||
if path == "" {
|
||||
t.Skipf("set %s or %s to run integration tests", valueName, fileName)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", fileName, err)
|
||||
}
|
||||
return strings.TrimSpace(string(data))
|
||||
}
|
||||
|
||||
func uniqueUsername() string {
|
||||
return "u_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:20]
|
||||
}
|
||||
|
||||
func chatCount(t *testing.T, pool *pgxpool.Pool) int {
|
||||
t.Helper()
|
||||
var count int
|
||||
if err := pool.QueryRow(context.Background(), "SELECT count(*) FROM chats").Scan(&count); err != nil {
|
||||
t.Fatalf("count chats: %v", err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package session_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/session"
|
||||
)
|
||||
|
||||
func TestSessionLifecycle(t *testing.T) {
|
||||
store := openTestStore(t)
|
||||
userID := uuid.NewString()
|
||||
token, expiresAt, err := store.Create(context.Background(), userID, time.Minute)
|
||||
if err != nil {
|
||||
t.Fatalf("Create() error = %v", err)
|
||||
}
|
||||
if token == "" || !expiresAt.After(time.Now()) {
|
||||
t.Fatalf("invalid token or expiry: token length = %d, expires_at = %v", len(token), expiresAt)
|
||||
}
|
||||
|
||||
actualUserID, err := store.UserID(context.Background(), token)
|
||||
if err != nil {
|
||||
t.Fatalf("UserID() error = %v", err)
|
||||
}
|
||||
if actualUserID != userID {
|
||||
t.Fatalf("UserID() = %q, want %q", actualUserID, userID)
|
||||
}
|
||||
if err := store.Delete(context.Background(), token); err != nil {
|
||||
t.Fatalf("Delete() error = %v", err)
|
||||
}
|
||||
if _, err := store.UserID(context.Background(), token); !errors.Is(err, session.ErrNotFound) {
|
||||
t.Fatalf("UserID() after delete error = %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionExpires(t *testing.T) {
|
||||
store := openTestStore(t)
|
||||
token, _, err := store.Create(context.Background(), uuid.NewString(), time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("Create() error = %v", err)
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
_, err := store.UserID(context.Background(), token)
|
||||
if errors.Is(err, session.ErrNotFound) {
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("UserID() error = %v", err)
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("session did not expire within three seconds")
|
||||
}
|
||||
|
||||
func openTestStore(t *testing.T) *session.Store {
|
||||
t.Helper()
|
||||
url := strings.TrimSpace(os.Getenv("TEST_CACHE_URL"))
|
||||
if url == "" {
|
||||
path := strings.TrimSpace(os.Getenv("TEST_CACHE_URL_FILE"))
|
||||
if path == "" {
|
||||
t.Skip("set TEST_CACHE_URL or TEST_CACHE_URL_FILE to run integration tests")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read TEST_CACHE_URL_FILE: %v", err)
|
||||
}
|
||||
url = strings.TrimSpace(string(data))
|
||||
}
|
||||
store, err := session.New(url)
|
||||
if err != nil {
|
||||
t.Fatalf("session.New() error = %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = store.Close() })
|
||||
return store
|
||||
}
|
||||
Executable
+87
@@ -0,0 +1,87 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
restore_database=${RESTORE_DATABASE:-messenger_restore}
|
||||
backup_file=$(mktemp)
|
||||
trap 'rm -f "$backup_file"' EXIT
|
||||
|
||||
started_at=$(date +%s)
|
||||
docker compose exec -T postgres sh -eu -c '
|
||||
export PGPASSWORD="$(cat /run/secrets/postgres_password)"
|
||||
pg_dump --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" --format custom
|
||||
' > "$backup_file"
|
||||
|
||||
docker compose exec -T postgres sh -eu -c '
|
||||
export PGPASSWORD="$(cat /run/secrets/postgres_password)"
|
||||
dropdb --username "$POSTGRES_USER" --if-exists "$1"
|
||||
createdb --username "$POSTGRES_USER" "$1"
|
||||
' sh "$restore_database"
|
||||
|
||||
docker compose exec -T postgres sh -eu -c '
|
||||
export PGPASSWORD="$(cat /run/secrets/postgres_password)"
|
||||
pg_restore --username "$POSTGRES_USER" --dbname "$1" --no-owner --no-privileges
|
||||
' sh "$restore_database" < "$backup_file"
|
||||
|
||||
original=$(docker compose exec -T postgres sh -eu -c '
|
||||
export PGPASSWORD="$(cat /run/secrets/postgres_password)"
|
||||
psql --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" --tuples-only --no-align --command "
|
||||
SELECT count(*) FROM users;
|
||||
SELECT count(*) FROM chats;
|
||||
SELECT count(*) FROM chat_members;
|
||||
SELECT count(*) FROM messages;
|
||||
WITH sample AS (
|
||||
(SELECT * FROM messages ORDER BY created_at, id LIMIT 5)
|
||||
UNION ALL
|
||||
(SELECT * FROM messages ORDER BY created_at DESC, id DESC LIMIT 5)
|
||||
)
|
||||
SELECT md5(COALESCE(string_agg(
|
||||
id::text || '\''|'\'' || chat_id::text || '\''|'\'' || sender_id::text || '\''|'\'' || md5(text) || '\''|'\'' || created_at::text,
|
||||
'\'';'\'' ORDER BY created_at, id
|
||||
), '\'''\'')) FROM sample;
|
||||
"
|
||||
')
|
||||
|
||||
restored=$(docker compose exec -T postgres sh -eu -c '
|
||||
export PGPASSWORD="$(cat /run/secrets/postgres_password)"
|
||||
psql --username "$POSTGRES_USER" --dbname "$1" --tuples-only --no-align --command "
|
||||
SELECT count(*) FROM users;
|
||||
SELECT count(*) FROM chats;
|
||||
SELECT count(*) FROM chat_members;
|
||||
SELECT count(*) FROM messages;
|
||||
WITH sample AS (
|
||||
(SELECT * FROM messages ORDER BY created_at, id LIMIT 5)
|
||||
UNION ALL
|
||||
(SELECT * FROM messages ORDER BY created_at DESC, id DESC LIMIT 5)
|
||||
)
|
||||
SELECT md5(COALESCE(string_agg(
|
||||
id::text || '\''|'\'' || chat_id::text || '\''|'\'' || sender_id::text || '\''|'\'' || md5(text) || '\''|'\'' || created_at::text,
|
||||
'\'';'\'' ORDER BY created_at, id
|
||||
), '\'''\'')) FROM sample;
|
||||
"
|
||||
' sh "$restore_database")
|
||||
|
||||
if [ "$original" != "$restored" ]; then
|
||||
echo "backup verification failed" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
finished_at=$(date +%s)
|
||||
users=$(printf '%s\n' "$original" | sed -n '1p')
|
||||
chats=$(printf '%s\n' "$original" | sed -n '2p')
|
||||
members=$(printf '%s\n' "$original" | sed -n '3p')
|
||||
messages=$(printf '%s\n' "$original" | sed -n '4p')
|
||||
|
||||
cat > evidence/lab2-backup-restore.txt <<EOF
|
||||
backup_format=PostgreSQL custom
|
||||
restore_database=$restore_database
|
||||
users=$users
|
||||
chats=$chats
|
||||
chat_members=$members
|
||||
messages=$messages
|
||||
message_sample_digest_match=true
|
||||
recovery_seconds=$((finished_at - started_at))
|
||||
rpo=backup snapshot; commits after pg_dump snapshot are outside the restore point
|
||||
EOF
|
||||
|
||||
cat evidence/lab2-backup-restore.txt
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
docker compose restart postgres valkey
|
||||
docker compose up -d --wait --wait-timeout 90 postgres valkey
|
||||
docker compose restart api
|
||||
docker compose up -d --wait --wait-timeout 90 api
|
||||
Reference in New Issue
Block a user