Skip to content

Commit

Permalink
Chores: comment and unused code minor cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <[email protected]>
  • Loading branch information
apostasie committed Oct 15, 2024
1 parent 2fc84d7 commit b908643
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 133 deletions.
25 changes: 13 additions & 12 deletions cmd/nerdctl/issues/issues_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,24 @@ import (

"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest/registry"
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
"github.com/containerd/nerdctl/v2/pkg/testutil/testregistry"
)

func TestIssue3425(t *testing.T) {
nerdtest.Setup()

var registry *testregistry.RegistryServer
var reg *registry.Server

testCase := &test.Case{
Require: nerdtest.Registry,
Setup: func(data test.Data, helpers test.Helpers) {
base := testutil.NewBase(t)
registry = testregistry.NewWithNoAuth(base, 0, false)
reg = nerdtest.RegistryWithNoAuth(data, helpers, 0, false)
reg.Setup(data, helpers)
},
Cleanup: func(data test.Data, helpers test.Helpers) {
if registry != nil {
registry.Cleanup(nil)
if reg != nil {
reg.Cleanup(data, helpers)
}
},
SubTests: []*test.Case{
Expand All @@ -52,14 +53,14 @@ func TestIssue3425(t *testing.T) {
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage)
helpers.Ensure("image", "rm", "-f", testutil.CommonImage)
helpers.Ensure("image", "pull", testutil.CommonImage)
helpers.Ensure("tag", testutil.CommonImage, fmt.Sprintf("localhost:%d/%s", registry.Port, data.Identifier()))
helpers.Ensure("tag", testutil.CommonImage, fmt.Sprintf("localhost:%d/%s", reg.Port, data.Identifier()))
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
helpers.Anyhow("rmi", "-f", fmt.Sprintf("localhost:%d/%s", registry.Port, data.Identifier()))
helpers.Anyhow("rmi", "-f", fmt.Sprintf("localhost:%d/%s", reg.Port, data.Identifier()))
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("push", fmt.Sprintf("localhost:%d/%s", registry.Port, data.Identifier()))
return helpers.Command("push", fmt.Sprintf("localhost:%d/%s", reg.Port, data.Identifier()))
},
Expected: test.Expects(0, nil, nil),
},
Expand All @@ -71,14 +72,14 @@ func TestIssue3425(t *testing.T) {
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "touch", "/something")
helpers.Ensure("image", "rm", "-f", testutil.CommonImage)
helpers.Ensure("image", "pull", testutil.CommonImage)
helpers.Ensure("commit", data.Identifier(), fmt.Sprintf("localhost:%d/%s", registry.Port, data.Identifier()))
helpers.Ensure("commit", data.Identifier(), fmt.Sprintf("localhost:%d/%s", reg.Port, data.Identifier()))
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
helpers.Anyhow("rmi", "-f", fmt.Sprintf("localhost:%d/%s", registry.Port, data.Identifier()))
helpers.Anyhow("rmi", "-f", fmt.Sprintf("localhost:%d/%s", reg.Port, data.Identifier()))
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("push", fmt.Sprintf("localhost:%d/%s", registry.Port, data.Identifier()))
return helpers.Command("push", fmt.Sprintf("localhost:%d/%s", reg.Port, data.Identifier()))
},
Expected: test.Expects(0, nil, nil),
},
Expand Down
9 changes: 5 additions & 4 deletions cmd/nerdctl/login/login_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/containerd/nerdctl/v2/pkg/imgutil/dockerconfigresolver"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
"github.com/containerd/nerdctl/v2/pkg/testutil/testca"
"github.com/containerd/nerdctl/v2/pkg/testutil/testregistry"
)
Expand Down Expand Up @@ -108,8 +109,8 @@ func TestLoginPersistence(t *testing.T) {
t.Run(fmt.Sprintf("Server %s", tc.auth), func(t *testing.T) {
t.Parallel()

username := testregistry.SafeRandomString(30) + "∞"
password := testregistry.SafeRandomString(30) + ":∞"
username := test.RandomStringBase64(30) + "∞"
password := test.RandomStringBase64(30) + ":∞"

// Add the requested authentication
var auth testregistry.Auth
Expand Down Expand Up @@ -297,8 +298,8 @@ func TestLoginAgainstVariants(t *testing.T) {
}

// Generate credentials that are specific to each registry, so that we never cross hit another one
username := testregistry.SafeRandomString(30) + "∞"
password := testregistry.SafeRandomString(30) + ":∞"
username := test.RandomStringBase64(30) + "∞"
password := test.RandomStringBase64(30) + ":∞"

// Get a CA if we want TLS
var ca *testca.CA
Expand Down
8 changes: 1 addition & 7 deletions cmd/nerdctl/network/network_create_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ func TestNetworkCreate(t *testing.T) {
testCase.SubTests = []*test.Case{
{
Description: "vanilla",
// #3491 and #3508 may have helped - commenting this out for now
// Require: nerdtest.IsFlaky("https://github.com/containerd/nerdctl/issues/3086"),
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("network", "create", data.Identifier())
netw := nerdtest.InspectNetwork(helpers, data.Identifier())
Expand Down Expand Up @@ -66,8 +64,6 @@ func TestNetworkCreate(t *testing.T) {
},
{
Description: "with MTU",
// #3491 and #3508 may have helped - commenting this out for now
// Require: nerdtest.IsFlaky("https://github.com/containerd/nerdctl/issues/3086"),
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("network", "create", data.Identifier(), "--driver", "bridge", "--opt", "com.docker.network.driver.mtu=9216")
},
Expand All @@ -81,9 +77,7 @@ func TestNetworkCreate(t *testing.T) {
},
{
Description: "with ipv6",
// #3491 and #3508 may have helped - commenting this out for now
// Require: nerdtest.IsFlaky("https://github.com/containerd/nerdctl/issues/3086"),
Require: nerdtest.OnlyIPv6,
Require: nerdtest.OnlyIPv6,
Setup: func(data test.Data, helpers test.Helpers) {
subnetStr := "2001:db8:8::/64"
data.Set("subnetStr", subnetStr)
Expand Down
3 changes: 0 additions & 3 deletions cmd/nerdctl/network/network_list_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ import (
func TestNetworkLsFilter(t *testing.T) {
testCase := nerdtest.Setup()

// #3491 and #3508 may have helped - commenting this out for now
// testCase.Require = nerdtest.IsFlaky("https://github.com/containerd/nerdctl/issues/3086"),

testCase.Setup = func(data test.Data, helpers test.Helpers) {
data.Set("identifier", data.Identifier())
data.Set("label", "mylabel=label-1")
Expand Down
3 changes: 0 additions & 3 deletions pkg/cmd/image/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ func ensureOne(ctx context.Context, client *containerd.Client, rawRef string, ta
return err
}
refDomain := distributionref.Domain(named)
// if platform == nil {
// platform = platforms.DefaultSpec()
//}
pltf := []ocispec.Platform{platform}
platformComparer := platformutil.NewMatchComparerFromOCISpecPlatformSlice(pltf)

Expand Down
97 changes: 0 additions & 97 deletions pkg/testutil/testregistry/testregistry_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package testregistry

import (
"crypto/rand"
"encoding/base64"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -249,75 +247,6 @@ func (ba *BasicAuth) Params(base *testutil.Base) []string {
return ret
}

func NewIPFSRegistry(base *testutil.Base, ca *testca.CA, port int, auth Auth, boundCleanup func(error)) *RegistryServer {
EnsureImages(base)

name := testutil.Identifier(base.T)
// listen on 0.0.0.0 to enable 127.0.0.1
listenIP := net.ParseIP("0.0.0.0")
hostIP, err := nettestutil.NonLoopbackIPv4()
assert.NilError(base.T, err, fmt.Errorf("failed finding ipv4 non loopback interface: %w", err))
port, err = portlock.Acquire(port)
assert.NilError(base.T, err, fmt.Errorf("failed acquiring port: %w", err))

containerName := fmt.Sprintf("ipfs-registry-%s-%d", name, port)
// Cleanup possible leftovers first
base.Cmd("rm", "-f", containerName).Run()

args := []string{
"run",
"--pull=never",
"-d",
"-p", fmt.Sprintf("%s:%d:%d", listenIP, port, port),
"--name", containerName,
"--entrypoint=/bin/sh",
testutil.KuboImage,
"-c", "--",
fmt.Sprintf("ipfs init && ipfs config Addresses.API /ip4/0.0.0.0/tcp/%d && ipfs daemon --offline", port),
}

cleanup := func(err error) {
result := base.Cmd("rm", "-f", containerName).Run()
errPortRelease := portlock.Release(port)
if boundCleanup != nil {
boundCleanup(err)
}
if err == nil {
assert.NilError(base.T, result.Error, fmt.Errorf("failed removing container: %w", err))
assert.NilError(base.T, errPortRelease, fmt.Errorf("failed releasing port: %w", err))
}
}

scheme := "http"

err = func() error {
cmd := base.Cmd(args...).Run()
if cmd.Error != nil {
base.T.Logf("%s:\n%s\n%s\n-------\n%s", containerName, cmd.Cmd, cmd.Stdout(), cmd.Stderr())
return cmd.Error
}

if _, err = nettestutil.HTTPGet(fmt.Sprintf("%s://%s:%s/api/v0", scheme, hostIP.String(), strconv.Itoa(port)), 30, true); err != nil {
return err
}

return nil
}()

assert.NilError(base.T, err, fmt.Errorf("failed starting IPFS registry container in a timely manner: %w", err))

return &RegistryServer{
IP: hostIP,
Port: port,
Scheme: scheme,
ListenIP: listenIP,
Cleanup: cleanup,
Logs: func() {
base.T.Logf("%s: %q", containerName, base.Cmd("logs", containerName).Run().String())
},
}
}

func NewRegistry(base *testutil.Base, ca *testca.CA, port int, auth Auth, boundCleanup func(error)) *RegistryServer {
EnsureImages(base)

Expand Down Expand Up @@ -469,29 +398,3 @@ func NewWithNoAuth(base *testutil.Base, port int, tls bool) *RegistryServer {
}
return NewRegistry(base, ca, port, &NoAuth{}, nil)
}

func NewWithBasicAuth(base *testutil.Base, user, pass string, port int, tls bool) *RegistryServer {
auth := &BasicAuth{
Username: user,
Password: pass,
}
var ca *testca.CA
if tls {
ca = testca.New(base.T)
}
return NewRegistry(base, ca, port, auth, nil)
}

func SafeRandomString(n int) string {
b := make([]byte, n)
l, err := rand.Read(b)
if err != nil {
panic(err)
}
if l != n {
panic(fmt.Errorf("expected %d bytes, got %d bytes", n, l))
}
// XXX WARNING there is something in the registry (or more likely in the way we generate htpasswd files)
// that is broken and does not resist truly random strings
return base64.URLEncoding.EncodeToString(b)
}
9 changes: 2 additions & 7 deletions pkg/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ func (b *Base) EnsureDaemonActive() {
sleep = 3 * time.Second
)
for i := 0; i < maxRetry; i++ {
cmd := exec.Command("systemctl",
append(systemctlArgs,
[]string{"is-active", target}...)...)
cmd := exec.Command("systemctl", append(systemctlArgs, "is-active", target)...)
out, err := cmd.CombinedOutput()
b.T.Logf("(retry=%d) %s", i, string(out))
if err == nil {
Expand All @@ -204,10 +202,7 @@ func (b *Base) DumpDaemonLogs(minutes int) {
b.T.Helper()
target := b.systemctlTarget()
cmd := exec.Command("journalctl",
append(b.systemctlArgs(),
[]string{"-u", target,
"--no-pager",
"-S", fmt.Sprintf("%d min ago", minutes)}...)...)
append(b.systemctlArgs(), "-u", target, "--no-pager", "-S", fmt.Sprintf("%d min ago", minutes))...)
b.T.Logf("===== %v =====", cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
Expand Down

0 comments on commit b908643

Please sign in to comment.