@@ -3,49 +3,81 @@ package cmd
33import (
44 "bytes"
55 "context"
6+ "fmt"
67 "os"
78 "os/exec"
9+ "path/filepath"
810 "testing"
911 "time"
1012
1113 "github.com/stretchr/testify/require"
14+
15+ arktesting "github.com/jetstack/preflight/internal/cyberark/testing"
1216)
1317
14- // TestAgentRunOneShot runs the agent in `--one-shot` mode and verifies that it exits
15- // after the first data gathering iteration.
16- func TestAgentRunOneShot (t * testing.T ) {
18+ // TestOutputModes tests the different output modes of the agent command.
19+ // It does this by running the agent command in a subprocess with the
20+ // appropriate flags and configuration files.
21+ // It assumes that the test is being run from the "cmd" directory and that
22+ // the repository root is the parent directory of the current working directory.
23+ func TestOutputModes (t * testing.T ) {
24+ repoRoot := findRepoRoot (t )
25+
26+ t .Run ("localfile" , func (t * testing.T ) {
27+ runSubprocess (t , repoRoot , []string {
28+ "--agent-config-file" , filepath .Join (repoRoot , "examples/localfile/config.yaml" ),
29+ "--input-path" , filepath .Join (repoRoot , "examples/localfile/input.json" ),
30+ "--output-path" , "/dev/null" ,
31+ })
32+ })
33+
34+ t .Run ("machinehub" , func (t * testing.T ) {
35+ arktesting .SkipIfNoEnv (t )
36+ runSubprocess (t , repoRoot , []string {
37+ "--agent-config-file" , filepath .Join (repoRoot , "examples/machinehub/config.yaml" ),
38+ "--input-path" , filepath .Join (repoRoot , "examples/machinehub/input.json" ),
39+ "--machine-hub" ,
40+ })
41+ })
42+ }
43+
44+ // findRepoRoot returns the absolute path to the repository root.
45+ // It assumes that the test is being run from the "cmd" directory.
46+ func findRepoRoot (t * testing.T ) string {
47+ cwd , err := os .Getwd ()
48+ require .NoError (t , err )
49+ repoRoot , err := filepath .Abs (filepath .Join (cwd , ".." ))
50+ require .NoError (t , err )
51+ return repoRoot
52+ }
53+
54+ // runSubprocess runs the current test in a subprocess with the given args.
55+ // It sets the GO_CHILD environment variable to indicate to the subprocess
56+ // that it should run the main function instead of the test function.
57+ // It captures and logs the stdout and stderr of the subprocess.
58+ // It fails the test if the subprocess exits with a non-zero status.
59+ // It uses a timeout to avoid hanging indefinitely.
60+ func runSubprocess (t * testing.T , repoRoot string , args []string ) {
1761 if _ , found := os .LookupEnv ("GO_CHILD" ); found {
18- os .Args = []string {
62+ os .Args = append ( []string {
1963 "preflight" ,
2064 "agent" ,
65+ "--log-level" , "6" ,
2166 "--one-shot" ,
22- "--agent-config-file=testdata/agent/one-shot/success/config.yaml" ,
23- "--input-path=testdata/agent/one-shot/success/input.json" ,
24- "--output-path=/dev/null" ,
25- "-v=9" ,
26- }
67+ }, args ... )
2768 Execute ()
2869 return
2970 }
30- t .Log ("Running child process" )
31- ctx , cancel := context .WithTimeout (t .Context (), time .Second * 3 )
71+ t .Log ("Running child process" , os . Args [ 0 ], "-test.run=^" + t . Name () + "$" )
72+ ctx , cancel := context .WithTimeout (t .Context (), time .Second * 10 )
3273 defer cancel ()
33- cmd := exec .CommandContext (ctx , os .Args [0 ], "-test.run=^TestAgentRunOneShot$" )
34- var (
35- stdout bytes.Buffer
36- stderr bytes.Buffer
37- )
74+ cmd := exec .CommandContext (ctx , os .Args [0 ], "-test.run=^" + t .Name ()+ "$" )
75+ var stdout , stderr bytes.Buffer
3876 cmd .Stdout = & stdout
3977 cmd .Stderr = & stderr
40- cmd .Env = append (
41- os .Environ (),
42- "GO_CHILD=true" ,
43- )
78+ cmd .Env = append (os .Environ (), "GO_CHILD=true" )
4479 err := cmd .Run ()
45-
46- stdoutStr := stdout .String ()
47- stderrStr := stderr .String ()
48- t .Logf ("STDOUT\n %s\n " , stdoutStr )
49- t .Logf ("STDERR\n %s\n " , stderrStr )
50- require .NoError (t , err , context .Cause (ctx ))
80+ t .Logf ("STDOUT\n %s\n " , stdout .String ())
81+ t .Logf ("STDERR\n %s\n " , stderr .String ())
82+ require .NoError (t , err , fmt .Sprintf ("Error: %v\n STDERR: %s" , err , stderr .String ()))
5183}
0 commit comments