Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main_test
- /*
- To test:
- * `go get github.com/docker/docker`
- * `go get github.com/smartystreets/goconvey/convey`
- * Edit the constants
- And then either:
- * run the goconvey web server
- * run 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 method", nil)
- return func() (string, error) {
- 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()
- }
- buf, _ := ioutil.ReadAll(resp)
- So(err, ShouldBeNil)
- So(resp, ShouldNotBeNil)
- 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)
- })
- })
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement