diff --git a/cache/cache.go b/cache/cache.go index eda8a29..2fb4965 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -16,7 +16,8 @@ func NewMemcached() *e2e.ConcreteService { images.Memcached, nil, e2e.NewTCPReadinessProbe(MemcachedPort), - MemcachedPort, + []int{MemcachedPort}, + nil, ) } @@ -35,6 +36,7 @@ func NewRedis() *e2e.ConcreteService { "--appendonly", "no", ), e2e.NewTCPReadinessProbe(RedisPort), - RedisPort, + []int{RedisPort}, + nil, ) } diff --git a/db/db.go b/db/db.go index e012c3c..3b19759 100644 --- a/db/db.go +++ b/db/db.go @@ -45,6 +45,8 @@ func newMinio(port int, envVars map[string]string, bktNames ...string) *e2e.HTTP e2e.NewCommandWithoutEntrypoint("sh", "-c", strings.Join(commands, " && ")), e2e.NewHTTPReadinessProbe(port, "/minio/health/ready", 200, 200), port, + nil, + nil, ) envVars["MINIO_ACCESS_KEY"] = MinioAccessKey envVars["MINIO_SECRET_KEY"] = MinioSecretKey @@ -71,6 +73,8 @@ func NewKES(port int, serverName, serverKeyFile, serverCertFile, clientKeyFile, e2e.NewCommandWithoutEntrypoint("sh", "-c", command), readinessProbe, port, + nil, + nil, ), nil } @@ -82,6 +86,8 @@ func NewConsul() *e2e.HTTPService { e2e.NewCommand("agent", "-server", "-client=0.0.0.0", "-dev", "-log-level=err"), e2e.NewHTTPReadinessProbe(8500, "/v1/operator/autopilot/health", 200, 200, `"Healthy": true`), 8500, + nil, + nil, ) } @@ -92,7 +98,8 @@ func NewETCD() *e2e.HTTPService { e2e.NewCommand("/usr/local/bin/etcd", "--listen-client-urls=http://0.0.0.0:2379", "--advertise-client-urls=http://0.0.0.0:2379", "--listen-metrics-urls=http://0.0.0.0:9000", "--log-level=error"), e2e.NewHTTPReadinessProbe(9000, "/health", 200, 204), 2379, - 9000, // Metrics + []int{9000}, // Metrics + nil, ) } @@ -104,5 +111,7 @@ func NewDynamoDB() *e2e.HTTPService { // DynamoDB doesn't have a readiness probe, so we check if the / works even if returns 400 e2e.NewHTTPReadinessProbe(8000, "/", 400, 400), 8000, + nil, + nil, ) } diff --git a/db/kafka.go b/db/kafka.go index fe91aed..beb0163 100644 --- a/db/kafka.go +++ b/db/kafka.go @@ -25,6 +25,8 @@ func NewKafka() *KafkaService { nil, // No custom command. NewKafkaReadinessProbe(9092), 9092, + nil, + nil, ), } } diff --git a/service.go b/service.go index 047ecbc..fa87b1d 100644 --- a/service.go +++ b/service.go @@ -32,18 +32,18 @@ var ( // ConcreteService can be reused (started and stopped many time), but it can represent only one running container // at the time. type ConcreteService struct { - name string - image string - networkPorts []int - env map[string]string - user string - command *Command - cmd *exec.Cmd - readiness ReadinessProbe - privileged bool + name string + image string + ports []int + env map[string]string + user string + command *Command + cmd *exec.Cmd + readiness ReadinessProbe + privileged bool // Maps container ports to dynamically binded local ports. - networkPortsContainerToLocal map[int]int + portMapContainerToLocal map[int]int // Generic retry backoff. retryBackoff *backoff.Backoff @@ -58,15 +58,19 @@ func NewConcreteService( image string, command *Command, readiness ReadinessProbe, - networkPorts ...int, + ports []int, + portMapContainerToLocal map[int]int, ) *ConcreteService { + if portMapContainerToLocal == nil { + portMapContainerToLocal = map[int]int{} + } return &ConcreteService{ - name: name, - image: image, - networkPorts: networkPorts, - command: command, - networkPortsContainerToLocal: map[int]int{}, - readiness: readiness, + name: name, + image: image, + ports: ports, + command: command, + portMapContainerToLocal: portMapContainerToLocal, + readiness: readiness, retryBackoff: backoff.New(context.Background(), backoff.Config{ MinBackoff: 300 * time.Millisecond, MaxBackoff: 600 * time.Millisecond, @@ -123,7 +127,7 @@ func (s *ConcreteService) Start(networkName, sharedDir string) (err error) { } // Get the dynamic local ports mapped to the container. - for _, containerPort := range s.networkPorts { + for _, containerPort := range s.ports { var out []byte out, err = RunCommandAndGetOutput("docker", "port", s.containerName(), strconv.Itoa(containerPort)) @@ -140,10 +144,10 @@ func (s *ConcreteService) Start(networkName, sharedDir string) (err error) { return errors.Wrapf(err, "unable to get mapping for port %d (output: %s); service: %s", containerPort, string(out), s.name) } - s.networkPortsContainerToLocal[containerPort] = localPort + s.portMapContainerToLocal[containerPort] = localPort } - logger.Log("Ports for container:", s.containerName(), "Mapping:", s.networkPortsContainerToLocal) + logger.Log("Ports for container:", s.containerName(), "Mapping:", s.portMapContainerToLocal) return nil } @@ -195,7 +199,7 @@ func (s *ConcreteService) Endpoint(port int) string { } // Map the container port to the local port. - localPort, ok := s.networkPortsContainerToLocal[port] + localPort, ok := s.portMapContainerToLocal[port] if !ok { return "" } @@ -324,8 +328,13 @@ func (s *ConcreteService) buildDockerRunArgs(networkName, sharedDir string) []st } // Published ports - for _, port := range s.networkPorts { - args = append(args, "-p", strconv.Itoa(port)) + for _, containerPort := range s.ports { + if localPort, ok := s.portMapContainerToLocal[containerPort]; ok { + args = append(args, "-p", strconv.Itoa(localPort)+":"+strconv.Itoa(containerPort)) + + } else { + args = append(args, "-p", strconv.Itoa(containerPort)) + } } // Disable entrypoint if required @@ -560,10 +569,11 @@ func NewHTTPService( command *Command, readiness ReadinessProbe, httpPort int, - otherPorts ...int, + otherPorts []int, + portMapContainerToLocal map[int]int, ) *HTTPService { return &HTTPService{ - ConcreteService: NewConcreteService(name, image, command, readiness, append(otherPorts, httpPort)...), + ConcreteService: NewConcreteService(name, image, command, readiness, append(otherPorts, httpPort), portMapContainerToLocal), metricsTimeout: time.Second, httpPort: httpPort, } @@ -575,7 +585,7 @@ func (s *HTTPService) SetMetricsTimeout(timeout time.Duration) { func (s *HTTPService) Metrics() (_ string, err error) { // Map the container port to the local port - localPort := s.networkPortsContainerToLocal[s.httpPort] + localPort := s.portMapContainerToLocal[s.httpPort] // Fetch metrics. // Use an IPv4 address instead of "localhost" hostname because our port mapping assumes IPv4 diff --git a/service_test.go b/service_test.go index 2e7838e..23e4861 100644 --- a/service_test.go +++ b/service_test.go @@ -74,7 +74,7 @@ metric_b_summary_count 1 s := &HTTPService{ httpPort: 0, ConcreteService: &ConcreteService{ - networkPortsContainerToLocal: map[int]int{ + portMapContainerToLocal: map[int]int{ 0: port, }, }, @@ -158,7 +158,7 @@ metric_b 1000 s := &HTTPService{ httpPort: 0, ConcreteService: &ConcreteService{ - networkPortsContainerToLocal: map[int]int{ + portMapContainerToLocal: map[int]int{ 0: port, }, },