Skip to content
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
21 changes: 11 additions & 10 deletions internal/cleanup/cleanup_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/opdev/virtwork/internal/cleanup"
"github.com/opdev/virtwork/internal/config"
"github.com/opdev/virtwork/internal/resources"
"github.com/opdev/virtwork/internal/testutil"
"github.com/opdev/virtwork/internal/vm"
Expand Down Expand Up @@ -44,7 +45,7 @@ var _ = Describe("CleanupAll [integration]", func() {
opts := testutil.DefaultVMOpts("cleanup-vm-0", namespace)
Expect(vm.CreateVM(ctx, c, vm.BuildVMSpec(opts))).To(Succeed())

result, err := cleanup.CleanupAll(ctx, c, namespace, false, "")
result, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: namespace}, false, "")
Expect(err).NotTo(HaveOccurred())
Expect(result.VMsDeleted).To(Equal(1))
})
Expand All @@ -65,7 +66,7 @@ var _ = Describe("CleanupAll [integration]", func() {
}
Expect(resources.CreateService(ctx, c, svc)).To(Succeed())

result, err := cleanup.CleanupAll(ctx, c, namespace, false, "")
result, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: namespace}, false, "")
Expect(err).NotTo(HaveOccurred())
Expect(result.ServicesDeleted).To(Equal(1))
})
Expand All @@ -82,7 +83,7 @@ var _ = Describe("CleanupAll [integration]", func() {
),
).To(Succeed())

result, err := cleanup.CleanupAll(ctx, c, namespace, false, "")
result, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: namespace}, false, "")
Expect(err).NotTo(HaveOccurred())
Expect(result.SecretsDeleted).To(Equal(1))
})
Expand All @@ -98,7 +99,7 @@ var _ = Describe("CleanupAll [integration]", func() {
}
Expect(c.Create(ctx, unmanaged)).To(Succeed())

result, err := cleanup.CleanupAll(ctx, c, namespace, false, "")
result, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: namespace}, false, "")
Expect(err).NotTo(HaveOccurred())
Expect(result.SecretsDeleted).To(Equal(0))

Expand All @@ -108,13 +109,13 @@ var _ = Describe("CleanupAll [integration]", func() {
})

It("should delete the namespace when flagged", func() {
result, err := cleanup.CleanupAll(ctx, c, namespace, true, "")
result, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: namespace}, true, "")
Expect(err).NotTo(HaveOccurred())
Expect(result.NamespaceDeleted).To(BeTrue())
})

It("should not delete the namespace when not flagged", func() {
result, err := cleanup.CleanupAll(ctx, c, namespace, false, "")
result, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: namespace}, false, "")
Expect(err).NotTo(HaveOccurred())
Expect(result.NamespaceDeleted).To(BeFalse())

Expand Down Expand Up @@ -154,7 +155,7 @@ var _ = Describe("CleanupAll [integration]", func() {
),
).To(Succeed())

result, err := cleanup.CleanupAll(ctx, c, namespace, false, "")
result, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: namespace}, false, "")
Expect(err).NotTo(HaveOccurred())
Expect(result.VMsDeleted).To(Equal(1))
Expect(result.ServicesDeleted).To(Equal(1))
Expand All @@ -163,7 +164,7 @@ var _ = Describe("CleanupAll [integration]", func() {
})

It("should handle empty namespace gracefully", func() {
result, err := cleanup.CleanupAll(ctx, c, namespace, false, "")
result, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: namespace}, false, "")
Expect(err).NotTo(HaveOccurred())
Expect(result.VMsDeleted).To(Equal(0))
Expect(result.ServicesDeleted).To(Equal(0))
Expand All @@ -175,7 +176,7 @@ var _ = Describe("CleanupAll [integration]", func() {
opts := testutil.DefaultVMOpts("cleanup-idem-vm", namespace)
Expect(vm.CreateVM(ctx, c, vm.BuildVMSpec(opts))).To(Succeed())

result1, err := cleanup.CleanupAll(ctx, c, namespace, false, "")
result1, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: namespace}, false, "")
Expect(err).NotTo(HaveOccurred())
Expect(result1.VMsDeleted).To(Equal(1))

Expand All @@ -186,7 +187,7 @@ var _ = Describe("CleanupAll [integration]", func() {
return len(vms)
}, 60*time.Second, 2*time.Second).Should(Equal(0))

result2, err := cleanup.CleanupAll(ctx, c, namespace, false, "")
result2, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: namespace}, false, "")
Expect(err).NotTo(HaveOccurred())
Expect(result2.VMsDeleted).To(Equal(0))
})
Expand Down
122 changes: 122 additions & 0 deletions internal/testutil/binary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2026 Red Hat
// SPDX-License-Identifier: Apache-2.0

