forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_test.go
More file actions
82 lines (74 loc) · 1.65 KB
/
format_test.go
File metadata and controls
82 lines (74 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package command
import (
"strings"
"testing"
"github.com/ghodss/yaml"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/helper/jsonutil"
)
var output string
type mockUi struct {
t *testing.T
SampleData string
}
func (m mockUi) Ask(_ string) (string, error) {
m.t.FailNow()
return "", nil
}
func (m mockUi) AskSecret(_ string) (string, error) {
m.t.FailNow()
return "", nil
}
func (m mockUi) Output(s string) {
output = s
}
func (m mockUi) Info(s string) {
m.t.Log(s)
}
func (m mockUi) Error(s string) {
m.t.Log(s)
}
func (m mockUi) Warn(s string) {
m.t.Log(s)
}
func TestJsonFormatter(t *testing.T) {
ui := mockUi{t: t, SampleData: "something"}
if err := outputWithFormat(ui, "json", nil, ui); err != 0 {
t.Fatal(err)
}
var newUi mockUi
if err := jsonutil.DecodeJSON([]byte(output), &newUi); err != nil {
t.Fatal(err)
}
if newUi.SampleData != ui.SampleData {
t.Fatalf(`values not equal ("%s" != "%s")`,
newUi.SampleData,
ui.SampleData)
}
}
func TestYamlFormatter(t *testing.T) {
ui := mockUi{t: t, SampleData: "something"}
if err := outputWithFormat(ui, "yaml", nil, ui); err != 0 {
t.Fatal(err)
}
var newUi mockUi
err := yaml.Unmarshal([]byte(output), &newUi)
if err != nil {
t.Fatal(err)
}
if newUi.SampleData != ui.SampleData {
t.Fatalf(`values not equal ("%s" != "%s")`,
newUi.SampleData,
ui.SampleData)
}
}
func TestTableFormatter(t *testing.T) {
ui := mockUi{t: t}
s := api.Secret{Data: map[string]interface{}{"k": "something"}}
if err := outputWithFormat(ui, "table", &s, &s); err != 0 {
t.Fatal(err)
}
if !strings.Contains(output, "something") {
t.Fatal("did not find 'something'")
}
}