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