ronantakizawa/codereview-bench huggingface dataset:
Repository: moby/moby
File: libnetwork/endpoint_store_test.go (Go)

Code before:
	nw := &Network{id: "testNetwork"}
	ep1 := &Endpoint{network: nw, id: "testEndpoint1"}
	err = c.storeEndpoint(context.Background(), ep1)
	assert.NilError(t, err)

	// Then a second endpoint
	ep2 := &Endpoint{network: nw, id: "testEndpoint2"}
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)

	epSorter := func(a, b *Endpoint) int {
		if a.id < b.id {
			return -1
		}
		if a.id > b.id {
			return 1
		}
		return 0
	}

	// Check that we can find both endpoints
	found := c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	slices.SortFunc(found, epSorter)
	assert.Equal(t, len(found), 2)
	assert.Check(t, found[0] == ep1, "got: %s; expected: %s", found[0].id, ep1.id)
	assert.Check(t, found[1] == ep2, "got: %s; expected: %s", found[1].id, ep2.id)

	// Delete the first endpoint
	err = c.deleteStoredEndpoint(ep1)
	assert.NilError(t, err)

	// Check that we can only find the second endpoint
	found = c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	assert.Equal(t, len(found), 1)
	assert.Check(t, found[0] == ep2, "got: %s; expected: %s", found[0].id, ep2.id)

	// Store the second endpoint again
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)
}


Review comment (suggestion):
I think this would work? ...
```suggestion
	assert.Check(t, is.Equal(found[0], ep1))
	assert.Check(t, is.Equal(found[1], ep2))
```

Code after:

	// Insert a first endpoint
	nw := &Network{id: "testNetwork"}
	ep1 := &Endpoint{network: nw, id: "testEndpoint1"}
	err = c.storeEndpoint(context.Background(), ep1)
	assert.NilError(t, err)

	// Then a second endpoint
	ep2 := &Endpoint{network: nw, id: "testEndpoint2"}
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)

	// Check that we can find both endpoints, and that the returned values are
	// not copies of the original ones.
	found := c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	slices.SortFunc(found, func(a, b *Endpoint) int { return strings.Compare(a.id, b.id) })
	assert.Equal(t, len(found), 2)
	assert.Check(t, is.Equal(found[0], ep1), "got: %s; expected: %s", found[0].id, ep1.id)
	assert.Check(t, is.Equal(found[1], ep2), "got: %s; expected: %s", found[1].id, ep1.id)

	// Delete the first endpoint
	err = c.deleteStoredEndpoint(ep1)
	assert.NilError(t, err)

	// Check that we can only find the second endpoint
	found = c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	assert.Equal(t, len(found), 1)
	assert.Check(t, is.Equal(found[0], ep2), "got: %s; expected: %s", found[0].id, ep2.id)

	// Store the second endpoint again
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)
}
Repository: moby/moby
File: libnetwork/endpoint_store_test.go (Go)

Code before:
	nw := &Network{id: "testNetwork"}
	ep1 := &Endpoint{network: nw, id: "testEndpoint1"}
	err = c.storeEndpoint(context.Background(), ep1)
	assert.NilError(t, err)

	// Then a second endpoint
	ep2 := &Endpoint{network: nw, id: "testEndpoint2"}
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)

	epSorter := func(a, b *Endpoint) int {
		if a.id < b.id {
			return -1
		}
		if a.id > b.id {
			return 1
		}
		return 0
	}

	// Check that we can find both endpoints
	found := c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	slices.SortFunc(found, epSorter)
	assert.Equal(t, len(found), 2)
	assert.Check(t, found[0] == ep1, "got: %s; expected: %s", found[0].id, ep1.id)
	assert.Check(t, found[1] == ep2, "got: %s; expected: %s", found[1].id, ep2.id)

	// Delete the first endpoint
	err = c.deleteStoredEndpoint(ep1)
	assert.NilError(t, err)

	// Check that we can only find the second endpoint
	found = c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	assert.Equal(t, len(found), 1)
	assert.Check(t, found[0] == ep2, "got: %s; expected: %s", found[0].id, ep2.id)

	// Store the second endpoint again
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)
}


Review comment (suggestion):
using `is.Equal` saves you from writing the message :) 
```suggestion
	assert.Check(t, is.Equal(found[0], ep1))
	assert.Check(t, is.Equal(found[1], ep2))
```

