Skip to content
Open
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
8 changes: 6 additions & 2 deletions cmd/finch/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ func virtualMachineCommands(
home string,
finchRootPath string,
) *cobra.Command {
finchDir := fp.FinchDir(finchRootPath)

return newVirtualMachineCommand(
ncc,
logger,
dependencies(ecc, fc, fp, fs, ncc, logger, fp.FinchDir(finchRootPath)),
dependencies(ecc, fc, fp, fs, ncc, logger, finchDir),
config.NewLimaApplier(
fc,
ecc,
Expand All @@ -95,13 +97,15 @@ func virtualMachineCommands(
fssh.NewDialer(),
fs,
fp.LimaSSHPrivateKeyPath(),
fp.FinchDir(finchRootPath),
finchDir,
home,
fp.LimaInstancePath(),
fc,
),
fp,
fs,
disk.NewUserDataDiskManager(ncc, ecc, &afero.OsFs{}, fp, finchRootPath, fc, logger),
finchDir,
fc,
)
}
6 changes: 4 additions & 2 deletions cmd/finch/virtual_machine_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ func newVirtualMachineCommand(
fp path.Finch,
fs afero.Fs,
diskManager disk.UserDataDiskManager,
finchDir string,
finchCfg *config.Finch,
) *cobra.Command {
virtualMachineCommand := &cobra.Command{
Use: virtualMachineRootCmd,
Short: "Manage the virtual machine lifecycle",
}

virtualMachineCommand.AddCommand(
newStartVMCommand(limaCmdCreator, logger, optionalDepGroups, lca, nca, fs, fp.LimaSSHPrivateKeyPath(), diskManager),
newStartVMCommand(limaCmdCreator, logger, optionalDepGroups, lca, nca, fs, fp.LimaSSHPrivateKeyPath(), diskManager, finchDir, finchCfg),
newStopVMCommand(limaCmdCreator, diskManager, logger),
newRemoveVMCommand(limaCmdCreator, diskManager, logger),
newStatusVMCommand(limaCmdCreator, logger, os.Stdout),
newInitVMCommand(limaCmdCreator, logger, optionalDepGroups, lca, nca, fp.BaseYamlFilePath(), fs,
fp.LimaSSHPrivateKeyPath(), diskManager),
fp.LimaSSHPrivateKeyPath(), diskManager, finchDir, finchCfg),
newSettingsVMCommand(logger, lca, fs, os.Stdout),
newDiskVMCommand(limaCmdCreator, logger),
)
Expand Down
21 changes: 20 additions & 1 deletion cmd/finch/virtual_machine_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ package main

import (
"fmt"
"path/filepath"
"runtime"

"github.com/runfinch/finch/pkg/dependency/credhelper"
"github.com/runfinch/finch/pkg/disk"

"github.com/runfinch/finch/pkg/command"
Expand All @@ -31,11 +33,13 @@ func newInitVMCommand(
fs afero.Fs,
privateKeyPath string,
diskManager disk.UserDataDiskManager,
finchDir string,
finchCfg *config.Finch,
) *cobra.Command {
initVMCommand := &cobra.Command{
Use: "init",
Short: "Initialize the virtual machine",
RunE: newInitVMAction(ncc, logger, optionalDepGroups, lca, baseYamlFilePath, diskManager).runAdapter,
RunE: newInitVMAction(ncc, logger, optionalDepGroups, lca, baseYamlFilePath, fs, diskManager, finchDir, finchCfg).runAdapter,
PostRunE: newPostVMStartInitAction(logger, ncc, fs, privateKeyPath, nca).runAdapter,
}

Expand All @@ -48,7 +52,10 @@ type initVMAction struct {
logger flog.Logger
optionalDepGroups []*dependency.Group
limaConfigApplier config.LimaConfigApplier
fs afero.Fs
diskManager disk.UserDataDiskManager
finchDir string
finchCfg *config.Finch
}

func newInitVMAction(
Expand All @@ -57,15 +64,21 @@ func newInitVMAction(
optionalDepGroups []*dependency.Group,
lca config.LimaConfigApplier,
baseYamlFilePath string,
fs afero.Fs,
diskManager disk.UserDataDiskManager,
finchDir string,
finchCfg *config.Finch,
) *initVMAction {
return &initVMAction{
creator: creator,
logger: logger,
optionalDepGroups: optionalDepGroups,
limaConfigApplier: lca,
baseYamlFilePath: baseYamlFilePath,
fs: fs,
diskManager: diskManager,
finchDir: finchDir,
finchCfg: finchCfg,
}
}

Expand All @@ -89,6 +102,12 @@ func (iva *initVMAction) run() error {
return err
}

configJSONPath := filepath.Join(iva.finchDir, "config.json")
err = credhelper.RefreshConfigFile(iva.fs, iva.finchCfg, configJSONPath)
if err != nil {
return err
}

err = dependency.InstallOptionalDeps(iva.optionalDepGroups, iva.logger)
if err != nil {
iva.logger.Errorf("Dependency error: %v", err)
Expand Down
61 changes: 45 additions & 16 deletions cmd/finch/virtual_machine_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,37 @@ import (
"runtime"
"testing"

"github.com/runfinch/finch/pkg/dependency"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/mocks"

"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/runfinch/finch/pkg/config"
"github.com/runfinch/finch/pkg/dependency"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/mocks"
)

const mockBaseYamlFilePath = "/os/os.yaml"

func TestNewInitVMCommand(t *testing.T) {
t.Parallel()

cmd := newInitVMCommand(nil, nil, nil, nil, nil, "", nil, "", nil)
cmd := newInitVMCommand(nil, nil, nil, nil, nil, "", nil, "", nil, "", nil)
assert.Equal(t, cmd.Name(), "init")
}

func TestInitVMAction_runAdapter(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
command *cobra.Command
args []string
groups func(*gomock.Controller) []*dependency.Group
mockSvc func(
name string
command *cobra.Command
fc *config.Finch
args []string
groups func(*gomock.Controller) []*dependency.Group
finchDir string
mockSvc func(
*mocks.NerdctlCmdCreator,
*mocks.Logger,
*mocks.LimaConfigApplier,
Expand All @@ -50,6 +54,7 @@ func TestInitVMAction_runAdapter(t *testing.T) {
command: &cobra.Command{
Use: "init",
},
fc: &config.Finch{},
args: []string{},
groups: func(ctrl *gomock.Controller) []*dependency.Group {
dep := mocks.NewDependency(ctrl)
Expand All @@ -62,6 +67,7 @@ func TestInitVMAction_runAdapter(t *testing.T) {

return groups
},
finchDir: "/.finch",
mockSvc: func(
ncc *mocks.NerdctlCmdCreator,
logger *mocks.Logger,
Expand Down Expand Up @@ -102,12 +108,16 @@ func TestInitVMAction_runAdapter(t *testing.T) {
logger := mocks.NewLogger(ctrl)
ncc := mocks.NewNerdctlCmdCreator(ctrl)
lca := mocks.NewLimaConfigApplier(ctrl)
fs := afero.NewMemMapFs()
dm := mocks.NewUserDataDiskManager(ctrl)

groups := tc.groups(ctrl)
tc.mockSvc(ncc, logger, lca, dm, ctrl)

assert.NoError(t, newInitVMAction(ncc, logger, groups, lca, mockBaseYamlFilePath, dm).runAdapter(tc.command, tc.args))
assert.NoError(
t,
newInitVMAction(ncc, logger, groups, lca, mockBaseYamlFilePath, fs, dm, tc.finchDir, tc.fc).runAdapter(tc.command, tc.args),
)
})
}
}
Expand All @@ -116,10 +126,12 @@ func TestInitVMAction_run(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
wantErr error
groups func(*gomock.Controller) []*dependency.Group
mockSvc func(
name string
wantErr error
groups func(*gomock.Controller) []*dependency.Group
finchDir string
fc *config.Finch
mockSvc func(
*mocks.NerdctlCmdCreator,
*mocks.Logger,
*mocks.LimaConfigApplier,
Expand All @@ -133,6 +145,8 @@ func TestInitVMAction_run(t *testing.T) {
groups: func(_ *gomock.Controller) []*dependency.Group {
return nil
},
finchDir: "/.finch",
fc: &config.Finch{},
mockSvc: func(
ncc *mocks.NerdctlCmdCreator,
logger *mocks.Logger,
Expand Down Expand Up @@ -170,6 +184,8 @@ func TestInitVMAction_run(t *testing.T) {
groups: func(_ *gomock.Controller) []*dependency.Group {
return nil
},
finchDir: "/.finch",
fc: &config.Finch{},
mockSvc: func(
ncc *mocks.NerdctlCmdCreator,
logger *mocks.Logger,
Expand All @@ -191,6 +207,8 @@ func TestInitVMAction_run(t *testing.T) {
groups: func(_ *gomock.Controller) []*dependency.Group {
return nil
},
finchDir: "/.finch",
fc: &config.Finch{},
mockSvc: func(
ncc *mocks.NerdctlCmdCreator,
logger *mocks.Logger,
Expand All @@ -210,6 +228,8 @@ func TestInitVMAction_run(t *testing.T) {
groups: func(_ *gomock.Controller) []*dependency.Group {
return nil
},
finchDir: "/.finch",
fc: &config.Finch{},
mockSvc: func(
ncc *mocks.NerdctlCmdCreator,
logger *mocks.Logger,
Expand All @@ -229,6 +249,8 @@ func TestInitVMAction_run(t *testing.T) {
groups: func(_ *gomock.Controller) []*dependency.Group {
return nil
},
finchDir: "/.finch",
fc: &config.Finch{},
mockSvc: func(
ncc *mocks.NerdctlCmdCreator,
_ *mocks.Logger,
Expand Down Expand Up @@ -257,6 +279,8 @@ func TestInitVMAction_run(t *testing.T) {

return groups
},
finchDir: "/.finch",
fc: &config.Finch{},
mockSvc: func(
ncc *mocks.NerdctlCmdCreator,
logger *mocks.Logger,
Expand Down Expand Up @@ -302,6 +326,8 @@ func TestInitVMAction_run(t *testing.T) {
groups: func(_ *gomock.Controller) []*dependency.Group {
return nil
},
finchDir: "/.finch",
fc: &config.Finch{},
mockSvc: func(
ncc *mocks.NerdctlCmdCreator,
logger *mocks.Logger,
Expand All @@ -324,6 +350,8 @@ func TestInitVMAction_run(t *testing.T) {
groups: func(_ *gomock.Controller) []*dependency.Group {
return nil
},
finchDir: "/.finch",
fc: &config.Finch{},
mockSvc: func(
ncc *mocks.NerdctlCmdCreator,
logger *mocks.Logger,
Expand Down Expand Up @@ -364,12 +392,13 @@ func TestInitVMAction_run(t *testing.T) {
logger := mocks.NewLogger(ctrl)
ncc := mocks.NewNerdctlCmdCreator(ctrl)
lca := mocks.NewLimaConfigApplier(ctrl)
fs := afero.NewMemMapFs()
dm := mocks.NewUserDataDiskManager(ctrl)

groups := tc.groups(ctrl)
tc.mockSvc(ncc, logger, lca, dm, ctrl)

err := newInitVMAction(ncc, logger, groups, lca, mockBaseYamlFilePath, dm).run()
err := newInitVMAction(ncc, logger, groups, lca, mockBaseYamlFilePath, fs, dm, tc.finchDir, tc.fc).run()
assert.Equal(t, err, tc.wantErr)
})
}
Expand Down
22 changes: 21 additions & 1 deletion cmd/finch/virtual_machine_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package main

import (
"fmt"
"path/filepath"

"github.com/runfinch/finch/pkg/dependency/credhelper"
"github.com/runfinch/finch/pkg/disk"

"github.com/runfinch/finch/pkg/command"
Expand All @@ -29,11 +31,13 @@ func newStartVMCommand(
fs afero.Fs,
privateKeyPath string,
dm disk.UserDataDiskManager,
finchDir string,
finchCfg *config.Finch,
) *cobra.Command {
return &cobra.Command{
Use: "start",
Short: "Start the virtual machine",
RunE: newStartVMAction(ncc, logger, optionalDepGroups, lca, dm).runAdapter,
RunE: newStartVMAction(ncc, logger, optionalDepGroups, lca, fs, dm, finchDir, finchCfg).runAdapter,
PostRunE: newPostVMStartInitAction(logger, ncc, fs, privateKeyPath, nca).runAdapter,
}
}
Expand All @@ -43,22 +47,31 @@ type startVMAction struct {
logger flog.Logger
optionalDepGroups []*dependency.Group
limaConfigApplier config.LimaConfigApplier
fs afero.Fs
userDataDiskManager disk.UserDataDiskManager
finchDir string
finchCfg *config.Finch
}

func newStartVMAction(
creator command.NerdctlCmdCreator,
logger flog.Logger,
optionalDepGroups []*dependency.Group,
lca config.LimaConfigApplier,
fs afero.Fs,
dm disk.UserDataDiskManager,
finchDir string,
finchCfg *config.Finch,
) *startVMAction {
return &startVMAction{
creator: creator,
logger: logger,
optionalDepGroups: optionalDepGroups,
limaConfigApplier: lca,
fs: fs,
userDataDiskManager: dm,
finchDir: finchDir,
finchCfg: finchCfg,
}
}

Expand All @@ -71,6 +84,13 @@ func (sva *startVMAction) run() error {
if err != nil {
return err
}

configJSONPath := filepath.Join(sva.finchDir, "config.json")
err = credhelper.RefreshConfigFile(sva.fs, sva.finchCfg, configJSONPath)
if err != nil {
return err
}

err = dependency.InstallOptionalDeps(sva.optionalDepGroups, sva.logger)
if err != nil {
sva.logger.Errorf("Dependency error: %v", err)
Expand Down
Loading