github.1git.de/docker/cli@v26.1.3+incompatible/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 formatter.Context{Format: "{{nil}}"}, 26 `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`, 27 }, 28 // Table format 29 { 30 formatter.Context{Format: NewFormat("table", false)}, 31 `ID NAME DRIVER CREATED UPDATED 32 1 passwords Less than a second ago Less than a second ago 33 2 id_rsa Less than a second ago Less than a second ago 34 `, 35 }, 36 { 37 formatter.Context{Format: NewFormat("table {{.Name}}", true)}, 38 `NAME 39 passwords 40 id_rsa 41 `, 42 }, 43 { 44 formatter.Context{Format: NewFormat("{{.ID}}-{{.Name}}", false)}, 45 `1-passwords 46 2-id_rsa 47 `, 48 }, 49 } 50 51 secrets := []swarm.Secret{ 52 { 53 ID: "1", 54 Meta: swarm.Meta{CreatedAt: time.Now(), UpdatedAt: time.Now()}, 55 Spec: swarm.SecretSpec{Annotations: swarm.Annotations{Name: "passwords"}}, 56 }, 57 { 58 ID: "2", 59 Meta: swarm.Meta{CreatedAt: time.Now(), UpdatedAt: time.Now()}, 60 Spec: swarm.SecretSpec{Annotations: swarm.Annotations{Name: "id_rsa"}}, 61 }, 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 := FormatWrite(tc.context, secrets); err != nil { 70 assert.Error(t, err, tc.expected) 71 } else { 72 assert.Equal(t, out.String(), tc.expected) 73 } 74 }) 75 } 76 }