2017-03-05 13:02:03 -05:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-03-05 13:02:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStackContextWrite(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
context Context
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
// Errors
|
|
|
|
{
|
|
|
|
Context{Format: "{{InvalidFunction}}"},
|
|
|
|
`Template parsing error: template: :1: function "InvalidFunction" not defined
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: "{{nil}}"},
|
|
|
|
`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Table format
|
|
|
|
{
|
2018-04-09 09:11:45 -04:00
|
|
|
Context{Format: Format(SwarmStackTableFormat)},
|
2018-03-27 10:38:47 -04:00
|
|
|
`NAME SERVICES ORCHESTRATOR
|
|
|
|
baz 2 orchestrator1
|
|
|
|
bar 1 orchestrator2
|
2017-03-05 13:02:03 -05:00
|
|
|
`,
|
|
|
|
},
|
2018-04-09 09:11:45 -04:00
|
|
|
// Kubernetes table format adds Namespace column
|
2017-03-05 13:02:03 -05:00
|
|
|
{
|
2018-04-09 09:11:45 -04:00
|
|
|
Context{Format: Format(KubernetesStackTableFormat)},
|
|
|
|
`NAME SERVICES ORCHESTRATOR NAMESPACE
|
|
|
|
baz 2 orchestrator1 namespace1
|
|
|
|
bar 1 orchestrator2 namespace2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: Format("table {{.Name}}")},
|
2017-03-05 13:02:03 -05:00
|
|
|
`NAME
|
|
|
|
baz
|
|
|
|
bar
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Custom Format
|
|
|
|
{
|
2018-04-09 09:11:45 -04:00
|
|
|
Context{Format: Format("{{.Name}}")},
|
2017-03-05 13:02:03 -05:00
|
|
|
`baz
|
|
|
|
bar
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
stacks := []*Stack{
|
2018-04-09 09:11:45 -04:00
|
|
|
{Name: "baz", Services: 2, Orchestrator: "orchestrator1", Namespace: "namespace1"},
|
|
|
|
{Name: "bar", Services: 1, Orchestrator: "orchestrator2", Namespace: "namespace2"},
|
2017-03-05 13:02:03 -05:00
|
|
|
}
|
|
|
|
for _, testcase := range cases {
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
testcase.context.Output = out
|
|
|
|
err := StackWrite(testcase.context, stacks)
|
|
|
|
if err != nil {
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.Check(t, is.ErrorContains(err, testcase.expected))
|
2017-03-05 13:02:03 -05:00
|
|
|
} else {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(out.String(), testcase.expected))
|
2017-03-05 13:02:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|