2015-12-11 01:36:08 +00:00
|
|
|
package inspect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-03-04 17:29:44 +00:00
|
|
|
|
2016-12-12 08:34:03 +00:00
|
|
|
"github.com/docker/docker/pkg/templates"
|
2015-12-11 01:36:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type testElement struct {
|
|
|
|
DNS string `json:"Dns"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateInspectorDefault(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
2016-03-04 17:29:44 +00:00
|
|
|
tmpl, err := templates.Parse("{{.DNS}}")
|
2015-12-11 01:36:08 +00:00
|
|
|
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)
|
2016-03-04 17:29:44 +00:00
|
|
|
tmpl, err := templates.Parse("{{.DNS}}")
|
2015-12-11 01:36:08 +00:00
|
|
|
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)
|
2016-03-04 17:29:44 +00:00
|
|
|
tmpl, err := templates.Parse("{{.Foo}}")
|
2015-12-11 01:36:08 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(err.Error(), "Template parsing error") {
|
|
|
|
t.Fatalf("Expected template error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateInspectorRawFallback(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
2016-03-04 17:29:44 +00:00
|
|
|
tmpl, err := templates.Parse("{{.Dns}}")
|
2015-12-11 01:36:08 +00:00
|
|
|
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)
|
2016-03-04 17:29:44 +00:00
|
|
|
tmpl, err := templates.Parse("{{.Dns}}")
|
2015-12-11 01:36:08 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(err.Error(), "Template parsing error") {
|
|
|
|
t.Fatalf("Expected template error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateInspectorMultiple(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
2016-03-04 17:29:44 +00:00
|
|
|
tmpl, err := templates.Parse("{{.DNS}}")
|
2015-12-11 01:36:08 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2015-12-18 01:46:48 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|