Repository: moby/moby
File: client/container_list_test.go (Go)

Code before:

	cerrdefs "github.com/containerd/errdefs"
	"github.com/moby/moby/api/types/container"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestContainerListError(t *testing.T) {
	client, err := New(
		WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
	)
	assert.NilError(t, err)

	_, err = client.ContainerList(t.Context(), ContainerListOptions{})
	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}

func TestContainerListLatest(t *testing.T) {
	client, err := New(
		WithMockClient(func(req *http.Request) (*http.Response, error) {
			if err := assertRequest(req, http.MethodGet, "/containers/json"); err != nil {
				return nil, err
			}
			query := req.URL.Query()
			limit := query.Get("limit")
			if limit != "1" {
				return nil, fmt.Errorf("limit not set properly for Latest option. Expected '1', got %s", limit)
			}
			return mockJSONResponse(http.StatusOK, nil, []container.Summary{
				{ID: "latest_container"},
			})(req)
		}),
	)
	assert.NilError(t, err)

	list, err := client.ContainerList(t.Context(), ContainerListOptions{
		Latest: true,
	})
	assert.NilError(t, err)
	assert.Check(t, is.Len(list.Items, 1))
}

func TestContainerList(t *testing.T) {
	const (
		expectedURL     = "/containers/json"
		expectedFilters = `{"before":{"container":true},"label":{"label1":true,"label2":true}}`
	)
	client, err := New(
		WithMockClient(func(req *http.Request) (*http.Response, error) {
			if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
				return nil, err

Review comment (refactor):
Ah, fun, so ... it looks like the `Latest` option is not implemented in the client, and the field probably shouldn't be there at all; looks like it's an option that's implemented in the CLI as a "convenience" option for `ContainerListOptions.Limit`

https://github.com/docker/cli/blob/d5ed03732077084b5cf3911e26ad25c33d036ca8/cli/command/container/list.go#L71-L81

So, looking at "why"; I see that these options were previously shared between the daemon and the client; https://github.com/moby/moby/commit/4d20b6fe56dfb2b06f4a5dd1f32913215a9c317b

(Some of those fields may have been used only on the daemon-side); digging a bit further, it looks like the `Latest` field was added as part of a large refactor, but never actually used; https://github.com/moby/moby/commit/d05aa418b0466553a24d42896f99176cfa29765f

I recall that PR was a very large refactor;

- https://github.com/moby/moby/pull/18472


And came with a note .. that it was too large to review, which clearly showed it was too large indeed;

> :warning: Too big to review, go to my fork and navigate the api/client package :warning: 





Code after:

	cerrdefs "github.com/containerd/errdefs"
	"github.com/moby/moby/api/types/container"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestContainerListError(t *testing.T) {
	client, err := New(
		WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
	)
	assert.NilError(t, err)

	_, err = client.ContainerList(t.Context(), ContainerListOptions{})
	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}

func TestContainerList(t *testing.T) {
	const expectedURL = "/containers/json"

	tests := []struct {
		doc      string
		options  ContainerListOptions
		expected url.Values
	}{
		{
			doc:      "no options",
			expected: url.Values{},
		},
		{
			doc:      "size",
			options:  ContainerListOptions{Size: true},
			expected: url.Values{"size": []string{"1"}},
		},
		{
			doc:      "all",
			options:  ContainerListOptions{All: true},
			expected: url.Values{"all": []string{"1"}},
		},
		{
			doc:      "latest", // FIXME(thaJeztah): the "latest" option is a cli-side shortcut for "limit: 1". It has never been used https://github.com/moby/moby/commit/d05aa418b0466553a24d42896f99176cfa29765f
			options:  ContainerListOptions{Latest: true},
			expected: url.Values{},
		},
		{
			doc:      "since", // FIXME(thaJeztah): the "since" option was deprecated, and is no longer handled by the API server; https://github.com/moby/moby/pull/22138
			options:  ContainerListOptions{Since: "container"},
			expected: url.Values{"since": []string{"container"}},
		},
		{
			doc:      "before", // FIXME(thaJeztah): the "before" option was deprecated, and is no longer handled by the API server; https://github.com/moby/moby/pull/22138