2016-09-08 13:11:39 -04:00
|
|
|
package inspect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-08 11:26:24 -04:00
|
|
|
"github.com/docker/cli/templates"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type testElement struct {
|
|
|
|
DNS string `json:"Dns"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateInspectorDefault(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
tmpl, err := templates.Parse("{{.DNS}}")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
i := NewTemplateInspector(b, tmpl)
|
|
|
|
if err := i.Inspect(testElement{"0.0.0.0"}, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if b.String() != "0.0.0.0\n" {
|
|
|
|
t.Fatalf("Expected `0.0.0.0\\n`, got `%s`", b.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateInspectorEmpty(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
tmpl, err := templates.Parse("{{.DNS}}")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
i := NewTemplateInspector(b, tmpl)
|
|
|
|
|
|
|
|
if err := i.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if b.String() != "\n" {
|
|
|
|
t.Fatalf("Expected `\\n`, got `%s`", b.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateInspectorTemplateError(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
tmpl, err := templates.Parse("{{.Foo}}")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
i := NewTemplateInspector(b, tmpl)
|
|
|
|
|
|
|
|
err = i.Inspect(testElement{"0.0.0.0"}, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error got nil")
|
|
|
|
}
|
|
|
|
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
if !strings.HasPrefix(err.Error(), "template parsing error") {
|
2016-09-08 13:11:39 -04:00
|
|
|
t.Fatalf("Expected template error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateInspectorRawFallback(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
tmpl, err := templates.Parse("{{.Dns}}")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
i := NewTemplateInspector(b, tmpl)
|
|
|
|
if err := i.Inspect(testElement{"0.0.0.0"}, []byte(`{"Dns": "0.0.0.0"}`)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if b.String() != "0.0.0.0\n" {
|
|
|
|
t.Fatalf("Expected `0.0.0.0\\n`, got `%s`", b.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateInspectorRawFallbackError(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
tmpl, err := templates.Parse("{{.Dns}}")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
i := NewTemplateInspector(b, tmpl)
|
|
|
|
err = i.Inspect(testElement{"0.0.0.0"}, []byte(`{"Foo": "0.0.0.0"}`))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error got nil")
|
|
|
|
}
|
|
|
|
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
if !strings.HasPrefix(err.Error(), "template parsing error") {
|
2016-09-08 13:11:39 -04:00
|
|
|
t.Fatalf("Expected template error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateInspectorMultiple(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
tmpl, err := templates.Parse("{{.DNS}}")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
i := NewTemplateInspector(b, tmpl)
|
|
|
|
|
|
|
|
if err := i.Inspect(testElement{"0.0.0.0"}, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := i.Inspect(testElement{"1.1.1.1"}, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if b.String() != "0.0.0.0\n1.1.1.1\n" {
|
|
|
|
t.Fatalf("Expected `0.0.0.0\\n1.1.1.1\\n`, got `%s`", b.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIndentedInspectorDefault(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
i := NewIndentedInspector(b)
|
|
|
|
if err := i.Inspect(testElement{"0.0.0.0"}, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := `[
|
|
|
|
{
|
|
|
|
"Dns": "0.0.0.0"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`
|
|
|
|
if b.String() != expected {
|
|
|
|
t.Fatalf("Expected `%s`, got `%s`", expected, b.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIndentedInspectorMultiple(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
i := NewIndentedInspector(b)
|
|
|
|
if err := i.Inspect(testElement{"0.0.0.0"}, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.Inspect(testElement{"1.1.1.1"}, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := `[
|
|
|
|
{
|
|
|
|
"Dns": "0.0.0.0"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Dns": "1.1.1.1"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`
|
|
|
|
if b.String() != expected {
|
|
|
|
t.Fatalf("Expected `%s`, got `%s`", expected, b.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIndentedInspectorEmpty(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
i := NewIndentedInspector(b)
|
|
|
|
|
|
|
|
if err := i.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "[]\n"
|
|
|
|
if b.String() != expected {
|
|
|
|
t.Fatalf("Expected `%s`, got `%s`", expected, b.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIndentedInspectorRawElements(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
i := NewIndentedInspector(b)
|
|
|
|
if err := i.Inspect(testElement{"0.0.0.0"}, []byte(`{"Dns": "0.0.0.0", "Node": "0"}`)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.Inspect(testElement{"1.1.1.1"}, []byte(`{"Dns": "1.1.1.1", "Node": "1"}`)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := `[
|
|
|
|
{
|
|
|
|
"Dns": "0.0.0.0",
|
|
|
|
"Node": "0"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Dns": "1.1.1.1",
|
|
|
|
"Node": "1"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`
|
|
|
|
if b.String() != expected {
|
|
|
|
t.Fatalf("Expected `%s`, got `%s`", expected, b.String())
|
|
|
|
}
|
|
|
|
}
|
2017-06-18 11:48:10 -04:00
|
|
|
|
|
|
|
// moby/moby#32235
|
|
|
|
// This test verifies that even if `tryRawInspectFallback` is called the fields containing
|
|
|
|
// numerical values are displayed correctly.
|
|
|
|
// For example, `docker inspect --format "{{.Id}} {{.Size}} alpine` and
|
|
|
|
// `docker inspect --format "{{.ID}} {{.Size}} alpine" will have the same output which is
|
|
|
|
// sha256:651aa95985aa4a17a38ffcf71f598ec461924ca96865facc2c5782ef2d2be07f 3983636
|
|
|
|
func TestTemplateInspectorRawFallbackNumber(t *testing.T) {
|
|
|
|
// Using typedElem to automatically fall to tryRawInspectFallback.
|
|
|
|
typedElem := struct {
|
|
|
|
ID string `json:"Id"`
|
|
|
|
}{"ad3"}
|
|
|
|
testcases := []struct {
|
|
|
|
raw []byte
|
|
|
|
exp string
|
|
|
|
}{
|
|
|
|
{raw: []byte(`{"Id": "ad3", "Size": 53317}`), exp: "53317 ad3\n"},
|
|
|
|
{raw: []byte(`{"Id": "ad3", "Size": 53317.102}`), exp: "53317.102 ad3\n"},
|
|
|
|
{raw: []byte(`{"Id": "ad3", "Size": 53317.0}`), exp: "53317.0 ad3\n"},
|
|
|
|
}
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
tmpl, err := templates.Parse("{{.Size}} {{.Id}}")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-06-18 11:48:10 -04:00
|
|
|
|
|
|
|
i := NewTemplateInspector(b, tmpl)
|
|
|
|
for _, tc := range testcases {
|
|
|
|
err = i.Inspect(typedElem, tc.raw)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-06-18 11:48:10 -04:00
|
|
|
|
|
|
|
err = i.Flush()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-06-18 11:48:10 -04:00
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(tc.exp, b.String()))
|
2017-06-18 11:48:10 -04:00
|
|
|
b.Reset()
|
|
|
|
}
|
|
|
|
}
|
2021-01-18 05:19:37 -05:00
|
|
|
|
|
|
|
func TestNewTemplateInspectorFromString(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
template string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty template outputs json by default",
|
|
|
|
template: "",
|
|
|
|
expected: `[
|
|
|
|
{
|
|
|
|
"Name": "test"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "json specific value outputs json",
|
|
|
|
template: "json",
|
2022-03-08 08:54:21 -05:00
|
|
|
expected: `[{"Name":"test"}]
|
2021-01-18 05:19:37 -05:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "template is applied",
|
|
|
|
template: "{{.Name}}",
|
|
|
|
expected: "test\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
value := struct {
|
|
|
|
Name string
|
|
|
|
}{
|
|
|
|
"test",
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
i, err := NewTemplateInspectorFromString(b, tc.template)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
err = i.Inspect(value, nil)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
err = i.Flush()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, b.String(), tc.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|