Skip to content

Commit

Permalink
fix docker package
Browse files Browse the repository at this point in the history
Go modules had pulled down the latest semvar tag, which
was actually super old, had to specify the commit hash
of github.com/moby/moby to get the actual up-to-date
module.

Fixed ipfs package dependencies as they are now go module
compatible and so indirect deps don't need to be listed in
this packages go.mod file.

Fixed tests so that they didn't attempt to pull a private
image, as well as not running TestRemoveAllImages by default,
as it does as it says...

Fixed util to take the official docker images into account,
as their url is docker.io/library/<container> but the container
name is just <container>, instead of library/<container>.
  • Loading branch information
lanzafame committed Mar 7, 2019
1 parent d8c07ab commit 58975f1
Show file tree
Hide file tree
Showing 552 changed files with 60,143 additions and 14,340 deletions.
12 changes: 6 additions & 6 deletions docker/docker.go
Expand Up @@ -35,10 +35,12 @@ func NewClient(config *Config) *Client {

// newEnvClient returns a new client instance based on environment variables
func newEnvClient(config *Config) *Client {
cl, err := client.NewEnvClient()
ctx := context.Background()
cl, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
log.Fatalf("[docker] %s", err)
}
cl.NegotiateAPIVersion(ctx)

return &Client{
client: cl,
Expand Down Expand Up @@ -97,13 +99,11 @@ func (c *Client) HasImage(imageID string) (bool, error) {
func (c *Client) PullImage(imageID string) error {
reader, err := c.client.ImagePull(context.Background(), imageID, types.ImagePullOptions{})
if err != nil {
fmt.Errorf("[docker] error pulling image: %v", err)
return err
return fmt.Errorf("[docker] error pulling image: %v", err)
}
defer reader.Close()

if c.debug {
io.Copy(os.Stdout, reader)
}
io.Copy(ioutil.Discard, reader)

return nil
}
Expand Down
38 changes: 29 additions & 9 deletions docker/docker_test.go
@@ -1,16 +1,25 @@
package docker

import (
"context"
"io"
"io/ioutil"
"os"
"testing"

types "github.com/docker/docker/api/types"
dclient "github.com/docker/docker/client"
)

var (
testImage = "docker.io/miguelmota/hello-world"
testImage = "docker.io/library/alpine"
testImageTar = "hello-world.tar"
)

func init() {
createTestTar()
}

func TestNew(t *testing.T) {
client := createClient()
if client == nil {
Expand Down Expand Up @@ -69,7 +78,7 @@ func TestReadImage(t *testing.T) {
t.Error(err)
}

io.Copy(os.Stdout, reader)
io.Copy(ioutil.Discard, reader)
}

func TestLoadImage(t *testing.T) {
Expand Down Expand Up @@ -140,6 +149,7 @@ func TestRemoveImage(t *testing.T) {
}

func TestRemoveAllImages(t *testing.T) {
t.Skip("Skipping TestRemoveAllImages... Comment skip call to run test. Caution it will remove all images.")
client := createClient()
err := client.RemoveAllImages()
if err != nil {
Expand Down Expand Up @@ -167,22 +177,32 @@ func createClient() *Client {
}

func createTestTar() {
client := createClient()
err := client.PullImage(testImage)
ctx := context.Background()
cli, err := dclient.NewClientWithOpts(dclient.FromEnv)
if err != nil {
panic(err)
}
cli.NegotiateAPIVersion(ctx)

err = client.SaveImageTar(testImage, testImageTar)
pullR, err := cli.ImagePull(ctx, testImage, types.ImagePullOptions{})
if err != nil {
panic(err)
}

io.Copy(ioutil.Discard, pullR)

saveR, err := cli.ImageSave(ctx, []string{testImage})

fo, err := os.Create(testImageTar)
if err != nil {
panic(err)
}

defer fo.Close()

io.Copy(fo, saveR)
}

func cleanUp() {
os.Remove(testImageTar)
}

func init() {
createTestTar()
}
8 changes: 6 additions & 2 deletions docker/util.go
@@ -1,17 +1,21 @@
package docker

import "regexp"
import (
"regexp"
"strings"
)

// ShortImageID returns the short version of an image ID
func ShortImageID(imageID string) string {
re := regexp.MustCompile(`(sha256:)?([0-9a-zA-Z]{12}).*`)
return re.ReplaceAllString(imageID, `$2`)
}

// TestStripImageTagHost strips the host from an image tag
// StripImageTagHost strips the host from an image tag
func StripImageTagHost(imageTag string) string {
re := regexp.MustCompile(`(.*\..*?\/)?(.*)`)
matches := re.FindStringSubmatch(imageTag)
imageTag = matches[len(matches)-1]
imageTag = strings.TrimPrefix(imageTag, "library/")
return imageTag
}
2 changes: 2 additions & 0 deletions docker/util_test.go
Expand Up @@ -48,6 +48,8 @@ func TestStripImageTagHost(t *testing.T) {
{"docker.io/miguelmota/hello-world", "miguelmota/hello-world"},
{"miguelmota/hello-world:latest", "miguelmota/hello-world:latest"},
{"miguelmota/hello-world", "miguelmota/hello-world"},
{"docker.io/library/alpine:latest", "alpine:latest"},
{"docker.io/library/alpine", "alpine"},
} {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
got := StripImageTagHost(tt.in)
Expand Down
42 changes: 15 additions & 27 deletions go.mod
@@ -1,42 +1,30 @@
module github.com/miguelmota/ipdr

go 1.12

require (
github.com/Microsoft/go-winio v0.4.11 // indirect
github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32 // indirect
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927 // indirect
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.12 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v1.13.1
github.com/docker/docker v0.7.3-0.20190307005417-54dddadc7d5d
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.3.3 // indirect
github.com/fatih/color v1.7.0
github.com/gogo/protobuf v1.2.0 // indirect
github.com/gxed/hashland v0.0.0-20180221191214-d9f6b97f8db2 // indirect
github.com/google/go-cmp v0.2.0 // indirect
github.com/gorilla/mux v1.7.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/ipfs/go-ipfs-api v1.3.5
github.com/ipfs/go-ipfs-cmdkit v1.1.3 // indirect
github.com/ipfs/go-ipfs-api v0.0.1
github.com/jbenet/go-base58 v0.0.0-20150317085156-6237cf65f3a6
github.com/libp2p/go-flow-metrics v0.2.0 // indirect
github.com/libp2p/go-libp2p-crypto v2.0.5+incompatible // indirect
github.com/libp2p/go-libp2p-metrics v2.1.7+incompatible // indirect
github.com/libp2p/go-libp2p-peer v2.4.0+incompatible // indirect
github.com/libp2p/go-libp2p-protocol v1.0.0 // indirect
github.com/libp2p/go-libp2p-pubsub v0.11.10 // indirect
github.com/mattn/go-colorable v0.1.0 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mr-tron/base58 v1.1.0 // indirect
github.com/multiformats/go-multiaddr v1.4.0 // indirect
github.com/multiformats/go-multiaddr-dns v0.2.5 // indirect
github.com/multiformats/go-multiaddr-net v1.7.1 // indirect
github.com/multiformats/go-multihash v1.0.8 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.6 // indirect
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/sirupsen/logrus v1.3.0
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c // indirect
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd // indirect
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect
google.golang.org/grpc v1.19.0 // indirect
gotest.tools v2.2.0+incompatible // indirect
)

0 comments on commit 58975f1

Please sign in to comment.