Skip to content

Commit

Permalink
Use unique names for relayer images & cleanup when purpose served (#1269
Browse files Browse the repository at this point in the history
)

* Use unique names for relayer images & cleanup when purpose served

* move random tag generation and teardown image to within BuildRelayerImage

* fix return line
  • Loading branch information
vimystic authored Aug 29, 2023
1 parent 3415e6a commit c3f07c6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
8 changes: 4 additions & 4 deletions interchaintest/client_threshold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ func TestScenarioClientThresholdUpdate(t *testing.T) {
g0, g1 := chains[0], chains[1]

client, network := interchaintest.DockerSetup(t)
relayerinterchaintest.BuildRelayerImage(t)
image := relayerinterchaintest.BuildRelayerImage(t)

// Relayer is set with "--time-threshold 5m"
// The client being created below also has a trusting period of 5m.
// The relayer should automatically update the client after chains re in sync.
r := interchaintest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
interchaintestrelayer.CustomDockerImage(relayerinterchaintest.RelayerImageName, "latest", "100:1000"),
interchaintestrelayer.CustomDockerImage(image, "latest", "100:1000"),
interchaintestrelayer.ImagePull(false),
interchaintestrelayer.StartupFlags("--time-threshold", "20s"),
).Build(t, client, network)
Expand Down Expand Up @@ -164,15 +164,15 @@ func TestScenarioClientTrustingPeriodUpdate(t *testing.T) {
g0, g1 := chains[0], chains[1]

client, network := interchaintest.DockerSetup(t)
relayerinterchaintest.BuildRelayerImage(t)
image := relayerinterchaintest.BuildRelayerImage(t)
logger := zaptest.NewLogger(t)

// Relayer is set with "--time-threshold 0"
// The Relayer should NOT continuously update clients
r := interchaintest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
logger,
interchaintestrelayer.CustomDockerImage(relayerinterchaintest.RelayerImageName, "latest", "100:1000"),
interchaintestrelayer.CustomDockerImage(image, "latest", "100:1000"),
interchaintestrelayer.ImagePull(false),
).Build(t, client, network)

Expand Down
34 changes: 31 additions & 3 deletions interchaintest/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (

dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/archive"
"github.com/google/uuid"
"github.com/moby/moby/client"
"github.com/stretchr/testify/require"
)

const RelayerImageName = "interchaintestrelayer"
const RelayerImagePrefix = "interchaintestrelayer"

type dockerLogLine struct {
Stream string `json:"stream"`
Expand All @@ -29,7 +30,14 @@ type dockerErrorDetail struct {
Message string `json:"message"`
}

func BuildRelayerImage(t *testing.T) {
func uniqueRelayerImageName() (string, error) {
uuid, err := uuid.NewRandom()
if err != nil {
return "", fmt.Errorf("failed to generate uuid %v", err)
}
return RelayerImagePrefix + uuid.String()[:6], nil
}
func BuildRelayerImage(t *testing.T) string {
_, b, _, _ := runtime.Caller(0)
basepath := filepath.Join(filepath.Dir(b), "..")

Expand All @@ -39,14 +47,34 @@ func BuildRelayerImage(t *testing.T) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
require.NoError(t, err, "error building docker client")

image, err := uniqueRelayerImageName()
require.NoError(t, err, "error generating unique tag for docker image")

res, err := cli.ImageBuild(context.Background(), tar, dockertypes.ImageBuildOptions{
Dockerfile: "local.Dockerfile",
Tags: []string{RelayerImageName},
Tags: []string{image},
})
require.NoError(t, err, "error building docker image")

defer res.Body.Close()
t.Cleanup(func() {
destroyRelayerImage(t, image)
})
handleDockerBuildOutput(t, res.Body)
return image
}

func destroyRelayerImage(t *testing.T, image string) {
// Create a Docker client
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
require.NoError(t, err, "error building docker client")

// Remove the Docker image using the provided tag (uniquestr)
_, err = cli.ImageRemove(context.Background(), image, dockertypes.ImageRemoveOptions{
Force: true, // Force remove the image
PruneChildren: true, // Remove all child images
})
require.NoError(t, err, "error removing docker image")
}

func handleDockerBuildOutput(t *testing.T, body io.Reader) {
Expand Down
7 changes: 4 additions & 3 deletions interchaintest/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ func TestRelayerInProcess(t *testing.T) {
func TestRelayerDockerEventProcessor(t *testing.T) {
t.Parallel()

image := relayerinterchaintest.BuildRelayerImage(t)
rf := interchaintest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
interchaintestrelayer.CustomDockerImage(relayerinterchaintest.RelayerImageName, "latest", "100:1000"),
interchaintestrelayer.CustomDockerImage(image, "latest", "100:1000"),
interchaintestrelayer.ImagePull(false),
interchaintestrelayer.StartupFlags("--processor", "events", "--block-history", "100"),
)
Expand All @@ -64,12 +65,12 @@ func TestRelayerDockerEventProcessor(t *testing.T) {
// Relayer runs using the legacy processor.
func TestRelayerDockerLegacyProcessor(t *testing.T) {
t.Parallel()
relayerinterchaintest.BuildRelayerImage(t)
image := relayerinterchaintest.BuildRelayerImage(t)

rf := interchaintest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
interchaintestrelayer.CustomDockerImage(relayerinterchaintest.RelayerImageName, "latest", "100:1000"),
interchaintestrelayer.CustomDockerImage(image, "latest", "100:1000"),
interchaintestrelayer.ImagePull(false),
interchaintestrelayer.StartupFlags("--processor", "legacy"),
)
Expand Down
4 changes: 2 additions & 2 deletions interchaintest/multi_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
)

func TestMultipleChannelsOneConnection(t *testing.T) {
relayerinterchaintest.BuildRelayerImage(t)
image := relayerinterchaintest.BuildRelayerImage(t)

client, network := interchaintest.DockerSetup(t)
r := interchaintest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
interchaintestrelayer.CustomDockerImage(relayerinterchaintest.RelayerImageName, "latest", "100:1000"),
interchaintestrelayer.CustomDockerImage(image, "latest", "100:1000"),
interchaintestrelayer.ImagePull(false),
).Build(t, client, network)

Expand Down
4 changes: 2 additions & 2 deletions interchaintest/relayer_override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
// is a client-id present in the relative path config. If the override flag is present, the relayer should always
// attempt to create a new light client and then overwrite the config file if successful.
func TestClientOverrideFlag(t *testing.T) {
relayerinterchaintest.BuildRelayerImage(t)
image := relayerinterchaintest.BuildRelayerImage(t)

client, network := interchaintest.DockerSetup(t)
r := interchaintest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
interchaintestrelayer.CustomDockerImage(relayerinterchaintest.RelayerImageName, "latest", "100:1000"),
interchaintestrelayer.CustomDockerImage(image, "latest", "100:1000"),
interchaintestrelayer.ImagePull(false),
).Build(t, client, network)

Expand Down

0 comments on commit c3f07c6

Please sign in to comment.