github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/secret/formatter_test.go (about) 1 package secret 2 3 import ( 4 "bytes" 5 "testing" 6 "time" 7 8 "github.com/docker/cli/cli/command/formatter" 9 "github.com/docker/docker/api/types/swarm" 10 "gotest.tools/v3/assert" 11 ) 12 13 func TestSecretContextFormatWrite(t *testing.T) { 14 // Check default output format (verbose and non-verbose mode) for table headers 15 cases := []struct { 16 context formatter.Context 17 expected string 18 }{ 19 // Errors 20 { 21 formatter.Context{Format: "{{InvalidFunction}}"}, 22 `Template parsing error: template: :1: function "InvalidFunction" not defined 23 `, 24 }, 25 { 26 formatter.Context{Format: "{{nil}}"}, 27 `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command 28 `, 29 }, 30 // Table format 31 {formatter.Context{Format: NewFormat("table", false)}, 32 `ID NAME DRIVER CREATED UPDATED 33 1 passwords Less than a second ago Less than a second ago 34 2 id_rsa Less than a second ago Less than a second ago 35 `}, 36 {formatter.Context{Format: NewFormat("table {{.Name}}", true)}, 37 `NAME 38 passwords 39 id_rsa 40 `}, 41 {formatter.Context{Format: NewFormat("{{.ID}}-{{.Name}}", false)}, 42 `1-passwords 43 2-id_rsa 44 `}, 45 } 46 47 secrets := []swarm.Secret{ 48 {ID: "1", 49 Meta: swarm.Meta{CreatedAt: time.Now(), UpdatedAt: time.Now()}, 50 Spec: swarm.SecretSpec{Annotations: swarm.Annotations{Name: "passwords"}}}, 51 {ID: "2", 52 Meta: swarm.Meta{CreatedAt: time.Now(), UpdatedAt: time.Now()}, 53 Spec: swarm.SecretSpec{Annotations: swarm.Annotations{Name: "id_rsa"}}}, 54 } 55 for _, tc := range cases { 56 tc := tc 57 t.Run(string(tc.context.Format), func(t *testing.T) { 58 var out bytes.Buffer 59 tc.context.Output = &out 60 61 if err := FormatWrite(tc.context, secrets); err != nil { 62 assert.Error(t, err, tc.expected) 63 } else { 64 assert.Equal(t, out.String(), tc.expected) 65 } 66 }) 67 } 68 }