Skip to content

Instantly share code, notes, and snippets.

@deinspanjer
Last active November 17, 2016 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deinspanjer/6245398f8b0607a508857f90c4e97bcd to your computer and use it in GitHub Desktop.
Save deinspanjer/6245398f8b0607a508857f90c4e97bcd to your computer and use it in GitHub Desktop.
package main_test
/*
To test:
* `go get github.com/docker/docker`
* `go get github.com/smartystreets/goconvey/convey`
* Edit the constants
And then run either:
* `goconvey`
* `go test .`
*/
import (
"testing"
"github.com/docker/docker/client"
. "github.com/smartystreets/goconvey/convey"
"context"
"github.com/docker/docker/api/types"
"io/ioutil"
"encoding/base64"
"strings"
"encoding/json"
)
const (
// Replace these with references to an image in a private repository
registryUsername = "AWS"
registryPassword = "Base64EncodedPassword"
imageNameTag = "12345.dkr.ecr.us-east-1.amazonaws.com/dir/dir/hello-world:1.0"
)
func registryAuthentication(image string) types.RequestPrivilegeFunc {
Convey("In registryAuthentication function", nil)
return func() (string, error) {
Convey("In RequestPrivilegeFunc", nil)
serverAddress := image[:strings.IndexRune(image, '/')]
authConfig := types.AuthConfig{
Username: registryUsername,
Password: registryPassword,
ServerAddress: serverAddress,
}
Print(authConfig.ServerAddress)
buf, err := json.Marshal(authConfig)
if err != nil {
return "", nil
}
return base64.URLEncoding.EncodeToString(buf), nil
}
}
func TestDockerRegistryPull(t *testing.T) {
Convey("With docker", t, func() {
c, err := client.NewClient("unix:///var/run/docker.sock", "v1.24", nil, nil)
Convey("Get a new Client", func() {
So(err, ShouldBeNil)
So(c, ShouldNotBeNil)
})
Convey("Inspect the version", func() {
So(c.ClientVersion(), ShouldEqual, "v1.24")
})
Convey("Pull hello world", func() {
resp, err := c.ImagePull(context.Background(), "hello-world", types.ImagePullOptions{})
if err == nil {
defer resp.Close()
}
Convey("Evaluating response of ImagePull", func() {
So(err, ShouldBeNil)
buf, _ := ioutil.ReadAll(resp)
msg := string(buf)
So(resp, ShouldNotBeNil)
So(msg, ShouldContainSubstring, "Pulling from library/hello-world")
Printf("\nImage pull response: %v; err: %#v", string(buf), err)
})
})
Convey("Pull a private image", func() {
image := imageNameTag
resp, err := c.ImagePull(context.Background(), image, types.ImagePullOptions{
PrivilegeFunc: registryAuthentication(image),
})
if err == nil {
defer resp.Close()
}
Convey("Evaluating response of ImagePull", func() {
So(err, ShouldBeNil)
buf, _ := ioutil.ReadAll(resp)
msg := string(buf)
So(resp, ShouldNotBeNil)
So(msg, ShouldNotContainSubstring, "unauthorized")
Printf("\nImage pull response: %v; err: %#v", msg, err)
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment