Merge pull request 'fix: use imageID in createAndStartContainer (closes #124)' (#127) from fix/use-image-id-in-container into main
All checks were successful
Check / check (push) Successful in 11m32s
All checks were successful
Check / check (push) Successful in 11m32s
Reviewed-on: #127
This commit is contained in:
commit
28f014ce95
41
internal/database/testing.go
Normal file
41
internal/database/testing.go
Normal file
@ -0,0 +1,41 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"git.eeqj.de/sneak/upaas/internal/config"
|
||||
"git.eeqj.de/sneak/upaas/internal/logger"
|
||||
)
|
||||
|
||||
// NewTestDatabase creates an in-memory Database for testing.
|
||||
// It runs migrations so all tables are available.
|
||||
func NewTestDatabase(t *testing.T) *Database {
|
||||
t.Helper()
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
cfg := &config.Config{
|
||||
DataDir: tmpDir,
|
||||
}
|
||||
|
||||
log := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
logWrapper := logger.NewForTest(log)
|
||||
|
||||
db, err := New(nil, Params{
|
||||
Logger: logWrapper,
|
||||
Config: cfg,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test database: %v", err)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
if db.database != nil {
|
||||
_ = db.database.Close()
|
||||
}
|
||||
})
|
||||
|
||||
return db
|
||||
}
|
||||
11
internal/logger/testing.go
Normal file
11
internal/logger/testing.go
Normal file
@ -0,0 +1,11 @@
|
||||
package logger
|
||||
|
||||
import "log/slog"
|
||||
|
||||
// NewForTest creates a Logger wrapping the given slog.Logger, for use in tests.
|
||||
func NewForTest(log *slog.Logger) *Logger {
|
||||
return &Logger{
|
||||
log: log,
|
||||
level: new(slog.LevelVar),
|
||||
}
|
||||
}
|
||||
@ -417,15 +417,13 @@ func (svc *Service) executeRollback(
|
||||
|
||||
svc.removeOldContainer(ctx, app, deployment)
|
||||
|
||||
rollbackOpts, err := svc.buildContainerOptions(ctx, app, deployment.ID)
|
||||
rollbackOpts, err := svc.buildContainerOptions(ctx, app, previousImageID)
|
||||
if err != nil {
|
||||
svc.failDeployment(bgCtx, app, deployment, err)
|
||||
|
||||
return fmt.Errorf("failed to build container options: %w", err)
|
||||
}
|
||||
|
||||
rollbackOpts.Image = previousImageID
|
||||
|
||||
containerID, err := svc.docker.CreateContainer(ctx, rollbackOpts)
|
||||
if err != nil {
|
||||
svc.failDeployment(bgCtx, app, deployment, fmt.Errorf("failed to create rollback container: %w", err))
|
||||
@ -1018,9 +1016,9 @@ func (svc *Service) createAndStartContainer(
|
||||
ctx context.Context,
|
||||
app *models.App,
|
||||
deployment *models.Deployment,
|
||||
_ string,
|
||||
imageID string,
|
||||
) (string, error) {
|
||||
containerOpts, err := svc.buildContainerOptions(ctx, app, deployment.ID)
|
||||
containerOpts, err := svc.buildContainerOptions(ctx, app, imageID)
|
||||
if err != nil {
|
||||
svc.failDeployment(ctx, app, deployment, err)
|
||||
|
||||
@ -1064,7 +1062,7 @@ func (svc *Service) createAndStartContainer(
|
||||
func (svc *Service) buildContainerOptions(
|
||||
ctx context.Context,
|
||||
app *models.App,
|
||||
deploymentID int64,
|
||||
imageID string,
|
||||
) (docker.CreateContainerOptions, error) {
|
||||
envVars, err := app.GetEnvVars(ctx)
|
||||
if err != nil {
|
||||
@ -1098,7 +1096,7 @@ func (svc *Service) buildContainerOptions(
|
||||
|
||||
return docker.CreateContainerOptions{
|
||||
Name: "upaas-" + app.Name,
|
||||
Image: fmt.Sprintf("upaas-%s:%d", app.Name, deploymentID),
|
||||
Image: imageID,
|
||||
Env: envMap,
|
||||
Labels: buildLabelMap(app, labels),
|
||||
Volumes: buildVolumeMounts(volumes),
|
||||
|
||||
44
internal/service/deploy/deploy_container_test.go
Normal file
44
internal/service/deploy/deploy_container_test.go
Normal file
@ -0,0 +1,44 @@
|
||||
package deploy_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"git.eeqj.de/sneak/upaas/internal/database"
|
||||
"git.eeqj.de/sneak/upaas/internal/models"
|
||||
"git.eeqj.de/sneak/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 = "sha256:abc123def456"
|
||||
|
||||
opts, err := svc.BuildContainerOptionsExported(context.Background(), app, expectedImageID)
|
||||
if err != nil {
|
||||
t.Fatalf("buildContainerOptions returned error: %v", err)
|
||||
}
|
||||
|
||||
if opts.Image != expectedImageID {
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -10,6 +10,7 @@ import (
|
||||
|
||||
"git.eeqj.de/sneak/upaas/internal/config"
|
||||
"git.eeqj.de/sneak/upaas/internal/docker"
|
||||
"git.eeqj.de/sneak/upaas/internal/models"
|
||||
)
|
||||
|
||||
// NewTestService creates a Service with minimal dependencies for testing.
|
||||
@ -80,3 +81,12 @@ func (svc *Service) CleanupCancelledDeploy(
|
||||
func (svc *Service) GetBuildDirExported(appName string) string {
|
||||
return svc.GetBuildDir(appName)
|
||||
}
|
||||
|
||||
// BuildContainerOptionsExported exposes buildContainerOptions for testing.
|
||||
func (svc *Service) BuildContainerOptionsExported(
|
||||
ctx context.Context,
|
||||
app *models.App,
|
||||
imageID string,
|
||||
) (docker.CreateContainerOptions, error) {
|
||||
return svc.buildContainerOptions(ctx, app, imageID)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user