Code after:

	// Insert a first endpoint
	nw := &Network{id: "testNetwork"}
	ep1 := &Endpoint{network: nw, id: "testEndpoint1"}
	err = c.storeEndpoint(context.Background(), ep1)
	assert.NilError(t, err)

	// Then a second endpoint
	ep2 := &Endpoint{network: nw, id: "testEndpoint2"}
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)

	// Check that we can find both endpoints, and that the returned values are
	// not copies of the original ones.
	found := c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	slices.SortFunc(found, func(a, b *Endpoint) int { return strings.Compare(a.id, b.id) })
	assert.Equal(t, len(found), 2)
	assert.Check(t, is.Equal(found[0], ep1), "got: %s; expected: %s", found[0].id, ep1.id)
	assert.Check(t, is.Equal(found[1], ep2), "got: %s; expected: %s", found[1].id, ep1.id)

	// Delete the first endpoint
	err = c.deleteStoredEndpoint(ep1)
	assert.NilError(t, err)

	// Check that we can only find the second endpoint
	found = c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	assert.Equal(t, len(found), 1)
	assert.Check(t, is.Equal(found[0], ep2), "got: %s; expected: %s", found[0].id, ep2.id)

	// Store the second endpoint again
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)
}
Repository: moby/moby
File: libnetwork/endpoint_store_test.go (Go)

Code before:

func TestEndpointStore(t *testing.T) {
	configOption := config.OptionDataDir(t.TempDir())
	c, err := New(configOption)
	assert.NilError(t, err)
	defer c.Stop()

	// Insert a first endpoint
	nw := &Network{id: "testNetwork"}
	ep1 := &Endpoint{network: nw, id: "testEndpoint1"}
	err = c.storeEndpoint(context.Background(), ep1)
	assert.NilError(t, err)

	// Then a second endpoint
	ep2 := &Endpoint{network: nw, id: "testEndpoint2"}
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)

	epSorter := func(a, b *Endpoint) int {
		if a.id < b.id {
			return -1
		}
		if a.id > b.id {
			return 1
		}
		return 0
	}

	// Check that we can find both endpoints
	found := c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	slices.SortFunc(found, epSorter)
	assert.Equal(t, len(found), 2)
	assert.Check(t, found[0] == ep1, "got: %s; expected: %s", found[0].id, ep1.id)
	assert.Check(t, found[1] == ep2, "got: %s; expected: %s", found[1].id, ep2.id)

	// Delete the first endpoint
	err = c.deleteStoredEndpoint(ep1)
	assert.NilError(t, err)

	// Check that we can only find the second endpoint
	found = c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	assert.Equal(t, len(found), 1)
	assert.Check(t, found[0] == ep2, "got: %s; expected: %s", found[0].id, ep2.id)

	// Store the second endpoint again
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)
}


Review comment (refactor):
Non-blocking; if you want to simplify, this could probably be;

```suggestion
		return strings.Compare(a.id, b.id)
```

Code after:
	is "gotest.tools/v3/assert/cmp"
)

func TestEndpointStore(t *testing.T) {
	configOption := config.OptionDataDir(t.TempDir())
	c, err := New(configOption)
	assert.NilError(t, err)
	defer c.Stop()

	// Insert a first endpoint
	nw := &Network{id: "testNetwork"}
	ep1 := &Endpoint{network: nw, id: "testEndpoint1"}
	err = c.storeEndpoint(context.Background(), ep1)
	assert.NilError(t, err)

	// Then a second endpoint
	ep2 := &Endpoint{network: nw, id: "testEndpoint2"}
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)

	// Check that we can find both endpoints, and that the returned values are
	// not copies of the original ones.
	found := c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	slices.SortFunc(found, func(a, b *Endpoint) int { return strings.Compare(a.id, b.id) })
	assert.Equal(t, len(found), 2)
	assert.Check(t, is.Equal(found[0], ep1), "got: %s; expected: %s", found[0].id, ep1.id)
	assert.Check(t, is.Equal(found[1], ep2), "got: %s; expected: %s", found[1].id, ep1.id)

	// Delete the first endpoint
	err = c.deleteStoredEndpoint(ep1)
	assert.NilError(t, err)

	// Check that we can only find the second endpoint
	found = c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	assert.Equal(t, len(found), 1)
	assert.Check(t, is.Equal(found[0], ep2), "got: %s; expected: %s", found[0].id, ep2.id)

	// Store the second endpoint again
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)
}
Repository: moby/moby
File: libnetwork/endpoint_store_test.go (Go)

