Skip to content

Commit

Permalink
Merge pull request #211 from dlion/524-insecure-registries
Browse files Browse the repository at this point in the history
Imgutil adding tests, test utils and refactor
  • Loading branch information
natalieparellano authored Aug 2, 2023
2 parents 3d8327d + 904440a commit 5c57feb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 34 deletions.
1 change: 1 addition & 0 deletions remote/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func WithPreviousImage(imageName string) ImageOption {
// the image. The referenced images could include the base image, a previous image, or the image itself.
func WithRegistrySetting(repository string, insecure, insecureSkipVerify bool) ImageOption {
return func(opts *options) error {
opts.registrySettings = make(map[string]registrySetting)
opts.registrySettings[repository] = registrySetting{
insecure: insecure,
insecureSkipVerify: insecureSkipVerify,
Expand Down
53 changes: 31 additions & 22 deletions remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertNil(t, img.Save())

os, err := img.OS()
osName, err := img.OS()
h.AssertNil(t, err)
h.AssertEq(t, os, "linux")
h.AssertEq(t, osName, "linux")

osVersion, err := img.OSVersion()
h.AssertNil(t, err)
Expand Down Expand Up @@ -162,9 +162,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertEq(t, arch, "arm")

os, err := img.OS()
osName, err := img.OS()
h.AssertNil(t, err)
h.AssertEq(t, os, "linux")
h.AssertEq(t, osName, "linux")

_, err = img.TopLayer()
h.AssertError(t, err, "has no layers")
Expand All @@ -185,9 +185,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
)
h.AssertNil(t, err)

os, err := img.OS()
osName, err := img.OS()
h.AssertNil(t, err)
h.AssertEq(t, os, "linux")
h.AssertEq(t, osName, "linux")

osVersion, err := img.OSVersion()
h.AssertNil(t, err)
Expand All @@ -201,6 +201,15 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
defer readCloser.Close()
})
it("tries to pull the image from an insecure registry if WithRegistrySettings insecure has been set", func() {
_, err := remote.NewImage(
repoName,
authn.DefaultKeychain,
remote.FromBaseImage("host.docker.internal/bar"),
remote.WithRegistrySetting("host.docker.internal", true, true))

h.AssertError(t, err, "http://")
})

it("sets the initial state from a windows/amd64 base image", func() {
baseImageName := "mcr.microsoft.com/windows/nanoserver@sha256:06281772b6a561411d4b338820d94ab1028fdeb076c85350bbc01e80c4bfa2b4"
Expand All @@ -213,9 +222,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
)
h.AssertNil(t, err)

os, err := img.OS()
osName, err := img.OS()
h.AssertNil(t, err)
h.AssertEq(t, os, "windows")
h.AssertEq(t, osName, "windows")

osVersion, err := img.OSVersion()
h.AssertNil(t, err)
Expand Down Expand Up @@ -243,9 +252,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
)
h.AssertNil(t, err)

os, err := img.OS()
osName, err := img.OS()
h.AssertNil(t, err)
h.AssertEq(t, os, "linux")
h.AssertEq(t, osName, "linux")

osVersion, err := img.OSVersion()
h.AssertNil(t, err)
Expand Down Expand Up @@ -300,9 +309,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertEq(t, arch, "amd64")

os, err := img.OS()
osName, err := img.OS()
h.AssertNil(t, err)
h.AssertEq(t, os, "windows")
h.AssertEq(t, osName, "windows")

osVersion, err := img.OSVersion()
h.AssertNil(t, err)
Expand Down Expand Up @@ -330,9 +339,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertEq(t, arch, "amd64")

os, err := img.OS()
osName, err := img.OS()
h.AssertNil(t, err)
h.AssertEq(t, os, "windows")
h.AssertEq(t, osName, "windows")

osVersion, err := img.OSVersion()
h.AssertNil(t, err)
Expand Down Expand Up @@ -361,9 +370,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertEq(t, arch, "amd64")

os, err := img.OS()
osName, err := img.OS()
h.AssertNil(t, err)
h.AssertEq(t, os, "linux")
h.AssertEq(t, osName, "linux")
})
})

Expand All @@ -387,9 +396,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertEq(t, arch, "arm")

os, err := img.OS()
osName, err := img.OS()
h.AssertNil(t, err)
h.AssertEq(t, os, "windows")
h.AssertEq(t, osName, "windows")

