Skip to content

Commit

Permalink
pkg/arch: move CurrentArch to pkg/arch
Browse files Browse the repository at this point in the history
  • Loading branch information
croissanne committed Nov 24, 2023
1 parent 2a7af31 commit cff59ad
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 63 deletions.
4 changes: 2 additions & 2 deletions cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"path/filepath"
"strings"

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/internal/dnfjson"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/distro"
Expand Down Expand Up @@ -341,7 +341,7 @@ func main() {
fail(fmt.Sprintf("invalid or unsupported distribution: %q", distroName))
}

archName := common.CurrentArch()
archName := arch.Current().String()
arch, err := distribution.GetArch(archName)
if err != nil {
fail(fmt.Sprintf("invalid arch name %q for distro %q: %s\n", archName, distroName, err.Error()))
Expand Down
4 changes: 2 additions & 2 deletions cmd/osbuild-playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
"path"

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distroregistry"
"github.com/osbuild/images/pkg/image"
Expand Down Expand Up @@ -42,7 +42,7 @@ func main() {
var distroArg string
flag.StringVar(&distroArg, "distro", "host", "distro to build from")
var archArg string
flag.StringVar(&archArg, "arch", common.CurrentArch(), "architecture to build for")
flag.StringVar(&archArg, "arch", arch.Current().String(), "architecture to build for")
var imageTypeArg string
flag.StringVar(&imageTypeArg, "type", "my-container", "image type to build")
flag.Parse()
Expand Down
4 changes: 2 additions & 2 deletions internal/boot/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"

"github.com/osbuild/images/internal/cloud/awscloud"
"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/arch"
)

type awsCredentials struct {
Expand Down Expand Up @@ -97,7 +97,7 @@ func UploadImageToAWS(c *awsCredentials, imagePath string, imageName string) err
if err != nil {
return fmt.Errorf("cannot upload the image: %v", err)
}
_, _, err = uploader.Register(imageName, c.Bucket, imageName, nil, common.CurrentArch(), nil)
_, _, err = uploader.Register(imageName, c.Bucket, imageName, nil, arch.Current().String(), nil)
if err != nil {
return fmt.Errorf("cannot register the image: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/boot/context-managers.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func WithBootedQemuImage(image string, ns NetNS, f func() error) error {
}

var qemuCmd *exec.Cmd
if common.CurrentArch() == arch.ARCH_X86_64.String() {
if arch.IsX86_64() {
hostDistroName, _, _, err := common.GetHostDistroName()
if err != nil {
return fmt.Errorf("cannot determing the current distro: %v", err)
Expand All @@ -136,7 +136,7 @@ func WithBootedQemuImage(image string, ns NetNS, f func() error) error {
"-nographic",
image,
)
} else if common.CurrentArch() == arch.ARCH_AARCH64.String() {
} else if arch.IsAarch64() {
// This command does not use KVM as I was unable to make it work in Beaker,
// once we have machines that can use KVM, enable it to make it faster
qemuCmd = ns.NamespacedCommand(
Expand Down
17 changes: 0 additions & 17 deletions internal/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,11 @@ import (
"fmt"
"io"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
)

var RuntimeGOARCH = runtime.GOARCH

func CurrentArch() string {
if RuntimeGOARCH == "amd64" {
return "x86_64"
} else if RuntimeGOARCH == "arm64" {
return "aarch64"
} else if RuntimeGOARCH == "ppc64le" {
return "ppc64le"
} else if RuntimeGOARCH == "s390x" {
return "s390x"
} else {
panic("unsupported architecture")
}
}

func PanicOnError(err error) {
if err != nil {
panic(err)
Expand Down
35 changes: 0 additions & 35 deletions internal/common/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestCurrentArchAMD64(t *testing.T) {
origRuntimeGOARCH := RuntimeGOARCH
defer func() { RuntimeGOARCH = origRuntimeGOARCH }()
RuntimeGOARCH = "amd64"
assert.Equal(t, "x86_64", CurrentArch())
}

func TestCurrentArchARM64(t *testing.T) {
origRuntimeGOARCH := RuntimeGOARCH
defer func() { RuntimeGOARCH = origRuntimeGOARCH }()
RuntimeGOARCH = "arm64"
assert.Equal(t, "aarch64", CurrentArch())
}

func TestCurrentArchPPC64LE(t *testing.T) {
origRuntimeGOARCH := RuntimeGOARCH
defer func() { RuntimeGOARCH = origRuntimeGOARCH }()
RuntimeGOARCH = "ppc64le"
assert.Equal(t, "ppc64le", CurrentArch())
}

func TestCurrentArchS390X(t *testing.T) {
origRuntimeGOARCH := RuntimeGOARCH
defer func() { RuntimeGOARCH = origRuntimeGOARCH }()
RuntimeGOARCH = "s390x"
assert.Equal(t, "s390x", CurrentArch())
}

func TestCurrentArchUnsupported(t *testing.T) {
origRuntimeGOARCH := RuntimeGOARCH
defer func() { RuntimeGOARCH = origRuntimeGOARCH }()
RuntimeGOARCH = "UKNOWN"
assert.PanicsWithValue(t, "unsupported architecture", func() { CurrentArch() })
}

func TestPanicOnError(t *testing.T) {
err := errors.New("Error message")
assert.PanicsWithValue(t, err, func() { PanicOnError(err) })
Expand Down
4 changes: 2 additions & 2 deletions internal/test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/distro"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -244,7 +244,7 @@ func GenerateCIArtifactName(prefix string) (string, error) {
return "", fmt.Errorf("The environment variables must specify BRANCH_NAME, BUILD_ID, and DISTRO_CODE")
}

arch := common.CurrentArch()
arch := arch.Current().String()

return fmt.Sprintf("%s%s-%s-%s-%s", prefix, distroCode, arch, branchName, buildId), nil
}
36 changes: 36 additions & 0 deletions pkg/arch/arch.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package arch

import (
"runtime"
)

type Arch uint64

const ( // architecture enum
Expand All @@ -23,3 +27,35 @@ func (a Arch) String() string {
panic("invalid architecture")
}
}

var runtimeGOARCH = runtime.GOARCH
func Current() Arch {
switch runtimeGOARCH {
case "amd64":
return ARCH_X86_64
case "arm64":
return ARCH_AARCH64
case "ppc64le":
return ARCH_PPC64LE
case "s390x":
return ARCH_S390X
default:
panic("unsupported architecture")
}
}

func IsX86_64() bool {
return Current() == ARCH_X86_64
}

func IsAarch64() bool {
return Current() == ARCH_AARCH64
}

func IsPPC() bool {
return Current() == ARCH_PPC64LE
}

func IsS390x() bool {
return Current() == ARCH_S390X
}
46 changes: 46 additions & 0 deletions pkg/arch/arch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package arch

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCurrentArchAMD64(t *testing.T) {
origRuntimeGOARCH := runtimeGOARCH
defer func() { runtimeGOARCH = origRuntimeGOARCH }()
runtimeGOARCH = "amd64"
assert.Equal(t, "x86_64", Current().String())
assert.True(t, IsX86_64())
}

func TestCurrentArchARM64(t *testing.T) {
origRuntimeGOARCH := runtimeGOARCH
defer func() { runtimeGOARCH = origRuntimeGOARCH }()
runtimeGOARCH = "arm64"
assert.Equal(t, "aarch64", Current().String())
assert.True(t, IsAarch64())
}

func TestCurrentArchPPC64LE(t *testing.T) {
origRuntimeGOARCH := runtimeGOARCH
defer func() { runtimeGOARCH = origRuntimeGOARCH }()
runtimeGOARCH = "ppc64le"
assert.Equal(t, "ppc64le", Current().String())
assert.True(t, IsPPC())
}

func TestCurrentArchS390X(t *testing.T) {
origRuntimeGOARCH := runtimeGOARCH
defer func() { runtimeGOARCH = origRuntimeGOARCH }()
runtimeGOARCH = "s390x"
assert.Equal(t, "s390x", Current().String())
assert.True(t, IsS390x())
}

func TestCurrentArchUnsupported(t *testing.T) {
origRuntimeGOARCH := runtimeGOARCH
defer func() { runtimeGOARCH = origRuntimeGOARCH }()
runtimeGOARCH = "UKNOWN"
assert.PanicsWithValue(t, "unsupported architecture", func() { Current() })
}
3 changes: 2 additions & 1 deletion pkg/distroregistry/distroregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distro/fedora"
"github.com/osbuild/images/pkg/distro/rhel7"
Expand Down Expand Up @@ -52,7 +53,7 @@ func New(hostDistro distro.Distro, distros ...distro.Distro) (*Registry, error)
reg := &Registry{
distros: make(map[string]distro.Distro),
hostDistro: hostDistro,
hostArchName: common.CurrentArch(),
hostArchName: arch.Current().String(),
}
for _, d := range distros {
name := d.Name()
Expand Down

0 comments on commit cff59ad

Please sign in to comment.