package testutil_test

import (
"os"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/opdev/virtwork/internal/testutil"
)

var _ = Describe("BinaryPath", func() {
var originalEnv string

BeforeEach(func() {
originalEnv = os.Getenv("VIRTWORK_BINARY")
})

AfterEach(func() {
if originalEnv != "" {
_ = os.Setenv("VIRTWORK_BINARY", originalEnv)
} else {
_ = os.Unsetenv("VIRTWORK_BINARY")
}
})

Context("when VIRTWORK_BINARY environment variable is set", func() {
It("should return the path from the environment variable", func() {
expectedPath := "/custom/path/to/virtwork"
_ = os.Setenv("VIRTWORK_BINARY", expectedPath)

path, err := testutil.BinaryPath()
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(expectedPath))
})
})

Context("when VIRTWORK_BINARY environment variable is not set", func() {
BeforeEach(func() {
_ = os.Unsetenv("VIRTWORK_BINARY")
})

It("should build and return the binary path", func() {
path, err := testutil.BinaryPath()
Expect(err).NotTo(HaveOccurred())
Expect(path).NotTo(BeEmpty())
Expect(path).To(BeAnExistingFile())
})

It("should return a path in a temp directory", func() {
path, err := testutil.BinaryPath()
Expect(err).NotTo(HaveOccurred())
Expect(filepath.Dir(path)).To(ContainSubstring("virtwork-e2e"))
})

It("should cache the built binary on subsequent calls", func() {
path1, err := testutil.BinaryPath()
Expect(err).NotTo(HaveOccurred())

path2, err := testutil.BinaryPath()
Expect(err).NotTo(HaveOccurred())

Expect(path1).To(Equal(path2))
})
})
})

var _ = Describe("RunVirtwork", func() {
Context("when running the virtwork binary with valid arguments", func() {
It("should execute successfully with --help flag", func() {
stdout, _, exitCode, err := testutil.RunVirtwork("--help")
Expect(err).NotTo(HaveOccurred())
Expect(exitCode).To(Equal(0))
Expect(stdout).To(ContainSubstring("virtwork"))
Expect(stdout).To(ContainSubstring("Usage:"))
})

It("should execute successfully with help command", func() {
stdout, _, exitCode, err := testutil.RunVirtwork("help")
Expect(err).NotTo(HaveOccurred())
Expect(exitCode).To(Equal(0))
Expect(stdout).To(ContainSubstring("virtwork"))
})

It("should show available commands in help output", func() {
stdout, _, exitCode, err := testutil.RunVirtwork("--help")
Expect(err).NotTo(HaveOccurred())
Expect(exitCode).To(Equal(0))
Expect(stdout).To(ContainSubstring("Available Commands"))
Expect(stdout).To(ContainSubstring("run"))
Expect(stdout).To(ContainSubstring("cleanup"))
})
})

Context("when running with invalid arguments", func() {
It("should return non-zero exit code for invalid flags", func() {
_, _, exitCode, err := testutil.RunVirtwork("--invalid-flag-that-does-not-exist")
Expect(err).NotTo(HaveOccurred())
Expect(exitCode).NotTo(Equal(0))
})

It("should return non-zero exit code for invalid commands", func() {
_, _, exitCode, err := testutil.RunVirtwork("invalid-command")
Expect(err).NotTo(HaveOccurred())
Expect(exitCode).NotTo(Equal(0))
})
})

Context("when running without arguments", func() {
It("should show help message", func() {
stdout, _, exitCode, err := testutil.RunVirtwork()
Expect(err).NotTo(HaveOccurred())
Expect(exitCode).To(Equal(0))
Expect(stdout).To(ContainSubstring("virtwork"))
Expect(stdout).To(ContainSubstring("Usage:"))
})
})
})
6 changes: 5 additions & 1 deletion internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,16 @@ func WaitForVMRunning(ctx context.Context, c client.Client, name, namespace stri
if err == nil && phase == "Running" {
return nil
}
if time.Now().After(deadline) {
remaining := time.Until(deadline)
if remaining <= 0 {
if err != nil {
return fmt.Errorf("timeout waiting for VM %s to be running: %w", name, err)
}
return fmt.Errorf("waiting for VM %s to be running (phase: %s); %w", name, phase, wait.ErrVMTimeout)
}
if remaining < interval {
interval = remaining
}
time.Sleep(interval)
}
}
Loading
Loading