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

     1  package formatter
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/docker/cli/cli/command/formatter"
     8  	"gotest.tools/assert"
     9  	is "gotest.tools/assert/cmp"
    10  )
    11  
    12  func TestStackContextWrite(t *testing.T) {
    13  	cases := []struct {
    14  		context  formatter.Context
    15  		expected string
    16  	}{
    17  		// Errors
    18  		{
    19  			formatter.Context{Format: "{{InvalidFunction}}"},
    20  			`Template parsing error: template: :1: function "InvalidFunction" not defined
    21  `,
    22  		},
    23  		{
    24  			formatter.Context{Format: "{{nil}}"},
    25  			`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
    26  `,
    27  		},
    28  		// Table format
    29  		{
    30  			formatter.Context{Format: formatter.Format(SwarmStackTableFormat)},
    31  			`NAME                SERVICES            ORCHESTRATOR
    32  baz                 2                   orchestrator1
    33  bar                 1                   orchestrator2
    34  `,
    35  		},
    36  		// Kubernetes table format adds Namespace column
    37  		{
    38  			formatter.Context{Format: formatter.Format(KubernetesStackTableFormat)},
    39  			`NAME                SERVICES            ORCHESTRATOR        NAMESPACE
    40  baz                 2                   orchestrator1       namespace1
    41  bar                 1                   orchestrator2       namespace2
    42  `,
    43  		},
    44  		{
    45  			formatter.Context{Format: formatter.Format("table {{.Name}}")},
    46  			`NAME
    47  baz
    48  bar
    49  `,
    50  		},
    51  		// Custom Format
    52  		{
    53  			formatter.Context{Format: formatter.Format("{{.Name}}")},
    54  			`baz
    55  bar
    56  `,
    57  		},
    58  	}
    59  
    60  	stacks := []*Stack{
    61  		{Name: "baz", Services: 2, Orchestrator: "orchestrator1", Namespace: "namespace1"},
    62  		{Name: "bar", Services: 1, Orchestrator: "orchestrator2", Namespace: "namespace2"},
    63  	}
    64  	for _, testcase := range cases {
    65  		out := bytes.NewBufferString("")
    66  		testcase.context.Output = out
    67  		err := StackWrite(testcase.context, stacks)
    68  		if err != nil {
    69  			assert.Check(t, is.ErrorContains(err, testcase.expected))
    70  		} else {
    71  			assert.Check(t, is.Equal(out.String(), testcase.expected))
    72  		}
    73  	}
    74  }