osVersion, err := img.OSVersion()
h.AssertNil(t, err)
Expand Down Expand Up @@ -422,9 +431,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertEq(t, arch, "arm")

os, err := img.OS()
osName, err := img.OS()
h.AssertNil(t, err)
h.AssertEq(t, os, "linux")
h.AssertEq(t, osName, "linux")

osVersion, err := img.OSVersion()
h.AssertNil(t, err)
Expand Down Expand Up @@ -452,9 +461,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertEq(t, arch, "arm")

os, err := img.OS()
osName, err := img.OS()
h.AssertNil(t, err)
h.AssertEq(t, os, "windows")
h.AssertEq(t, osName, "windows")

osVersion, err := img.OSVersion()
h.AssertNil(t, err)
Expand Down
6 changes: 3 additions & 3 deletions testhelpers/docker_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func WithAuth(dockerConfigDir string) RegistryOption {
}

func NewDockerRegistry(ops ...RegistryOption) *DockerRegistry {
registry := &DockerRegistry{
dockerRegistry := &DockerRegistry{
Name: "test-registry-" + RandString(10),
}

for _, op := range ops {
op(registry)
op(dockerRegistry)
}

return registry
return dockerRegistry
}

// BasicAuth wraps a handler, allowing requests with matching username and password headers, otherwise rejecting with a 401
Expand Down
18 changes: 9 additions & 9 deletions testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func RandString(n int) string {
return string(b)
}

// Assert deep equality (and provide useful difference as a test failure)
// AssertEq asserts deep equality (and provides a useful difference as a test failure)
func AssertEq(t *testing.T, actual, expected interface{}) {
t.Helper()
if diff := cmp.Diff(actual, expected); diff != "" {
Expand Down Expand Up @@ -191,10 +191,10 @@ func PullIfMissing(t *testing.T, docker dockercli.CommonAPIClient, ref string) {
func DockerRmi(dockerCli dockercli.CommonAPIClient, repoNames ...string) error {
var err error
ctx := context.Background()
for _, name := range repoNames {
for _, repoName := range repoNames {
_, e := dockerCli.ImageRemove(
ctx,
name,
repoName,
dockertypes.ImageRemoveOptions{PruneChildren: true},
)
if e != nil && err == nil {
Expand Down Expand Up @@ -231,12 +231,12 @@ func PushImage(t *testing.T, dockerCli dockercli.CommonAPIClient, refStr string)
func DeleteRegistryBlob(t *testing.T, repoName string, digest v1.Hash, encodedAuth string) {
ref, err := name.ParseReference(repoName, name.WeakValidation)
AssertNil(t, err)
url := url.URL{
urlFromRegistryInfo := url.URL{
Scheme: ref.Context().Registry.Scheme(),
Host: ref.Context().RegistryStr(),
Path: fmt.Sprintf("/v2/%s/blobs/%s", ref.Context().RepositoryStr(), digest),
}
req, err := http.NewRequest(http.MethodDelete, url.String(), nil)
req, err := http.NewRequest(http.MethodDelete, urlFromRegistryInfo.String(), nil)
AssertNil(t, err)
req.Header.Add("Authorization", "Basic "+encodedAuth)
client := &http.Client{}
Expand Down Expand Up @@ -526,8 +526,8 @@ func AssertOCIMediaTypes(t *testing.T, image v1.Image) {
AssertNotEq(t, manifest.MediaType, "")
AssertEq(t, manifest.Config.MediaType, types.OCIConfigJSON)

for _, layer := range manifest.Layers {
AssertEq(t, layer.MediaType, types.OCILayer)
for _, manifestLayer := range manifest.Layers {
AssertEq(t, manifestLayer.MediaType, types.OCILayer)
}
}

Expand All @@ -543,8 +543,8 @@ func AssertDockerMediaTypes(t *testing.T, image v1.Image) {
AssertNotEq(t, manifest.MediaType, "")
AssertEq(t, manifest.Config.MediaType, types.DockerConfigJSON)

for _, layer := range manifest.Layers {
AssertEq(t, layer.MediaType, types.DockerLayer)
for _, manifestLayer := range manifest.Layers {
AssertEq(t, manifestLayer.MediaType, types.DockerLayer)
}
}

Expand Down

0 comments on commit 5c57feb

Please sign in to comment.