1package asset
2
3import (
4	"errors"
5	"testing"
6
7	client "github.com/sensu/sensu-go/cli/client/testing"
8	test "github.com/sensu/sensu-go/cli/commands/testing"
9	"github.com/sensu/sensu-go/types"
10	"github.com/stretchr/testify/assert"
11	"github.com/stretchr/testify/require"
12)
13
14func TestInfoCommand(t *testing.T) {
15	assert := assert.New(t)
16
17	cli := test.NewCLI()
18	cmd := InfoCommand(cli)
19
20	assert.NotNil(cmd, "cmd should be returned")
21	assert.NotNil(cmd.RunE, "cmd should be able to be executed")
22	assert.Regexp("info", cmd.Use)
23	assert.Regexp("asset", cmd.Short)
24}
25
26func TestInfoCommandRunEClosure(t *testing.T) {
27	assert := assert.New(t)
28
29	cli := test.NewCLI()
30	client := cli.Client.(*client.MockClient)
31	client.On("FetchAsset", "in").Return(types.FixtureAsset("name-one"), nil)
32
33	cmd := InfoCommand(cli)
34	out, err := test.RunCmd(cmd, []string{"in"})
35
36	assert.NotEmpty(out)
37	assert.Contains(out, "name-one")
38	assert.Nil(err)
39}
40
41func TestInfoCommandRunMissingArgs(t *testing.T) {
42	assert := assert.New(t)
43
44	cli := test.NewCLI()
45	cmd := InfoCommand(cli)
46	out, err := test.RunCmd(cmd, []string{})
47
48	assert.NotEmpty(out)
49	assert.Contains(out, "Usage")
50	assert.Error(err)
51}
52
53func TestInfoCommandRunEClosureWithTable(t *testing.T) {
54	assert := assert.New(t)
55
56	cli := test.NewCLI()
57	client := cli.Client.(*client.MockClient)
58	client.On("FetchAsset", "in").Return(types.FixtureAsset("name-one"), nil)
59
60	cmd := InfoCommand(cli)
61	require.NoError(t, cmd.Flags().Set("format", "tabular"))
62
63	out, err := test.RunCmd(cmd, []string{"in"})
64
65	assert.NotEmpty(out)
66	assert.Contains(out, "Name")
67	assert.Contains(out, "Filters")
68	assert.Nil(err)
69}
70
71func TestInfoCommandRunEClosureWithErr(t *testing.T) {
72	assert := assert.New(t)
73
74	cli := test.NewCLI()
75	client := cli.Client.(*client.MockClient)
76	client.On("FetchAsset", "in").Return(&types.Asset{}, errors.New("my-err"))
77
78	cmd := InfoCommand(cli)
79	out, err := test.RunCmd(cmd, []string{"in"})
80
81	assert.Error(err)
82	assert.Equal("my-err", err.Error())
83	assert.Empty(out)
84}
85