Skip to content

Commit

Permalink
Test that one-shot mode exits
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Wall <richard.wall@venafi.com>
  • Loading branch information
wallrj committed Nov 5, 2024
1 parent c87f7c3 commit 2c5c0a7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
60 changes: 60 additions & 0 deletions pkg/agent/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package agent

import (
"bytes"
"context"
"os"
"os/exec"
"testing"
"time"

"github.com/jetstack/preflight/pkg/logs"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)

// TestRunOneShot runs the agent in `--one-shot` mode and verifies that it exits
// after the first data gathering iteration.
func TestRunOneShot(t *testing.T) {
if _, found := os.LookupEnv("GO_CHILD"); found {
c := &cobra.Command{}
InitAgentCmdFlags(c, &Flags)
logs.AddFlags(c.Flags())

err := c.ParseFlags([]string{
"--one-shot",
"--api-token=should-not-be-required",
"--install-namespace=default",
"--agent-config-file=testdata/success/config.yaml",
"--output-path=/dev/null",
"-v=1",
})
require.NoError(t, err)

logs.Initialize()
Run(c, nil)
return
}
t.Log("Running child process")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestRunOneShot$")
var (
stdout bytes.Buffer
stderr bytes.Buffer
)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Env = append(
os.Environ(),
"POD_NAME=venafi-kubernetes-agent-e2e",
"GO_CHILD=true",
)
err := cmd.Run()

stdoutStr := stdout.String()
stderrStr := stderr.String()
t.Logf("STDOUT\n%s\n", stdoutStr)
t.Logf("STDERR\n%s\n", stderrStr)
require.NoError(t, err, context.Cause(ctx))
}
2 changes: 2 additions & 0 deletions pkg/agent/testdata/success/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cluster_id: "venafi-kubernetes-agent-e2e"
organization_id: "venafi-kubernetes-agent-e2e"

0 comments on commit 2c5c0a7

Please sign in to comment.