Skip to content

Commit 455883b

Browse files
committed
Test that one-shot mode exits
Signed-off-by: Richard Wall <[email protected]>
1 parent c87f7c3 commit 455883b

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

pkg/agent/run_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package agent
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"os"
7+
"os/exec"
8+
"testing"
9+
"time"
10+
11+
"github.com/jetstack/preflight/pkg/logs"
12+
"github.com/spf13/cobra"
13+
"github.com/stretchr/testify/require"
14+
"k8s.io/klog/v2"
15+
)
16+
17+
// TestRunOneShot runs the agent in `--one-shot` mode and verifies that it exits
18+
// after the first data gathering iteration.
19+
func TestRunOneShot(t *testing.T) {
20+
if _, found := os.LookupEnv("GO_CHILD"); found {
21+
c := &cobra.Command{}
22+
InitAgentCmdFlags(c, &Flags)
23+
logs.AddFlags(c.Flags())
24+
25+
err := c.ParseFlags([]string{
26+
"--one-shot",
27+
"--api-token=should-not-be-required",
28+
"--install-namespace=default",
29+
"--agent-config-file=testdata/success/config.yaml",
30+
"--input-path=testdata/success/input.json",
31+
"--output-path=/dev/null",
32+
"-v=1",
33+
})
34+
require.NoError(t, err)
35+
36+
logs.Initialize()
37+
Run(c, nil)
38+
klog.Flush()
39+
return
40+
}
41+
t.Log("Running child process")
42+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
43+
defer cancel()
44+
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestRunOneShot$")
45+
var (
46+
stdout bytes.Buffer
47+
stderr bytes.Buffer
48+
)
49+
cmd.Stdout = &stdout
50+
cmd.Stderr = &stderr
51+
cmd.Env = append(
52+
os.Environ(),
53+
"POD_NAME=venafi-kubernetes-agent-e2e",
54+
"GO_CHILD=true",
55+
)
56+
err := cmd.Run()
57+
58+
stdoutStr := stdout.String()
59+
stderrStr := stderr.String()
60+
t.Logf("STDOUT\n%s\n", stdoutStr)
61+
t.Logf("STDERR\n%s\n", stderrStr)
62+
require.NoError(t, err, context.Cause(ctx))
63+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cluster_id: "venafi-kubernetes-agent-e2e"
2+
organization_id: "venafi-kubernetes-agent-e2e"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

0 commit comments

Comments
 (0)