Skip to content

Commit

Permalink
Revert "fix: always force stop when using Virtualization.framework (#350
Browse files Browse the repository at this point in the history
)"

This reverts commit c521f1f.
  • Loading branch information
pendo324 committed Apr 17, 2023
1 parent 5e03a4d commit 9f4073b
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 161 deletions.
11 changes: 0 additions & 11 deletions cmd/finch/virtual_machine_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ func (sva *stopVMAction) runAdapter(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

if !force {
if vmType, err := lima.GetVMType(sva.creator, sva.logger, limaInstanceName); err == nil {
if vmType == lima.VZ {
force = true
}
} else {
return err
}
}

return sva.run(force)
}

Expand Down
37 changes: 1 addition & 36 deletions cmd/finch/virtual_machine_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@ func TestStopVMAction_runAdapter(t *testing.T) {
name string
mockSvc func(*mocks.Logger, *mocks.LimaCmdCreator, *gomock.Controller)
args []string
wantErr error
}{
{
name: "should stop the instance",
args: []string{},
mockSvc: func(logger *mocks.Logger, creator *mocks.LimaCmdCreator, ctrl *gomock.Controller) {
getVMTypeC := mocks.NewCommand(ctrl)
creator.EXPECT().CreateWithoutStdio("ls", "-f", "{{.VMType}}", limaInstanceName).Return(getVMTypeC)
getVMTypeC.EXPECT().Output().Return([]byte("qemu"), nil)
logger.EXPECT().Debugf("VMType of virtual machine: %s", "qemu")

getVMStatusC := mocks.NewCommand(ctrl)
creator.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC)
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
Expand All @@ -49,7 +43,6 @@ func TestStopVMAction_runAdapter(t *testing.T) {
command.EXPECT().CombinedOutput()
logger.EXPECT().Info(gomock.Any()).AnyTimes()
},
wantErr: nil,
},
{
name: "should force stop the instance",
Expand All @@ -62,33 +55,6 @@ func TestStopVMAction_runAdapter(t *testing.T) {
command.EXPECT().CombinedOutput()
logger.EXPECT().Info(gomock.Any()).AnyTimes()
},
wantErr: nil,
},
{
name: "should stop the instance and use --force even when not specified if VMType == vz",
args: []string{},
mockSvc: func(logger *mocks.Logger, creator *mocks.LimaCmdCreator, ctrl *gomock.Controller) {
getVMTypeC := mocks.NewCommand(ctrl)
creator.EXPECT().CreateWithoutStdio("ls", "-f", "{{.VMType}}", limaInstanceName).Return(getVMTypeC)
getVMTypeC.EXPECT().Output().Return([]byte("vz"), nil)
logger.EXPECT().Debugf("VMType of virtual machine: %s", "vz")

command := mocks.NewCommand(ctrl)
creator.EXPECT().CreateWithoutStdio("stop", "--force", limaInstanceName).Return(command)
command.EXPECT().CombinedOutput()
logger.EXPECT().Info(gomock.Any()).AnyTimes()
},
wantErr: nil,
},
{
name: "get VMType returns an error",
args: []string{},
mockSvc: func(logger *mocks.Logger, creator *mocks.LimaCmdCreator, ctrl *gomock.Controller) {
getVMTypeC := mocks.NewCommand(ctrl)
creator.EXPECT().CreateWithoutStdio("ls", "-f", "{{.VMType}}", limaInstanceName).Return(getVMTypeC)
getVMTypeC.EXPECT().Output().Return([]byte("unknown"), errors.New("unrecognized VMType"))
},
wantErr: errors.New("unrecognized VMType"),
},
}

Expand All @@ -104,8 +70,7 @@ func TestStopVMAction_runAdapter(t *testing.T) {

cmd := newStopVMCommand(lcc, logger)
cmd.SetArgs(tc.args)
err := cmd.Execute()
assert.Equal(t, tc.wantErr, err)
assert.NoError(t, cmd.Execute())
})
}
}
Expand Down
35 changes: 0 additions & 35 deletions pkg/lima/lima.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,12 @@ import (
// https://github.com/lima-vm/lima/blob/fc783ec455a91d70639f9a1d7f22e9890fe6b1cd/pkg/store/instance.go#L23.
type VMStatus int64

// VMType for Lima.
// Relevant type defined in Lima upstream:
// https://github.com/lima-vm/lima/blob/f0282b2dfc4b6795295fceb2c86acd1312cef436/pkg/limayaml/limayaml.go#L53-L54.
type VMType 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
Stopped
Nonexistent
Unknown
QEMU VMType = "qemu"
VZ VMType = "vz"
NonexistentVMType VMType = "nonexistant"
UnknownVMType VMType = "unknown"
)

