Перенесите реализацию ЛР 4 на стенд ЛР 5
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
package testenv
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/google/uuid"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/s3store"
|
||||
)
|
||||
|
||||
const S3Image = "chrislusf/seaweedfs:4.53@sha256:ccad23e18639c8b98cee17e7100b95830a06077b56d8e5b006d6dcf8ec5e94a0"
|
||||
|
||||
// S3 keeps independent API, reconciler and bootstrap identities. Store uses the
|
||||
// reconciler identity so tests can inspect and clean up their objects.
|
||||
type S3 struct {
|
||||
Store, APIStore, Admin *s3store.Store
|
||||
Endpoint, Region, Bucket string
|
||||
GRPCAddress string
|
||||
AccessKey, SecretKey string
|
||||
APIAccessKey, APISecretKey string
|
||||
AdminAccessKey, AdminSecretKey string
|
||||
container testcontainers.Container
|
||||
volume string
|
||||
identities, options []byte
|
||||
}
|
||||
|
||||
func StartS3(ctx context.Context) (*S3, error) {
|
||||
useDockerDesktopPipe()
|
||||
s := &S3{Region: "us-east-1", Bucket: "messenger-attachments", volume: "lab4-s3-test-" + uuid.NewString()}
|
||||
keys := []*string{&s.AccessKey, &s.SecretKey, &s.APIAccessKey, &s.APISecretKey, &s.AdminAccessKey, &s.AdminSecretKey}
|
||||
for _, key := range keys {
|
||||
b := make([]byte, 24)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
*key = hex.EncodeToString(b)
|
||||
}
|
||||
type credential struct {
|
||||
AccessKey string `json:"accessKey"`
|
||||
SecretKey string `json:"secretKey"`
|
||||
}
|
||||
type identity struct {
|
||||
Name string `json:"name"`
|
||||
Credentials []credential `json:"credentials"`
|
||||
Actions []string `json:"actions"`
|
||||
}
|
||||
config := struct {
|
||||
Identities []identity `json:"identities"`
|
||||
}{Identities: []identity{
|
||||
{"bootstrap", []credential{{s.AdminAccessKey, s.AdminSecretKey}}, []string{"Admin", "Read", "Write", "List"}},
|
||||
{"api", []credential{{s.APIAccessKey, s.APISecretKey}}, []string{"Read:" + s.Bucket, "Write:" + s.Bucket}},
|
||||
{"reconciler", []credential{{s.AccessKey, s.SecretKey}}, []string{"List:" + s.Bucket, "Read:" + s.Bucket, "Write:" + s.Bucket}},
|
||||
}}
|
||||
var err error
|
||||
s.identities, err = json.Marshal(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, source, _, _ := runtime.Caller(0)
|
||||
s.options, err = os.ReadFile(filepath.Join(filepath.Dir(source), "../../infra/s3/seaweedfs.options"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read S3 options: %w", err)
|
||||
}
|
||||
startCtx, cancel := context.WithTimeout(ctx, 90*time.Second)
|
||||
defer cancel()
|
||||
if err := s.launch(startCtx); err != nil {
|
||||
s.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := s.Admin.Bootstrap(startCtx); err != nil {
|
||||
s.Close()
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *S3) launch(ctx context.Context) error {
|
||||
pids := int64(256)
|
||||
firstStatus := 0
|
||||
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
||||
ContainerRequest: testcontainers.ContainerRequest{
|
||||
Image: S3Image,
|
||||
Cmd: []string{"server", "-options=/etc/seaweedfs/lab.options"},
|
||||
Env: map[string]string{"GOMEMLIMIT": "460MiB"},
|
||||
ExposedPorts: []string{"8333/tcp", "18333/tcp"},
|
||||
Mounts: testcontainers.Mounts(testcontainers.VolumeMount(s.volume, "/data")),
|
||||
Files: []testcontainers.ContainerFile{
|
||||
{Reader: bytes.NewReader(s.identities), ContainerFilePath: "/run/secrets/s3_identities", FileMode: 0444},
|
||||
{Reader: bytes.NewReader(s.options), ContainerFilePath: "/etc/seaweedfs/lab.options", FileMode: 0444},
|
||||
},
|
||||
HostConfigModifier: func(h *container.HostConfig) {
|
||||
h.Resources.Memory = 512 * 1024 * 1024
|
||||
h.Resources.NanoCPUs = 1_000_000_000
|
||||
h.Resources.PidsLimit = &pids
|
||||
h.PortBindings = nat.PortMap{"8333/tcp": {{HostIP: "127.0.0.1"}}, "18333/tcp": {{HostIP: "127.0.0.1"}}}
|
||||
},
|
||||
// The first reachable S3 response must already reject anonymous access.
|
||||
WaitingFor: wait.ForHTTP("/").WithPort("8333/tcp").WithStatusCodeMatcher(func(code int) bool { firstStatus = code; return true }).WithStartupTimeout(60 * time.Second),
|
||||
},
|
||||
Started: true,
|
||||
})
|
||||
s.container = c
|
||||
if err != nil {
|
||||
return fmt.Errorf("start S3: %w", err)
|
||||
}
|
||||
if firstStatus != http.StatusForbidden {
|
||||
return fmt.Errorf("first anonymous S3 response was %d, expected 403", firstStatus)
|
||||
}
|
||||
s.Endpoint, err = c.PortEndpoint(ctx, "8333/tcp", "http")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.GRPCAddress, err = c.PortEndpoint(ctx, "18333/tcp", "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.Store, err = s3store.New(config.S3{Endpoint: s.Endpoint, Region: s.Region, Bucket: s.Bucket, AccessKey: s.AccessKey, SecretKey: s.SecretKey})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.APIStore, err = s3store.New(config.S3{Endpoint: s.Endpoint, Region: s.Region, Bucket: s.Bucket, AccessKey: s.APIAccessKey, SecretKey: s.APISecretKey})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.Admin, err = s3store.New(config.S3{Endpoint: s.Endpoint, Region: s.Region, Bucket: s.Bucket, AccessKey: s.AdminAccessKey, SecretKey: s.AdminSecretKey})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *S3) Stop(ctx context.Context) error {
|
||||
timeout := 5 * time.Second
|
||||
return s.container.Stop(ctx, &timeout)
|
||||
}
|
||||
|
||||
func (s *S3) Start(ctx context.Context) error {
|
||||
if err := s.container.Start(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.Admin.WaitReady(ctx)
|
||||
}
|
||||
|
||||
// Recreate replaces the container while keeping only its named /data volume.
|
||||
// Bootstrap is deliberately not called: bucket metadata must survive as well.
|
||||
func (s *S3) Recreate(ctx context.Context) error {
|
||||
if err := testcontainers.TerminateContainer(s.container); err != nil {
|
||||
return err
|
||||
}
|
||||
s.container = nil
|
||||
if err := s.launch(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.Admin.WaitReady(ctx)
|
||||
}
|
||||
|
||||
// Diagnostics retains bounded startup evidence without exposing fixture keys.
|
||||
func (s *S3) Diagnostics(ctx context.Context) string {
|
||||
if s.container == nil {
|
||||
return "no container"
|
||||
}
|
||||
r, err := s.container.Logs(ctx)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
defer r.Close()
|
||||
redact := strings.NewReplacer(s.AccessKey, "<redacted>", s.SecretKey, "<redacted>", s.APIAccessKey, "<redacted>", s.APISecretKey, "<redacted>", s.AdminAccessKey, "<redacted>", s.AdminSecretKey, "<redacted>")
|
||||
var lines []string
|
||||
scan := bufio.NewScanner(io.LimitReader(r, 256*1024))
|
||||
for scan.Scan() {
|
||||
line := scan.Text()
|
||||
lower := strings.ToLower(line)
|
||||
if strings.Contains(lower, "volume") || strings.Contains(lower, "master") || strings.Contains(lower, "leader") || strings.Contains(lower, "error") || strings.Contains(lower, "lookup") {
|
||||
lines = append(lines, redact.Replace(line))
|
||||
if len(lines) > 60 {
|
||||
lines = lines[1:]
|
||||
}
|
||||
}
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func (s *S3) Close() {
|
||||
if s.container != nil {
|
||||
_ = testcontainers.TerminateContainer(s.container)
|
||||
}
|
||||
if strings.HasPrefix(s.volume, "lab4-s3-test-") {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
_ = withDocker(ctx, func(cli *testcontainers.DockerClient) error { return cli.VolumeRemove(ctx, s.volume, true) })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
// Package testenv starts disposable PostgreSQL and Valkey containers for tests
|
||||
// that exercise the real stores. Only _test files import it, so it never
|
||||
// reaches the server binary. The images match compose.yaml.
|
||||
package testenv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
tcvalkey "github.com/testcontainers/testcontainers-go/modules/valkey"
|
||||
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/platform/valkey"
|
||||
)
|
||||
|
||||
const (
|
||||
PostgresImage = "postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873"
|
||||
ValkeyImage = "valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11"
|
||||
)
|
||||
|
||||
// useDockerDesktopPipe names the Docker Desktop engine pipe explicitly on
|
||||
// Windows. Without DOCKER_HOST, testcontainers probes the pipe with a file
|
||||
// stat, which fails while the pipe is busy; with several test packages
|
||||
// starting containers at once that probe fails spuriously and the run panics.
|
||||
// Dialling the named host instead waits for a free pipe instance.
|
||||
var useDockerDesktopPipe = sync.OnceFunc(func() {
|
||||
if runtime.GOOS == "windows" && os.Getenv("DOCKER_HOST") == "" {
|
||||
_ = os.Setenv("DOCKER_HOST", "npipe:////./pipe/docker_engine")
|
||||
}
|
||||
})
|
||||
|
||||
// DB is a migrated PostgreSQL instance.
|
||||
type DB struct {
|
||||
Pool *pgxpool.Pool
|
||||
DSN string
|
||||
container *tcpostgres.PostgresContainer
|
||||
}
|
||||
|
||||
func StartPostgres(ctx context.Context) (*DB, error) {
|
||||
useDockerDesktopPipe()
|
||||
c, err := tcpostgres.Run(ctx, PostgresImage,
|
||||
tcpostgres.WithDatabase("messenger"),
|
||||
tcpostgres.WithUsername("messenger"),
|
||||
tcpostgres.WithPassword("messenger"),
|
||||
tcpostgres.BasicWaitStrategies(),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("start postgres: %w", err)
|
||||
}
|
||||
db := &DB{container: c}
|
||||
db.DSN, err = c.ConnectionString(ctx, "sslmode=disable")
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("postgres address: %w", err)
|
||||
}
|
||||
db.Pool, err = postgres.NewPool(ctx, db.DSN, 10)
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
if _, err := postgres.Migrate(ctx, db.Pool); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// Reset empties every application table, so each test starts from an empty
|
||||
// schema. The table list comes from the catalog: a table added by a later
|
||||
// migration cannot be forgotten here.
|
||||
func (d *DB) Reset(t testing.TB) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
var tables string
|
||||
err := d.Pool.QueryRow(ctx, `
|
||||
SELECT coalesce(string_agg(format('%I.%I', schemaname, tablename), ', '), '')
|
||||
FROM pg_tables
|
||||
WHERE schemaname = 'public' AND tablename <> 'goose_db_version'`).Scan(&tables)
|
||||
if err != nil {
|
||||
t.Fatalf("list tables: %v", err)
|
||||
}
|
||||
if tables == "" {
|
||||
return
|
||||
}
|
||||
if _, err := d.Pool.Exec(ctx, "TRUNCATE "+tables+" CASCADE"); err != nil {
|
||||
t.Fatalf("truncate: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Stop stops the container, as when the database goes down.
|
||||
func (d *DB) Stop(ctx context.Context) error {
|
||||
timeout := 5 * time.Second
|
||||
return d.container.Stop(ctx, &timeout)
|
||||
}
|
||||
|
||||
// Pause freezes the server: connections stay open but nothing answers, as
|
||||
// with a hung database.
|
||||
func (d *DB) Pause(ctx context.Context) error {
|
||||
return withDocker(ctx, func(cli *testcontainers.DockerClient) error {
|
||||
return cli.ContainerPause(ctx, d.container.GetContainerID())
|
||||
})
|
||||
}
|
||||
|
||||
func (d *DB) Unpause(ctx context.Context) error {
|
||||
return withDocker(ctx, func(cli *testcontainers.DockerClient) error {
|
||||
return cli.ContainerUnpause(ctx, d.container.GetContainerID())
|
||||
})
|
||||
}
|
||||
|
||||
func withDocker(ctx context.Context, fn func(cli *testcontainers.DockerClient) error) error {
|
||||
cli, err := testcontainers.NewDockerClientWithOpts(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cli.Close()
|
||||
return fn(cli)
|
||||
}
|
||||
|
||||
func (d *DB) Close() {
|
||||
if d.Pool != nil {
|
||||
d.Pool.Close()
|
||||
}
|
||||
_ = testcontainers.TerminateContainer(d.container)
|
||||
}
|
||||
|
||||
// KV is a Valkey instance without persistence or password.
|
||||
type KV struct {
|
||||
Client *redis.Client
|
||||
URL string
|
||||
container *tcvalkey.ValkeyContainer
|
||||
}
|
||||
|
||||
func StartValkey(ctx context.Context) (*KV, error) {
|
||||
useDockerDesktopPipe()
|
||||
c, err := tcvalkey.Run(ctx, ValkeyImage)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("start valkey: %w", err)
|
||||
}
|
||||
kv := &KV{container: c}
|
||||
kv.URL, err = c.ConnectionString(ctx)
|
||||
if err != nil {
|
||||
kv.Close()
|
||||
return nil, fmt.Errorf("valkey address: %w", err)
|
||||
}
|
||||
// The production client, so outage tests see its fail-fast settings.
|
||||
kv.Client, err = valkey.NewClient(ctx, kv.URL)
|
||||
if err != nil {
|
||||
kv.Close()
|
||||
return nil, err
|
||||
}
|
||||
return kv, nil
|
||||
}
|
||||
|
||||
func (k *KV) Reset(t testing.TB) {
|
||||
t.Helper()
|
||||
if err := k.Client.FlushDB(context.Background()).Err(); err != nil {
|
||||
t.Fatalf("flushdb: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Stop stops the container, as when the session store goes down.
|
||||
func (k *KV) Stop(ctx context.Context) error {
|
||||
timeout := 5 * time.Second
|
||||
return k.container.Stop(ctx, &timeout)
|
||||
}
|
||||
|
||||
func (k *KV) Close() {
|
||||
if k.Client != nil {
|
||||
_ = k.Client.Close()
|
||||
}
|
||||
_ = testcontainers.TerminateContainer(k.container)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package testenv_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
func TestValkeyStartsAndResetFlushesKeys(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
kv, err := testenv.StartValkey(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer kv.Close()
|
||||
|
||||
if err := kv.Client.Set(ctx, "probe", "1", 0).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kv.Reset(t)
|
||||
n, err := kv.Client.DBSize(ctx).Result()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatalf("%d keys left after Reset", n)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user