open-vault/vendor/gopkg.in/ory-am/dockertest.v2/mockserver.go
Jeff Mitchell 80dc5819d3 Use dockertest.v2 (#2247)
New dockertest has a totally different API and will require some serious
refactoring. This will tide over until then by pinning the API version.
2017-01-09 13:46:54 -05:00

67 lines
2 KiB
Go

package dockertest
import (
"fmt"
"github.com/go-errors/errors"
"log"
"time"
)
// SetupMockserverContainer sets up a real Mockserver instance for testing purposes
// using a Docker container. It returns the container ID and its IP address,
// or makes the test fail on error.
func SetupMockserverContainer() (c ContainerID, ip string, mockPort, proxyPort int, err error) {
mockPort = RandomPort()
proxyPort = RandomPort()
mockForward := fmt.Sprintf("%d:%d", mockPort, 1080)
proxyForward := fmt.Sprintf("%d:%d", proxyPort, 1090)
if BindDockerToLocalhost != "" {
mockForward = "127.0.0.1:" + mockForward
proxyForward = "127.0.0.1:" + proxyForward
}
c, ip, err = SetupMultiportContainer(RabbitMQImageName, []int{mockPort, proxyPort}, 10*time.Second, func() (string, error) {
res, err := run("--name", GenerateContainerID(), "-d", "-P", "-p", mockForward, "-p", proxyForward, MockserverImageName)
return res, err
})
return
}
// ConnectToMockserver starts a Mockserver image and passes the mock and proxy urls to the connector callback functions.
// The urls will match the http://ip:port pattern (e.g. http://123.123.123.123:4241)
func ConnectToMockserver(tries int, delay time.Duration, mockConnector func(url string) bool, proxyConnector func(url string) bool) (c ContainerID, err error) {
c, ip, mockPort, proxyPort, err := SetupMockserverContainer()
if err != nil {
return c, fmt.Errorf("Could not set up Mockserver container: %v", err)
}
var mockOk, proxyOk bool
for try := 0; try <= tries; try++ {
time.Sleep(delay)
if !mockOk {
if mockConnector(fmt.Sprintf("http://%s:%d", ip, mockPort)) {
mockOk = true
} else {
log.Printf("Try %d failed for mock. Retrying.", try)
}
}
if !proxyOk {
if proxyConnector(fmt.Sprintf("http://%s:%d", ip, proxyPort)) {
proxyOk = true
} else {
log.Printf("Try %d failed for proxy. Retrying.", try)
}
}
}
if mockOk && proxyOk {
return c, nil
} else {
return c, errors.New("Could not set up Mockserver container.")
}
}