Update all *DockerContainer receivers to handle nil

- Use errors.New() instead of fmt.Errorf() if no formatter string is needed
    - Spacing changes due to `go fmt`
This commit is contained in:
Dale Hui 2018-01-19 14:22:55 -08:00
parent 042e307bef
commit 7159f40304
1 changed files with 46 additions and 11 deletions

View File

@ -5,17 +5,18 @@ import (
"bufio" "bufio"
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
dockertypes "github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
dockernetwork "github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client"
"io" "io"
"math/rand" "math/rand"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
dockertypes "github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
dockernetwork "github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client"
) )
func NewDockerContainer(t testing.TB, image string, env []string, cmd []string) (*DockerContainer, error) { func NewDockerContainer(t testing.TB, image string, env []string, cmd []string) (*DockerContainer, error) {
@ -33,7 +34,7 @@ func NewDockerContainer(t testing.TB, image string, env []string, cmd []string)
client: c, client: c,
ImageName: image, ImageName: image,
ENV: env, ENV: env,
Cmd: cmd, Cmd: cmd,
} }
if err := contr.PullImage(); err != nil { if err := contr.PullImage(); err != nil {
@ -62,6 +63,9 @@ type DockerContainer struct {
} }
func (d *DockerContainer) PullImage() error { func (d *DockerContainer) PullImage() error {
if d == nil {
return errors.New("Cannot pull image on a nil *DockerContainer")
}
d.t.Logf("Docker: Pull image %v", d.ImageName) d.t.Logf("Docker: Pull image %v", d.ImageName)
r, err := d.client.ImagePull(context.Background(), d.ImageName, dockertypes.ImagePullOptions{}) r, err := d.client.ImagePull(context.Background(), d.ImageName, dockertypes.ImagePullOptions{})
if err != nil { if err != nil {
@ -84,7 +88,11 @@ func (d *DockerContainer) PullImage() error {
} }
func (d *DockerContainer) Start() error { func (d *DockerContainer) Start() error {
containerName := fmt.Sprintf("migrate_test_%v", pseudoRandStr(10)) if d == nil {
return errors.New("Cannot start a nil *DockerContainer")
}
containerName := fmt.Sprintf("migrate_test_%s", pseudoRandStr(10))
// create container first // create container first
resp, err := d.client.ContainerCreate(context.Background(), resp, err := d.client.ContainerCreate(context.Background(),
@ -119,16 +127,24 @@ func (d *DockerContainer) Start() error {
} }
func (d *DockerContainer) KeepForDebugging() { func (d *DockerContainer) KeepForDebugging() {
if d == nil {
return
}
d.keepForDebugging = true d.keepForDebugging = true
} }
func (d *DockerContainer) Remove() error { func (d *DockerContainer) Remove() error {
if d == nil {
return errors.New("Cannot remove a nil *DockerContainer")
}
if d.keepForDebugging { if d.keepForDebugging {
return nil return nil
} }
if len(d.ContainerId) == 0 { if len(d.ContainerId) == 0 {
return fmt.Errorf("missing containerId") return errors.New("missing containerId")
} }
if err := d.client.ContainerRemove(context.Background(), d.ContainerId, if err := d.client.ContainerRemove(context.Background(), d.ContainerId,
dockertypes.ContainerRemoveOptions{ dockertypes.ContainerRemoveOptions{
@ -142,8 +158,12 @@ func (d *DockerContainer) Remove() error {
} }
func (d *DockerContainer) Inspect() error { func (d *DockerContainer) Inspect() error {
if d == nil {
return errors.New("Cannot inspect a nil *DockerContainer")
}
if len(d.ContainerId) == 0 { if len(d.ContainerId) == 0 {
return fmt.Errorf("missing containerId") return errors.New("missing containerId")
} }
resp, err := d.client.ContainerInspect(context.Background(), d.ContainerId) resp, err := d.client.ContainerInspect(context.Background(), d.ContainerId)
if err != nil { if err != nil {
@ -156,8 +176,11 @@ func (d *DockerContainer) Inspect() error {
} }
func (d *DockerContainer) Logs() (io.ReadCloser, error) { func (d *DockerContainer) Logs() (io.ReadCloser, error) {
if d == nil {
return nil, errors.New("Cannot view logs for a nil *DockerContainer")
}
if len(d.ContainerId) == 0 { if len(d.ContainerId) == 0 {
return nil, fmt.Errorf("missing containerId") return nil, errors.New("missing containerId")
} }
return d.client.ContainerLogs(context.Background(), d.ContainerId, dockertypes.ContainerLogsOptions{ return d.client.ContainerLogs(context.Background(), d.ContainerId, dockertypes.ContainerLogsOptions{
@ -190,13 +213,16 @@ func (d *DockerContainer) portMapping(selectFirst bool, cPort int) (containerPor
} }
if selectFirst { if selectFirst {
return 0, "", 0, fmt.Errorf("no port binding") return 0, "", 0, errors.New("no port binding")
} else { } else {
return 0, "", 0, fmt.Errorf("specified port not bound") return 0, "", 0, errors.New("specified port not bound")
} }
} }
func (d *DockerContainer) Host() string { func (d *DockerContainer) Host() string {
if d == nil {
panic("Cannot get host for a nil *DockerContainer")
}
_, hostIP, _, err := d.portMapping(true, -1) _, hostIP, _, err := d.portMapping(true, -1)
if err != nil { if err != nil {
d.t.Fatal(err) d.t.Fatal(err)
@ -210,6 +236,9 @@ func (d *DockerContainer) Host() string {
} }
func (d *DockerContainer) Port() uint { func (d *DockerContainer) Port() uint {
if d == nil {
panic("Cannot get port for a nil *DockerContainer")
}
_, _, port, err := d.portMapping(true, -1) _, _, port, err := d.portMapping(true, -1)
if err != nil { if err != nil {
d.t.Fatal(err) d.t.Fatal(err)
@ -218,6 +247,9 @@ func (d *DockerContainer) Port() uint {
} }
func (d *DockerContainer) PortFor(cPort int) uint { func (d *DockerContainer) PortFor(cPort int) uint {
if d == nil {
panic("Cannot get port for a nil *DockerContainer")
}
_, _, port, err := d.portMapping(false, cPort) _, _, port, err := d.portMapping(false, cPort)
if err != nil { if err != nil {
d.t.Fatal(err) d.t.Fatal(err)
@ -226,6 +258,9 @@ func (d *DockerContainer) PortFor(cPort int) uint {
} }
func (d *DockerContainer) NetworkSettings() dockertypes.NetworkSettings { func (d *DockerContainer) NetworkSettings() dockertypes.NetworkSettings {
if d == nil {
panic("Cannot get network settings for a nil *DockerContainer")
}
netSettings := d.ContainerJSON.NetworkSettings netSettings := d.ContainerJSON.NetworkSettings
return *netSettings return *netSettings
} }