@@ -3,111 +3,74 @@ package cmd
33import (
44 "bytes"
55 "context"
6+ "fmt"
67 "os"
78 "os/exec"
89 "path/filepath"
910 "testing"
1011 "time"
1112
1213 "github.com/stretchr/testify/require"
14+
15+ "github.com/jetstack/preflight/pkg/testutil"
1316)
1417
15- // TestAgentRunOneShot runs the agent in `--one-shot` mode and verifies that it exits
16- // after the first data gathering iteration.
17- 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 currently only tests the --machine-hub mode, as that is the only
22+ // output mode that has a configuration file in the examples directory.
23+ // It assumes that the test is being run from the "cmd" directory and that
24+ // the repository root is the parent directory of the current working directory.
25+ func TestOutputModes (t * testing.T ) {
26+ repoRoot := findRepoRoot (t )
27+ t .Run ("machinehub" , func (t * testing.T ) {
28+ testutil .ArkSkipIfNoEnv (t )
29+ runSubprocess (t , repoRoot , []string {
30+ "--agent-config-file" , filepath .Join (repoRoot , "examples/machinehub/config.yaml" ),
31+ "--input-path" , filepath .Join (repoRoot , "examples/machinehub/input.json" ),
32+ "--machine-hub" ,
33+ })
34+ })
35+ }
36+
37+ // findRepoRoot returns the absolute path to the repository root.
38+ // It assumes that the test is being run from the "cmd" directory.
39+ func findRepoRoot (t * testing.T ) string {
40+ cwd , err := os .Getwd ()
41+ require .NoError (t , err )
42+ repoRoot , err := filepath .Abs (filepath .Join (cwd , ".." ))
43+ require .NoError (t , err )
44+ return repoRoot
45+ }
46+
47+ // runSubprocess runs the current test in a subprocess with the given args.
48+ // It sets the GO_CHILD environment variable to indicate to the subprocess
49+ // that it should run the main function instead of the test function.
50+ // It captures and logs the stdout and stderr of the subprocess.
51+ // It fails the test if the subprocess exits with a non-zero status.
52+ // It uses a timeout to avoid hanging indefinitely.
53+ func runSubprocess (t * testing.T , repoRoot string , args []string ) {
1854 if _ , found := os .LookupEnv ("GO_CHILD" ); found {
19- os .Args = []string {
55+ os .Args = append ( []string {
2056 "preflight" ,
2157 "agent" ,
58+ "--log-level" , "6" ,
2259 "--one-shot" ,
23- "--agent-config-file=testdata/agent/one-shot/success/config.yaml" ,
24- "--input-path=testdata/agent/one-shot/success/input.json" ,
25- "--output-path=/dev/null" ,
26- "-v=9" ,
27- }
60+ }, args ... )
2861 Execute ()
2962 return
3063 }
31- t .Log ("Running child process" )
32- ctx , cancel := context .WithTimeout (t .Context (), time .Second * 3 )
64+ t .Log ("Running child process" , os . Args [ 0 ], "-test.run=^" + t . Name () + "$" )
65+ ctx , cancel := context .WithTimeout (t .Context (), time .Second * 10 )
3366 defer cancel ()
34- cmd := exec .CommandContext (ctx , os .Args [0 ], "-test.run=^TestAgentRunOneShot$" )
35- var (
36- stdout bytes.Buffer
37- stderr bytes.Buffer
38- )
67+ cmd := exec .CommandContext (ctx , os .Args [0 ], "-test.run=^" + t .Name ()+ "$" )
68+ var stdout , stderr bytes.Buffer
3969 cmd .Stdout = & stdout
4070 cmd .Stderr = & stderr
41- cmd .Env = append (
42- os .Environ (),
43- "GO_CHILD=true" ,
44- )
71+ cmd .Env = append (os .Environ (), "GO_CHILD=true" )
4572 err := cmd .Run ()
46-
47- stdoutStr := stdout .String ()
48- stderrStr := stderr .String ()
49- t .Logf ("STDOUT\n %s\n " , stdoutStr )
50- t .Logf ("STDERR\n %s\n " , stderrStr )
51- require .NoError (t , err , context .Cause (ctx ))
52- }
53-
54- func TestOutputModes (t * testing.T ) {
55- cwd , err := os .Getwd ()
56- require .NoError (t , err )
57- repoRoot , err := filepath .Abs (filepath .Join (cwd , ".." ))
58- require .NoError (t , err )
59-
60- type testCase struct {
61- args []string
62- }
63-
64- tests := map [string ]testCase {
65- "machinehub" : {
66- args : []string {
67- "--agent-config-file" , "examples/machinehub/config.yaml" ,
68- "--input-path" , "examples/machinehub/input.json" ,
69- "--machine-hub" ,
70- },
71- },
72- }
73-
74- for name , testSpec := range tests {
75- t .Run (name , func (t * testing.T ) {
76- if _ , found := os .LookupEnv ("GO_CHILD" ); found {
77- os .Args = append ([]string {
78- "preflight" ,
79- "agent" ,
80- "--log-level" , "6" ,
81- "--one-shot" ,
82- }, testSpec .args ... )
83-
84- Execute ()
85- return
86- }
87- t .Log ("Running child process" )
88- ctx , cancel := context .WithTimeout (t .Context (), time .Second * 10 )
89- defer cancel ()
90-
91- cmd := exec .CommandContext (ctx , os .Args [0 ], "-test.run=^" + t .Name ()+ "$" )
92- var (
93- stdout bytes.Buffer
94- stderr bytes.Buffer
95- )
96- cmd .Stdout = & stdout
97- cmd .Stderr = & stderr
98- cmd .Dir = repoRoot
99-
100- cmd .Env = append (
101- os .Environ (),
102- "GO_CHILD=true" ,
103- )
104- err := cmd .Run ()
105-
106- stdoutStr := stdout .String ()
107- stderrStr := stderr .String ()
108- t .Logf ("STDOUT\n %s\n " , stdoutStr )
109- t .Logf ("STDERR\n %s\n " , stderrStr )
110- require .NoError (t , err , context .Cause (ctx ))
111- })
112- }
73+ t .Logf ("STDOUT\n %s\n " , stdout .String ())
74+ t .Logf ("STDERR\n %s\n " , stderr .String ())
75+ require .NoError (t , err , fmt .Sprintf ("Error: %v\n STDERR: %s" , err , stderr .String ()))
11376}
0 commit comments