github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/plugin/formatter_test.go (about)

     1  package plugin
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/docker/cli/cli/command/formatter"
    10  	"github.com/docker/cli/internal/test"
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/pkg/stringid"
    13  	"gotest.tools/assert"
    14  	is "gotest.tools/assert/cmp"
    15  )
    16  
    17  func TestPluginContext(t *testing.T) {
    18  	pluginID := stringid.GenerateRandomID()
    19  
    20  	var ctx pluginContext
    21  	cases := []struct {
    22  		pluginCtx pluginContext
    23  		expValue  string
    24  		call      func() string
    25  	}{
    26  		{pluginContext{
    27  			p:     types.Plugin{ID: pluginID},
    28  			trunc: false,
    29  		}, pluginID, ctx.ID},
    30  		{pluginContext{
    31  			p:     types.Plugin{ID: pluginID},
    32  			trunc: true,
    33  		}, stringid.TruncateID(pluginID), ctx.ID},
    34  		{pluginContext{
    35  			p: types.Plugin{Name: "plugin_name"},
    36  		}, "plugin_name", ctx.Name},
    37  		{pluginContext{
    38  			p: types.Plugin{Config: types.PluginConfig{Description: "plugin_description"}},
    39  		}, "plugin_description", ctx.Description},
    40  	}
    41  
    42  	for _, c := range cases {
    43  		ctx = c.pluginCtx
    44  		v := c.call()
    45  		if strings.Contains(v, ",") {
    46  			test.CompareMultipleValues(t, v, c.expValue)
    47  		} else if v != c.expValue {
    48  			t.Fatalf("Expected %s, was %s\n", c.expValue, v)
    49  		}
    50  	}
    51  }
    52  
    53  func TestPluginContextWrite(t *testing.T) {
    54  	cases := []struct {
    55  		context  formatter.Context
    56  		expected string
    57  	}{
    58  
    59  		// Errors
    60  		{
    61  			formatter.Context{Format: "{{InvalidFunction}}"},
    62  			`Template parsing error: template: :1: function "InvalidFunction" not defined
    63  `,
    64  		},
    65  		{
    66  			formatter.Context{Format: "{{nil}}"},
    67  			`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
    68  `,
    69  		},
    70  		// Table format
    71  		{
    72  			formatter.Context{Format: NewFormat("table", false)},
    73  			`ID                  NAME                DESCRIPTION         ENABLED
    74  pluginID1           foobar_baz          description 1       true
    75  pluginID2           foobar_bar          description 2       false
    76  `,
    77  		},
    78  		{
    79  			formatter.Context{Format: NewFormat("table", true)},
    80  			`pluginID1
    81  pluginID2
    82  `,
    83  		},
    84  		{
    85  			formatter.Context{Format: NewFormat("table {{.Name}}", false)},
    86  			`NAME
    87  foobar_baz
    88  foobar_bar
    89  `,
    90  		},
    91  		{
    92  			formatter.Context{Format: NewFormat("table {{.Name}}", true)},
    93  			`NAME
    94  foobar_baz
    95  foobar_bar
    96  `,
    97  		},
    98  		// Raw Format
    99  		{
   100  			formatter.Context{Format: NewFormat("raw", false)},
   101  			`plugin_id: pluginID1
   102  name: foobar_baz
   103  description: description 1
   104  enabled: true
   105  
   106  plugin_id: pluginID2
   107  name: foobar_bar
   108  description: description 2
   109  enabled: false
   110  
   111  `,
   112  		},
   113  		{
   114  			formatter.Context{Format: NewFormat("raw", true)},
   115  			`plugin_id: pluginID1
   116  plugin_id: pluginID2
   117  `,
   118  		},
   119  		// Custom Format
   120  		{
   121  			formatter.Context{Format: NewFormat("{{.Name}}", false)},
   122  			`foobar_baz
   123  foobar_bar
   124  `,
   125  		},
   126  	}
   127  
   128  	for _, testcase := range cases {
   129  		plugins := []*types.Plugin{
   130  			{ID: "pluginID1", Name: "foobar_baz", Config: types.PluginConfig{Description: "description 1"}, Enabled: true},
   131  			{ID: "pluginID2", Name: "foobar_bar", Config: types.PluginConfig{Description: "description 2"}, Enabled: false},
   132  		}
   133  		out := bytes.NewBufferString("")
   134  		testcase.context.Output = out
   135  		err := FormatWrite(testcase.context, plugins)
   136  		if err != nil {
   137  			assert.Error(t, err, testcase.expected)
   138  		} else {
   139  			assert.Check(t, is.Equal(testcase.expected, out.String()))
   140  		}
   141  	}
   142  }
   143  
   144  func TestPluginContextWriteJSON(t *testing.T) {
   145  	plugins := []*types.Plugin{
   146  		{ID: "pluginID1", Name: "foobar_baz"},
   147  		{ID: "pluginID2", Name: "foobar_bar"},
   148  	}
   149  	expectedJSONs := []map[string]interface{}{
   150  		{"Description": "", "Enabled": false, "ID": "pluginID1", "Name": "foobar_baz", "PluginReference": ""},
   151  		{"Description": "", "Enabled": false, "ID": "pluginID2", "Name": "foobar_bar", "PluginReference": ""},
   152  	}
   153  
   154  	out := bytes.NewBufferString("")
   155  	err := FormatWrite(formatter.Context{Format: "{{json .}}", Output: out}, plugins)
   156  	if err != nil {
   157  		t.Fatal(err)
   158  	}
   159  	for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
   160  		var m map[string]interface{}
   161  		if err := json.Unmarshal([]byte(line), &m); err != nil {
   162  			t.Fatal(err)
   163  		}
   164  		assert.Check(t, is.DeepEqual(expectedJSONs[i], m))
   165  	}
   166  }
   167  
   168  func TestPluginContextWriteJSONField(t *testing.T) {
   169  	plugins := []*types.Plugin{
   170  		{ID: "pluginID1", Name: "foobar_baz"},
   171  		{ID: "pluginID2", Name: "foobar_bar"},
   172  	}
   173  	out := bytes.NewBufferString("")
   174  	err := FormatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, plugins)
   175  	if err != nil {
   176  		t.Fatal(err)
   177  	}
   178  	for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
   179  		var s string
   180  		if err := json.Unmarshal([]byte(line), &s); err != nil {
   181  			t.Fatal(err)
   182  		}
   183  		assert.Check(t, is.Equal(plugins[i].ID, s))
   184  	}
   185  }