Skip to content

Instantly share code, notes, and snippets.

@Abdulsametileri
Created November 16, 2022 21:14
Show Gist options
  • Save Abdulsametileri/64006e3d367f5f79ae603bb9f264804b to your computer and use it in GitHub Desktop.
Save Abdulsametileri/64006e3d367f5f79ae603bb9f264804b to your computer and use it in GitHub Desktop.
type DockerTestWrapper struct {
container *dockertest.Resource
pool *dockertest.Pool
hostPort int
}
func (l *DockerTestWrapper) RunContainer() error {
hostPort, err := getFreePort()
if err != nil {
return fmt.Errorf("could not get free hostPort: %w", err)
}
pool, err := dockertest.NewPool("")
if err != nil {
return fmt.Errorf("could not connect to docker: %w", err)
}
container, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: RedpandaImage,
Tag: RedpandaVersion,
Cmd: []string{
"redpanda",
"start",
fmt.Sprintf("--advertise-kafka-addr localhost:%v", hostPort),
},
ExposedPorts: []string{
"9092/tcp",
},
PortBindings: map[docker.Port][]docker.PortBinding{
"9092/tcp": {{HostIP: "localhost", HostPort: strconv.Itoa(hostPort)}},
},
}, func(config *docker.HostConfig) {
config.AutoRemove = true // set AutoRemove to true so that stopped container goes away by itself
})
if err != nil {
return fmt.Errorf("could not start container: %w", err)
}
if err = pool.Retry(retryFunc(hostPort)); err != nil {
return fmt.Errorf("could not retry the pool: %w", err)
}
l.pool = pool
l.container = container
l.hostPort = hostPort
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment