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

Use unique names for relayer images & cleanup when purpose served #1269

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 32 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,15 @@ type dockerErrorDetail struct {
Message string `json:"message"`
}

func BuildRelayerImage(t *testing.T) {
func uniqueRelayerImageName() (string, error) {
uuid, err := uuid.NewRandom()
if err != nil {
fmt.Printf("Failed to generate UUID: %v\n", err)
vimystic marked this conversation as resolved.
Show resolved Hide resolved
vimystic marked this conversation as resolved.
Show resolved Hide resolved
return "", 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 +48,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