test: move test utils files to the test-utils folder

Tests that are currently on the main branch only runs against a qemu VM. We have other use cases that needs to be tested like running against a vfkit VM.
This commit reorganizes the tests code a bit by moving the files that can be shared to support different implementation in their own folder.
The reasoning behind this is that every hypervisor should have its own beforeSuite func to download/run a specific VM image. By moving the utils files we can reuse the same code.

For the same reason the code targeting qemu is moved to the test-qemu folder. By doing so, we can run the tests within the test-qemu folder on the ubuntu workflow and, in future, when the nested virt will be enabled on github runners, the vfkit tests on macOS.

Signed-off-by: Luca Stocchi <lstocchi@redhat.com>
This commit is contained in:
Luca Stocchi
2024-11-19 17:02:57 +01:00
parent 5f09250426
commit b525dc8c69
13 changed files with 161 additions and 108 deletions

95
test-utils/pull.go Normal file
View File

@@ -0,0 +1,95 @@
package e2eutils
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strings"
"github.com/sirupsen/logrus"
)
// DownloadVMImage downloads a VM image from url to given path
// with download status
func DownloadVMImage(downloadURL string, localImagePath string) error {
fmt.Println("Downloading VM image: " + downloadURL)
out, err := os.Create(localImagePath)
if err != nil {
return err
}
defer func() {
if err := out.Close(); err != nil {
logrus.Error(err)
}
}()
// #nosec
resp, err := http.Get(downloadURL)
if err != nil {
return err
}
defer func() {
if err := resp.Body.Close(); err != nil {
logrus.Error(err)
}
}()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error downloading VM image %s: %s", downloadURL, resp.Status)
}
if _, err := io.Copy(out, resp.Body); err != nil {
return err
}
return nil
}
func Decompress(localPath string) (string, error) {
uncompressedPath := ""
if strings.HasSuffix(localPath, ".xz") {
uncompressedPath = strings.TrimSuffix(localPath, ".xz")
}
if uncompressedPath == "" {
return "", fmt.Errorf("unsupported compression for %s", localPath)
}
// we remove the uncompressed file if already exists. Maybe it has been used earlier and can affect the tests result
os.Remove(uncompressedPath)
uncompressedFileWriter, err := os.OpenFile(uncompressedPath, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return "", err
}
fmt.Printf("Extracting %s\n", localPath)
err = decompressXZ(localPath, uncompressedFileWriter)
if err != nil {
return "", err
}
return uncompressedPath, nil
}
// Will error out if file without .xz already exists
// Maybe extracting then renameing is a good idea here..
// depends on xz: not pre-installed on mac, so it becomes a brew dependency
func decompressXZ(src string, output io.Writer) error {
cmd := exec.Command("xzcat", "-T0", "-k", src)
stdOut, err := cmd.StdoutPipe()
if err != nil {
return err
}
cmd.Stderr = os.Stderr
go func() {
if _, err := io.Copy(output, stdOut); err != nil {
logrus.Error(err)
}
}()
return cmd.Run()
}