2015-09-15 23:44:38 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2016-07-13 19:23:33 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
2015-09-15 23:44:38 +00:00
|
|
|
"testing"
|
2016-04-11 22:20:49 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
2015-09-15 23:44:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHelpers_FormatKV(t *testing.T) {
|
2015-09-27 21:04:53 +00:00
|
|
|
in := []string{"alpha|beta", "charlie|delta", "echo|"}
|
2015-09-15 23:44:38 +00:00
|
|
|
out := formatKV(in)
|
|
|
|
|
|
|
|
expect := "alpha = beta\n"
|
2015-09-27 21:04:53 +00:00
|
|
|
expect += "charlie = delta\n"
|
|
|
|
expect += "echo = <none>"
|
2015-09-15 23:44:38 +00:00
|
|
|
|
|
|
|
if out != expect {
|
|
|
|
t.Fatalf("expect: %s, got: %s", expect, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHelpers_FormatList(t *testing.T) {
|
|
|
|
in := []string{"alpha|beta||delta"}
|
|
|
|
out := formatList(in)
|
|
|
|
|
|
|
|
expect := "alpha beta <none> delta"
|
|
|
|
|
|
|
|
if out != expect {
|
|
|
|
t.Fatalf("expect: %s, got: %s", expect, out)
|
|
|
|
}
|
|
|
|
}
|
2016-04-11 22:20:49 +00:00
|
|
|
|
|
|
|
func TestHelpers_NodeID(t *testing.T) {
|
|
|
|
srv, _, _ := testServer(t, nil)
|
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
meta := Meta{Ui: new(cli.MockUi)}
|
|
|
|
client, err := meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is because there is no client
|
|
|
|
if _, err := getLocalNodeID(client); err == nil {
|
|
|
|
t.Fatalf("getLocalNodeID() should fail")
|
|
|
|
}
|
|
|
|
}
|
2016-07-13 19:23:33 +00:00
|
|
|
|
|
|
|
func TestHelpers_LineLimitReader(t *testing.T) {
|
|
|
|
helloString := `hello
|
|
|
|
world
|
|
|
|
this
|
|
|
|
is
|
|
|
|
a
|
|
|
|
test`
|
|
|
|
|
|
|
|
noLines := "jskdfhjasdhfjkajkldsfdlsjkahfkjdsafa"
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output string
|
|
|
|
Lines int
|
|
|
|
SearchLimit int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: helloString,
|
|
|
|
Output: helloString,
|
|
|
|
Lines: 6,
|
|
|
|
SearchLimit: 1000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: helloString,
|
|
|
|
Output: `world
|
|
|
|
this
|
|
|
|
is
|
|
|
|
a
|
|
|
|
test`,
|
|
|
|
Lines: 5,
|
|
|
|
SearchLimit: 1000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: helloString,
|
|
|
|
Output: `test`,
|
|
|
|
Lines: 1,
|
|
|
|
SearchLimit: 1000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: helloString,
|
|
|
|
Output: "",
|
|
|
|
Lines: 0,
|
|
|
|
SearchLimit: 1000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: helloString,
|
|
|
|
Output: helloString,
|
|
|
|
Lines: 6,
|
|
|
|
SearchLimit: 1, // Exceed the limit
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: noLines,
|
|
|
|
Output: noLines,
|
|
|
|
Lines: 10,
|
|
|
|
SearchLimit: 1000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: noLines,
|
|
|
|
Output: noLines,
|
|
|
|
Lines: 10,
|
|
|
|
SearchLimit: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cases {
|
|
|
|
in := ioutil.NopCloser(strings.NewReader(c.Input))
|
|
|
|
limit := NewLineLimitReader(in, c.Lines, c.SearchLimit)
|
|
|
|
outBytes, err := ioutil.ReadAll(limit)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("case %d failed: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := string(outBytes)
|
|
|
|
if out != c.Output {
|
|
|
|
t.Fatalf("case %d: got %q; want %q", i, out, c.Output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|