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) } }