Code before:
	defer c.Stop()

	// Insert a first endpoint
	nw := &Network{id: "testNetwork"}
	ep1 := &Endpoint{network: nw, id: "testEndpoint1"}
	err = c.storeEndpoint(context.Background(), ep1)
	assert.NilError(t, err)

	// Then a second endpoint
	ep2 := &Endpoint{network: nw, id: "testEndpoint2"}
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)

	epSorter := func(a, b *Endpoint) int {
		if a.id < b.id {
			return -1
		}
		if a.id > b.id {
			return 1
		}
		return 0
	}

	// Check that we can find both endpoints
	found := c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	slices.SortFunc(found, epSorter)
	assert.Equal(t, len(found), 2)
	assert.Check(t, found[0] == ep1, "got: %s; expected: %s", found[0].id, ep1.id)
	assert.Check(t, found[1] == ep2, "got: %s; expected: %s", found[1].id, ep2.id)

	// Delete the first endpoint
	err = c.deleteStoredEndpoint(ep1)
	assert.NilError(t, err)

	// Check that we can only find the second endpoint
	found = c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	assert.Equal(t, len(found), 1)
	assert.Check(t, found[0] == ep2, "got: %s; expected: %s", found[0].id, ep2.id)

	// Store the second endpoint again
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)
}


Review comment (suggestion):
In which case, it's so small, it could even be inlined;

```suggestion
	slices.SortFunc(found, func(a, b *Endpoint) int { return strings.Compare(a.id, b.id) })
```

Code after:
	c, err := New(configOption)
	assert.NilError(t, err)
	defer c.Stop()

	// Insert a first endpoint
	nw := &Network{id: "testNetwork"}
	ep1 := &Endpoint{network: nw, id: "testEndpoint1"}
	err = c.storeEndpoint(context.Background(), ep1)
	assert.NilError(t, err)

	// Then a second endpoint
	ep2 := &Endpoint{network: nw, id: "testEndpoint2"}
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)

	// Check that we can find both endpoints, and that the returned values are
	// not copies of the original ones.
	found := c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	slices.SortFunc(found, func(a, b *Endpoint) int { return strings.Compare(a.id, b.id) })
	assert.Equal(t, len(found), 2)
	assert.Check(t, is.Equal(found[0], ep1), "got: %s; expected: %s", found[0].id, ep1.id)
	assert.Check(t, is.Equal(found[1], ep2), "got: %s; expected: %s", found[1].id, ep1.id)

	// Delete the first endpoint
	err = c.deleteStoredEndpoint(ep1)
	assert.NilError(t, err)

	// Check that we can only find the second endpoint
	found = c.findEndpoints(filterEndpointByNetworkId("testNetwork"))
	assert.Equal(t, len(found), 1)
	assert.Check(t, is.Equal(found[0], ep2), "got: %s; expected: %s", found[0].id, ep2.id)

	// Store the second endpoint again
	err = c.storeEndpoint(context.Background(), ep2)
	assert.NilError(t, err)
}
Repository: moby/moby
File: integration-cli/requirements_unix_test.go (Go)

Code before:
//go:build !windows

package main

import (
	"os"
	"strings"

	cgroups "github.com/containerd/cgroups/v3"
	"github.com/docker/docker/pkg/sysinfo"
)

var sysInfo *sysinfo.SysInfo

func setupLocalInfo() {
	sysInfo = sysinfo.New()
}

func cpuCfsPeriod() bool {
	return testEnv.DaemonInfo.CPUCfsPeriod
}

func cpuCfsQuota() bool {
	return testEnv.DaemonInfo.CPUCfsQuota
}

func cpuShare() bool {
	return testEnv.DaemonInfo.CPUShares
}

func oomControl() bool {
	return testEnv.DaemonInfo.OomKillDisable
}


Review comment (suggestion):
nit; alias is redundant here;

```suggestion
	"github.com/containerd/cgroups/v3"
```


