Skip to content

Commit 9db7f18

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

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

pkg/agent/run_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
// Silence the warning about missing pod name for event generation
22+
// TODO(wallrj): This should not be required when an `--input-file` has been supplied.
23+
t.Setenv("POD_NAME", "venafi-kubernetes-e2e")
24+
// Silence the error about missing kubeconfig.
25+
// TODO(wallrj): This should not be required when an `--input-file` has been supplied.
26+
t.Setenv("KUBECONFIG", "testdata/success/kubeconfig.yaml")
27+
28+
c := &cobra.Command{}
29+
InitAgentCmdFlags(c, &Flags)
30+
logs.AddFlags(c.Flags())
31+
32+
err := c.ParseFlags([]string{
33+
"--one-shot",
34+
// TODO(wallrj): This should not be required when an `--input-file` has been supplied.
35+
"--api-token=should-not-be-required",
36+
// TODO(wallrj): This should not be required when an `--input-file` has been supplied.
37+
"--install-namespace=default",
38+
"--agent-config-file=testdata/success/config.yaml",
39+
"--input-path=testdata/success/input.json",
40+
"--output-path=/dev/null",
41+
"-v=1",
42+
})
43+
require.NoError(t, err)
44+
45+
logs.Initialize()
46+
Run(c, nil)
47+
klog.Flush()
48+
return
49+
}
50+
t.Log("Running child process")
51+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
52+
defer cancel()
53+
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestRunOneShot$")
54+
var (
55+
stdout bytes.Buffer
56+
stderr bytes.Buffer
57+
)
58+
cmd.Stdout = &stdout
59+
cmd.Stderr = &stderr
60+
cmd.Env = append(
61+
os.Environ(),
62+
"GO_CHILD=true",
63+
)
64+
err := cmd.Run()
65+
66+
stdoutStr := stdout.String()
67+
stderrStr := stderr.String()
68+
t.Logf("STDOUT\n%s\n", stdoutStr)
69+
t.Logf("STDERR\n%s\n", stderrStr)
70+
require.NoError(t, err, context.Cause(ctx))
71+
}
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+
[]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Just enough kubeconfig to satisfy client-go
2+
apiVersion: v1
3+
kind: Config
4+
current-context: cluster-1
5+
contexts:
6+
- name: cluster-1
7+
context:
8+
cluster: cluster-1
9+
user: user-1
10+
clusters:
11+
- name: cluster-1
12+
cluster:
13+
server: https://192.0.2.1:8443
14+
preferences: {}
15+
users: []

0 commit comments

Comments
 (0)