|
| 1 | +package ocilayout |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "os/exec" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/pivotal/image-relocation/pkg/image" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/cnabio/cnab-go/imagestore" |
| 14 | + "github.com/cnabio/cnab-go/imagestore/tests" |
| 15 | +) |
| 16 | + |
| 17 | +func TestOCILayout_PushToInsecureRegistry(t *testing.T) { |
| 18 | + // Start an insecure registry and get the port that it's running on |
| 19 | + regPort := tests.StartTestRegistry(t) |
| 20 | + registry := fmt.Sprintf("localhost:%s", regPort) |
| 21 | + |
| 22 | + // Using hello-world |
| 23 | + srcDigest, err := image.NewDigest("sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4") |
| 24 | + require.NoError(t, err, "image.NewDigest failed") |
| 25 | + sourceImg, err := image.NewName(fmt.Sprintf("docker.io/library/hello-world@%s", srcDigest.String())) |
| 26 | + require.NoError(t, err, "image.NewName failed") |
| 27 | + destImg, err := image.NewName(fmt.Sprintf("%s/hello-world:latest", registry)) |
| 28 | + require.NoError(t, err, "image.NewName failed") |
| 29 | + |
| 30 | + t.Run("failed push", func(t *testing.T) { |
| 31 | + // Try to push without passing in a http client with skipTLS set to true |
| 32 | + store, err := Create(imagestore.WithArchiveDir(t.TempDir())) |
| 33 | + require.NoError(t, err, "ocilayout.Create failed") |
| 34 | + |
| 35 | + // Add the source image to our registry |
| 36 | + dig, err := store.Add(sourceImg.String()) |
| 37 | + require.NoError(t, err, "Add failed") |
| 38 | + assert.Equal(t, srcDigest.String(), dig, "incorrect content digest returned by Add") |
| 39 | + |
| 40 | + err = store.Push(srcDigest, sourceImg, destImg) |
| 41 | + require.Errorf(t, err, "expected push to fail because skipTLS was not specified") |
| 42 | + assert.Contains(t, err.Error(), "Client sent an HTTP request to an HTTPS server", "expected push to fail because TLS wasn't configured properly") |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("successful push", func(t *testing.T) { |
| 46 | + // Create a http transport that allows connecting to our insecure registry |
| 47 | + skipTLS := http.DefaultTransport.(*http.Transport).Clone() |
| 48 | + skipTLS.TLSClientConfig.InsecureSkipVerify = true |
| 49 | + |
| 50 | + store, err := Create(imagestore.WithTransport(skipTLS), imagestore.WithArchiveDir(t.TempDir())) |
| 51 | + require.NoError(t, err, "ocilayout.Create failed") |
| 52 | + |
| 53 | + // Add the source image to our registry |
| 54 | + dig, err := store.Add(sourceImg.String()) |
| 55 | + require.NoError(t, err, "Add failed") |
| 56 | + assert.Equal(t, srcDigest.String(), dig, "incorrect content digest returned by Add") |
| 57 | + |
| 58 | + // Push a test container (hello-world) into our registry |
| 59 | + err = store.Push(srcDigest, sourceImg, destImg) |
| 60 | + require.NoError(t, err, "Push failed") |
| 61 | + |
| 62 | + // Validate the image was copied to the new location |
| 63 | + err = exec.Command("docker", "pull", destImg.String()).Run() |
| 64 | + require.NoError(t, err, "the image was not present in the destination registry") |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("push from archive", func(t *testing.T) { |
| 68 | + // Create a http transport that allows connecting to our insecure registry |
| 69 | + skipTLS := http.DefaultTransport.(*http.Transport).Clone() |
| 70 | + skipTLS.TLSClientConfig.InsecureSkipVerify = true |
| 71 | + |
| 72 | + parms := imagestore.Create( |
| 73 | + imagestore.WithArchiveDir("testdata"), |
| 74 | + imagestore.WithTransport(skipTLS)) |
| 75 | + layout, err := LocateOciLayout(parms) |
| 76 | + require.NoError(t, err, "LocateOciLayout failed") |
| 77 | + |
| 78 | + // |
| 79 | + // Add the image from the archive into the index |
| 80 | + // |
| 81 | + img, err := image.NewName("docker.io/library/hello-world@sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4") |
| 82 | + require.NoError(t, err, "NewName failed") |
| 83 | + |
| 84 | + dig, err := layout.Add(img.String()) |
| 85 | + require.NoError(t, err, "Add failed") |
| 86 | + assert.Equal(t, "sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4", dig, "incorrect content digest returned by Add") |
| 87 | + |
| 88 | + imgDig, err := image.NewDigest(dig) |
| 89 | + require.NoError(t, err, "NewDigest failed") |
| 90 | + |
| 91 | + // Push the image |
| 92 | + err = layout.Push(imgDig, img, destImg) |
| 93 | + require.NoError(t, err, "Push failed") |
| 94 | + |
| 95 | + // Validate the image was copied to the new location |
| 96 | + err = exec.Command("docker", "pull", destImg.String()).Run() |
| 97 | + require.NoError(t, err, "the image was not present in the destination registry") |
| 98 | + }) |
| 99 | +} |
0 commit comments