func TestExpose(t *testing.T) { b := &Builder{flags: &BFlags{}, runConfig: &container.Config{}, disableCommit: true} exposedPort := "80" if err := expose(b, []string{exposedPort}, nil, ""); err != nil { t.Fatalf("Error should be empty, got: %s", err.Error()) } if b.runConfig.ExposedPorts == nil { t.Fatalf("ExposedPorts should be set") } if len(b.runConfig.ExposedPorts) != 1 { t.Fatalf("ExposedPorts should contain only 1 element. Got %s", b.runConfig.ExposedPorts) } portsMapping, err := nat.ParsePortSpec(exposedPort) if err != nil { t.Fatalf("Error when parsing port spec: %s", err.Error()) } if _, ok := b.runConfig.ExposedPorts[portsMapping[0].Port]; !ok { t.Fatalf("Port %s should be present. Got %s", exposedPort, b.runConfig.ExposedPorts) } }
// ValidatePort validates a string is in the expected format for a port definition func ValidatePort(value string) (string, error) { portMappings, err := nat.ParsePortSpec(value) for _, portMapping := range portMappings { if portMapping.Binding.HostIP != "" { return "", fmt.Errorf("HostIP is not supported by a service.") } } return value, err }