Skip to content

Commit

Permalink
Fix network accessor for port-forwarding feature (testcontainers#2551)
Browse files Browse the repository at this point in the history
* fix(port-forwarding): fix network accessor for port-forwarding feature

Signed-off-by: Julien Breux <[email protected]>

* feat(core/network): improve filtering getter

Signed-off-by: Julien Breux <[email protected]>

---------

Signed-off-by: Julien Breux <[email protected]>
  • Loading branch information
JulienBreux authored May 24, 2024
1 parent d393a38 commit d4a21ea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
21 changes: 19 additions & 2 deletions internal/core/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,25 @@ import (
"github.com/testcontainers/testcontainers-go/internal/core"
)

const (
// FilterByID uses to filter network by identifier.
FilterByID = "id"

// FilterByName uses to filter network by name.
FilterByName = "name"
)

// Get returns a network by its ID.
func Get(ctx context.Context, id string) (types.NetworkResource, error) {
return get(ctx, FilterByID, id)
}

// GetByName returns a network by its name.
func GetByName(ctx context.Context, name string) (types.NetworkResource, error) {
return get(ctx, FilterByName, name)
}

func get(ctx context.Context, filter string, value string) (types.NetworkResource, error) {
var nw types.NetworkResource // initialize to the zero value

cli, err := core.NewClient(ctx)
Expand All @@ -21,15 +38,15 @@ func Get(ctx context.Context, id string) (types.NetworkResource, error) {
defer cli.Close()

filters := filters.NewArgs()
filters.Add("id", id)
filters.Add(filter, value)

list, err := cli.NetworkList(ctx, types.NetworkListOptions{Filters: filters})
if err != nil {
return nw, fmt.Errorf("failed to list networks: %w", err)
}

if len(list) == 0 {
return nw, fmt.Errorf("network %s not found", id)
return nw, fmt.Errorf("network %s not found (filtering by %s)", value, filter)
}

return list[0], nil
Expand Down
2 changes: 1 addition & 1 deletion port_forwarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func exposeHostPorts(ctx context.Context, req *ContainerRequest, p ...int) (Cont
opts := []ContainerCustomizer{}
if len(req.Networks) > 0 {
// get the first network of the container to connect the SSHD container to it.
nw, err := network.Get(ctx, sshdFirstNetwork)
nw, err := network.GetByName(ctx, sshdFirstNetwork)
if err != nil {
return sshdConnectHook, fmt.Errorf("failed to get the network: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions port_forwarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func TestExposeHostPorts(t *testing.T) {
}
})

req.Networks = []string{nw.ID}
req.NetworkAliases = map[string][]string{nw.ID: {"myalpine"}}
req.Networks = []string{nw.Name}
req.NetworkAliases = map[string][]string{nw.Name: {"myalpine"}}
}

ctx := context.Background()
Expand Down

0 comments on commit d4a21ea

Please sign in to comment.