Skip to content

Commit

Permalink
fix osutil.LimaUser call
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Alvarez <alvajus@amazon.com>
  • Loading branch information
pendo324 committed Jan 13, 2025
1 parent 9a85a3d commit 7e40eb9
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ SUPPORTED_ARCH = false
LICENSEDIR := $(OUTDIR)/license-files
VERSION ?= $(shell git describe --match 'v[0-9]*' --dirty='.modified' --always --tags)
GITCOMMIT ?= $(shell git rev-parse HEAD)$(shell test -z "$(git status --porcelain)" || echo .m)
LDFLAGS = "-w -X $(PACKAGE)/pkg/version.Version=$(VERSION) -X $(PACKAGE)/pkg/version.GitCommit=$(GITCOMMIT)"
VERSION_INJECTION := -X $(PACKAGE)/pkg/version.Version=$(VERSION)
VERSION_INJECTION += -X $(PACKAGE)/pkg/version.GitCommit=$(GITCOMMIT)
VERSION_INJECTION += -X $(PACKAGE)/pkg/version.GitCommit=$(GITCOMMIT)
LDFLAGS = "-w $(VERSION_INJECTION)"
MIN_MACOS_VERSION ?= 11.0

GOOS ?= $(shell $(GO) env GOOS)
Expand All @@ -53,6 +56,14 @@ else ifneq (,$(findstring x86_64,$(ARCH)))
LIMA_ARCH = x86_64
endif

# This variable is used to inject the version of Lima (via ldflags) to be used with Lima's
# osutil.LimaUser function.
LIMA_TAG=$(shell cd deps/finch-core/src/lima && git describe --match 'v[0-9]*' --dirty='.modified' --always --tags)
LIMA_VERSION := $(patsubst v%,%,$(LIMA_TAG))
# This value isn't used on Linux, but the symbol is currently defined on all platforms, so
# it doesn't hurt to always inject it right now.
VERSION_INJECTION += -X $(PACKAGE)/pkg/lima.LimaVersion=$(LIMA_VERSION)

.PHONY: arch-test
arch-test:
@if [ $(SUPPORTED_ARCH) != "true" ]; then echo "Unsupported architecture: $(ARCH)"; exit "1"; fi
Expand Down
3 changes: 3 additions & 0 deletions pkg/lima/lima.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type VMStatus int64
// https://github.com/lima-vm/lima/blob/f0282b2dfc4b6795295fceb2c86acd1312cef436/pkg/limayaml/limayaml.go#L53-L54.
type VMType string

// LimaVersion is injected at build time to be used in the call to osutil.LimaUser.
var LimaVersion string

// Finch CLI assumes there are only 4 VM status below. Adding more statuses will need to make changes in the caller side.
const (
Running VMStatus = iota
Expand Down
8 changes: 5 additions & 3 deletions pkg/lima/wrapper/lima_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"os/user"

"github.com/lima-vm/lima/pkg/osutil"

"github.com/runfinch/finch/pkg/lima"
)

// LimaWrapper provides Lima utils in an interface to facilitate mocking
//
//go:generate mockgen --destination=../../mocks/lima_wrapper.go -package=mocks github.com/runfinch/finch/pkg/lima/wrapper LimaWrapper
type LimaWrapper interface {
LimaUser(warn bool) (*user.User, error)
LimaUser(warn bool) *user.User
}

type limaWrapper struct{}
Expand All @@ -25,6 +27,6 @@ func NewLimaWrapper() LimaWrapper {
}

// LimaUser returns the user that will be used inside the Lima VM.
func (*limaWrapper) LimaUser(warn bool) (*user.User, error) {
return osutil.LimaUser(warn)
func (*limaWrapper) LimaUser(warn bool) *user.User {
return osutil.LimaUser(lima.LimaVersion, warn)
}
5 changes: 2 additions & 3 deletions pkg/mocks/lima_wrapper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions pkg/support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,7 @@ func (bb *bundleBuilder) copyAndRedactFile(writer io.Writer, reader bufReader) e
return err
}

user, err := bb.lima.LimaUser(false)
if err != nil {
return err
}
user := bb.lima.LimaUser(false)
line, err = redactUsername(line, user.Username)
if err != nil {
return err
Expand Down
14 changes: 7 additions & 7 deletions pkg/support/support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) {
logger.EXPECT().Debugf("Copying %s...", "config2")
logger.EXPECT().Debugln("Copying in additional files...")

lima.EXPECT().LimaUser(false).Return(mockUser, nil).AnyTimes()
lima.EXPECT().LimaUser(false).Return(mockUser).AnyTimes()
},
include: []string{},
exclude: []string{},
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) {
logger.EXPECT().Debugln("Copying in additional files...")
logger.EXPECT().Debugf("Copying %s...", "extra1")

lima.EXPECT().LimaUser(false).Return(mockUser, nil).AnyTimes()
lima.EXPECT().LimaUser(false).Return(mockUser).AnyTimes()
},
include: []string{"extra1"},
exclude: []string{},
Expand Down Expand Up @@ -213,7 +213,7 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) {
logger.EXPECT().Debugf("Copying %s...", "config1")
logger.EXPECT().Debugln("Copying in additional files...")

lima.EXPECT().LimaUser(false).Return(mockUser, nil).AnyTimes()
lima.EXPECT().LimaUser(false).Return(mockUser).AnyTimes()
},
include: []string{},
exclude: []string{"log1"},
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) {
logger.EXPECT().Infof("Excluding %s...", "config1")
logger.EXPECT().Debugln("Copying in additional files...")

lima.EXPECT().LimaUser(false).Return(mockUser, nil).AnyTimes()
lima.EXPECT().LimaUser(false).Return(mockUser).AnyTimes()
},
include: []string{},
exclude: []string{"config1"},
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) {
logger.EXPECT().Debugln("Copying in additional files...")
logger.EXPECT().Infof("Excluding %s...", "extra1")

lima.EXPECT().LimaUser(false).Return(mockUser, nil).AnyTimes()
lima.EXPECT().LimaUser(false).Return(mockUser).AnyTimes()
},
include: []string{"extra1"},
exclude: []string{"extra1"},
Expand Down Expand Up @@ -391,7 +391,7 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) {
return nil
})

lima.EXPECT().LimaUser(false).Return(mockUser, nil).AnyTimes()
lima.EXPECT().LimaUser(false).Return(mockUser).AnyTimes()
},
include: []string{"vm:extra1"},
},
Expand Down Expand Up @@ -448,7 +448,7 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) {
// hard to write a matcher for the second argument
logger.EXPECT().Warnf("Could not add additional file %s. Error: %s", "vm:extra1", gomock.Any())

lima.EXPECT().LimaUser(false).Return(mockUser, nil).AnyTimes()
lima.EXPECT().LimaUser(false).Return(mockUser).AnyTimes()
},
include: []string{"vm:extra1"},
},
Expand Down

0 comments on commit 7e40eb9

Please sign in to comment.