Alternatively we could use the same alias as we use elsewhere (but in those cases we alias because we import both `containerd/cgroups` and `opencontainers/cgroups`;

```go
cdcgroups "github.com/containerd/cgroups/v3"
```

Code after:
//go:build !windows

package main

import (
	"os"
	"strings"

	"github.com/containerd/cgroups/v3"
	"github.com/docker/docker/pkg/sysinfo"
)

var sysInfo *sysinfo.SysInfo

func setupLocalInfo() {
	sysInfo = sysinfo.New()
}

func cpuCfsPeriod() bool {
	return testEnv.DaemonInfo.CPUCfsPeriod
}

func cpuCfsQuota() bool {
	return testEnv.DaemonInfo.CPUCfsQuota
}

func cpuShare() bool {
	return testEnv.DaemonInfo.CPUShares
}

func oomControl() bool {
	return testEnv.DaemonInfo.OomKillDisable
}
Repository: moby/moby
File: integration/build/build_test.go (Go)

Code before:
	apiClient := testEnv.APIClient()

	buildImage := func(imgName string) error {
		resp, err := apiClient.ImageBuild(ctx, source.AsTarReader(t), types.ImageBuildOptions{
			Remove:      true,
			ForceRemove: true,
			Tags:        []string{imgName},
			Version:     types.BuilderBuildKit,
		})
		if err != nil {
			return err
		}
		defer resp.Body.Close()

		_, err = io.Copy(io.Discard, resp.Body)
		return err
	}

	err := buildImage("target-a")
	assert.NilError(t, err)

	resp, err := apiClient.ImageRemove(ctx, "target-a", image.RemoveOptions{})
	assert.NilError(t, err)
	assert.Check(t, slices.ContainsFunc(resp, func(r image.DeleteResponse) bool {
		return r.Deleted != ""
	}))
}

func readBuildImageIDs(t *testing.T, rd io.Reader) string {
	t.Helper()
	decoder := json.NewDecoder(rd)
	for {
		var jm jsonmessage.JSONMessage
		if err := decoder.Decode(&jm); err != nil {
			if err == io.EOF {
				break
			}
			assert.NilError(t, err)
		}

		if jm.Aux == nil {
			continue
		}

		var auxId struct {
			ID string `json:"ID"`
		}

		if json.Unmarshal(*jm.Aux, &auxId); auxId.ID != "" {
			return auxId.ID
		}

Review comment (performance):
IIRC the issue was that the image index was deleted just fine (so it would get reported in the `Deleted`) - it was only the actual blobs/snapshots that weren't GC'd due to buildkit holding the gc labels.

I think we could just make this test c8d-integration only, and then check with `ctr` if the content is indeed gone at this point.

(We probably need to also prune build cache before this?)

Code after:

	buildImage := func(imgName string) error {
		resp, err := apiClient.ImageBuild(ctx, source.AsTarReader(t), types.ImageBuildOptions{
			Remove:      true,
			ForceRemove: true,
			Tags:        []string{imgName},
			Version:     types.BuilderBuildKit,
		})
		if err != nil {
			return err
		}
		defer resp.Body.Close()

		_, err = io.Copy(io.Discard, resp.Body)
		return err
	}

	err := buildImage("history-a")
	assert.NilError(t, err)

	resp, err := apiClient.ImageRemove(ctx, "history-a", image.RemoveOptions{})
	assert.NilError(t, err)
	assert.Check(t, slices.ContainsFunc(resp, func(r image.DeleteResponse) bool {
		return r.Deleted != ""
	}))
}

func readBuildImageIDs(t *testing.T, rd io.Reader) string {
	t.Helper()
	decoder := json.NewDecoder(rd)
	for {
		var jm jsonmessage.JSONMessage
		if err := decoder.Decode(&jm); err != nil {
			if err == io.EOF {
				break
			}
			assert.NilError(t, err)
		}

		if jm.Aux == nil {
			continue
		}

		var auxId struct {
			ID string `json:"ID"`
		}

		if json.Unmarshal(*jm.Aux, &auxId); auxId.ID != "" {
			return auxId.ID
		}
	}
Repository: moby/moby
File: libnetwork/drivers/bridge/internal/firewaller/stub.go (Go)

Code before:
	return &StubFirewaller{
		Config:   config,
		Networks: make(map[string]*StubFirewallerNetwork),
	}
}

func (fw *StubFirewaller) NewNetwork(_ context.Context, nc NetworkConfig) (Network, error) {
	if nw, ok := fw.Networks[nc.IfName]; ok && !nw.Deleted {
		return nil, fmt.Errorf("StubFirewaller: network with IfName %q already exists", nc.IfName)
	}
	nw := &StubFirewallerNetwork{
		NetworkConfig: nc,
		Endpoints:     map[stubEndpoint]struct{}{},
	}
	fw.Networks[nc.IfName] = nw
	return nw, nil
}

func (fw *StubFirewaller) FilterForwardDrop(_ context.Context, _ IPVersion) error {
	fw.FFD = true
	return nil
}

type stubFirewallerLink struct {
	parentIP netip.Addr
	childIP  netip.Addr
	ports    []types.TransportPort
}

type stubEndpoint struct {
	addr4 netip.Addr
	addr6 netip.Addr
}

type StubFirewallerNetwork struct {
	NetworkConfig
	Deleted   bool
	Endpoints map[stubEndpoint]struct{}
	Ports     []types.PortBinding
	Links     []stubFirewallerLink
}

func (nw *StubFirewallerNetwork) ReapplyNetworkLevelRules(_ context.Context) error {
	return nil
}

func (nw *StubFirewallerNetwork) DelNetworkLevelRules(_ context.Context) error {
	nw.Deleted = true
	return nil
}


Review comment (suggestion):
Given that `FilterForwardDrop` takes an `IPVersion` parameter, shouldn't the firewaller fake be separately tracking calls for each address family?
```go
type StubFirewaller struct {
	...
	FFD map[IPVersion]bool
}
```

Code after:
	return &StubFirewaller{
		Config: config,
		// A real Firewaller shouldn't hold on to its own networks, the bridge driver is doing that.
		// But, for unit tests cross-checking the driver, this is useful.
		Networks: make(map[string]*StubFirewallerNetwork),
		FFD:      make(map[IPVersion]bool),
	}
}

