github.com/kunnos/engine@v1.13.1/cli/command/container/ps_test.go (about)

     1  package container
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/docker/opts"
     7  	"github.com/docker/docker/pkg/testutil/assert"
     8  )
     9  
    10  func TestBuildContainerListOptions(t *testing.T) {
    11  	filters := opts.NewFilterOpt()
    12  	assert.NilError(t, filters.Set("foo=bar"))
    13  	assert.NilError(t, filters.Set("baz=foo"))
    14  
    15  	contexts := []struct {
    16  		psOpts          *psOptions
    17  		expectedAll     bool
    18  		expectedSize    bool
    19  		expectedLimit   int
    20  		expectedFilters map[string]string
    21  	}{
    22  		{
    23  			psOpts: &psOptions{
    24  				all:    true,
    25  				size:   true,
    26  				last:   5,
    27  				filter: filters,
    28  			},
    29  			expectedAll:   true,
    30  			expectedSize:  true,
    31  			expectedLimit: 5,
    32  			expectedFilters: map[string]string{
    33  				"foo": "bar",
    34  				"baz": "foo",
    35  			},
    36  		},
    37  		{
    38  			psOpts: &psOptions{
    39  				all:     true,
    40  				size:    true,
    41  				last:    -1,
    42  				nLatest: true,
    43  			},
    44  			expectedAll:     true,
    45  			expectedSize:    true,
    46  			expectedLimit:   1,
    47  			expectedFilters: make(map[string]string),
    48  		},
    49  		{
    50  			psOpts: &psOptions{
    51  				all:    true,
    52  				size:   false,
    53  				last:   5,
    54  				filter: filters,
    55  				// With .Size, size should be true
    56  				format: "{{.Size}}",
    57  			},
    58  			expectedAll:   true,
    59  			expectedSize:  true,
    60  			expectedLimit: 5,
    61  			expectedFilters: map[string]string{
    62  				"foo": "bar",
    63  				"baz": "foo",
    64  			},
    65  		},
    66  		{
    67  			psOpts: &psOptions{
    68  				all:    true,
    69  				size:   false,
    70  				last:   5,
    71  				filter: filters,
    72  				// With .Size, size should be true
    73  				format: "{{.Size}} {{.CreatedAt}} {{.Networks}}",
    74  			},
    75  			expectedAll:   true,
    76  			expectedSize:  true,
    77  			expectedLimit: 5,
    78  			expectedFilters: map[string]string{
    79  				"foo": "bar",
    80  				"baz": "foo",
    81  			},
    82  		},
    83  		{
    84  			psOpts: &psOptions{
    85  				all:    true,
    86  				size:   false,
    87  				last:   5,
    88  				filter: filters,
    89  				// Without .Size, size should be false
    90  				format: "{{.CreatedAt}} {{.Networks}}",
    91  			},
    92  			expectedAll:   true,
    93  			expectedSize:  false,
    94  			expectedLimit: 5,
    95  			expectedFilters: map[string]string{
    96  				"foo": "bar",
    97  				"baz": "foo",
    98  			},
    99  		},
   100  	}
   101  
   102  	for _, c := range contexts {
   103  		options, err := buildContainerListOptions(c.psOpts)
   104  		assert.NilError(t, err)
   105  
   106  		assert.Equal(t, c.expectedAll, options.All)
   107  		assert.Equal(t, c.expectedSize, options.Size)
   108  		assert.Equal(t, c.expectedLimit, options.Limit)
   109  		assert.Equal(t, options.Filters.Len(), len(c.expectedFilters))
   110  
   111  		for k, v := range c.expectedFilters {
   112  			f := options.Filters
   113  			if !f.ExactMatch(k, v) {
   114  				t.Fatalf("Expected filter with key %s to be %s but got %s", k, v, f.Get(k))
   115  			}
   116  		}
   117  	}
   118  }