2018-10-23 11:05:44 -04:00
|
|
|
package registry
|
2017-08-15 06:58:49 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-08-15 06:58:49 -04:00
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-08-15 06:58:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSearchContext(t *testing.T) {
|
|
|
|
var ctx searchContext
|
|
|
|
cases := []struct {
|
|
|
|
searchCtx searchContext
|
|
|
|
expValue string
|
|
|
|
call func() string
|
|
|
|
}{
|
2023-08-08 11:31:03 -04:00
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Name: "nginx"},
|
|
|
|
},
|
|
|
|
expValue: "nginx",
|
|
|
|
call: ctx.Name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{StarCount: 5000},
|
|
|
|
},
|
|
|
|
expValue: "5000",
|
|
|
|
call: ctx.StarCount,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{IsOfficial: true},
|
|
|
|
},
|
|
|
|
expValue: "[OK]",
|
|
|
|
call: ctx.IsOfficial,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{IsOfficial: false},
|
|
|
|
},
|
|
|
|
call: ctx.IsOfficial,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
|
|
|
|
},
|
|
|
|
expValue: "[OK]",
|
2023-07-10 14:18:28 -04:00
|
|
|
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
|
2023-08-08 11:31:03 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{},
|
|
|
|
},
|
2023-07-10 14:18:28 -04:00
|
|
|
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
|
2023-08-08 11:31:03 -04:00
|
|
|
},
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.searchCtx
|
|
|
|
v := c.call()
|
2023-08-08 11:31:03 -04:00
|
|
|
assert.Check(t, is.Equal(v, c.expValue))
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchContextDescription(t *testing.T) {
|
2023-08-08 11:31:03 -04:00
|
|
|
const (
|
|
|
|
shortDescription = "Official build of Nginx."
|
|
|
|
longDescription = "Automated Nginx reverse proxy for docker containers"
|
|
|
|
descriptionWReturns = "Automated\nNginx reverse\rproxy\rfor docker\ncontainers"
|
|
|
|
)
|
2017-08-15 06:58:49 -04:00
|
|
|
|
|
|
|
var ctx searchContext
|
|
|
|
cases := []struct {
|
|
|
|
searchCtx searchContext
|
|
|
|
expValue string
|
|
|
|
call func() string
|
|
|
|
}{
|
2023-08-08 11:31:03 -04:00
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: shortDescription},
|
|
|
|
trunc: true,
|
|
|
|
},
|
|
|
|
expValue: shortDescription,
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: shortDescription},
|
|
|
|
trunc: false,
|
|
|
|
},
|
|
|
|
expValue: shortDescription,
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: longDescription},
|
|
|
|
trunc: false,
|
|
|
|
},
|
|
|
|
expValue: longDescription,
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: longDescription},
|
|
|
|
trunc: true,
|
|
|
|
},
|
|
|
|
expValue: formatter.Ellipsis(longDescription, 45),
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: descriptionWReturns},
|
|
|
|
trunc: false,
|
|
|
|
},
|
|
|
|
expValue: longDescription,
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: descriptionWReturns},
|
|
|
|
trunc: true,
|
|
|
|
},
|
|
|
|
expValue: formatter.Ellipsis(longDescription, 45),
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.searchCtx
|
|
|
|
v := c.call()
|
2023-08-08 11:31:03 -04:00
|
|
|
assert.Check(t, is.Equal(v, c.expValue))
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchContextWrite(t *testing.T) {
|
|
|
|
cases := []struct {
|
2023-08-08 11:31:03 -04:00
|
|
|
doc string
|
|
|
|
format formatter.Format
|
|
|
|
expected string
|
|
|
|
expectedErr string
|
2017-08-15 06:58:49 -04:00
|
|
|
}{
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "Errors",
|
|
|
|
format: "{{InvalidFunction}}",
|
|
|
|
expectedErr: `template parsing error: template: :1: function "InvalidFunction" not defined`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "Nil format",
|
|
|
|
format: "{{nil}}",
|
|
|
|
expectedErr: `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "JSON format",
|
|
|
|
format: "{{json .}}",
|
|
|
|
expected: `{"Description":"Official build","IsAutomated":"false","IsOfficial":"true","Name":"result1","StarCount":"5000"}
|
|
|
|
{"Description":"Not official","IsAutomated":"true","IsOfficial":"false","Name":"result2","StarCount":"5"}
|
|
|
|
`,
|
2017-08-15 06:58:49 -04:00
|
|
|
},
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "JSON format, single field",
|
|
|
|
format: "{{json .Name}}",
|
|
|
|
expected: `"result1"
|
|
|
|
"result2"
|
|
|
|
`,
|
2017-08-15 06:58:49 -04:00
|
|
|
},
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "Table format",
|
|
|
|
format: NewSearchFormat("table"),
|
|
|
|
expected: string(golden.Get(t, "search-context-write-table.golden")),
|
2017-08-15 06:58:49 -04:00
|
|
|
},
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "Table format, single column",
|
|
|
|
format: NewSearchFormat("table {{.Name}}"),
|
|
|
|
expected: `NAME
|
2017-08-15 06:58:49 -04:00
|
|
|
result1
|
|
|
|
result2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "Custom format, single field",
|
|
|
|
format: NewSearchFormat("{{.Name}}"),
|
|
|
|
expected: `result1
|
2017-08-15 06:58:49 -04:00
|
|
|
result2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "Custom Format, two columns",
|
|
|
|
format: NewSearchFormat("{{.Name}} {{.StarCount}}"),
|
|
|
|
expected: `result1 5000
|
2017-08-15 06:58:49 -04:00
|
|
|
result2 5
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-28 17:00:21 -04:00
|
|
|
results := []registrytypes.SearchResult{
|
2023-08-07 14:21:39 -04:00
|
|
|
{Name: "result1", Description: "Official build", StarCount: 5000, IsOfficial: true},
|
|
|
|
{Name: "result2", Description: "Not official", StarCount: 5, IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
|
2020-08-28 17:00:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
2023-08-08 11:31:03 -04:00
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
2020-08-28 17:00:21 -04:00
|
|
|
var out bytes.Buffer
|
2023-08-08 11:31:03 -04:00
|
|
|
err := SearchWrite(formatter.Context{Format: tc.format, Output: &out}, results)
|
|
|
|
if tc.expectedErr != "" {
|
|
|
|
assert.Check(t, is.Error(err, tc.expectedErr))
|
2020-08-28 17:00:21 -04:00
|
|
|
} else {
|
2023-08-08 11:31:03 -04:00
|
|
|
assert.Check(t, err)
|
2020-08-28 17:00:21 -04:00
|
|
|
}
|
2023-08-08 11:31:03 -04:00
|
|
|
assert.Check(t, is.Equal(out.String(), tc.expected))
|
2020-08-28 17:00:21 -04:00
|
|
|
})
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
}
|