Skip to content

Commit

Permalink
pkg/config: fix more linting warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Alvarez <alvajus@amazon.com>
  • Loading branch information
pendo324 committed Mar 24, 2023
1 parent a3aa3af commit 1a2001a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 20 deletions.
23 changes: 15 additions & 8 deletions pkg/config/lima_config_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"strings"

"github.com/lima-vm/lima/pkg/limayaml"
"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/system"
"github.com/spf13/afero"
"github.com/xorcare/pointer"
"gopkg.in/yaml.v3"

"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/system"
)

const userModeEmulationProvisioningScriptHeader = "# cross-arch tools"
Expand All @@ -38,7 +39,13 @@ var _ LimaConfigApplier = (*limaConfigApplier)(nil)

// NewLimaApplier creates a new LimaConfigApplier that
// applies lima configuration changes by writing to the lima config file on the disk.
func NewLimaApplier(cfg *Finch, cmdCreator command.Creator, fs afero.Fs, limaConfigPath string, systemDeps LimaConfigApplierSystemDeps) LimaConfigApplier {
func NewLimaApplier(
cfg *Finch,
cmdCreator command.Creator,
fs afero.Fs,
limaConfigPath string,
systemDeps LimaConfigApplierSystemDeps,
) LimaConfigApplier {
return &limaConfigApplier{
cfg: cfg,
cmdCreator: cmdCreator,
Expand Down Expand Up @@ -101,14 +108,13 @@ func (lca *limaConfigApplier) Apply(isInit bool) error {
// applyInit changes settings that will only apply to the VM after a new init.
func (lca *limaConfigApplier) applyInit(limaCfg *limayaml.LimaYAML) (*limayaml.LimaYAML, error) {
hasSupport, hasSupportErr := lca.supportsVirtualizationFramework()
if *lca.cfg.Rosetta == true &&
if *lca.cfg.Rosetta &&
lca.systemDeps.OS() == "darwin" &&
lca.systemDeps.Arch() == "arm64" {

if hasSupportErr != nil {
return nil, fmt.Errorf("failed to check for virtualization framework support: %w", hasSupportErr)
}
if hasSupport == false {
if !hasSupport {
return nil, fmt.Errorf(`system does not have virtualization framework support, change vmType to "qemu"`)
}

Expand Down Expand Up @@ -145,7 +151,8 @@ func toggleUserModeEmulationInstallationScript(limaCfg *limayaml.LimaYAML, enabl
Script: fmt.Sprintf(`%s
#!/bin/bash
dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-user-static-arm qemu-user-static-x86
`, userModeEmulationProvisioningScriptHeader)})
`, userModeEmulationProvisioningScriptHeader),
})
} else if hasScript && !enabled {
if len(limaCfg.Provision) > 0 {
limaCfg.Provision = append(limaCfg.Provision[:idx], limaCfg.Provision[idx+1:]...)
Expand Down Expand Up @@ -175,7 +182,7 @@ func (lca *limaConfigApplier) supportsVirtualizationFramework() (bool, error) {
}

splitVer := strings.Split(string(out), ".")
if len(splitVer) <= 0 {
if len(splitVer) == 0 {
return false, fmt.Errorf("unexpected result from string split: %v", splitVer)
}

Expand Down
72 changes: 60 additions & 12 deletions pkg/config/lima_config_applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ func TestDiskLimaConfigApplier_Apply(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
config *Finch
path string
isInit bool
mockSvc func(fs afero.Fs, l *mocks.Logger, cmd *mocks.Command, creator *mocks.CommandCreator, deps *mocks.LimaConfigApplierSystemDeps)
name string
config *Finch
path string
isInit bool
mockSvc func(
fs afero.Fs,
l *mocks.Logger,
cmd *mocks.Command,
creator *mocks.CommandCreator,
deps *mocks.LimaConfigApplierSystemDeps,
)
postRunCheck func(t *testing.T, fs afero.Fs)
want error
}{
Expand All @@ -39,7 +45,13 @@ func TestDiskLimaConfigApplier_Apply(t *testing.T) {
},
path: "/lima.yaml",
isInit: true,
mockSvc: func(fs afero.Fs, l *mocks.Logger, cmd *mocks.Command, creator *mocks.CommandCreator, deps *mocks.LimaConfigApplierSystemDeps) {
mockSvc: func(
fs afero.Fs,
l *mocks.Logger,
cmd *mocks.Command,
creator *mocks.CommandCreator,
deps *mocks.LimaConfigApplierSystemDeps,
) {
err := afero.WriteFile(fs, "/lima.yaml", []byte("memory: 4GiB\ncpus: 8"), 0o600)
require.NoError(t, err)
cmd.EXPECT().Output().Return([]byte("12.6.1"), nil)
Expand Down Expand Up @@ -73,7 +85,13 @@ dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-us
},
path: "/lima.yaml",
isInit: true,
mockSvc: func(fs afero.Fs, l *mocks.Logger, cmd *mocks.Command, creator *mocks.CommandCreator, deps *mocks.LimaConfigApplierSystemDeps) {
mockSvc: func(
fs afero.Fs,
l *mocks.Logger,
cmd *mocks.Command,
creator *mocks.CommandCreator,
deps *mocks.LimaConfigApplierSystemDeps,
) {
err := afero.WriteFile(fs, "/lima.yaml", []byte(`memory: 4GiB
cpus: 8
vmType: "qemu"
Expand Down Expand Up @@ -117,7 +135,13 @@ provision:
},
path: "/lima.yaml",
isInit: true,
mockSvc: func(fs afero.Fs, l *mocks.Logger, cmd *mocks.Command, creator *mocks.CommandCreator, deps *mocks.LimaConfigApplierSystemDeps) {
mockSvc: func(
fs afero.Fs,
l *mocks.Logger,
cmd *mocks.Command,
creator *mocks.CommandCreator,
deps *mocks.LimaConfigApplierSystemDeps,
) {
err := afero.WriteFile(fs, "/lima.yaml", []byte(`memory: 4GiB
cpus: 8
vmType: "vz"
Expand Down Expand Up @@ -160,7 +184,13 @@ dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-us
},
path: "/lima.yaml",
isInit: false,
mockSvc: func(fs afero.Fs, l *mocks.Logger, cmd *mocks.Command, creator *mocks.CommandCreator, deps *mocks.LimaConfigApplierSystemDeps) {
mockSvc: func(
fs afero.Fs,
l *mocks.Logger,
cmd *mocks.Command,
creator *mocks.CommandCreator,
deps *mocks.LimaConfigApplierSystemDeps,
) {
err := afero.WriteFile(fs, "/lima.yaml", []byte(`memory: 4GiB
cpus: 8
vmType: "qemu"
Expand Down Expand Up @@ -203,7 +233,13 @@ dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-us
},
path: "/lima.yaml",
isInit: true,
mockSvc: func(fs afero.Fs, l *mocks.Logger, cmd *mocks.Command, creator *mocks.CommandCreator, deps *mocks.LimaConfigApplierSystemDeps) {
mockSvc: func(
fs afero.Fs,
l *mocks.Logger,
cmd *mocks.Command,
creator *mocks.CommandCreator,
deps *mocks.LimaConfigApplierSystemDeps,
) {
err := afero.WriteFile(fs, "/lima.yaml", []byte("memory: 4GiB\ncpus: 8"), 0o600)
require.NoError(t, err)
cmd.EXPECT().Output().Return([]byte("12.6.1"), nil)
Expand Down Expand Up @@ -232,7 +268,13 @@ dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-us
config: nil,
path: "/lima.yaml",
isInit: true,
mockSvc: func(fs afero.Fs, l *mocks.Logger, cmd *mocks.Command, creator *mocks.CommandCreator, deps *mocks.LimaConfigApplierSystemDeps) {
mockSvc: func(
fs afero.Fs,
l *mocks.Logger,
cmd *mocks.Command,
creator *mocks.CommandCreator,
deps *mocks.LimaConfigApplierSystemDeps,
) {
err := afero.WriteFile(fs, "/lima.yaml", []byte("this isn't YAML"), 0o600)
require.NoError(t, err)
},
Expand All @@ -258,7 +300,13 @@ dnf install -y --setopt=install_weak_deps=False qemu-user-static-aarch64 qemu-us
},
path: "/lima.yaml",
isInit: true,
mockSvc: func(fs afero.Fs, l *mocks.Logger, cmd *mocks.Command, creator *mocks.CommandCreator, deps *mocks.LimaConfigApplierSystemDeps) {
mockSvc: func(
fs afero.Fs,
l *mocks.Logger,
cmd *mocks.Command,
creator *mocks.CommandCreator,
deps *mocks.LimaConfigApplierSystemDeps,
) {
err := afero.WriteFile(fs, "/lima.yaml", []byte("memory: 4GiB\ncpus: 8"), 0o600)
require.NoError(t, err)
cmd.EXPECT().Output().Return([]byte("13.0.0"), nil)
Expand Down

0 comments on commit 1a2001a

Please sign in to comment.