func (fw *StubFirewaller) NewNetwork(_ context.Context, nc NetworkConfig) (Network, error) {
	if _, ok := fw.Networks[nc.IfName]; ok {
		return nil, fmt.Errorf("StubFirewaller: network with IfName %q already exists", nc.IfName)
	}
	nw := &StubFirewallerNetwork{
		NetworkConfig: nc,
		Endpoints:     map[stubEndpoint]struct{}{},
		parent:        fw,
	}
	fw.Networks[nc.IfName] = nw
	return nw, nil
}

func (fw *StubFirewaller) FilterForwardDrop(_ context.Context, ipv IPVersion) error {
	fw.FFD[ipv] = true
	return nil
}

type stubFirewallerLink struct {
	parentIP netip.Addr
	childIP  netip.Addr
	ports    []types.TransportPort
}

type stubEndpoint struct {
	addr4 netip.Addr
	addr6 netip.Addr
}

type StubFirewallerNetwork struct {
	NetworkConfig
	Deleted   bool
	Endpoints map[stubEndpoint]struct{}
	Ports     []types.PortBinding
	Links     []stubFirewallerLink

	parent *StubFirewaller
}

func (nw *StubFirewallerNetwork) ReapplyNetworkLevelRules(_ context.Context) error {
	return nil
}
Repository: moby/moby
File: daemon/wait.go (Go)

Code before:
package daemon // import "github.com/docker/docker/daemon"

import (
	"context"

	containertypes "github.com/docker/docker/api/types/container"
	"github.com/docker/docker/container"
)

// ContainerWait waits until the given container is in a certain state
// indicated by the given condition. If the container is not found, a nil
// channel and non-nil error is returned immediately. If the container is
// found, a status result will be sent on the returned channel once the wait
// condition is met or if an error occurs waiting for the container (such as a
// context timeout or cancellation). On a successful wait, the exit code of the
// container is returned in the status with a non-nil Err() value.
func (daemon *Daemon) ContainerWait(ctx context.Context, name string, condition containertypes.WaitCondition) (<-chan container.StateStatus, error) {
	cntr, err := daemon.GetContainer(name)
	if err != nil {
		return nil, err
	}

	return cntr.Wait(ctx, condition), nil
}


Review comment (suggestion):
```
daemon/wait.go:17:119: SA1019: container.StateStatus is deprecated: use [container.StateStatus] instead. (staticcheck)
func (daemon *Daemon) ContainerWait(ctx context.Context, name string, condition containertypes.WaitCondition) (<-chan container.StateStatus, error) {
                                                                                                                      ^
```