// GetVMStatus returns the Lima VM status.
Expand All @@ -46,18 +37,6 @@ func GetVMStatus(creator command.LimaCmdCreator, logger flog.Logger, instanceNam
return toVMStatus(status, logger)
}

// GetVMType returns the Lima VMType for a running instance.
func GetVMType(creator command.LimaCmdCreator, logger flog.Logger, instanceName string) (VMType, error) {
args := []string{"ls", "-f", "{{.VMType}}", instanceName}
cmd := creator.CreateWithoutStdio(args...)
out, err := cmd.Output()
if err != nil {
return UnknownVMType, err
}
status := strings.TrimSpace(string(out))
return toVMType(status, logger)
}

func toVMStatus(status string, logger flog.Logger) (VMStatus, error) {
logger.Debugf("Status of virtual machine: %s", status)
switch status {
Expand All @@ -71,17 +50,3 @@ func toVMStatus(status string, logger flog.Logger) (VMStatus, error) {
return Unknown, errors.New("unrecognized system status")
}
}

func toVMType(vmType string, logger flog.Logger) (VMType, error) {
logger.Debugf("VMType of virtual machine: %s", vmType)
switch vmType {
case "":
return NonexistentVMType, nil
case "qemu":
return QEMU, nil
case "vz":
return VZ, nil
default:
return UnknownVMType, errors.New("unrecognized VMType")
}
}
79 changes: 0 additions & 79 deletions pkg/lima/lima_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,82 +92,3 @@ func TestGetVMStatus(t *testing.T) {
})
}
}

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

instanceName := "finch"
mockArgs := []string{"ls", "-f", "{{.VMType}}", instanceName}
testCases := []struct {
name string
want lima.VMType
wantErr error
mockSvc func(*mocks.LimaCmdCreator, *mocks.Logger, *mocks.Command)
}{
{
name: "qemu VM",
want: lima.QEMU,
wantErr: nil,
mockSvc: func(creator *mocks.LimaCmdCreator, logger *mocks.Logger, cmd *mocks.Command) {
creator.EXPECT().CreateWithoutStdio(mockArgs).Return(cmd)
cmd.EXPECT().Output().Return([]byte("qemu"), nil)
logger.EXPECT().Debugf("VMType of virtual machine: %s", "qemu")
},
},
{
name: "vz VM",
want: lima.VZ,
wantErr: nil,
mockSvc: func(creator *mocks.LimaCmdCreator, logger *mocks.Logger, cmd *mocks.Command) {
creator.EXPECT().CreateWithoutStdio(mockArgs).Return(cmd)
cmd.EXPECT().Output().Return([]byte("vz"), nil)
logger.EXPECT().Debugf("VMType of virtual machine: %s", "vz")
},
},
{
name: "nonexistent VM",
want: lima.NonexistentVMType,
wantErr: nil,
mockSvc: func(creator *mocks.LimaCmdCreator, logger *mocks.Logger, cmd *mocks.Command) {
creator.EXPECT().CreateWithoutStdio(mockArgs).Return(cmd)
cmd.EXPECT().Output().Return([]byte(" "), nil)
logger.EXPECT().Debugf("VMType of virtual machine: %s", "")
},
},
{
name: "unknown VM type",
want: lima.UnknownVMType,
wantErr: errors.New("unrecognized VMType"),
mockSvc: func(creator *mocks.LimaCmdCreator, logger *mocks.Logger, cmd *mocks.Command) {
creator.EXPECT().CreateWithoutStdio(mockArgs).Return(cmd)
cmd.EXPECT().Output().Return([]byte("Broken "), nil)
logger.EXPECT().Debugf("VMType of virtual machine: %s", "Broken")
},
},
{
name: "type command returns an error",
want: lima.UnknownVMType,
wantErr: errors.New("get VMType error"),
mockSvc: func(creator *mocks.LimaCmdCreator, logger *mocks.Logger, cmd *mocks.Command) {
creator.EXPECT().CreateWithoutStdio(mockArgs).Return(cmd)
cmd.EXPECT().Output().Return([]byte("Broken "), errors.New("get VMType error"))
},
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
creator := mocks.NewLimaCmdCreator(ctrl)
statusCmd := mocks.NewCommand(ctrl)
logger := mocks.NewLogger(ctrl)
tc.mockSvc(creator, logger, statusCmd)
got, err := lima.GetVMType(creator, logger, instanceName)
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.want, got)
})
}
}

0 comments on commit 9f4073b

Please sign in to comment.