pkg/ioutils: some cleanups in tests
- remove gotest.tools dependency as it was only used in one test, and only for a trivial check
- use t.TempDir()
- rename vars that collided with package types
- don't use un-keyed structs
- explicitly ignore some errors to please linters


**- A picture of a cute animal (not mandatory but encouraged)**


pkg/ioutils/readers_test.go
package ioutils // import "github.com/docker/docker/pkg/ioutils"

import (
	"context"
	"fmt"
	"io"
	"strings"
	"testing"
	"time"

	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

// Implement io.Reader
type errorReader struct{}

func (r *errorReader) Read(p []byte) (int, error) {
	return 0, fmt.Errorf("error reader always fail")
}

func TestReadCloserWrapperClose(t *testing.T) {
	reader := strings.NewReader("A string reader")
	wrapper := NewReadCloserWrapper(reader, func() error {
		return fmt.Errorf("This will be called when closing")
	})
	err := wrapper.Close()
	if err == nil || !strings.Contains(err.Error(), "This will be called when closing") {
		t.Fatalf("readCloserWrapper should have call the anonymous func and thus, fail.")
	}
}

func TestReaderErrWrapperReadOnError(t *testing.T) {
	called := false
	reader := &errorReader{}
	wrapper := NewReaderErrWrapper(reader, func() {
		called = true
	})
	_, err := wrapper.Read([]byte{})
	assert.Check(t, is.Error(err, "error reader always fail"))
	if !called {
		t.Fatalf("readErrWrapper should have call the anonymous function on failure")
	}
}

func TestReaderErrWrapperRead(t *testing.T) {
	reader := strings.NewReader("a string reader.")
	wrapper := NewReaderErrWrapper(reader, func() {
		t.Fatalf("readErrWrapper should not have called the anonymous function")
	})
	// Read 20 byte (should be ok with the string above)
	num, err := wrapper.Read(make([]byte, 20))
	if err != nil {
		t.Fatal(err)
	}
	if num != 16 {
		t.Fatalf("readerErrWrapper should have read 16 byte, but read %d", num)
	}
}

type perpetualReader struct{}

func (p *perpetualReader) Read(buf []byte) (n int, err error) {
	for i := 0; i != len(buf); i++ {
		buf[i] = 'a'
	}
	return len(buf), nil
}

func TestCancelReadCloser(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
	defer cancel()
	cancelReadCloser := NewCancelReadCloser(ctx, io.NopCloser(&perpetualReader{}))
	for {
		var buf [128]byte
		_, err := cancelReadCloser.Read(buf[:])
		if err == context.DeadlineExceeded {
			break
		} else if err != nil {
			t.Fatalf("got unexpected error: %v", err)
		}
	}
}

package ioutils // import "github.com/docker/docker/pkg/ioutils"

import (
	"context"
	"errors"
	"io"
	"strings"
	"testing"
	"testing/iotest"
	"time"
)

func TestReadCloserWrapperClose(t *testing.T) {
	const text = "hello world"
	testErr := errors.New("this will be called when closing")
	wrapper := NewReadCloserWrapper(strings.NewReader(text), func() error {
		return testErr
	})

	buf, err := io.ReadAll(wrapper)
	if err != nil {
		t.Errorf("io.ReadAll(wrapper) err = %v", err)
	}
	if string(buf) != text {
		t.Errorf("expected %v, got: %v", text, string(buf))
	}
	err = wrapper.Close()
	if !errors.Is(err, testErr) {
		// readCloserWrapper should have called the anonymous func and thus, fail
		t.Errorf("expected %v, got: %v", testErr, err)
	}
}

func TestReaderErrWrapperReadOnError(t *testing.T) {
	called := false
	expectedErr := errors.New("error reader always fail")
	wrapper := NewReaderErrWrapper(iotest.ErrReader(expectedErr), func() {
		called = true
	})
	_, err := wrapper.Read([]byte{})
	if !errors.Is(err, expectedErr) {
		t.Errorf("expected %v, got: %v", expectedErr, err)
	}
	if !called {
		t.Fatalf("readErrWrapper should have called the anonymous function on failure")
	}
}

func TestReaderErrWrapperRead(t *testing.T) {
	const text = "hello world"
	wrapper := NewReaderErrWrapper(strings.NewReader(text), func() {
		t.Fatalf("readErrWrapper should not have called the anonymous function")
	})
	num, err := wrapper.Read(make([]byte, len(text)+10))
	if err != nil {
		t.Error(err)
	}
	if expected := len(text); num != expected {
		t.Errorf("readerErrWrapper should have read %d byte, but read %d", expected, num)
	}
}

type perpetualReader struct{}

func (p *perpetualReader) Read(buf []byte) (n int, err error) {
	for i := 0; i != len(buf); i++ {
		buf[i] = 'a'
	}
	return len(buf), nil
}

func TestCancelReadCloser(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
	defer cancel()
	crc := NewCancelReadCloser(ctx, io.NopCloser(&perpetualReader{}))
	for {
		var buf [128]byte
		_, err := crc.Read(buf[:])
		if err == context.DeadlineExceeded {
			break
		} else if err != nil {
			t.Fatalf("got unexpected error: %v", err)
		}
	}
}

TIL (I really should look at Go's utils more often).

Let me make that change 👍 