Advertisement
Guest User

Untitled

a guest
Nov 17th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. package main_test
  2.  
  3. /*
  4. To test:
  5.  
  6. * `go get github.com/docker/docker`
  7. * `go get github.com/smartystreets/goconvey/convey`
  8.  
  9. * Edit the constants
  10.  
  11. And then either:
  12. * run the goconvey web server
  13. * run go test .
  14.  
  15. */
  16. import (
  17. "testing"
  18. "github.com/docker/docker/client"
  19. . "github.com/smartystreets/goconvey/convey"
  20. "context"
  21. "github.com/docker/docker/api/types"
  22. "io/ioutil"
  23. "encoding/base64"
  24. "strings"
  25. "encoding/json"
  26. )
  27.  
  28. const (
  29. // Replace these with references to an image in a private repository
  30. registryUsername = "AWS"
  31. registryPassword = "Base64EncodedPassword"
  32. imageNameTag = "12345.dkr.ecr.us-east-1.amazonaws.com/dir/dir/hello-world:1.0"
  33. )
  34.  
  35. func registryAuthentication(image string) types.RequestPrivilegeFunc {
  36. Convey("In registryAuthentication method", nil)
  37. return func() (string, error) {
  38. serverAddress := image[:strings.IndexRune(image, '/')]
  39. authConfig := types.AuthConfig{
  40. Username: registryUsername,
  41. Password: registryPassword,
  42. ServerAddress: serverAddress,
  43. }
  44. Print(authConfig.ServerAddress)
  45. buf, err := json.Marshal(authConfig)
  46. if err != nil {
  47. return "", nil
  48. }
  49. return base64.URLEncoding.EncodeToString(buf), nil
  50. }
  51. }
  52.  
  53. func TestDockerRegistryPull(t *testing.T) {
  54. Convey("With docker", t, func() {
  55. c, err := client.NewClient("unix:///var/run/docker.sock", "v1.24", nil, nil)
  56.  
  57. Convey("Get a new Client", func() {
  58.  
  59. So(err, ShouldBeNil)
  60. So(c, ShouldNotBeNil)
  61. })
  62.  
  63. Convey("Inspect the version", func() {
  64.  
  65. So(c.ClientVersion(), ShouldEqual, "v1.24")
  66.  
  67. })
  68.  
  69. Convey("Pull hello world", func() {
  70.  
  71. resp, err := c.ImagePull(context.Background(), "hello-world", types.ImagePullOptions{})
  72. if err == nil {
  73. defer resp.Close()
  74. }
  75. buf, _ := ioutil.ReadAll(resp)
  76. So(err, ShouldBeNil)
  77. So(resp, ShouldNotBeNil)
  78. Printf("\nImage pull response: %v; err: %#v", string(buf), err)
  79. })
  80.  
  81. Convey("Pull a private image", func() {
  82.  
  83. image := imageNameTag
  84. resp, err := c.ImagePull(context.Background(), image, types.ImagePullOptions{
  85. PrivilegeFunc: registryAuthentication(image),
  86. })
  87. if err == nil {
  88. defer resp.Close()
  89. }
  90. Convey("Evaluating response of ImagePull", func() {
  91. So(err, ShouldBeNil)
  92. buf, _ := ioutil.ReadAll(resp)
  93. msg := string(buf)
  94. So(resp, ShouldNotBeNil)
  95. So(msg, ShouldNotContainSubstring, "unauthorized")
  96. Printf("\nImage pull response: %v; err: %#v", msg, err)
  97. })
  98. })
  99.  
  100. })
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement