github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/formatter/disk_usage_test.go (about)

     1  package formatter
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"gotest.tools/v3/assert"
     8  	"gotest.tools/v3/golden"
     9  )
    10  
    11  func TestDiskUsageContextFormatWrite(t *testing.T) {
    12  	cases := []struct {
    13  		context  DiskUsageContext
    14  		expected string
    15  	}{
    16  		// Check default output format (verbose and non-verbose mode) for table headers
    17  		{
    18  			DiskUsageContext{
    19  				Context: Context{
    20  					Format: NewDiskUsageFormat("table", false),
    21  				},
    22  				Verbose: false},
    23  			`TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
    24  Images          0         0         0B        0B
    25  Containers      0         0         0B        0B
    26  Local Volumes   0         0         0B        0B
    27  Build Cache     0         0         0B        0B
    28  `,
    29  		},
    30  		{
    31  			DiskUsageContext{Verbose: true, Context: Context{Format: NewDiskUsageFormat("table", true)}},
    32  			`Images space usage:
    33  
    34  REPOSITORY   TAG       IMAGE ID   CREATED   SIZE      SHARED SIZE   UNIQUE SIZE   CONTAINERS
    35  
    36  Containers space usage:
    37  
    38  CONTAINER ID   IMAGE     COMMAND   LOCAL VOLUMES   SIZE      CREATED   STATUS    NAMES
    39  
    40  Local Volumes space usage:
    41  
    42  VOLUME NAME   LINKS     SIZE
    43  
    44  Build cache usage: 0B
    45  
    46  CACHE ID   CACHE TYPE   SIZE      CREATED   LAST USED   USAGE     SHARED
    47  `,
    48  		},
    49  		{
    50  			DiskUsageContext{Verbose: true, Context: Context{Format: NewDiskUsageFormat("raw", true)}},
    51  			``,
    52  		},
    53  		{
    54  			DiskUsageContext{Verbose: true, Context: Context{Format: NewDiskUsageFormat("{{json .}}", true)}},
    55  			`{"Images":[],"Containers":[],"Volumes":[],"BuildCache":[]}`,
    56  		},
    57  		// Errors
    58  		{
    59  			DiskUsageContext{
    60  				Context: Context{
    61  					Format: "{{InvalidFunction}}",
    62  				},
    63  			},
    64  			`Template parsing error: template: :1: function "InvalidFunction" not defined
    65  `,
    66  		},
    67  		{
    68  			DiskUsageContext{
    69  				Context: Context{
    70  					Format: "{{nil}}",
    71  				},
    72  			},
    73  			`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
    74  `,
    75  		},
    76  		// Table Format
    77  		{
    78  			DiskUsageContext{
    79  				Context: Context{
    80  					Format: NewDiskUsageFormat("table", false),
    81  				},
    82  			},
    83  			`TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
    84  Images          0         0         0B        0B
    85  Containers      0         0         0B        0B
    86  Local Volumes   0         0         0B        0B
    87  Build Cache     0         0         0B        0B
    88  `,
    89  		},
    90  		{
    91  			DiskUsageContext{
    92  				Context: Context{
    93  					Format: NewDiskUsageFormat("table {{.Type}}\t{{.Active}}", false),
    94  				},
    95  			},
    96  			string(golden.Get(t, "disk-usage-context-write-custom.golden")),
    97  		},
    98  		// Raw Format
    99  		{
   100  			DiskUsageContext{
   101  				Context: Context{
   102  					Format: NewDiskUsageFormat("raw", false),
   103  				},
   104  			},
   105  			string(golden.Get(t, "disk-usage-raw-format.golden")),
   106  		},
   107  	}
   108  
   109  	for _, tc := range cases {
   110  		tc := tc
   111  		t.Run(string(tc.context.Format), func(t *testing.T) {
   112  			var out bytes.Buffer
   113  			tc.context.Output = &out
   114  			if err := tc.context.Write(); err != nil {
   115  				assert.Error(t, err, tc.expected)
   116  			} else {
   117  				assert.Equal(t, out.String(), tc.expected)
   118  			}
   119  		})
   120  	}
   121  }