Created
November 16, 2022 21:14
-
-
Save Abdulsametileri/64006e3d367f5f79ae603bb9f264804b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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