Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VC-35738] Move the agent --one-shot test to the cmd package #610

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions pkg/agent/run_test.go → cmd/agent_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agent
package cmd

import (
"bytes"
Expand All @@ -8,53 +8,40 @@ import (
"testing"
"time"

"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"k8s.io/klog/v2"

"github.com/jetstack/preflight/pkg/logs"
)

// TestRunOneShot runs the agent in `--one-shot` mode and verifies that it exits
// TestAgentRunOneShot runs the agent in `--one-shot` mode and verifies that it exits
// after the first data gathering iteration.
func TestRunOneShot(t *testing.T) {
func TestAgentRunOneShot(t *testing.T) {
if _, found := os.LookupEnv("GO_CHILD"); found {
// Silence the warning about missing pod name for event generation
// TODO(wallrj): This should not be required when an `--input-file` has been supplied.
t.Setenv("POD_NAME", "venafi-kubernetes-e2e")
// Silence the error about missing kubeconfig.
// TODO(wallrj): This should not be required when an `--input-file` has been supplied.
t.Setenv("KUBECONFIG", "testdata/one-shot/success/kubeconfig.yaml")

c := &cobra.Command{}
InitAgentCmdFlags(c, &Flags)
logs.AddFlags(c.Flags())
t.Setenv("KUBECONFIG", "testdata/agent/one-shot/success/kubeconfig.yaml")

err := c.ParseFlags([]string{
os.Args = []string{
"preflight",
"agent",
"--one-shot",
// TODO(wallrj): This should not be required when an `--input-file` has been supplied.
"--api-token=should-not-be-required",
// TODO(wallrj): This should not be required when an `--input-file` has been supplied.
"--install-namespace=default",
"--agent-config-file=testdata/one-shot/success/config.yaml",
"--input-path=testdata/one-shot/success/input.json",
"--agent-config-file=testdata/agent/one-shot/success/config.yaml",
"--input-path=testdata/agent/one-shot/success/input.json",
"--output-path=/dev/null",
"-v=1",
})
require.NoError(t, err)

logs.Initialize()
defer klog.Flush()

runErr := Run(c, nil)
require.NoError(t, runErr, "Run returned an unexpected error")

}
Execute()
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$")
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestAgentRunOneShot$")
var (
stdout bytes.Buffer
stderr bytes.Buffer
Expand Down