Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow testsuite to run on Apple Silicon hardware #362

Merged
merged 5 commits into from
Jun 27, 2024
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
2 changes: 1 addition & 1 deletion test/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var _ = ginkgo.Describe("dns", func() {
ginkgo.It("should resolve TXT for wikipedia.org", func() {
out, err := sshExec("nslookup -query=txt wikipedia.org")
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(string(out)).To(gomega.ContainSubstring(`"v=spf1 include:wikimedia.org ~all"`))
gomega.Expect(string(out)).To(gomega.ContainSubstring(`"v=spf1 -all"`))
})

ginkgo.It("should resolve gateway.containers.internal", func() {
Expand Down
54 changes: 54 additions & 0 deletions test/efi_darwin_arm64_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package e2e

import (
"fmt"
"os"
"path/filepath"
)

func efiArgs() (string, error) {
// file may not exist, that's ok
_ = os.Remove("ovmf_vars.fd")
ovmfVars, err := os.Create("ovmf_vars.fd")
if err != nil {
return "", err
}
defer ovmfVars.Close()
if err := ovmfVars.Truncate(67108864); err != nil {
return "", err
}

edk2Path := getEdk2CodeFd("edk2-aarch64-code.fd")
return fmt.Sprintf(`-drive file=%s,if=pflash,format=raw,readonly=on -drive file=%s,if=pflash,format=raw `, edk2Path, ovmfVars.Name()), nil
}

/*
* When QEmu is installed in a non-default location in the system
* we can use the qemu-system-* binary path to figure the install
* location for Qemu and use it to look for edk2-code-fd
*/
func getEdk2CodeFdPathFromQemuBinaryPath() string {
return filepath.Clean(filepath.Join(filepath.Dir(qemuExecutable()), "..", "share", "qemu"))
}

/*
* QEmu can be installed in multiple locations on MacOS, especially on
* Apple Silicon systems. A build from source will likely install it in
* /usr/local/bin, whereas Homebrew package management standard is to
* install in /opt/homebrew
*/
func getEdk2CodeFd(name string) string {
dirs := []string{
getEdk2CodeFdPathFromQemuBinaryPath(),
"/opt/homebrew/opt/podman/libexec/share/qemu",
"/usr/local/share/qemu",
"/opt/homebrew/share/qemu",
}
for _, dir := range dirs {
fullpath := filepath.Join(dir, name)
if _, err := os.Stat(fullpath); err == nil {
return fullpath
}
}
return name
}
7 changes: 7 additions & 0 deletions test/efi_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !(darwin && arm64)

package e2e

func efiArgs() (string, error) {
return "", nil
}
78 changes: 78 additions & 0 deletions test/fcos.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ import (
"path/filepath"
"strings"

"encoding/json"
"fmt"
"io"
"net/http"
"runtime"

"github.com/opencontainers/go-digest"

"github.com/coreos/stream-metadata-go/fedoracoreos"
"github.com/coreos/stream-metadata-go/stream"
"github.com/sirupsen/logrus"
)

type FcosDownload struct {
Expand Down Expand Up @@ -78,3 +88,71 @@ func (downloader *FcosDownload) updateAvailable(info *fcosDownloadInfo, compress
}
return false, nil
}

// as of 2024-05-28, these are the 4 architectures available in
// curl https://builds.coreos.fedoraproject.org/streams/next.json
func coreosArch() string {
switch runtime.GOARCH {
case "amd64":
return "x86_64"
case "arm64":
return "aarch64"
case "ppc64le":
return "ppc64le"
case "s390x":
return "s390x"
}
panic(fmt.Sprintf("unknown arch: %s", runtime.GOOS))
}

// This should get Exported and stay put as it will apply to all fcos downloads
// getFCOS parses fedoraCoreOS's stream and returns the image download URL and the release version
func getFCOSDownload() (*fcosDownloadInfo, error) {
streamurl := fedoracoreos.GetStreamURL(fedoracoreos.StreamNext)
resp, err := http.Get(streamurl.String())
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer func() {
if err := resp.Body.Close(); err != nil {
logrus.Error(err)
}
}()

var fcosstable stream.Stream
if err := json.Unmarshal(body, &fcosstable); err != nil {
return nil, err
}
arch, ok := fcosstable.Architectures[coreosArch()]
if !ok {
return nil, fmt.Errorf("unable to pull VM image: no targetArch in stream")
}
artifacts := arch.Artifacts
if artifacts == nil {
return nil, fmt.Errorf("unable to pull VM image: no artifact in stream")
}
qemu, ok := artifacts["qemu"]
if !ok {
return nil, fmt.Errorf("unable to pull VM image: no qemu artifact in stream")
}
formats := qemu.Formats
if formats == nil {
return nil, fmt.Errorf("unable to pull VM image: no formats in stream")
}
qcow, ok := formats["qcow2.xz"]
if !ok {
return nil, fmt.Errorf("unable to pull VM image: no qcow2.xz format in stream")
}
disk := qcow.Disk
if disk == nil {
return nil, fmt.Errorf("unable to pull VM image: no disk in stream")
}
return &fcosDownloadInfo{
Location: disk.Location,
Sha256Sum: disk.Sha256,
}, nil
}
64 changes: 0 additions & 64 deletions test/fcos_amd64.go

This file was deleted.

21 changes: 17 additions & 4 deletions test/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ outer:
break
}

template := `%s -m 2048 -nographic -serial file:%s -snapshot -drive if=virtio,file=%s -fw_cfg name=opt/com.coreos/config,file=%s -netdev socket,id=vlan,connect=127.0.0.1:%d -device virtio-net-pci,netdev=vlan,mac=5a:94:ef:e4:0c:ee`
efiArgs, err := efiArgs()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

template := `%s%s-m 2048 -nographic -serial file:%s -drive if=virtio,file=%s,snapshot=on -fw_cfg name=opt/com.coreos/config,file=%s -netdev socket,id=vlan,connect=127.0.0.1:%d -device virtio-net-pci,netdev=vlan,mac=5a:94:ef:e4:0c:ee`

// #nosec
client = exec.Command(qemuExecutable(), strings.Split(fmt.Sprintf(template, qemuArgs(), qconLog, qemuImage, ignFile, qemuPort), " ")...)
client = exec.Command(qemuExecutable(), strings.Split(fmt.Sprintf(template, qemuArgs(), efiArgs, qconLog, qemuImage, ignFile, qemuPort), " ")...)
client.Stderr = os.Stderr
client.Stdout = os.Stdout
gomega.Expect(client.Start()).Should(gomega.Succeed())
Expand Down Expand Up @@ -150,7 +154,7 @@ outer:
})

func qemuExecutable() string {
qemuBinaries := []string{"qemu-kvm", "qemu-system-x86_64"}
qemuBinaries := []string{"qemu-kvm", fmt.Sprintf("qemu-system-%s", coreosArch())}
for _, binary := range qemuBinaries {
path, err := exec.LookPath(binary)
if err == nil && path != "" {
Expand All @@ -166,7 +170,16 @@ func qemuArgs() string {
if runtime.GOOS == "darwin" {
accel = "hvf"
}
return fmt.Sprintf("-machine q35,accel=%s:tcg -smp 4 -cpu host", accel)
machine := "q35"
switch runtime.GOARCH {
case "amd64":
machine = "q35"
case "arm64":
machine = "virt"
default:
panic(fmt.Sprintf("unsupported arch: %s", runtime.GOARCH))
}
return fmt.Sprintf("-machine %s,accel=%s:tcg -smp 4 -cpu host ", machine, accel)
}

func createSSHKeys() (string, error) {
Expand Down
Loading