|
| 1 | +/* |
| 2 | + Copyright (C) nerdctl authors. |
| 3 | + Copyright (C) containerd authors. |
| 4 | +
|
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "net" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/AkihiroSuda/nerdctl/pkg/testutil" |
| 27 | + "github.com/containerd/containerd/errdefs" |
| 28 | + "github.com/pkg/errors" |
| 29 | + "gotest.tools/v3/assert" |
| 30 | +) |
| 31 | + |
| 32 | +func getNonLoopbackIPv4() (net.IP, error) { |
| 33 | + addrs, err := net.InterfaceAddrs() |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + for _, addr := range addrs { |
| 38 | + ip, _, err := net.ParseCIDR(addr.String()) |
| 39 | + if err != nil { |
| 40 | + continue |
| 41 | + } |
| 42 | + ipv4 := ip.To4() |
| 43 | + if ipv4 == nil { |
| 44 | + continue |
| 45 | + } |
| 46 | + if ipv4.IsLoopback() { |
| 47 | + continue |
| 48 | + } |
| 49 | + return ipv4, nil |
| 50 | + } |
| 51 | + return nil, errors.Wrapf(errdefs.ErrNotFound, "non-loopback IPv4 address not found, attempted=%+v", addrs) |
| 52 | +} |
| 53 | + |
| 54 | +type testRegistry struct { |
| 55 | + ip net.IP |
| 56 | + listenIP net.IP |
| 57 | + listenPort int |
| 58 | + cleanup func() |
| 59 | +} |
| 60 | + |
| 61 | +func newTestRegistry(base *testutil.Base, name string) *testRegistry { |
| 62 | + hostIP, err := getNonLoopbackIPv4() |
| 63 | + assert.NilError(base.T, err) |
| 64 | + // listen on 0.0.0.0 to enable 127.0.0.1 |
| 65 | + listenIP := net.ParseIP("0.0.0.0") |
| 66 | + const listenPort = 5000 // TODO: choose random empty port |
| 67 | + base.T.Logf("hostIP=%q, listenIP=%q, listenPort=%d", hostIP, listenIP, listenPort) |
| 68 | + |
| 69 | + registryContainerName := "reg-" + name |
| 70 | + cmd := base.Cmd("run", |
| 71 | + "-d", |
| 72 | + "-p", fmt.Sprintf("%s:%d:5000", listenIP, listenPort), |
| 73 | + "--name", registryContainerName, |
| 74 | + testutil.RegistryImage) |
| 75 | + cmd.AssertOK() |
| 76 | + if _, err = httpGet(fmt.Sprintf("http://%s:%d/v2", hostIP.String(), listenPort), 30); err != nil { |
| 77 | + base.Cmd("rm", "-f", registryContainerName).Run() |
| 78 | + base.T.Fatal(err) |
| 79 | + } |
| 80 | + return &testRegistry{ |
| 81 | + ip: hostIP, |
| 82 | + listenIP: listenIP, |
| 83 | + listenPort: listenPort, |
| 84 | + cleanup: func() { base.Cmd("rm", "-f", registryContainerName).Run() }, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestPushPlainHTTPFails(t *testing.T) { |
| 89 | + base := testutil.NewBase(t) |
| 90 | + reg := newTestRegistry(base, "test-push-plain-http-fails") |
| 91 | + defer reg.cleanup() |
| 92 | + |
| 93 | + base.Cmd("pull", testutil.AlpineImage).AssertOK() |
| 94 | + testImageRef := fmt.Sprintf("%s:%d/test-push-plain-http-fails:%s", |
| 95 | + reg.ip.String(), reg.listenPort, strings.Split(testutil.AlpineImage, ":")[1]) |
| 96 | + t.Logf("testImageRef=%q", testImageRef) |
| 97 | + base.Cmd("tag", testutil.AlpineImage, testImageRef).AssertOK() |
| 98 | + |
| 99 | + res := base.Cmd("push", testImageRef).Run() |
| 100 | + resCombined := res.Combined() |
| 101 | + t.Logf("result: exitCode=%d, out=%q", res.ExitCode, res.Combined()) |
| 102 | + assert.Assert(t, res.ExitCode != 0) |
| 103 | + assert.Assert(t, strings.Contains(resCombined, "server gave HTTP response to HTTPS client")) |
| 104 | +} |
| 105 | + |
| 106 | +func TestPushPlainHTTPLocalhost(t *testing.T) { |
| 107 | + base := testutil.NewBase(t) |
| 108 | + reg := newTestRegistry(base, "test-push-plain-localhost") |
| 109 | + defer reg.cleanup() |
| 110 | + localhostIP := "127.0.0.1" |
| 111 | + t.Logf("localhost IP=%q", localhostIP) |
| 112 | + |
| 113 | + base.Cmd("pull", testutil.AlpineImage).AssertOK() |
| 114 | + testImageRef := fmt.Sprintf("%s:%d/test-push-plain-http-insecure:%s", |
| 115 | + localhostIP, reg.listenPort, strings.Split(testutil.AlpineImage, ":")[1]) |
| 116 | + t.Logf("testImageRef=%q", testImageRef) |
| 117 | + base.Cmd("tag", testutil.AlpineImage, testImageRef).AssertOK() |
| 118 | + |
| 119 | + base.Cmd("push", testImageRef).AssertOK() |
| 120 | +} |
| 121 | + |
| 122 | +func TestPushPlainHTTPInsecure(t *testing.T) { |
| 123 | + // Skip docker, because "dockerd --insecure-registries" requires restarting the daemon |
| 124 | + testutil.DockerIncompatible(t) |
| 125 | + |
| 126 | + base := testutil.NewBase(t) |
| 127 | + reg := newTestRegistry(base, "test-push-plain-http-insecure") |
| 128 | + defer reg.cleanup() |
| 129 | + |
| 130 | + base.Cmd("pull", testutil.AlpineImage).AssertOK() |
| 131 | + testImageRef := fmt.Sprintf("%s:%d/test-push-plain-http-insecure:%s", |
| 132 | + reg.ip.String(), reg.listenPort, strings.Split(testutil.AlpineImage, ":")[1]) |
| 133 | + t.Logf("testImageRef=%q", testImageRef) |
| 134 | + base.Cmd("tag", testutil.AlpineImage, testImageRef).AssertOK() |
| 135 | + |
| 136 | + base.Cmd("--insecure-registry", "push", testImageRef).AssertOK() |
| 137 | +} |
0 commit comments