-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
2,075 additions
and
39 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package dockerutil | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"sync" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/filters" | ||
"github.com/docker/docker/client" | ||
) | ||
|
||
// Allow multiple goroutines to check for busybox | ||
// by using a protected package-level variable. | ||
// | ||
// A mutex allows for retries upon error, if we ever need that; | ||
// whereas a sync.Once would not be simple to retry. | ||
var ( | ||
ensureBusyboxMu sync.Mutex | ||
hasBusybox bool | ||
) | ||
|
||
const busyboxRef = "busybox:stable" | ||
|
||
func ensureBusybox(ctx context.Context, cli *client.Client) error { | ||
ensureBusyboxMu.Lock() | ||
defer ensureBusyboxMu.Unlock() | ||
|
||
if hasBusybox { | ||
return nil | ||
} | ||
|
||
images, err := cli.ImageList(ctx, types.ImageListOptions{ | ||
Filters: filters.NewArgs(filters.Arg("reference", busyboxRef)), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("listing images to check busybox presence: %w", err) | ||
} | ||
|
||
if len(images) > 0 { | ||
hasBusybox = true | ||
return nil | ||
} | ||
|
||
rc, err := cli.ImagePull(ctx, busyboxRef, types.ImagePullOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, _ = io.Copy(io.Discard, rc) | ||
_ = rc.Close() | ||
|
||
hasBusybox = true | ||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package dockerutil | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"strings" | ||
|
||
dockertypes "github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/api/types/mount" | ||
"github.com/docker/docker/api/types/network" | ||
dockerclient "github.com/docker/docker/client" | ||
"github.com/docker/docker/errdefs" | ||
"github.com/docker/go-connections/nat" | ||
"go.uber.org/zap" | ||
|
||
"github.com/strangelove-ventures/interchaintest/v8/ibc" | ||
) | ||
|
||
type ContainerLifecycle struct { | ||
log *zap.Logger | ||
client *dockerclient.Client | ||
containerName string | ||
id string | ||
preStartListeners Listeners | ||
} | ||
|
||
func NewContainerLifecycle(log *zap.Logger, client *dockerclient.Client, containerName string) *ContainerLifecycle { | ||
return &ContainerLifecycle{ | ||
log: log, | ||
client: client, | ||
containerName: containerName, | ||
} | ||
} | ||
|
||
func (c *ContainerLifecycle) CreateContainer( | ||
ctx context.Context, | ||
testName string, | ||
networkID string, | ||
image ibc.DockerImage, | ||
ports nat.PortMap, | ||
volumeBinds []string, | ||
mounts []mount.Mount, | ||
hostName string, | ||
cmd []string, | ||
env []string, | ||
) error { | ||
imageRef := image.Ref() | ||
c.log.Info( | ||
"Will run command", | ||
zap.String("image", imageRef), | ||
zap.String("container", c.containerName), | ||
zap.String("command", strings.Join(cmd, " ")), | ||
) | ||
|
||
pS := nat.PortSet{} | ||
for k := range ports { | ||
pS[k] = struct{}{} | ||
} | ||
|
||
pb, listeners, err := GeneratePortBindings(ports) | ||
if err != nil { | ||
return fmt.Errorf("failed to generate port bindings: %w", err) | ||
} | ||
|
||
c.preStartListeners = listeners | ||
|
||
cc, err := c.client.ContainerCreate( | ||
ctx, | ||
&container.Config{ | ||
Image: imageRef, | ||
|
||
Entrypoint: []string{}, | ||
Cmd: cmd, | ||
Env: env, | ||
|
||
Hostname: hostName, | ||
|
||
Labels: map[string]string{CleanupLabel: testName}, | ||
|
||
ExposedPorts: pS, | ||
}, | ||
&container.HostConfig{ | ||
Binds: volumeBinds, | ||
PortBindings: pb, | ||
PublishAllPorts: true, | ||
AutoRemove: false, | ||
DNS: []string{}, | ||
Mounts: mounts, | ||
}, | ||
&network.NetworkingConfig{ | ||
EndpointsConfig: map[string]*network.EndpointSettings{ | ||
networkID: {}, | ||
}, | ||
}, | ||
nil, | ||
c.containerName, | ||
) | ||
if err != nil { | ||
listeners.CloseAll() | ||
c.preStartListeners = []net.Listener{} | ||
return err | ||
} | ||
c.id = cc.ID | ||
return nil | ||
} | ||
|
||
func (c *ContainerLifecycle) StartContainer(ctx context.Context) error { | ||
// lock port allocation for the time between freeing the ports from the | ||
// temporary listeners to the consumption of the ports by the container | ||
mu.RLock() | ||
defer mu.RUnlock() | ||
|
||
c.preStartListeners.CloseAll() | ||
c.preStartListeners = []net.Listener{} | ||
|
||
if err := StartContainer(ctx, c.client, c.id); err != nil { | ||
return err | ||
} | ||
|
||
c.log.Info("Container started", zap.String("container", c.containerName)) | ||
|
||
return nil | ||
} | ||
|
||
func (c *ContainerLifecycle) PauseContainer(ctx context.Context) error { | ||
return c.client.ContainerPause(ctx, c.id) | ||
} | ||
|
||
func (c *ContainerLifecycle) UnpauseContainer(ctx context.Context) error { | ||
return c.client.ContainerUnpause(ctx, c.id) | ||
} | ||
|
||
func (c *ContainerLifecycle) StopContainer(ctx context.Context) error { | ||
var timeout container.StopOptions | ||
timeoutSec := 30 | ||
timeout.Timeout = &timeoutSec | ||
|
||
return c.client.ContainerStop(ctx, c.id, timeout) | ||
} | ||
|
||
func (c *ContainerLifecycle) RemoveContainer(ctx context.Context) error { | ||
err := c.client.ContainerRemove(ctx, c.id, dockertypes.ContainerRemoveOptions{ | ||
Force: true, | ||
RemoveVolumes: true, | ||
}) | ||
if err != nil && !errdefs.IsNotFound(err) { | ||
return fmt.Errorf("remove container %s: %w", c.containerName, err) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *ContainerLifecycle) ContainerID() string { | ||
return c.id | ||
} | ||
|
||
func (c *ContainerLifecycle) GetHostPorts(ctx context.Context, portIDs ...string) ([]string, error) { | ||
cjson, err := c.client.ContainerInspect(ctx, c.id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ports := make([]string, len(portIDs)) | ||
for i, p := range portIDs { | ||
ports[i] = GetHostPort(cjson, p) | ||
} | ||
return ports, nil | ||
} | ||
|
||
// Running will inspect the container and check its state to determine if it is currently running. | ||
// If the container is running nil will be returned, otherwise an error is returned. | ||
func (c *ContainerLifecycle) Running(ctx context.Context) error { | ||
cjson, err := c.client.ContainerInspect(ctx, c.id) | ||
if err != nil { | ||
return err | ||
} | ||
if cjson.State.Running { | ||
return nil | ||
} | ||
return fmt.Errorf("container with name %s and id %s is not running", c.containerName, c.id) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package dockerutil contains helpers for interacting with Docker containers. | ||
package dockerutil |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dockerutil | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
func CopyFile(src, dst string) (int64, error) { | ||
sourceFileStat, err := os.Stat(src) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if !sourceFileStat.Mode().IsRegular() { | ||
return 0, fmt.Errorf("%s is not a regular file", src) | ||
} | ||
|
||
source, err := os.Open(src) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer source.Close() | ||
|
||
destination, err := os.Create(dst) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer destination.Close() | ||
nBytes, err := io.Copy(destination, source) | ||
return nBytes, err | ||
} |
Oops, something went wrong.