From 45a5d778969db3685bf3ed756608ab65d3327546 Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Tue, 12 Nov 2024 06:55:48 +0000 Subject: [PATCH] Move the agent --one-shot test to the cmd package (#610) And use the Execute function Signed-off-by: Richard Wall --- pkg/agent/run_test.go => cmd/agent_test.go | 37 ++++++------------- .../agent}/one-shot/success/config.yaml | 0 .../agent}/one-shot/success/input.json | 0 .../agent}/one-shot/success/kubeconfig.yaml | 0 4 files changed, 12 insertions(+), 25 deletions(-) rename pkg/agent/run_test.go => cmd/agent_test.go (63%) rename {pkg/agent/testdata => cmd/testdata/agent}/one-shot/success/config.yaml (100%) rename {pkg/agent/testdata => cmd/testdata/agent}/one-shot/success/input.json (100%) rename {pkg/agent/testdata => cmd/testdata/agent}/one-shot/success/kubeconfig.yaml (100%) diff --git a/pkg/agent/run_test.go b/cmd/agent_test.go similarity index 63% rename from pkg/agent/run_test.go rename to cmd/agent_test.go index 3bd54773..9c92144d 100644 --- a/pkg/agent/run_test.go +++ b/cmd/agent_test.go @@ -1,4 +1,4 @@ -package agent +package cmd import ( "bytes" @@ -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 diff --git a/pkg/agent/testdata/one-shot/success/config.yaml b/cmd/testdata/agent/one-shot/success/config.yaml similarity index 100% rename from pkg/agent/testdata/one-shot/success/config.yaml rename to cmd/testdata/agent/one-shot/success/config.yaml diff --git a/pkg/agent/testdata/one-shot/success/input.json b/cmd/testdata/agent/one-shot/success/input.json similarity index 100% rename from pkg/agent/testdata/one-shot/success/input.json rename to cmd/testdata/agent/one-shot/success/input.json diff --git a/pkg/agent/testdata/one-shot/success/kubeconfig.yaml b/cmd/testdata/agent/one-shot/success/kubeconfig.yaml similarity index 100% rename from pkg/agent/testdata/one-shot/success/kubeconfig.yaml rename to cmd/testdata/agent/one-shot/success/kubeconfig.yaml