Skip to content

Commit

Permalink
refactor: Enhance error handling and add health check logic in deploy…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
yarlson committed Oct 21, 2024
1 parent 51f766d commit 57b1442
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pkg/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ func (d *Deployment) startContainer(service *config.Service, network, suffix str
}

func (d *Deployment) performHealthChecks(container string, healthCheck *config.HealthCheck) error {
if healthCheck == nil {
return nil
}

for i := 0; i < healthCheck.Retries; i++ {
output, err := d.runCommand(context.Background(), "docker", "inspect", "--format={{.State.Health.Status}}", container)
if err == nil && strings.TrimSpace(output) == "healthy" {
Expand Down Expand Up @@ -369,6 +373,10 @@ func (d *Deployment) pullImage(imageName string) (string, error) {

func (d *Deployment) runCommand(ctx context.Context, command string, args ...string) (string, error) {
output, err := d.executor.RunCommand(ctx, command, args...)
if err != nil {
return "", fmt.Errorf("failed to run command: %w", err)
}

outputBytes, readErr := io.ReadAll(output)
if readErr != nil {
return "", fmt.Errorf("failed to read command output: %v (original error: %w)", readErr, err)
Expand Down
110 changes: 109 additions & 1 deletion pkg/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (suite *DeploymentTestSuite) removeContainer(containerName string) {
_ = exec.Command("docker", "rm", "-f", containerName).Run()
}

func (suite *DeploymentTestSuite) removeVolume(volumeName string) {
_ = exec.Command("docker", "volume", "rm", volumeName).Run() // nolint: errcheck
}

func (suite *DeploymentTestSuite) inspectContainer(containerName string) map[string]interface{} {
cmd := exec.Command("docker", "inspect", containerName)
output, err := cmd.Output()
Expand Down Expand Up @@ -166,6 +170,18 @@ func (suite *DeploymentTestSuite) TestUpdateService() {
},
},
},
Storages: []config.Storage{
{
Name: "storage",
Image: "postgres:16",
Volumes: []string{
"postgres_data:/var/lib/postgresql/data",
},
},
},
Volumes: []string{
"postgres_data",
},
}

projectPath, err := suite.updater.prepareProjectFolder(project)
Expand All @@ -187,10 +203,17 @@ func (suite *DeploymentTestSuite) TestUpdateService() {
}

suite.removeContainer("proxy")
suite.removeContainer("storage")
suite.removeVolume("postgres_data")

err = suite.updater.StartProxy(project, cfg, network)
assert.NoError(suite.T(), err)

defer suite.removeContainer("proxy")
defer func() {
suite.removeContainer("proxy")
suite.removeContainer("storage")
suite.removeVolume("postgres_data")
}()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -289,3 +312,88 @@ func (suite *DeploymentTestSuite) TestMakeProjectFolder() {
suite.Require().NoError(err)
})
}

func (suite *DeploymentTestSuite) TestDeploy() {
projectName := "test-project"
network := "ftl-test-network"

cfg := &config.Config{
Project: config.Project{
Name: projectName,
Domain: "localhost",
Email: "[email protected]",
},
Services: []config.Service{
{
Name: "web",
Image: "nginx:1.20",
Port: 80,
Routes: []config.Route{
{
PathPrefix: "/",
StripPrefix: false,
},
},
},
},
Storages: []config.Storage{
{
Name: "storage",
Image: "postgres:16",
Volumes: []string{
"postgres_data:/var/lib/postgresql/data",
},
EnvVars: []config.EnvVar{
{
Name: "POSTGRES_PASSWORD",
Value: "S3cret",
},
{
Name: "POSTGRES_USER",
Value: "test",
},
{
Name: "POSTGRES_DB",
Value: "test",
},
},
},
},
Volumes: []string{
"postgres_data",
},
}

projectPath, err := suite.updater.prepareProjectFolder(projectName)
assert.NoError(suite.T(), err)

proxyCertPath := filepath.Join(projectPath, "localhost.crt")
proxyKeyPath := filepath.Join(projectPath, "localhost.key")
mkcertCmds := [][]string{
{"mkcert", "-install"},
{"mkcert", "-cert-file", proxyCertPath, "-key-file", proxyKeyPath, "localhost"},
}

for _, cmd := range mkcertCmds {
if output, err := suite.updater.runCommand(context.Background(), cmd[0], cmd[1:]...); err != nil {
assert.NoError(suite.T(), err)
suite.T().Log(output)
return
}
}

suite.removeContainer("proxy")
suite.removeContainer("web")
suite.removeContainer("storage")
suite.removeVolume("postgres_data")

defer func() {
suite.removeContainer("proxy")
suite.removeContainer("web")
suite.removeContainer("storage")
suite.removeVolume("postgres_data")
}()

err = suite.updater.Deploy(cfg, network)
assert.NoError(suite.T(), err)
}

0 comments on commit 57b1442

Please sign in to comment.