From eb714f7c0e8375912e2ddc8df74b5c06d4a375d4 Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 23 Jan 2019 14:26:12 +0000 Subject: [PATCH] Add unit test for `formatInfo`. Signed-off-by: Ian Campbell --- cli/command/system/info_test.go | 37 ++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/cli/command/system/info_test.go b/cli/command/system/info_test.go index 942a645b2e..fa1a957f8b 100644 --- a/cli/command/system/info_test.go +++ b/cli/command/system/info_test.go @@ -21,8 +21,10 @@ func base64Decode(val string) []byte { return decoded } +const sampleID = "EKHL:QDUU:QZ7U:MKGD:VDXK:S27Q:GIPU:24B7:R7VT:DGN6:QCSF:2UBX" + var sampleInfoNoSwarm = types.Info{ - ID: "EKHL:QDUU:QZ7U:MKGD:VDXK:S27Q:GIPU:24B7:R7VT:DGN6:QCSF:2UBX", + ID: sampleID, Containers: 0, ContainersRunning: 0, ContainersPaused: 0, @@ -263,3 +265,36 @@ func TestPrettyPrintInfo(t *testing.T) { }) } } + +func TestFormatInfo(t *testing.T) { + for _, tc := range []struct { + doc string + template string + expectedError string + expectedOut string + }{ + { + doc: "basic", + template: "{{.ID}}", + expectedOut: sampleID + "\n", + }, + { + doc: "syntax", + template: "{{}", + expectedError: `Status: Template parsing error: template: :1: unexpected "}" in command, Code: 64`, + }, + } { + t.Run(tc.doc, func(t *testing.T) { + cli := test.NewFakeCli(&fakeClient{}) + err := formatInfo(cli, sampleInfoNoSwarm, tc.template) + if tc.expectedOut != "" { + assert.NilError(t, err) + assert.Equal(t, cli.OutBuffer().String(), tc.expectedOut) + } else if tc.expectedError != "" { + assert.Error(t, err, tc.expectedError) + } else { + t.Fatal("test expected to neither pass nor fail") + } + }) + } +}