```suggestion
func (daemon *Daemon) ContainerWait(ctx context.Context, name string, condition containertypes.WaitCondition) (<-chan containertypes.StateStatus, error) {
```

But it looks like this is the only remaining use of `"github.com/docker/docker/container"`, so we could also  remove the alias for the new import instead.


Code after:
package daemon // import "github.com/docker/docker/daemon"

import (
	"context"

	containertypes "github.com/docker/docker/api/types/container"
)

// ContainerWait waits until the given container is in a certain state
// indicated by the given condition. If the container is not found, a nil
// channel and non-nil error is returned immediately. If the container is
// found, a status result will be sent on the returned channel once the wait
// condition is met or if an error occurs waiting for the container (such as a
// context timeout or cancellation). On a successful wait, the exit code of the
// container is returned in the status with a non-nil Err() value.
func (daemon *Daemon) ContainerWait(ctx context.Context, name string, condition containertypes.WaitCondition) (<-chan containertypes.StateStatus, error) {
	cntr, err := daemon.GetContainer(name)
	if err != nil {
		return nil, err
	}

	return cntr.Wait(ctx, condition), nil
}
Repository: moby/moby
File: daemon/stats_unix_test.go (Go)

Code before:
cpu103 4 0 10 51283 0 0 0 0 0 0
cpu104 3 0 15 51284 0 0 0 0 0 0
cpu105 7 0 13 51283 0 0 0 0 0 0
cpu106 1 0 12 51286 0 0 0 0 0 0
cpu107 4 0 18 51278 0 0 0 0 0 0
cpu108 9 0 7 51283 0 0 0 0 0 0
cpu109 7 0 9 51281 0 0 0 0 0 0
cpu110 8 0 9 51279 0 0 0 0 0 0
cpu111 3 0 21 51278 0 0 0 0 0 0
cpu112 9 0 10 51275 0 0 0 0 0 0
cpu113 3 0 10 51287 0 0 0 0 0 0
cpu114 5 0 11 51285 0 0 0 0 0 0
cpu115 2 0 13 51284 0 0 0 0 0 0
cpu116 9 0 11 51280 0 0 0 0 0 0
cpu117 5 0 11 51285 0 0 0 0 0 0
cpu118 4 0 12 51287 0 0 0 0 0 0
cpu119 3 0 10 51285 0 0 0 0 0 0
cpu120 5 0 13 51280 0 0 0 0 0 0
cpu121 3 0 12 51288 0 0 0 0 0 0
cpu122 3 0 7 51286 0 0 0 0 0 0
cpu123 2 0 16 51281 0 0 0 0 0 0
cpu124 2 0 8 51289 0 0 0 0 0 0
cpu125 9 0 12 51274 0 0 0 0 0 0
cpu126 3 0 8 51281 0 0 0 0 0 0
cpu127 3 0 14 51286 0 0 0 0 0 0
intr [ ... snipped for brevity ... ]
ctxt 1005576
btime 1743413731
processes 4599
procs_running 1
procs_blocked 0
softirq 382342 14959 34614 10 2141 811 0 11598 275792 0 42417
`)
	cpuUsage, cpuNum, err := readSystemCPUUsage(input)
	assert.NilError(t, err)
	assert.Check(t, is.Equal(cpuUsage, uint64(65647090000000)))
	assert.Check(t, is.Equal(cpuNum, uint32(128)))
}


Review comment (bug):
The `intr` part was actually crucial here since the original change fixed a bug where too long line would make the function error out.

But yeah it might actually be too long for inlining...
So looks like we need to keep it in a separate file - although I'd be more happy to access it via `go:embed` instead of opening it at runtime.
This way the test is self-contained and verified at compile-time.

Code after:
//go:build !windows

package daemon

import (
	_ "embed"
	"strings"
	"testing"

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

//go:embed testdata/stat
var statData string

func TestGetSystemCPUUsageParsing(t *testing.T) {
	input := strings.NewReader(statData)
	cpuUsage, cpuNum, _ := readSystemCPUUsage(input)
	assert.Check(t, is.Equal(cpuUsage, uint64(65647090000000)))
	assert.Check(t, is.Equal(cpuNum, uint32(128)))
}