Skip to content

Commit

Permalink
Update functional tests to use port-forwarding in vmexport and memory…
Browse files Browse the repository at this point in the history
…-dump

Signed-off-by: Alvaro Romero <alromero@redhat.com>
  • Loading branch information
alromeros committed Jul 25, 2023
1 parent 9f86d0a commit 566a1a8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 84 deletions.
1 change: 0 additions & 1 deletion tests/storage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ go_library(
"//tests/libstorage:go_default_library",
"//tests/libvmi:go_default_library",
"//tests/libwait:go_default_library",
"//tests/monitoring:go_default_library",
"//tests/testsuite:go_default_library",
"//tests/util:go_default_library",
"//tests/watcher:go_default_library",
Expand Down
121 changes: 41 additions & 80 deletions tests/storage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@ import (
"encoding/pem"
goerrors "errors"
"fmt"
"io"
"os"
osexec "os/exec"
"path/filepath"
"strings"
"time"

"sigs.k8s.io/yaml"

"kubevirt.io/kubevirt/tests/framework/kubevirt"
"kubevirt.io/kubevirt/tests/monitoring"

"kubevirt.io/kubevirt/tests/exec"
"kubevirt.io/kubevirt/tests/testsuite"
Expand Down Expand Up @@ -2191,51 +2188,45 @@ var _ = SIGDescribe("Export", func() {
})

Context("Download a volume with vmexport", func() {
var (
forwardCmd *osexec.Cmd
)

BeforeEach(func() {
outputFile = fmt.Sprintf(defaultOutput, rand.String(12))
})

AfterEach(func() {
err = monitoring.KillPortForwardCommand(forwardCmd)
Expect(err).ToNot(HaveOccurred())
if err := os.Remove(outputFile); err != nil && !goerrors.Is(err, os.ErrNotExist) {
Fail(err.Error())
}
})

It("Download succeeds creating and downloading a vmexport using PVC source", func() {
// Remove this once we can pass an argument to have virtctl start a port-forward
if !checks.IsOpenShift() {
Skip("Not on openshift")
}
// Create populated PVC
pvc, _ := populateKubeVirtContent(sc, k8sv1.PersistentVolumeFilesystem)
// Run vmexport
By("Running vmexport command")
virtctlCmd := clientcmd.NewRepeatableVirtctlCommand(commandName,
cmdArgs := []string{
commandName,
"download",
vmeName,
"--pvc", pvc.Name,
"--output", outputFile,
"--volume", pvc.Name,
"--insecure",
"--namespace", testsuite.GetTestNamespace(pvc))
"--namespace", testsuite.GetTestNamespace(pvc),
}

if !checks.IsOpenShift() {
targetPort := fmt.Sprintf("%d", 37548+rand.Intn(6000))
cmdArgs = append(cmdArgs, "--port-forward", targetPort)
}

virtctlCmd := clientcmd.NewRepeatableVirtctlCommand(cmdArgs...)
err = virtctlCmd()
Expect(err).ToNot(HaveOccurred())
_, err := os.Stat(outputFile)
Expect(err).ToNot(HaveOccurred())
})

It("Download succeeds creating and downloading a vmexport using Snapshot source", func() {
// Remove this once we can pass an argument to have virtctl start a port-forward
if !checks.IsOpenShift() {
Skip("Not on openshift")
}
FIt("Download succeeds creating and downloading a vmexport using Snapshot source", func() {
sc, err := libstorage.GetSnapshotStorageClass(virtClient)
if err != nil {
Skip("Skip test when storage with snapshot is not present")
Expand Down Expand Up @@ -2266,26 +2257,30 @@ var _ = SIGDescribe("Export", func() {

// Run vmexport
By("Running vmexport command")
virtctlCmd := clientcmd.NewRepeatableVirtctlCommand(commandName,
cmdArgs := []string{
commandName,
"download",
vmeName,
"--snapshot", snapshot.Name,
"--output", outputFile,
"--volume", export.Status.Links.External.Volumes[0].Name,
"--volume", vm.Spec.Template.Spec.Volumes[0].DataVolume.Name,
"--insecure",
"--namespace", testsuite.GetTestNamespace(export))
"--namespace", testsuite.GetTestNamespace(export),
}

if !checks.IsOpenShift() {
targetPort := fmt.Sprintf("%d", 37548+rand.Intn(6000))
cmdArgs = append(cmdArgs, "--port-forward", targetPort)
}

virtctlCmd := clientcmd.NewRepeatableVirtctlCommand(cmdArgs...)
err = virtctlCmd()
Expect(err).ToNot(HaveOccurred())
_, err = os.Stat(outputFile)
Expect(err).ToNot(HaveOccurred())
})

It("Download succeeds creating and downloading a vmexport using VM source", func() {
// Remove this once we can pass an argument to have virtctl start a port-forward
if !checks.IsOpenShift() {
Skip("Not on openshift")
}
// Create a populated VM
vm := tests.NewRandomVMWithDataVolumeWithRegistryImport(
cd.DataVolumeImportUrlForContainerDisk(cd.ContainerDiskAlpine),
Expand All @@ -2308,15 +2303,23 @@ var _ = SIGDescribe("Export", func() {

// Run vmexport
By("Running vmexport command")
virtctlCmd := clientcmd.NewRepeatableVirtctlCommand(commandName,
cmdArgs := []string{
commandName,
"download",
vmeName,
"--vm", vm.Name,
"--output", outputFile,
"--volume", vm.Spec.Template.Spec.Volumes[0].DataVolume.Name,
"--insecure",
"--namespace", testsuite.GetTestNamespace(vm))
"--namespace", testsuite.GetTestNamespace(vm),
}

if !checks.IsOpenShift() {
targetPort := fmt.Sprintf("%d", 37548+rand.Intn(6000))
cmdArgs = append(cmdArgs, "--port-forward", targetPort)
}

virtctlCmd := clientcmd.NewRepeatableVirtctlCommand(cmdArgs...)
err = virtctlCmd()
Expect(err).ToNot(HaveOccurred())
_, err := os.Stat(outputFile)
Expand All @@ -2338,10 +2341,8 @@ var _ = SIGDescribe("Export", func() {
export, err := virtClient.VirtualMachineExport(vmExport.Namespace).Get(context.Background(), vmExport.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
if export.Status.Links.External == nil {
targetPort := 37548 + rand.Intn(6000)
forwardCmd = startPortForward(vmeName, vmExport.Namespace, targetPort)
cmdArgs = append(cmdArgs, "--volume", vmExport.Status.Links.Internal.Volumes[0].Name, "--service-url", fmt.Sprintf("127.0.0.1:%d", targetPort),
"--insecure", "--keep-vme")
targetPort := fmt.Sprintf("%d", 37548+rand.Intn(6000))
cmdArgs = append(cmdArgs, "--volume", vmExport.Status.Links.Internal.Volumes[0].Name, "--port-forward", targetPort, "--keep-vme")
} else {
cmdArgs = append(cmdArgs, "--volume", vmExport.Status.Links.External.Volumes[0].Name)
}
Expand Down Expand Up @@ -2377,9 +2378,8 @@ var _ = SIGDescribe("Export", func() {
"--namespace", testsuite.GetTestNamespace(export),
}
if export.Status.Links.External == nil {
targetPort := 37548 + rand.Intn(6000)
forwardCmd = startPortForward(vmeName, pvc.Namespace, targetPort)
cmdArgs = append(cmdArgs, "--service-url", fmt.Sprintf("127.0.0.1:%d", targetPort),
targetPort := fmt.Sprintf("%d", 37548+rand.Intn(6000))
cmdArgs = append(cmdArgs, "--port-forward", targetPort,
"--insecure", "--keep-vme")
}
By("Running vmexport command")
Expand All @@ -2391,7 +2391,7 @@ var _ = SIGDescribe("Export", func() {
Expect(err).ToNot(HaveOccurred())
})

It("Download succeeds and keeps the vmexport after finishing the download", func() {
FIt("Download succeeds and keeps the vmexport after finishing the download", func() {
vmExport := createRunningPVCExport(sc, k8sv1.PersistentVolumeFilesystem)
vmeName = vmExport.Name
vme, err := virtClient.VirtualMachineExport(vmExport.Namespace).Get(context.Background(), vmeName, metav1.GetOptions{})
Expand All @@ -2407,10 +2407,8 @@ var _ = SIGDescribe("Export", func() {
}
Expect(err).ToNot(HaveOccurred())
if vme.Status.Links.External == nil {
targetPort := 37548 + rand.Intn(6000)
forwardCmd = startPortForward(vmeName, vmExport.Namespace, targetPort)
cmdArgs = append(cmdArgs, "--volume", vmExport.Status.Links.Internal.Volumes[0].Name, "--service-url", fmt.Sprintf("127.0.0.1:%d", targetPort),
"--insecure")
targetPort := fmt.Sprintf("%d", 37548+rand.Intn(6000))
cmdArgs = append(cmdArgs, "--volume", vmExport.Status.Links.Internal.Volumes[0].Name, "--port-forward", targetPort)
} else {
cmdArgs = append(cmdArgs, "--volume", vmExport.Status.Links.External.Volumes[0].Name)
}
Expand All @@ -2428,15 +2426,6 @@ var _ = SIGDescribe("Export", func() {
})

Context("Get export manifest from vmexport", func() {
var (
forwardCmd *osexec.Cmd
)

AfterEach(func() {
err = monitoring.KillPortForwardCommand(forwardCmd)
Expect(err).ToNot(HaveOccurred())
})

DescribeTable("manifest should be successfully retrieved on running VM export", func(expectedObjects int, extraArgs ...string) {
// Create a populated VM
vm := tests.NewRandomVMWithDataVolumeWithRegistryImport(
Expand Down Expand Up @@ -2491,10 +2480,8 @@ var _ = SIGDescribe("Export", func() {
testsuite.GetTestNamespace(vm),
}
if vme.Status.Links.External == nil {
targetPort := 37548 + rand.Intn(6000)
forwardCmd = startPortForward(vmeName, vm.Namespace, targetPort)
cmdArgs = append(cmdArgs, "--service-url", fmt.Sprintf("127.0.0.1:%d", targetPort),
"--insecure")
targetPort := fmt.Sprintf("%d", 37548+rand.Intn(6000))
cmdArgs = append(cmdArgs, "--service-url", fmt.Sprintf("127.0.0.1:%s", targetPort), "--port-forward", targetPort)
}
cmdArgs = append(cmdArgs, extraArgs...)
virtctlCmdOut := clientcmd.NewRepeatableVirtctlCommandWithOut(cmdArgs...)
Expand All @@ -2520,22 +2507,6 @@ var _ = SIGDescribe("Export", func() {
})
})

func startPortForward(vmeName, namespace string, targetPort int) *osexec.Cmd {
_, forwardCmd, err := clientcmd.CreateCommandWithNS(namespace, clientcmd.GetK8sCmdClient(),
"port-forward", fmt.Sprintf("service/virt-export-%s", vmeName), fmt.Sprintf("%d:443", targetPort))
Expect(err).ToNot(HaveOccurred())
go func() {
defer GinkgoRecover()
Expect(err).ToNot(HaveOccurred())
stdout, err := forwardCmd.StdoutPipe()
Expect(err).ToNot(HaveOccurred())
err = forwardCmd.Start()
Expect(err).ToNot(HaveOccurred())
waitForPortForwardCmd(stdout, 443, targetPort)
}()
return forwardCmd
}

func logToGinkgoWritter(format string, parameters ...interface{}) {
_, _ = fmt.Fprintf(GinkgoWriter, format, parameters...)
}
Expand Down Expand Up @@ -2569,13 +2540,3 @@ func (matcher *ConditionNoTimeMatcher) FailureMessage(actual interface{}) (messa
func (matcher *ConditionNoTimeMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to match without time", matcher.Cond)
}

func waitForPortForwardCmd(stdout io.ReadCloser, src, dst int) {
Eventually(func() string {
tmp := make([]byte, 1024)
_, err := stdout.Read(tmp)
Expect(err).NotTo(HaveOccurred())

return string(tmp)
}, 30*time.Second, 1*time.Second).Should(ContainSubstring(fmt.Sprintf("Handling connection for %d", dst)))
}
12 changes: 9 additions & 3 deletions tests/storage/memorydump.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const (
virtCtlCreate = "--create-claim"
virtCtlOutputFile = "--output=%s"
virtCtlStorageClass = "--storage-class=%s"
virtCtlPortForward = "--port-forward=%s"
waitMemoryDumpRequest = "waiting on memory dump request in vm status"
waitMemoryDumpPvcVolume = "waiting on memory dump pvc in vm"
waitMemoryDumpRequestRemove = "waiting on memory dump request to be remove from vm status"
Expand Down Expand Up @@ -633,6 +634,10 @@ var _ = SIGDescribe("Memory dump", func() {
By("Invoking virtctl memory dump download")
commandAndArgs := []string{commandMemoryDump, "download", name, virtCtlNamespace, namespace}
commandAndArgs = append(commandAndArgs, fmt.Sprintf(virtCtlOutputFile, outputFile))
if !checks.IsOpenShift() {
targetPort := fmt.Sprintf("%d", 37548+rand.Intn(6000))
commandAndArgs = append(commandAndArgs, fmt.Sprintf(virtCtlPortForward, targetPort))
}
memorydumpCommand := clientcmd.NewRepeatableVirtctlCommand(commandAndArgs...)
Eventually(func() error {
return memorydumpCommand()
Expand All @@ -656,6 +661,10 @@ var _ = SIGDescribe("Memory dump", func() {
commandAndArgs = append(commandAndArgs, fmt.Sprintf(virtCtlClaimName, claimName))
commandAndArgs = append(commandAndArgs, virtCtlCreate)
commandAndArgs = append(commandAndArgs, fmt.Sprintf(virtCtlOutputFile, outputFile))
if !checks.IsOpenShift() {
targetPort := fmt.Sprintf("%d", 37548+rand.Intn(6000))
commandAndArgs = append(commandAndArgs, fmt.Sprintf(virtCtlPortForward, targetPort))
}
memorydumpCommand := clientcmd.NewRepeatableVirtctlCommand(commandAndArgs...)
Eventually(func() error {
err := memorydumpCommand()
Expand All @@ -674,9 +683,6 @@ var _ = SIGDescribe("Memory dump", func() {
}

BeforeEach(func() {
if !checks.IsOpenShift() {
Skip("Need ingress to run this test which we only have on openshift")
}
sc, exists := libstorage.GetRWOFileSystemStorageClass()
if !exists {
Skip("Skip no filesystem storage class available")
Expand Down

0 comments on commit 566a1a8

Please sign in to comment.