Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mysql canned container #59

Closed
wants to merge 2 commits into from
Closed

Mysql canned container #59

wants to merge 2 commits into from

Conversation

gianarb
Copy link
Collaborator

@gianarb gianarb commented Apr 20, 2019

Reference #19

I bootstrapped a mysql canned container.

ctx := context.Background()
c, err := MySQLContainer(ctx, MySQLContainerRequest{
        RootPassword: "root",
        Database:     "hello",
})
if err != nil {
        t.Fatal(err.Error())
}
defer c.Container.Terminate(ctx)
sqlC, err := c.GetDriver("root", "root", "hello")
if err != nil {
        t.Fatal(err.Error())
}
_, err = sqlC.ExecContext(ctx, "CREATE TABLE example ( id integer, data varchar(32) )")
if err != nil {
        t.Fatal(err.Error())
}

Signed-off-by: Gianluca Arbezzano gianarb92@gmail.com

@gianarb
Copy link
Collaborator Author

gianarb commented Apr 20, 2019

@mraerino @alde what do you think?

Copy link
Contributor

@mraerino mraerino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comments, but I like the general approach

canned/mysql.go Outdated
ctx context.Context
}

func (c *mysqlContainer) GetDriver(username, password, database string) (*sql.DB, error) {
Copy link
Contributor

@mraerino mraerino Apr 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider taking the parameters from the request and fallbacking to the defaults of the image

canned/mysql.go Outdated
return nil, err
}

req.Image = "mysql:8.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making the version configurable from the request and having a default in a package constant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also make the image configurable so somebody can choose a custom image that is compatible with the options of the default image

canned/mysql.go Outdated
req.Image = "mysql:8.0"
req.ExposedPorts = []string{"3306/tcp"}
req.Env = map[string]string{}
req.Started = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these options basicly overwrite options that may be set by a user. Try checking if this is empty in the ContainerRequest

canned/mysql.go Outdated
req.Env["MYSQL_DATABASE"] = req.Database
}

req.WaitingFor = wait.ForLog("port: 3306 MySQL Community Server - GPL")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The java implementation actually tries connecting via a mysql client to verify that the database is ready.

canned/mysql.go Outdated
Container: c,
}
mysqlC.req = req
mysqlC.ctx = ctx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a common pattern since a context is usually valid for a single function call. Consider e.g. context.WithTimeout()

canned/mysql.go Outdated
if err := c.Start(ctx); err != nil {
return mysqlC, errors.Wrap(err, "failed to start container")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this functionality not implemented in the testcontainer-go core?

@alde
Copy link
Contributor

alde commented Apr 29, 2019

Sorry, I've been away on vacation with limited internet access.

Love the direction, and I agree with most of @mraerino's points :)

@gianarb gianarb added the feature New functionality or new behaviors on the existing one label Sep 4, 2019
@borgoat borgoat mentioned this pull request Sep 28, 2019
This was referenced Oct 25, 2019
Gianluca Arbezzano added 2 commits November 21, 2019 09:06
Reference #19

I bootstrapped a mysql canned container.

```
ctx := context.Background()
c, err := MySQLContainer(ctx, MySQLContainerRequest{
        RootPassword: "root",
        Database:     "hello",
})
if err != nil {
        t.Fatal(err.Error())
}
defer c.Container.Terminate(ctx)
sqlC, err := c.GetDriver("root", "root", "hello")
if err != nil {
        t.Fatal(err.Error())
}
_, err = sqlC.ExecContext(ctx, "CREATE TABLE example ( id integer, data varchar(32) )")
if err != nil {
        t.Fatal(err.Error())
}
```

Signed-off-by: Gianluca Arbezzano <gianarb92@gmail.com>
Signed-off-by: Gianluca Arbezzano <gianarb92@gmail.com>
)

type MySQLContainerRequest struct {
testcontainers.GenericContainerRequest
Copy link
Collaborator Author

@gianarb gianarb Nov 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not accept a request here, we should build it based on what we know from the image the other params we get from the user. What do you think? @mraerino @alde

As a canned container, it should be easy to use and the fact that it can pass the entire request makes it more confused, hard to use to write for what I can see since today.

@gianarb gianarb closed this Apr 14, 2020
@mdelapenya mdelapenya deleted the feature/canned-mysql branch November 15, 2022 12:18
@mdelapenya mdelapenya restored the feature/canned-mysql branch November 15, 2022 12:18
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or new behaviors on the existing one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants