|
| 1 | +package command_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + |
| 8 | + "github.com/salsadigitalauorg/shipshape/pkg/fact" |
| 9 | + . "github.com/salsadigitalauorg/shipshape/pkg/fact/command" |
| 10 | + "github.com/salsadigitalauorg/shipshape/pkg/internal" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCommandInit(t *testing.T) { |
| 14 | + assert := assert.New(t) |
| 15 | + |
| 16 | + // Test that the command plugin is registered. |
| 17 | + factPlugin := fact.Registry["command"]("TestCommand") |
| 18 | + assert.NotNil(factPlugin) |
| 19 | + keyFacter, ok := factPlugin.(*Command) |
| 20 | + assert.True(ok) |
| 21 | + assert.Equal("TestCommand", keyFacter.Name) |
| 22 | +} |
| 23 | + |
| 24 | +func TestCommandPluginName(t *testing.T) { |
| 25 | + commandF := Command{Name: "TestCommand"} |
| 26 | + assert.Equal(t, "command", commandF.PluginName()) |
| 27 | +} |
| 28 | + |
| 29 | +func TestCommandSupportedConnections(t *testing.T) { |
| 30 | + commandF := Command{Name: "TestCommand"} |
| 31 | + supportLevel, connections := commandF.SupportedConnections() |
| 32 | + assert.Equal(t, fact.SupportNone, supportLevel) |
| 33 | + assert.Empty(t, connections) |
| 34 | +} |
| 35 | + |
| 36 | +func TestCommandSupportedInputs(t *testing.T) { |
| 37 | + commandF := Command{Name: "TestCommand"} |
| 38 | + supportLevel, inputs := commandF.SupportedInputs() |
| 39 | + assert.Equal(t, fact.SupportNone, supportLevel) |
| 40 | + assert.ElementsMatch(t, []string{}, inputs) |
| 41 | +} |
| 42 | + |
| 43 | +func TestCommandCollect(t *testing.T) { |
| 44 | + tests := []internal.FactCollectTest{ |
| 45 | + { |
| 46 | + Name: "emptyCommand", |
| 47 | + Facter: &Command{Name: "TestCommand"}, |
| 48 | + ExpectedData: map[string]string{ |
| 49 | + "code": "1", "stderr": "exec: no command", "stdout": "", |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + Name: "echo", |
| 54 | + Facter: &Command{Name: "TestCommand", Cmd: "echo", Args: []string{"hello"}}, |
| 55 | + ExpectedData: map[string]string{ |
| 56 | + "code": "0", "stderr": "", "stdout": "hello", |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + Name: "multiline", |
| 61 | + Facter: &Command{Name: "TestCommand", Cmd: "ls", Args: []string{"-A1"}}, |
| 62 | + ExpectedData: map[string]string{ |
| 63 | + "code": "0", "stderr": "", "stdout": "command.go\ncommand_gen.go\ncommand_test.go", |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tt := range tests { |
| 69 | + t.Run(tt.Name, func(t *testing.T) { |
| 70 | + internal.TestFactCollect(t, tt) |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments