New creates new, isolated docker environment.
(opts ...EnvironmentOption)
| 86 | |
| 87 | // New creates new, isolated docker environment. |
| 88 | func New(opts ...EnvironmentOption) (_ *DockerEnvironment, err error) { |
| 89 | e := environmentOptions{} |
| 90 | for _, o := range opts { |
| 91 | o(&e) |
| 92 | } |
| 93 | if e.name == "" { |
| 94 | e.name, err = generateName() |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | } |
| 99 | if err := validateName(e.name); err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | |
| 103 | if e.logger == nil { |
| 104 | e.logger = NewLogger(os.Stdout) |
| 105 | } |
| 106 | |
| 107 | d := &DockerEnvironment{ |
| 108 | logger: e.logger, |
| 109 | networkName: e.name, |
| 110 | verbose: e.verbose, |
| 111 | registered: map[string]struct{}{}, |
| 112 | dockerVolumes: e.volumes, |
| 113 | } |
| 114 | |
| 115 | // Force a shutdown in order to cleanup from a spurious situation in case |
| 116 | // the previous tests run didn't cleanup correctly. |
| 117 | d.close() |
| 118 | |
| 119 | dir, err := getTmpDirectory() |
| 120 | if err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | d.dir = dir |
| 124 | |
| 125 | // Setup the docker network. |
| 126 | if out, err := d.exec("docker", "network", "create", "-d", "bridge", d.networkName).CombinedOutput(); err != nil { |
| 127 | e.logger.Log(string(out)) |
| 128 | d.Close() |
| 129 | return nil, errors.Wrapf(err, "create docker network '%s'", d.networkName) |
| 130 | } |
| 131 | |
| 132 | switch runtime.GOOS { |
| 133 | case "darwin": |
| 134 | d.hostAddr = dockerMacOSGatewayAddr |
| 135 | default: |
| 136 | out, err := d.exec("docker", "network", "inspect", d.networkName).CombinedOutput() |
| 137 | if err != nil { |
| 138 | e.logger.Log(string(out)) |
| 139 | d.Close() |
| 140 | return nil, errors.Wrapf(err, "inspect docker network '%s'", d.networkName) |
| 141 | } |
| 142 | |
| 143 | var inspectDetails []struct { |
| 144 | IPAM struct { |
| 145 | Config []struct { |