Skip to content

Commit

Permalink
Make protocol comparison case insensitive
Browse files Browse the repository at this point in the history
This commit makes it easier to avoid custom port configuration mistakes,
such as using "TCP" port instead of "tcp". The error message returned
from that area is now more clear.
  • Loading branch information
orlangure committed Nov 12, 2021
1 parent f592785 commit 277cf53
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 14 deletions.
19 changes: 9 additions & 10 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
dockerSockAddr = "/var/run/docker.sock"
)

var duplicateContainerRegexp = regexp.MustCompile(duplicateContainerPattern)

type docker struct {
client *client.Client
log *zap.SugaredLogger
Expand Down Expand Up @@ -307,12 +309,7 @@ func (d *docker) createContainer(ctx context.Context, image string, ports NamedP
return &resp, nil
}

rxp, rxpErr := regexp.Compile(duplicateContainerPattern)
if rxpErr != nil {
return nil, fmt.Errorf("can't find conflicting container id: %w", err)
}

matches := rxp.FindStringSubmatch(err.Error())
matches := duplicateContainerRegexp.FindStringSubmatch(err.Error())
if len(matches) == 2 {
d.log.Infow("duplicate container found, stopping", "container", matches[1])

Expand All @@ -339,16 +336,18 @@ func (d *docker) boundNamedPorts(json types.ContainerJSON, namedPorts NamedPorts

hostPortNum, err := strconv.Atoi(bindings[0].HostPort)
if err != nil {
return nil, err
return nil, fmt.Errorf("invalid host port value '%s': %w", bindings[0].HostPort, err)
}

portName, err := namedPorts.Find(containerPort.Proto(), containerPort.Int())
proto, intPort := containerPort.Proto(), containerPort.Int()

portName, err := namedPorts.Find(proto, intPort)
if err != nil {
return nil, err
return nil, fmt.Errorf("can't find port %s/%d: %w", proto, intPort, err)
}

boundNamedPorts[portName] = Port{
Protocol: containerPort.Proto(),
Protocol: proto,
Port: hostPortNum,
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/aws/aws-sdk-go v1.42.2
github.com/aws/aws-sdk-go v1.42.3
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/containerd/containerd v1.5.7 // indirect
github.com/denisenkom/go-mssqldb v0.11.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:l
github.com/aws/aws-sdk-go v1.15.11/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0=
github.com/aws/aws-sdk-go v1.42.2 h1:SXA+B3DT4N3+wJw5X4Jz9/PazkQZQ7k1nXLGZRdFbO4=
github.com/aws/aws-sdk-go v1.42.2/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/aws/aws-sdk-go v1.42.3 h1:lBKr3tQ06m1uykiychMNKLK1bRfOzaIEQpsI/S3QiNc=
github.com/aws/aws-sdk-go v1.42.3/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down
1 change: 1 addition & 0 deletions internal/israce/norace.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !race
// +build !race

// Package israce reports if the Go race detector is enabled.
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func WithDisableAutoCleanup() Option {
}
}

// UseLocalImagesFirst if possible to avoid hitting the Docker Hub pull rate limit.
// WithUseLocalImagesFirst if possible to avoid hitting the Docker Hub pull rate limit.
func WithUseLocalImagesFirst() Option {
return func(o *Options) {
o.UseLocalImagesFirst = true
Expand Down
7 changes: 5 additions & 2 deletions ports.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gnomock

import "errors"
import (
"errors"
"strings"
)

// DefaultPort should be used with simple containers that expose only one TCP
// port. Use DefaultTCP function when creating a container, and use DefaultPort
Expand Down Expand Up @@ -59,7 +62,7 @@ func (p NamedPorts) Get(name string) Port {
// protocol are known.
func (p NamedPorts) Find(proto string, portNum int) (string, error) {
for name, port := range p {
if proto == port.Protocol && portNum == port.Port {
if strings.EqualFold(proto, port.Protocol) && portNum == port.Port {
return name, nil
}
}
Expand Down

0 comments on commit 277cf53

Please sign in to comment.