fleet/cmd/fleetctl/convert_test.go
guangwu 33858d7301
chore: remove refs to deprecated io/ioutil (#14485)
# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [ ] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [ ] Documented any API changes (docs/Using-Fleet/REST-API.md or
docs/Contributing/API-for-contributors.md)
- [ ] Documented any permissions changes (docs/Using
Fleet/manage-access.md)
- [ ] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)
- [ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for
new osquery data ingestion features.
- [ ] Added/updated tests
- [ ] Manual QA for all new/changed functionality
  - For Orbit and Fleet Desktop changes:
- [ ] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.
- [ ] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).

Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
2023-10-27 15:28:54 -03:00

69 lines
1.7 KiB
Go

package main
import (
"io"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
)
func TestConvertFileOutput(t *testing.T) {
// setup the cli and the convert command
app := cli.NewApp()
app.Commands = []*cli.Command{convertCommand()}
app.Reader = os.Stdin
app.Writer = os.Stdout
app.Setup()
// read the expected output file
expected, err := os.ReadFile(filepath.Join("testdata", "convert_output.yml"))
require.NoError(t, err)
// setup a file for the convert command to write to
file, err := os.CreateTemp(t.TempDir(), "convert_output.yml")
defer file.Close()
require.NoError(t, err)
// get the program name
args := os.Args[0:1]
args = append(args, []string{"convert", "-f", filepath.Join("testdata", "convert_input.conf"), "-o", file.Name()}...)
err = app.Run(args)
require.NoError(t, err)
// convert command ran and wrote converted file to output destination
got, err := os.ReadFile(file.Name())
require.NoError(t, err)
require.YAMLEq(t, string(expected), string(got))
}
func TestConvertFileStdout(t *testing.T) {
r, w, _ := os.Pipe()
oldStdout := os.Stdout
os.Stdout = w
// setup the cli and the convert command
app := cli.NewApp()
app.Commands = []*cli.Command{convertCommand()}
app.Reader = os.Stdin
app.Writer = os.Stdout
app.Setup()
// read the expected output file
expected, err := os.ReadFile(filepath.Join("testdata", "convert_output.yml"))
require.NoError(t, err)
// get the program name
args := os.Args[0:1]
args = append(args, []string{"convert", "-f", filepath.Join("testdata", "convert_input.conf")}...)
err = app.Run(args)
require.NoError(t, err)
os.Stdout = oldStdout
w.Close()
out, _ := io.ReadAll(r)
require.YAMLEq(t, string(expected), string(out))
}