All checks were successful
Check / check (pull_request) Successful in 3m24s
- Add cpu_limit (REAL) and memory_limit (INTEGER) columns to apps table via migration 007 - Add CPULimit and MemoryLimit fields to App model with full CRUD support - Add resource limits fields to app edit form with human-friendly memory input (e.g. 256m, 1g, 512k) - Pass CPU and memory limits to Docker container creation via NanoCPUs and Memory host config fields - Extract Docker container creation helpers (buildEnvSlice, buildMounts, buildResources) for cleaner code - Add formatMemoryBytes template function for display - Add comprehensive tests for parsing, formatting, model persistence, and container options
137 lines
3.1 KiB
Go
137 lines
3.1 KiB
Go
package deploy_test
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"log/slog"
|
|
"os"
|
|
"testing"
|
|
|
|
"sneak.berlin/go/upaas/internal/database"
|
|
"sneak.berlin/go/upaas/internal/docker"
|
|
"sneak.berlin/go/upaas/internal/models"
|
|
"sneak.berlin/go/upaas/internal/service/deploy"
|
|
)
|
|
|
|
func TestBuildContainerOptionsUsesImageID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db := database.NewTestDatabase(t)
|
|
|
|
app := models.NewApp(db)
|
|
app.Name = "myapp"
|
|
|
|
err := app.Save(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("failed to save app: %v", err)
|
|
}
|
|
|
|
log := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
|
svc := deploy.NewTestService(log)
|
|
|
|
const expectedImageID = docker.ImageID("sha256:abc123def456")
|
|
|
|
opts, err := svc.BuildContainerOptionsExported(context.Background(), app, expectedImageID)
|
|
if err != nil {
|
|
t.Fatalf("buildContainerOptions returned error: %v", err)
|
|
}
|
|
|
|
if opts.Image != expectedImageID.String() {
|
|
t.Errorf("expected Image=%q, got %q", expectedImageID, opts.Image)
|
|
}
|
|
|
|
if opts.Name != "upaas-myapp" {
|
|
t.Errorf("expected Name=%q, got %q", "upaas-myapp", opts.Name)
|
|
}
|
|
}
|
|
|
|
func TestBuildContainerOptionsNoResourceLimits(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db := database.NewTestDatabase(t)
|
|
|
|
app := models.NewApp(db)
|
|
app.Name = "nolimits"
|
|
|
|
err := app.Save(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("failed to save app: %v", err)
|
|
}
|
|
|
|
log := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
|
svc := deploy.NewTestService(log)
|
|
|
|
opts, err := svc.BuildContainerOptionsExported(
|
|
context.Background(), app, docker.ImageID("test:latest"),
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("buildContainerOptions returned error: %v", err)
|
|
}
|
|
|
|
if opts.CPULimit != 0 {
|
|
t.Errorf("expected CPULimit=0, got %v", opts.CPULimit)
|
|
}
|
|
|
|
if opts.MemoryLimit != 0 {
|
|
t.Errorf("expected MemoryLimit=0, got %v", opts.MemoryLimit)
|
|
}
|
|
}
|
|
|
|
func TestBuildContainerOptionsCPULimit(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db := database.NewTestDatabase(t)
|
|
|
|
app := models.NewApp(db)
|
|
app.Name = "cpulimit"
|
|
app.CPULimit = sql.NullFloat64{Float64: 0.5, Valid: true}
|
|
|
|
err := app.Save(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("failed to save app: %v", err)
|
|
}
|
|
|
|
log := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
|
svc := deploy.NewTestService(log)
|
|
|
|
opts, err := svc.BuildContainerOptionsExported(
|
|
context.Background(), app, docker.ImageID("test:latest"),
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("buildContainerOptions returned error: %v", err)
|
|
}
|
|
|
|
if opts.CPULimit != 0.5 {
|
|
t.Errorf("expected CPULimit=0.5, got %v", opts.CPULimit)
|
|
}
|
|
}
|
|
|
|
func TestBuildContainerOptionsMemoryLimit(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db := database.NewTestDatabase(t)
|
|
|
|
app := models.NewApp(db)
|
|
app.Name = "memlimit"
|
|
app.MemoryLimit = sql.NullInt64{Int64: 536870912, Valid: true} // 512m
|
|
|
|
err := app.Save(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("failed to save app: %v", err)
|
|
}
|
|
|
|
log := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
|
svc := deploy.NewTestService(log)
|
|
|
|
opts, err := svc.BuildContainerOptionsExported(
|
|
context.Background(), app, docker.ImageID("test:latest"),
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("buildContainerOptions returned error: %v", err)
|
|
}
|
|
|
|
if opts.MemoryLimit != 536870912 {
|
|
t.Errorf("expected MemoryLimit=536870912, got %v", opts.MemoryLimit)
|
|
}
|
|
}
|