Skip to content

Commit

Permalink
feat: [#519] Save container to a temp file
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 11, 2024
1 parent ab4c85c commit 5d8a412
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
11 changes: 6 additions & 5 deletions contracts/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ type DatabaseDriver interface {
}

type DatabaseConfig struct {
Host string
Port int
Database string
Username string
Password string
Host string
Port int
Database string
Username string
Password string
ContainerID string
}

type Image struct {
Expand Down
13 changes: 12 additions & 1 deletion support/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package docker

import (
"fmt"

"github.com/goravel/framework/contracts/testing"
"github.com/goravel/framework/errors"
"os"
)

// Define different test model, to improve the local testing speed.
Expand Down Expand Up @@ -134,3 +134,14 @@ func Stop() error {

return nil
}

func getContainers() map[ContainerType][]testing.DatabaseDriver {

Check failure on line 138 in support/docker/docker.go

View workflow job for this annotation

GitHub Actions / lint / lint

func `getContainers` is unused (unused)
file, err := os.CreateTemp(os.TempDir(), "goravel_docker")
if err != nil {
panic(err)

Check warning on line 141 in support/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

support/docker/docker.go#L138-L141

Added lines #L138 - L141 were not covered by tests
}
_, err = file.WriteString("file content")

Check failure on line 143 in support/docker/docker.go

View workflow job for this annotation

GitHub Actions / lint / lint

ineffectual assignment to err (ineffassign)
defer file.Close()

return nil

Check warning on line 146 in support/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

support/docker/docker.go#L143-L146

Added lines #L143 - L146 were not covered by tests
}
41 changes: 21 additions & 20 deletions support/docker/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func NewMysqlImpl(database, username, password string) *MysqlImpl {
}
}

func (receiver *MysqlImpl) Build() error {
command, exposedPorts := imageToCommand(receiver.image)
func (r *MysqlImpl) Build() error {
command, exposedPorts := imageToCommand(r.image)
containerID, err := run(command)
if err != nil {
return fmt.Errorf("init Mysql docker error: %v", err)
Expand All @@ -56,32 +56,33 @@ func (receiver *MysqlImpl) Build() error {
return errors.DockerMissingContainerId.Args("Mysql")
}

receiver.containerID = containerID
receiver.port = getExposedPort(exposedPorts, 3306)
r.containerID = containerID
r.port = getExposedPort(exposedPorts, 3306)

if _, err := receiver.connect(); err != nil {
if _, err := r.connect(); err != nil {
return fmt.Errorf("connect Mysql docker error: %v", err)
}

return nil
}

func (receiver *MysqlImpl) Config() testing.DatabaseConfig {
func (r *MysqlImpl) Config() testing.DatabaseConfig {

Check warning on line 69 in support/docker/mysql.go

View check run for this annotation

Codecov / codecov/patch

support/docker/mysql.go#L69

Added line #L69 was not covered by tests
return testing.DatabaseConfig{
Host: receiver.host,
Port: receiver.port,
Database: receiver.database,
Username: receiver.username,
Password: receiver.password,
Host: r.host,
Port: r.port,
Database: r.database,
Username: r.username,
Password: r.password,
ContainerID: r.containerID,

Check warning on line 76 in support/docker/mysql.go

View check run for this annotation

Codecov / codecov/patch

support/docker/mysql.go#L71-L76

Added lines #L71 - L76 were not covered by tests
}
}

func (receiver *MysqlImpl) Driver() database.Driver {
func (r *MysqlImpl) Driver() database.Driver {

Check warning on line 80 in support/docker/mysql.go

View check run for this annotation

Codecov / codecov/patch

support/docker/mysql.go#L80

Added line #L80 was not covered by tests
return database.DriverMysql
}

func (receiver *MysqlImpl) Fresh() error {
instance, err := receiver.connect()
func (r *MysqlImpl) Fresh() error {
instance, err := r.connect()
if err != nil {
return fmt.Errorf("connect Mysql error when clearing: %v", err)
}
Expand All @@ -107,19 +108,19 @@ func (receiver *MysqlImpl) Fresh() error {
return nil
}

func (receiver *MysqlImpl) Image(image testing.Image) {
receiver.image = &image
func (r *MysqlImpl) Image(image testing.Image) {
r.image = &image

Check warning on line 112 in support/docker/mysql.go

View check run for this annotation

Codecov / codecov/patch

support/docker/mysql.go#L111-L112

Added lines #L111 - L112 were not covered by tests
}

func (receiver *MysqlImpl) Stop() error {
if _, err := run(fmt.Sprintf("docker stop %s", receiver.containerID)); err != nil {
func (r *MysqlImpl) Stop() error {
if _, err := run(fmt.Sprintf("docker stop %s", r.containerID)); err != nil {

Check warning on line 116 in support/docker/mysql.go

View check run for this annotation

Codecov / codecov/patch

support/docker/mysql.go#L115-L116

Added lines #L115 - L116 were not covered by tests
return fmt.Errorf("stop Mysql error: %v", err)
}

return nil
}

func (receiver *MysqlImpl) connect() (*gormio.DB, error) {
func (r *MysqlImpl) connect() (*gormio.DB, error) {
var (
instance *gormio.DB
err error
Expand All @@ -128,7 +129,7 @@ func (receiver *MysqlImpl) connect() (*gormio.DB, error) {
// docker compose need time to start
for i := 0; i < 60; i++ {
instance, err = gormio.Open(mysql.New(mysql.Config{
DSN: fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", receiver.username, receiver.password, receiver.host, receiver.port, receiver.database),
DSN: fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", r.username, r.password, r.host, r.port, r.database),
}))

if err == nil {
Expand Down

0 comments on commit 5d8a412

Please sign in to comment.