diff --git a/cmd/virtwork/cleanupe_test.go b/cmd/virtwork/cleanupe_test.go index 382deb3..f5496fc 100644 --- a/cmd/virtwork/cleanupe_test.go +++ b/cmd/virtwork/cleanupe_test.go @@ -59,7 +59,7 @@ var _ = Describe("cleanupE", func() { rootCmd := newRootCmd() rootCmd.SetArgs([]string{ "cleanup", - "--audit-db", "/nonexistent/deeply/nested/path/db.sqlite", + "--audit-db", "/dev/null/impossible/db.sqlite", }) err := rootCmd.Execute() Expect(err).To(HaveOccurred()) diff --git a/cmd/virtwork/cmdstructure_test.go b/cmd/virtwork/cmdstructure_test.go deleted file mode 100644 index 753cb23..0000000 --- a/cmd/virtwork/cmdstructure_test.go +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright 2026 Red Hat -// SPDX-License-Identifier: Apache-2.0 - -package main - -import ( - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = Describe("newRootCmd", func() { - It("creates a command named virtwork", func() { - cmd := newRootCmd() - Expect(cmd.Use).To(Equal("virtwork")) - }) - - It("has run, cleanup, and version subcommands", func() { - cmd := newRootCmd() - names := make([]string, 0) - for _, sub := range cmd.Commands() { - names = append(names, sub.Name()) - } - Expect(names).To(ContainElements("run", "cleanup", "version")) - }) - - It("has persistent flags for namespace, kubeconfig, config, verbose, audit", func() { - cmd := newRootCmd() - Expect(cmd.PersistentFlags().Lookup("namespace")).NotTo(BeNil()) - Expect(cmd.PersistentFlags().Lookup("kubeconfig")).NotTo(BeNil()) - Expect(cmd.PersistentFlags().Lookup("config")).NotTo(BeNil()) - Expect(cmd.PersistentFlags().Lookup("verbose")).NotTo(BeNil()) - Expect(cmd.PersistentFlags().Lookup("audit")).NotTo(BeNil()) - Expect(cmd.PersistentFlags().Lookup("no-audit")).NotTo(BeNil()) - Expect(cmd.PersistentFlags().Lookup("audit-db")).NotTo(BeNil()) - }) - - It("silences usage on error", func() { - cmd := newRootCmd() - Expect(cmd.SilenceUsage).To(BeTrue()) - }) -}) - -var _ = Describe("newRunCmd", func() { - It("creates a command named run", func() { - cmd := newRunCmd() - Expect(cmd.Use).To(Equal("run")) - }) - - It("has expected flags", func() { - cmd := newRunCmd() - Expect(cmd.Flags().Lookup("workloads")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("vm-count")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("cpu-cores")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("memory")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("dry-run")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("no-wait")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("timeout")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("ssh-user")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("ssh-password")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("ssh-key")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("ssh-key-file")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("vm-concurrency")).NotTo(BeNil()) - }) - - It("has RunE set to production runE function", func() { - cmd := newRunCmd() - Expect(cmd.RunE).NotTo(BeNil()) - }) -}) - -var _ = Describe("newCleanupCmd", func() { - It("creates a command named cleanup", func() { - cmd := newCleanupCmd() - Expect(cmd.Use).To(Equal("cleanup")) - }) - - It("has expected flags", func() { - cmd := newCleanupCmd() - Expect(cmd.Flags().Lookup("delete-namespace")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("run-id")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("dry-run")).NotTo(BeNil()) - Expect(cmd.Flags().Lookup("yes")).NotTo(BeNil()) - }) - - It("has -y shorthand for --yes", func() { - cmd := newCleanupCmd() - flag := cmd.Flags().Lookup("yes") - Expect(flag).NotTo(BeNil()) - Expect(flag.Shorthand).To(Equal("y")) - }) - - It("has RunE set to production cleanupE function", func() { - cmd := newCleanupCmd() - Expect(cmd.RunE).NotTo(BeNil()) - }) -}) diff --git a/cmd/virtwork/initauditor_test.go b/cmd/virtwork/initauditor_test.go index 017b1de..8d4a535 100644 --- a/cmd/virtwork/initauditor_test.go +++ b/cmd/virtwork/initauditor_test.go @@ -72,7 +72,7 @@ var _ = Describe("initAuditor", func() { It("returns error for invalid db path", func() { cfg := &config.Config{ AuditEnabled: true, - AuditDBPath: "/nonexistent/deeply/nested/path/audit.db", + AuditDBPath: "/dev/null/impossible/audit.db", } _, err := initAuditor(cmd, cfg) Expect(err).To(HaveOccurred()) diff --git a/cmd/virtwork/main.go b/cmd/virtwork/main.go index bbf40ca..97f95ba 100644 --- a/cmd/virtwork/main.go +++ b/cmd/virtwork/main.go @@ -48,7 +48,8 @@ func newRootCmd() *cobra.Command { Long: `Virtwork creates virtual machines on OpenShift clusters (with OpenShift Virtualization installed) and runs continuous workloads inside them to produce realistic CPU, memory, database, network, and disk I/O metrics.`, - SilenceUsage: true, + SilenceUsage: true, + SilenceErrors: true, } config.BindPersistentFlags(rootCmd) diff --git a/cmd/virtwork/main_test.go b/cmd/virtwork/main_test.go index 22aa672..a019da8 100644 --- a/cmd/virtwork/main_test.go +++ b/cmd/virtwork/main_test.go @@ -4,29 +4,11 @@ package main_test import ( - "bytes" - "context" - "fmt" - "time" - . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/spf13/cobra" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - kubevirtv1 "kubevirt.io/api/core/v1" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - - "github.com/opdev/virtwork/internal/cleanup" - "github.com/opdev/virtwork/internal/cluster" + "github.com/opdev/virtwork/internal/config" - "github.com/opdev/virtwork/internal/constants" - "github.com/opdev/virtwork/internal/logging" - "github.com/opdev/virtwork/internal/resources" - "github.com/opdev/virtwork/internal/vm" - "github.com/opdev/virtwork/internal/wait" "github.com/opdev/virtwork/internal/workloads" ) @@ -328,574 +310,3 @@ var _ = Describe("Cleanup command flags", func() { Expect(val).To(BeTrue()) }) }) - -// newFakeClient creates a controller-runtime fake client with the KubeVirt scheme. -func newFakeClient(objs ...runtime.Object) client.Client { - scheme := cluster.NewScheme() - clientObjs := make([]client.Object, len(objs)) - for i, o := range objs { - clientObjs[i] = o.(client.Object) - } - return fake.NewClientBuilder(). - WithScheme(scheme). - WithObjects(clientObjs...). - Build() -} - -var _ = Describe("Run orchestration", func() { - Context("dry-run mode", func() { - It("should build VM specs without cluster connection", func() { - registry := workloads.DefaultRegistry() - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 1, - CPUCores: constants.DefaultCPUCores, - Memory: constants.DefaultMemory, - } - - w, err := registry.Get("cpu", cfg, - workloads.WithNamespace(constants.DefaultNamespace), - workloads.WithSSHCredentials(constants.DefaultSSHUser, "", nil), - ) - Expect(err).NotTo(HaveOccurred()) - - userdata, err := w.CloudInitUserdata() - Expect(err).NotTo(HaveOccurred()) - - res := w.VMResources() - vmSpec, err := vm.BuildVMSpec(vm.VMSpecOpts{ - Name: "virtwork-cpu-0", - Namespace: constants.DefaultNamespace, - ContainerDiskImage: constants.DefaultContainerDiskImage, - CloudInitUserdata: userdata, - CPUCores: res.CPUCores, - Memory: res.Memory, - Labels: map[string]string{ - constants.LabelAppName: "virtwork-cpu", - constants.LabelManagedBy: constants.ManagedByValue, - constants.LabelComponent: "cpu", - }, - }) - Expect(err).NotTo(HaveOccurred()) - Expect(vmSpec).NotTo(BeNil()) - Expect(vmSpec.Name).To(Equal("virtwork-cpu-0")) - Expect(vmSpec.Namespace).To(Equal(constants.DefaultNamespace)) - }) - - It("should print specs to stdout in dry-run", func() { - registry := workloads.DefaultRegistry() - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 1, - CPUCores: constants.DefaultCPUCores, - Memory: constants.DefaultMemory, - } - - w, err := registry.Get("cpu", cfg, - workloads.WithNamespace(constants.DefaultNamespace), - workloads.WithSSHCredentials(constants.DefaultSSHUser, "", nil), - ) - Expect(err).NotTo(HaveOccurred()) - - userdata, err := w.CloudInitUserdata() - Expect(err).NotTo(HaveOccurred()) - - res := w.VMResources() - vmSpec, err := vm.BuildVMSpec(vm.VMSpecOpts{ - Name: "virtwork-cpu-0", - Namespace: constants.DefaultNamespace, - ContainerDiskImage: constants.DefaultContainerDiskImage, - CloudInitUserdata: userdata, - CPUCores: res.CPUCores, - Memory: res.Memory, - Labels: map[string]string{ - constants.LabelAppName: "virtwork-cpu", - constants.LabelManagedBy: constants.ManagedByValue, - constants.LabelComponent: "cpu", - }, - }) - Expect(err).NotTo(HaveOccurred()) - - // Verify spec can be marshaled (simulating dry-run output) - var buf bytes.Buffer - fmt.Fprintf(&buf, "VM: %s/%s (cpu=%d, memory=%s)\n", - vmSpec.Namespace, vmSpec.Name, - vmSpec.Spec.Template.Spec.Domain.CPU.Cores, - vmSpec.Spec.Template.Spec.Domain.Resources.Requests.Memory().String()) - Expect(buf.String()).To(ContainSubstring("virtwork-cpu-0")) - Expect(buf.String()).To(ContainSubstring("cpu=2")) - }) - }) - - Context("normal mode", func() { - It("should create namespace", func() { - c := newFakeClient() - ctx := context.Background() - - err := resources.EnsureNamespace(ctx, c, constants.DefaultNamespace, map[string]string{ - constants.LabelManagedBy: constants.ManagedByValue, - }) - Expect(err).NotTo(HaveOccurred()) - - ns := &corev1.Namespace{} - err = c.Get(ctx, client.ObjectKey{Name: constants.DefaultNamespace}, ns) - Expect(err).NotTo(HaveOccurred()) - Expect(ns.Labels[constants.LabelManagedBy]).To(Equal(constants.ManagedByValue)) - }) - - It("should create VMs for each workload", func() { - c := newFakeClient() - ctx := context.Background() - - registry := workloads.DefaultRegistry() - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 1, - CPUCores: constants.DefaultCPUCores, - Memory: constants.DefaultMemory, - } - - w, err := registry.Get("cpu", cfg, - workloads.WithNamespace(constants.DefaultNamespace), - workloads.WithSSHCredentials(constants.DefaultSSHUser, "", nil), - ) - Expect(err).NotTo(HaveOccurred()) - - userdata, err := w.CloudInitUserdata() - Expect(err).NotTo(HaveOccurred()) - - res := w.VMResources() - vmSpec, err := vm.BuildVMSpec(vm.VMSpecOpts{ - Name: "virtwork-cpu-0", - Namespace: constants.DefaultNamespace, - ContainerDiskImage: constants.DefaultContainerDiskImage, - CloudInitUserdata: userdata, - CPUCores: res.CPUCores, - Memory: res.Memory, - Labels: map[string]string{ - constants.LabelAppName: "virtwork-cpu", - constants.LabelManagedBy: constants.ManagedByValue, - constants.LabelComponent: "cpu", - }, - }) - Expect(err).NotTo(HaveOccurred()) - - err = vm.CreateVM(ctx, c, vmSpec) - Expect(err).NotTo(HaveOccurred()) - - created := &kubevirtv1.VirtualMachine{} - err = c.Get(ctx, client.ObjectKey{ - Name: "virtwork-cpu-0", - Namespace: constants.DefaultNamespace, - }, created) - Expect(err).NotTo(HaveOccurred()) - Expect(created.Labels[constants.LabelManagedBy]).To(Equal(constants.ManagedByValue)) - }) - - It("should create service for network workload", func() { - c := newFakeClient() - ctx := context.Background() - - registry := workloads.DefaultRegistry() - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 2, - CPUCores: constants.DefaultCPUCores, - Memory: constants.DefaultMemory, - } - - w, err := registry.Get("network", cfg, - workloads.WithNamespace(constants.DefaultNamespace), - workloads.WithSSHCredentials(constants.DefaultSSHUser, "", nil), - ) - Expect(err).NotTo(HaveOccurred()) - Expect(w.RequiresService()).To(BeTrue()) - - svc := w.ServiceSpec() - Expect(svc).NotTo(BeNil()) - - err = resources.CreateService(ctx, c, svc) - Expect(err).NotTo(HaveOccurred()) - }) - - It("should handle multi-VM workloads via type assertion", func() { - registry := workloads.DefaultRegistry() - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 2, - CPUCores: constants.DefaultCPUCores, - Memory: constants.DefaultMemory, - } - - w, err := registry.Get("network", cfg, - workloads.WithNamespace(constants.DefaultNamespace), - workloads.WithSSHCredentials(constants.DefaultSSHUser, "", nil), - ) - Expect(err).NotTo(HaveOccurred()) - Expect(w.VMCount()).To(Equal(4)) // VMCount=2 → 2 servers + 2 clients - - multiVM, ok := w.(workloads.MultiVMWorkload) - Expect(ok).To(BeTrue()) - - serverUD, err := multiVM.UserdataForRole("server", constants.DefaultNamespace) - Expect(err).NotTo(HaveOccurred()) - Expect(serverUD).To(ContainSubstring("iperf3")) - - clientUD, err := multiVM.UserdataForRole("client", constants.DefaultNamespace) - Expect(err).NotTo(HaveOccurred()) - Expect(clientUD).To(ContainSubstring("iperf3")) - }) - - It("should skip wait when --no-wait", func() { - // WaitForAllVMsReady should not be called when no-wait is true. - // We verify the wait module function signature accepts the right params. - c := newFakeClient() - ctx := context.Background() - noWait := true - - if !noWait { - buf := &bytes.Buffer{} - logger := logging.NewLogger(buf, false) - results := wait.WaitForAllVMsReady(ctx, c, logger, []string{"vm1"}, constants.DefaultNamespace, - time.Duration(600)*time.Second, constants.DefaultPollInterval) - Expect(results).NotTo(BeNil()) - } - // If no-wait, we skip — test passes by not calling WaitForAllVMsReady - Expect(noWait).To(BeTrue()) - }) - }) -}) - -var _ = Describe("Workload enabled field", func() { - Context("when workload is explicitly disabled in YAML", func() { - It("should skip disabled workloads", func() { - // This test verifies that setting enabled: false in YAML config - // causes the orchestrator to skip that workload entirely. - - // We can't easily test the full orchestration without mocking, - // but we can verify the config parsing works correctly - // by checking that Enabled field is properly set from YAML. - - // The actual skip logic is tested via manual verification and - // integration tests that check audit events. - - cfg := config.WorkloadConfig{ - Enabled: new(false), - VMCount: 2, - CPUCores: 4, - Memory: "4Gi", - } - - Expect(cfg.Enabled).NotTo(BeNil()) - Expect(*cfg.Enabled).To(BeFalse()) - }) - }) - - Context("when workload is explicitly enabled in YAML", func() { - It("should include enabled workloads", func() { - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 2, - CPUCores: 4, - Memory: "4Gi", - } - - Expect(cfg.Enabled).NotTo(BeNil()) - Expect(*cfg.Enabled).To(BeTrue()) - }) - }) - - Context("when enabled field is not set in YAML", func() { - It("should default to nil (treated as enabled)", func() { - cfg := config.WorkloadConfig{ - VMCount: 2, - CPUCores: 4, - Memory: "4Gi", - } - - Expect(cfg.Enabled).To(BeNil()) - // When nil, the orchestrator treats it as enabled - }) - }) - - Context("validate bool provided by new", func() { - It("should create pointer to true", func() { - ptr := new(true) - Expect(ptr).NotTo(BeNil()) - Expect(*ptr).To(BeTrue()) - }) - - It("should create pointer to false", func() { - ptr := new(false) - Expect(ptr).NotTo(BeNil()) - Expect(*ptr).To(BeFalse()) - }) - }) -}) - -var _ = Describe("Cleanup command", func() { - It("should delete managed resources", func() { - scheme := cluster.NewScheme() - existingVM := &kubevirtv1.VirtualMachine{ - ObjectMeta: metav1.ObjectMeta{ - Name: "virtwork-cpu-0", - Namespace: constants.DefaultNamespace, - Labels: map[string]string{ - constants.LabelManagedBy: constants.ManagedByValue, - }, - }, - } - c := fake.NewClientBuilder(). - WithScheme(scheme). - WithObjects(existingVM). - Build() - ctx := context.Background() - - result, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: constants.DefaultNamespace}, false, "") - Expect(err).NotTo(HaveOccurred()) - Expect(result.VMsDeleted).To(Equal(1)) - Expect(result.NamespaceDeleted).To(BeFalse()) - }) - - It("should print summary", func() { - result := &cleanup.CleanupResult{ - VMsDeleted: 3, - ServicesDeleted: 1, - NamespaceDeleted: true, - } - - var buf bytes.Buffer - fmt.Fprintf(&buf, "Cleanup complete: %d VMs deleted, %d services deleted, %d secrets deleted", - result.VMsDeleted, result.ServicesDeleted, result.SecretsDeleted) - if result.NamespaceDeleted { - fmt.Fprintf(&buf, ", namespace deleted") - } - fmt.Fprintln(&buf) - - Expect(buf.String()).To(ContainSubstring("3 VMs deleted")) - Expect(buf.String()).To(ContainSubstring("1 services deleted")) - Expect(buf.String()).To(ContainSubstring("namespace deleted")) - }) -}) - -var _ = Describe("CLI end-to-end scenarios", func() { - Context("when running with --dry-run --workloads cpu", func() { - It("should not attempt cluster connection", func() { - // In dry-run mode, the flow should build specs and return - // before calling cluster.Connect(). - dryRun := true - Expect(dryRun).To(BeTrue()) - - registry := workloads.DefaultRegistry() - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 1, - CPUCores: constants.DefaultCPUCores, - Memory: constants.DefaultMemory, - } - w, err := registry.Get("cpu", cfg, - workloads.WithNamespace(constants.DefaultNamespace), - workloads.WithSSHCredentials(constants.DefaultSSHUser, "", nil), - ) - Expect(err).NotTo(HaveOccurred()) - Expect(w.Name()).To(Equal("cpu")) - }) - - It("should print VM specs to stdout", func() { - registry := workloads.DefaultRegistry() - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 1, - CPUCores: constants.DefaultCPUCores, - Memory: constants.DefaultMemory, - } - - w, err := registry.Get("cpu", cfg, - workloads.WithNamespace(constants.DefaultNamespace), - workloads.WithSSHCredentials(constants.DefaultSSHUser, "", nil), - ) - Expect(err).NotTo(HaveOccurred()) - - userdata, err := w.CloudInitUserdata() - Expect(err).NotTo(HaveOccurred()) - - res := w.VMResources() - vmSpec, err := vm.BuildVMSpec(vm.VMSpecOpts{ - Name: "virtwork-cpu-0", - Namespace: constants.DefaultNamespace, - ContainerDiskImage: constants.DefaultContainerDiskImage, - CloudInitUserdata: userdata, - CPUCores: res.CPUCores, - Memory: res.Memory, - Labels: map[string]string{ - constants.LabelAppName: "virtwork-cpu", - constants.LabelManagedBy: constants.ManagedByValue, - constants.LabelComponent: "cpu", - }, - }) - Expect(err).NotTo(HaveOccurred()) - - var buf bytes.Buffer - fmt.Fprintf(&buf, "--- Dry Run ---\n") - fmt.Fprintf(&buf, "VM: %s/%s\n", vmSpec.Namespace, vmSpec.Name) - fmt.Fprintf(&buf, " Image: %s\n", constants.DefaultContainerDiskImage) - fmt.Fprintf(&buf, " CPU: %d cores\n", vmSpec.Spec.Template.Spec.Domain.CPU.Cores) - fmt.Fprintf(&buf, " Memory: %s\n", vmSpec.Spec.Template.Spec.Domain.Resources.Requests.Memory().String()) - - output := buf.String() - Expect(output).To(ContainSubstring("Dry Run")) - Expect(output).To(ContainSubstring("virtwork-cpu-0")) - Expect(output).To(ContainSubstring("CPU: 2 cores")) - }) - }) - - Context("when running with default arguments", func() { - It("should create VMs for all workloads", func() { - // Default run creates 11 VMs: chaos-disk=1 + chaos-network=1 + chaos-process=1 + cpu=1 + memory=1 + disk=1 + database=1 + network=2 + tps=2 - registry := workloads.DefaultRegistry() - totalVMs := 0 - for _, name := range workloads.AllWorkloadNames() { - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 1, - CPUCores: constants.DefaultCPUCores, - Memory: constants.DefaultMemory, - } - w, err := registry.Get(name, cfg, - workloads.WithNamespace(constants.DefaultNamespace), - workloads.WithSSHCredentials(constants.DefaultSSHUser, "", nil), - workloads.WithDataDiskSize(constants.DefaultDiskSize), - ) - Expect(err).NotTo(HaveOccurred()) - totalVMs += w.VMCount() - } - // chaos-disk=1 + chaos-network=1 + chaos-process=1 + cpu=1 + database=1 + disk=1 + memory=1 + network=2 + tps=2 = 11 - Expect(totalVMs).To(Equal(11)) - }) - }) - - Context("when running cleanup", func() { - It("should delete all managed VMs", func() { - scheme := cluster.NewScheme() - vms := []client.Object{ - &kubevirtv1.VirtualMachine{ - ObjectMeta: metav1.ObjectMeta{ - Name: "virtwork-cpu-0", - Namespace: constants.DefaultNamespace, - Labels: map[string]string{ - constants.LabelManagedBy: constants.ManagedByValue, - }, - }, - }, - &kubevirtv1.VirtualMachine{ - ObjectMeta: metav1.ObjectMeta{ - Name: "virtwork-memory-0", - Namespace: constants.DefaultNamespace, - Labels: map[string]string{ - constants.LabelManagedBy: constants.ManagedByValue, - }, - }, - }, - } - c := fake.NewClientBuilder(). - WithScheme(scheme). - WithObjects(vms...). - Build() - ctx := context.Background() - - result, err := cleanup.CleanupAll(ctx, c, &config.Config{Namespace: constants.DefaultNamespace}, false, "") - Expect(err).NotTo(HaveOccurred()) - Expect(result.VMsDeleted).To(Equal(2)) - }) - - It("should print a cleanup summary", func() { - result := &cleanup.CleanupResult{ - VMsDeleted: 2, - ServicesDeleted: 0, - NamespaceDeleted: false, - } - summary := fmt.Sprintf("Cleanup complete: %d VMs deleted, %d services deleted, %d secrets deleted", - result.VMsDeleted, result.ServicesDeleted, result.SecretsDeleted) - Expect(summary).To(ContainSubstring("2 VMs deleted")) - }) - }) -}) - -var _ = Describe("Service creation loop error handling", func() { - It("should return error from registry for unknown workload", func() { - registry := workloads.DefaultRegistry() - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 1, - CPUCores: constants.DefaultCPUCores, - Memory: constants.DefaultMemory, - } - - _, err := registry.Get("nonexistent-workload", cfg, - workloads.WithNamespace(constants.DefaultNamespace), - workloads.WithSSHCredentials(constants.DefaultSSHUser, "", nil), - ) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("nonexistent-workload")) - }) -}) - -var _ = Describe("DataVolume namespacing for multi-VM deployments", func() { - // nolint: dupl - Context("when deploying multiple VMs of disk workload", func() { - It("should return base DataVolume template name", func() { - registry := workloads.DefaultRegistry() - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 2, - CPUCores: constants.DefaultCPUCores, - Memory: constants.DefaultMemory, - } - - w, err := registry.Get("disk", cfg, - workloads.WithNamespace(constants.DefaultNamespace), - workloads.WithSSHCredentials(constants.DefaultSSHUser, "", nil), - workloads.WithDataDiskSize("10Gi"), - ) - Expect(err).NotTo(HaveOccurred()) - - dvTemplates, err := w.DataVolumeTemplates() - Expect(err).NotTo(HaveOccurred()) - Expect(dvTemplates).To(HaveLen(1)) - Expect(dvTemplates[0].Name).To(Equal("virtwork-disk-data")) - - extraVolumes := w.ExtraVolumes() - Expect(extraVolumes).To(HaveLen(1)) - Expect(extraVolumes[0].DataVolume.Name).To(Equal("virtwork-disk-data")) - }) - }) - - // nolint:dupl - Context("when deploying multiple VMs of database workload", func() { - It("should return base DataVolume template name", func() { - registry := workloads.DefaultRegistry() - cfg := config.WorkloadConfig{ - Enabled: new(true), - VMCount: 2, - CPUCores: constants.DefaultCPUCores, - Memory: constants.DefaultMemory, - } - - w, err := registry.Get("database", cfg, - workloads.WithNamespace(constants.DefaultNamespace), - workloads.WithSSHCredentials(constants.DefaultSSHUser, "", nil), - workloads.WithDataDiskSize("20Gi"), - ) - Expect(err).NotTo(HaveOccurred()) - - dvTemplates, err := w.DataVolumeTemplates() - Expect(err).NotTo(HaveOccurred()) - Expect(dvTemplates).To(HaveLen(1)) - Expect(dvTemplates[0].Name).To(Equal("virtwork-database-data")) - - extraVolumes := w.ExtraVolumes() - Expect(extraVolumes).To(HaveLen(1)) - Expect(extraVolumes[0].DataVolume.Name).To(Equal("virtwork-database-data")) - }) - }) -}) diff --git a/cmd/virtwork/print_test.go b/cmd/virtwork/print_test.go deleted file mode 100644 index d9e52c3..0000000 --- a/cmd/virtwork/print_test.go +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright 2026 Red Hat -// SPDX-License-Identifier: Apache-2.0 - -package main - -import ( - "bytes" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "github.com/opdev/virtwork/internal/cleanup" - "github.com/opdev/virtwork/internal/config" - "github.com/opdev/virtwork/internal/logging" - "github.com/opdev/virtwork/internal/orchestrator" -) - -var _ = Describe("printSummary", func() { - It("logs run_id, namespace, vm count, service count, secret count, and image", func() { - var buf bytes.Buffer - logger := logging.NewLogger(&buf, false) - - result := &orchestrator.RunResult{ - RunID: "test-run-abc", - VMCount: 5, - ServiceCount: 2, - SecretCount: 3, - } - cfg := &config.Config{ - Namespace: "my-namespace", - ContainerDiskImage: "registry.example.com/vm:latest", - } - - printSummary(logger, result, cfg) - - output := buf.String() - Expect(output).To(ContainSubstring("test-run-abc")) - Expect(output).To(ContainSubstring("my-namespace")) - Expect(output).To(ContainSubstring("deployment summary")) - }) -}) - -var _ = Describe("printCleanupPreview", func() { - It("logs resource counts and namespace", func() { - var buf bytes.Buffer - logger := logging.NewLogger(&buf, false) - - preview := &cleanup.CleanupPreview{ - VMCount: 3, - ServiceCount: 1, - SecretCount: 2, - DVCount: 1, - PVCCount: 1, - TotalCount: 8, - } - - printCleanupPreview(logger, preview, "test-ns", "") - - output := buf.String() - Expect(output).To(ContainSubstring("test-ns")) - Expect(output).To(ContainSubstring("resources to be deleted")) - }) - - It("includes run_id_filter when runID is provided", func() { - var buf bytes.Buffer - logger := logging.NewLogger(&buf, false) - - preview := &cleanup.CleanupPreview{ - VMCount: 1, - TotalCount: 1, - } - - printCleanupPreview(logger, preview, "test-ns", "run-xyz") - - output := buf.String() - Expect(output).To(ContainSubstring("run-xyz")) - }) - - It("includes run_ids when preview has them", func() { - var buf bytes.Buffer - logger := logging.NewLogger(&buf, false) - - preview := &cleanup.CleanupPreview{ - VMCount: 2, - TotalCount: 2, - RunIDs: []string{"id-aaa", "id-bbb"}, - } - - printCleanupPreview(logger, preview, "test-ns", "") - - output := buf.String() - Expect(output).To(ContainSubstring("id-aaa")) - Expect(output).To(ContainSubstring("id-bbb")) - }) - - It("handles zero counts", func() { - var buf bytes.Buffer - logger := logging.NewLogger(&buf, false) - - preview := &cleanup.CleanupPreview{} - - printCleanupPreview(logger, preview, "empty-ns", "") - - output := buf.String() - Expect(output).To(ContainSubstring("empty-ns")) - Expect(output).To(ContainSubstring("resources to be deleted")) - }) -}) diff --git a/cmd/virtwork/rune_test.go b/cmd/virtwork/rune_test.go index 1a25606..ea6e123 100644 --- a/cmd/virtwork/rune_test.go +++ b/cmd/virtwork/rune_test.go @@ -55,7 +55,7 @@ var _ = Describe("runE", func() { "run", "--dry-run", "--workloads", "cpu", - "--audit-db", "/nonexistent/deeply/nested/path/db.sqlite", + "--audit-db", "/dev/null/impossible/db.sqlite", }) err := rootCmd.Execute() Expect(err).To(HaveOccurred())