From dca252dc1f96a3afc924809c14b8554b6eaea897 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Sun, 27 Nov 2022 21:13:29 +0100 Subject: [PATCH 01/19] Feature: Add vm status command Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/virtual_machine.go | 1 + cmd/virtual_machine_status.go | 68 +++++++++++++++++++++++++++++++++++ cmd/virtual_machine_test.go | 2 +- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 cmd/virtual_machine_status.go diff --git a/cmd/virtual_machine.go b/cmd/virtual_machine.go index fb8d4ebd3..adeb05594 100644 --- a/cmd/virtual_machine.go +++ b/cmd/virtual_machine.go @@ -40,6 +40,7 @@ func newVirtualMachineCommand( newStartVMCommand(limaCmdCreator, logger, optionalDepGroups, lca, nca, fs, fp.LimaSSHPrivateKeyPath()), newStopVMCommand(limaCmdCreator, logger), newRemoveVMCommand(limaCmdCreator, logger), + newStatusVMCommand(limaCmdCreator, logger), newInitVMCommand(limaCmdCreator, logger, optionalDepGroups, lca, nca, fp.BaseYamlFilePath(), fs, fp.LimaSSHPrivateKeyPath()), ) diff --git a/cmd/virtual_machine_status.go b/cmd/virtual_machine_status.go new file mode 100644 index 000000000..0b8e7704f --- /dev/null +++ b/cmd/virtual_machine_status.go @@ -0,0 +1,68 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "fmt" + + "github.com/runfinch/finch/pkg/command" + "github.com/runfinch/finch/pkg/flog" + "github.com/runfinch/finch/pkg/lima" + + "github.com/spf13/cobra" +) + +func newStatusVMCommand(limaCmdCreator command.LimaCmdCreator, logger flog.Logger) *cobra.Command { + statusVMCommand := &cobra.Command{ + Use: "status", + Short: "Status of the virtual machine", + RunE: newStatusVMAction(limaCmdCreator, logger).runAdapter, + } + + return statusVMCommand +} + +type statusVMAction struct { + creator command.LimaCmdCreator + logger flog.Logger +} + +func newStatusVMAction(creator command.LimaCmdCreator, logger flog.Logger) *statusVMAction { + return &statusVMAction{creator: creator, logger: logger} +} + +func (sva *statusVMAction) runAdapter(cmd *cobra.Command, args []string) error { + return sva.run() +} + +func (sva *statusVMAction) run() error { + err := sva.assertVMIsRunning(sva.creator, sva.logger) + if err != nil { + return err + } + + limaCmd := sva.creator.CreateWithoutStdio("ls", limaInstanceName) + logs, err := limaCmd.CombinedOutput() + if err != nil { + sva.logger.Errorf("Finch virtual machine status failed, debug logs: %s", logs) + return err + } + sva.logger.Infof("%s", logs) + return nil +} + +func (sva *statusVMAction) assertVMIsRunning(creator command.LimaCmdCreator, logger flog.Logger) error { + status, err := lima.GetVMStatus(creator, logger, limaInstanceName) + if err != nil { + return err + } + switch status { + case lima.Nonexistent: + return fmt.Errorf("the instance %q does not exist", limaInstanceName) + case lima.Stopped: + return fmt.Errorf("the instance %q is stopped. run `finch %s start` to start the instance", limaInstanceName, virtualMachineRootCmd) + default: + return nil + } +} diff --git a/cmd/virtual_machine_test.go b/cmd/virtual_machine_test.go index 0a8d9f38d..08485d88f 100644 --- a/cmd/virtual_machine_test.go +++ b/cmd/virtual_machine_test.go @@ -21,7 +21,7 @@ func TestVirtualMachineCommand(t *testing.T) { assert.Equal(t, cmd.Use, virtualMachineRootCmd) // check the number of subcommand for vm - assert.Equal(t, len(cmd.Commands()), 4) + assert.Equal(t, len(cmd.Commands()), 5) } func TestPostVMStartInitAction_runAdapter(t *testing.T) { From bce5bd6f286ef424e7c623db01b63a73663cc5ca Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Tue, 29 Nov 2022 20:53:41 +0100 Subject: [PATCH 02/19] Refactor: Update run to call internal status func Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/virtual_machine_status.go | 25 +++++++------------------ cmd/virtual_machine_status_test.go | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 cmd/virtual_machine_status_test.go diff --git a/cmd/virtual_machine_status.go b/cmd/virtual_machine_status.go index 0b8e7704f..b9feb7826 100644 --- a/cmd/virtual_machine_status.go +++ b/cmd/virtual_machine_status.go @@ -37,32 +37,21 @@ func (sva *statusVMAction) runAdapter(cmd *cobra.Command, args []string) error { } func (sva *statusVMAction) run() error { - err := sva.assertVMIsRunning(sva.creator, sva.logger) - if err != nil { - return err - } - - limaCmd := sva.creator.CreateWithoutStdio("ls", limaInstanceName) - logs, err := limaCmd.CombinedOutput() - if err != nil { - sva.logger.Errorf("Finch virtual machine status failed, debug logs: %s", logs) - return err - } - sva.logger.Infof("%s", logs) - return nil -} - -func (sva *statusVMAction) assertVMIsRunning(creator command.LimaCmdCreator, logger flog.Logger) error { - status, err := lima.GetVMStatus(creator, logger, limaInstanceName) + status, err := lima.GetVMStatus(sva.creator, sva.logger, limaInstanceName) if err != nil { return err } switch status { + case lima.Running: + sva.logger.Infof("the instance %q is running", limaInstanceName) + return nil case lima.Nonexistent: return fmt.Errorf("the instance %q does not exist", limaInstanceName) case lima.Stopped: return fmt.Errorf("the instance %q is stopped. run `finch %s start` to start the instance", limaInstanceName, virtualMachineRootCmd) + case lima.Unknown: + return fmt.Errorf("the instance status of %q is unknown", limaInstanceName) default: - return nil + return fmt.Errorf("the instance %q gave a not defined status", limaInstanceName) } } diff --git a/cmd/virtual_machine_status_test.go b/cmd/virtual_machine_status_test.go new file mode 100644 index 000000000..487d674ef --- /dev/null +++ b/cmd/virtual_machine_status_test.go @@ -0,0 +1,17 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewStatusVMCommand(t *testing.T) { + t.Parallel() + + cmd := newStatusVMCommand(nil, nil) + assert.Equal(t, cmd.Name(), "status") +} From 49bd274f71e0273b01834386301be23f0fa5a2f3 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Fri, 2 Dec 2022 16:46:23 +0100 Subject: [PATCH 03/19] Fix: Update to stdout prints Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/virtual_machine_status.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/virtual_machine_status.go b/cmd/virtual_machine_status.go index b9feb7826..fa13a9ba4 100644 --- a/cmd/virtual_machine_status.go +++ b/cmd/virtual_machine_status.go @@ -43,15 +43,19 @@ func (sva *statusVMAction) run() error { } switch status { case lima.Running: - sva.logger.Infof("the instance %q is running", limaInstanceName) + fmt.Println("Running") return nil case lima.Nonexistent: - return fmt.Errorf("the instance %q does not exist", limaInstanceName) + fmt.Println("Notexistent") + return nil case lima.Stopped: - return fmt.Errorf("the instance %q is stopped. run `finch %s start` to start the instance", limaInstanceName, virtualMachineRootCmd) + fmt.Println("Stopped") + return nil case lima.Unknown: - return fmt.Errorf("the instance status of %q is unknown", limaInstanceName) + fmt.Println("Unknown") + return nil default: - return fmt.Errorf("the instance %q gave a not defined status", limaInstanceName) + fmt.Println("Undefined") + return nil } } From 83c2ab4258127c826365ee332b3534b14c004bf7 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Sun, 27 Nov 2022 21:13:29 +0100 Subject: [PATCH 04/19] Feature: Add vm status command Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine.go | 1 + cmd/finch/virtual_machine_test.go | 2 +- cmd/virtual_machine_status.go | 68 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 cmd/virtual_machine_status.go diff --git a/cmd/finch/virtual_machine.go b/cmd/finch/virtual_machine.go index fb8d4ebd3..adeb05594 100644 --- a/cmd/finch/virtual_machine.go +++ b/cmd/finch/virtual_machine.go @@ -40,6 +40,7 @@ func newVirtualMachineCommand( newStartVMCommand(limaCmdCreator, logger, optionalDepGroups, lca, nca, fs, fp.LimaSSHPrivateKeyPath()), newStopVMCommand(limaCmdCreator, logger), newRemoveVMCommand(limaCmdCreator, logger), + newStatusVMCommand(limaCmdCreator, logger), newInitVMCommand(limaCmdCreator, logger, optionalDepGroups, lca, nca, fp.BaseYamlFilePath(), fs, fp.LimaSSHPrivateKeyPath()), ) diff --git a/cmd/finch/virtual_machine_test.go b/cmd/finch/virtual_machine_test.go index 0a8d9f38d..08485d88f 100644 --- a/cmd/finch/virtual_machine_test.go +++ b/cmd/finch/virtual_machine_test.go @@ -21,7 +21,7 @@ func TestVirtualMachineCommand(t *testing.T) { assert.Equal(t, cmd.Use, virtualMachineRootCmd) // check the number of subcommand for vm - assert.Equal(t, len(cmd.Commands()), 4) + assert.Equal(t, len(cmd.Commands()), 5) } func TestPostVMStartInitAction_runAdapter(t *testing.T) { diff --git a/cmd/virtual_machine_status.go b/cmd/virtual_machine_status.go new file mode 100644 index 000000000..0b8e7704f --- /dev/null +++ b/cmd/virtual_machine_status.go @@ -0,0 +1,68 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "fmt" + + "github.com/runfinch/finch/pkg/command" + "github.com/runfinch/finch/pkg/flog" + "github.com/runfinch/finch/pkg/lima" + + "github.com/spf13/cobra" +) + +func newStatusVMCommand(limaCmdCreator command.LimaCmdCreator, logger flog.Logger) *cobra.Command { + statusVMCommand := &cobra.Command{ + Use: "status", + Short: "Status of the virtual machine", + RunE: newStatusVMAction(limaCmdCreator, logger).runAdapter, + } + + return statusVMCommand +} + +type statusVMAction struct { + creator command.LimaCmdCreator + logger flog.Logger +} + +func newStatusVMAction(creator command.LimaCmdCreator, logger flog.Logger) *statusVMAction { + return &statusVMAction{creator: creator, logger: logger} +} + +func (sva *statusVMAction) runAdapter(cmd *cobra.Command, args []string) error { + return sva.run() +} + +func (sva *statusVMAction) run() error { + err := sva.assertVMIsRunning(sva.creator, sva.logger) + if err != nil { + return err + } + + limaCmd := sva.creator.CreateWithoutStdio("ls", limaInstanceName) + logs, err := limaCmd.CombinedOutput() + if err != nil { + sva.logger.Errorf("Finch virtual machine status failed, debug logs: %s", logs) + return err + } + sva.logger.Infof("%s", logs) + return nil +} + +func (sva *statusVMAction) assertVMIsRunning(creator command.LimaCmdCreator, logger flog.Logger) error { + status, err := lima.GetVMStatus(creator, logger, limaInstanceName) + if err != nil { + return err + } + switch status { + case lima.Nonexistent: + return fmt.Errorf("the instance %q does not exist", limaInstanceName) + case lima.Stopped: + return fmt.Errorf("the instance %q is stopped. run `finch %s start` to start the instance", limaInstanceName, virtualMachineRootCmd) + default: + return nil + } +} From 5f70bbc9b6fc4db42cea2cc0ab80322e2b04379f Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Tue, 29 Nov 2022 20:53:41 +0100 Subject: [PATCH 05/19] Refactor: Update run to call internal status func Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/virtual_machine_status.go | 25 +++++++------------------ cmd/virtual_machine_status_test.go | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 cmd/virtual_machine_status_test.go diff --git a/cmd/virtual_machine_status.go b/cmd/virtual_machine_status.go index 0b8e7704f..b9feb7826 100644 --- a/cmd/virtual_machine_status.go +++ b/cmd/virtual_machine_status.go @@ -37,32 +37,21 @@ func (sva *statusVMAction) runAdapter(cmd *cobra.Command, args []string) error { } func (sva *statusVMAction) run() error { - err := sva.assertVMIsRunning(sva.creator, sva.logger) - if err != nil { - return err - } - - limaCmd := sva.creator.CreateWithoutStdio("ls", limaInstanceName) - logs, err := limaCmd.CombinedOutput() - if err != nil { - sva.logger.Errorf("Finch virtual machine status failed, debug logs: %s", logs) - return err - } - sva.logger.Infof("%s", logs) - return nil -} - -func (sva *statusVMAction) assertVMIsRunning(creator command.LimaCmdCreator, logger flog.Logger) error { - status, err := lima.GetVMStatus(creator, logger, limaInstanceName) + status, err := lima.GetVMStatus(sva.creator, sva.logger, limaInstanceName) if err != nil { return err } switch status { + case lima.Running: + sva.logger.Infof("the instance %q is running", limaInstanceName) + return nil case lima.Nonexistent: return fmt.Errorf("the instance %q does not exist", limaInstanceName) case lima.Stopped: return fmt.Errorf("the instance %q is stopped. run `finch %s start` to start the instance", limaInstanceName, virtualMachineRootCmd) + case lima.Unknown: + return fmt.Errorf("the instance status of %q is unknown", limaInstanceName) default: - return nil + return fmt.Errorf("the instance %q gave a not defined status", limaInstanceName) } } diff --git a/cmd/virtual_machine_status_test.go b/cmd/virtual_machine_status_test.go new file mode 100644 index 000000000..487d674ef --- /dev/null +++ b/cmd/virtual_machine_status_test.go @@ -0,0 +1,17 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewStatusVMCommand(t *testing.T) { + t.Parallel() + + cmd := newStatusVMCommand(nil, nil) + assert.Equal(t, cmd.Name(), "status") +} From dd613a230fb049789c78d6a598f248a8642210bd Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Fri, 2 Dec 2022 16:46:23 +0100 Subject: [PATCH 06/19] Fix: Update to stdout prints Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/virtual_machine_status.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/virtual_machine_status.go b/cmd/virtual_machine_status.go index b9feb7826..fa13a9ba4 100644 --- a/cmd/virtual_machine_status.go +++ b/cmd/virtual_machine_status.go @@ -43,15 +43,19 @@ func (sva *statusVMAction) run() error { } switch status { case lima.Running: - sva.logger.Infof("the instance %q is running", limaInstanceName) + fmt.Println("Running") return nil case lima.Nonexistent: - return fmt.Errorf("the instance %q does not exist", limaInstanceName) + fmt.Println("Notexistent") + return nil case lima.Stopped: - return fmt.Errorf("the instance %q is stopped. run `finch %s start` to start the instance", limaInstanceName, virtualMachineRootCmd) + fmt.Println("Stopped") + return nil case lima.Unknown: - return fmt.Errorf("the instance status of %q is unknown", limaInstanceName) + fmt.Println("Unknown") + return nil default: - return fmt.Errorf("the instance %q gave a not defined status", limaInstanceName) + fmt.Println("Undefined") + return nil } } From 97024a62beb256a8a3b589fca51ffa6a711e09c9 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Fri, 2 Dec 2022 16:56:32 +0100 Subject: [PATCH 07/19] Move files to cmd/finch/ directory Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/{ => finch}/virtual_machine_status.go | 0 cmd/{ => finch}/virtual_machine_status_test.go | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename cmd/{ => finch}/virtual_machine_status.go (100%) rename cmd/{ => finch}/virtual_machine_status_test.go (100%) diff --git a/cmd/virtual_machine_status.go b/cmd/finch/virtual_machine_status.go similarity index 100% rename from cmd/virtual_machine_status.go rename to cmd/finch/virtual_machine_status.go diff --git a/cmd/virtual_machine_status_test.go b/cmd/finch/virtual_machine_status_test.go similarity index 100% rename from cmd/virtual_machine_status_test.go rename to cmd/finch/virtual_machine_status_test.go From d1cfc5f0a07e9cc5391aa03e3d1a01b78bb89ed1 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Sat, 3 Dec 2022 13:52:05 +0100 Subject: [PATCH 08/19] fix: apply pr feedback Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine_status.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/finch/virtual_machine_status.go b/cmd/finch/virtual_machine_status.go index fa13a9ba4..06f10bc23 100644 --- a/cmd/finch/virtual_machine_status.go +++ b/cmd/finch/virtual_machine_status.go @@ -52,10 +52,8 @@ func (sva *statusVMAction) run() error { fmt.Println("Stopped") return nil case lima.Unknown: - fmt.Println("Unknown") - return nil + return fmt.Errorf("instance state of %q is unknown", limaInstanceName) default: - fmt.Println("Undefined") - return nil + return fmt.Errorf("instance state of %q is undefined", limaInstanceName) } } From d9bcc5d7b42edd97618cedda53ea33b8803576f9 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Sat, 17 Dec 2022 18:37:20 +0100 Subject: [PATCH 09/19] fix: typo in nonexistent vm status log Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine_status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/finch/virtual_machine_status.go b/cmd/finch/virtual_machine_status.go index 06f10bc23..c13cd51c3 100644 --- a/cmd/finch/virtual_machine_status.go +++ b/cmd/finch/virtual_machine_status.go @@ -46,7 +46,7 @@ func (sva *statusVMAction) run() error { fmt.Println("Running") return nil case lima.Nonexistent: - fmt.Println("Notexistent") + fmt.Println("Nonexistent") return nil case lima.Stopped: fmt.Println("Stopped") From 13eeaf5aea689eb7d261173459602c6c4e899a9c Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Sat, 17 Dec 2022 18:38:02 +0100 Subject: [PATCH 10/19] tests: checkin current tests Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine_status_test.go | 109 +++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/cmd/finch/virtual_machine_status_test.go b/cmd/finch/virtual_machine_status_test.go index 487d674ef..6940d1808 100644 --- a/cmd/finch/virtual_machine_status_test.go +++ b/cmd/finch/virtual_machine_status_test.go @@ -4,8 +4,12 @@ package main import ( + "errors" "testing" + "github.com/golang/mock/gomock" + "github.com/runfinch/finch/pkg/dependency" + "github.com/runfinch/finch/pkg/mocks" "github.com/stretchr/testify/assert" ) @@ -15,3 +19,108 @@ func TestNewStatusVMCommand(t *testing.T) { cmd := newStatusVMCommand(nil, nil) assert.Equal(t, cmd.Name(), "status") } + +func TestStatusVMAction_run(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + wantErr error + groups func(*gomock.Controller) []*dependency.Group + mockSvc func( + *mocks.LimaCmdCreator, + *mocks.Logger, + *mocks.LimaConfigApplier, + *gomock.Controller, + ) + }{ + { + name: "running VM", + wantErr: nil, + groups: func(ctrl *gomock.Controller) []*dependency.Group { + return nil + }, + mockSvc: func( + lcc *mocks.LimaCmdCreator, + logger *mocks.Logger, + lca *mocks.LimaConfigApplier, + ctrl *gomock.Controller, + ) { + getVMStatusC := mocks.NewCommand(ctrl) + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) + getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil) + logger.EXPECT().Debugf("Status of virtual machine: %s", "Running") + }, + }, + { + name: "stopped VM", + wantErr: nil, + groups: func(ctrl *gomock.Controller) []*dependency.Group { + return nil + }, + mockSvc: func( + lcc *mocks.LimaCmdCreator, + logger *mocks.Logger, + lca *mocks.LimaConfigApplier, + ctrl *gomock.Controller, + ) { + getVMStatusC := mocks.NewCommand(ctrl) + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) + getVMStatusC.EXPECT().Output().Return([]byte("Stopped"), nil) + logger.EXPECT().Debugf("Status of virtual machine: %s", "Stopped") + }, + }, + { + name: "unknown VM status", + wantErr: errors.New("unrecognized system status"), + groups: func(ctrl *gomock.Controller) []*dependency.Group { + return nil + }, + mockSvc: func( + lcc *mocks.LimaCmdCreator, + logger *mocks.Logger, + lca *mocks.LimaConfigApplier, + ctrl *gomock.Controller, + ) { + getVMStatusC := mocks.NewCommand(ctrl) + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) + getVMStatusC.EXPECT().Output().Return([]byte("Broken"), nil) + logger.EXPECT().Debugf("Status of virtual machine: %s", "Broken") + }, + }, + { + name: "status command returns an error", + wantErr: errors.New("get status error"), + groups: func(ctrl *gomock.Controller) []*dependency.Group { + return nil + }, + mockSvc: func( + lcc *mocks.LimaCmdCreator, + logger *mocks.Logger, + lca *mocks.LimaConfigApplier, + ctrl *gomock.Controller, + ) { + getVMStatusC := mocks.NewCommand(ctrl) + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) + getVMStatusC.EXPECT().Output().Return([]byte("Broken"), errors.New("get status error")) + }, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + ctrl := gomock.NewController(t) + logger := mocks.NewLogger(ctrl) + lcc := mocks.NewLimaCmdCreator(ctrl) + lca := mocks.NewLimaConfigApplier(ctrl) + + tc.mockSvc(lcc, logger, lca, ctrl) + + err := newStatusVMAction(lcc, logger).run() + assert.Equal(t, err, tc.wantErr) + }) + } +} \ No newline at end of file From 453e2dc0b6d091b19b6fab2e6a19b0ba4e1fc267 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Fri, 23 Dec 2022 11:28:22 +0100 Subject: [PATCH 11/19] Tests: Add nonexistent and undefined test Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine_status_test.go | 40 ++++++++++++++++++++++-- pkg/lima/lima.go | 2 +- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/cmd/finch/virtual_machine_status_test.go b/cmd/finch/virtual_machine_status_test.go index 6940d1808..e60ec8394 100644 --- a/cmd/finch/virtual_machine_status_test.go +++ b/cmd/finch/virtual_machine_status_test.go @@ -53,7 +53,7 @@ func TestStatusVMAction_run(t *testing.T) { }, }, { - name: "stopped VM", + name: "stopped VM", wantErr: nil, groups: func(ctrl *gomock.Controller) []*dependency.Group { return nil @@ -70,6 +70,42 @@ func TestStatusVMAction_run(t *testing.T) { logger.EXPECT().Debugf("Status of virtual machine: %s", "Stopped") }, }, + { + name: "nonExistent VM", + wantErr: nil, + groups: func(ctrl *gomock.Controller) []*dependency.Group { + return nil + }, + mockSvc: func( + lcc *mocks.LimaCmdCreator, + logger *mocks.Logger, + lca *mocks.LimaConfigApplier, + ctrl *gomock.Controller, + ) { + getVMStatusC := mocks.NewCommand(ctrl) + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) + getVMStatusC.EXPECT().Output().Return([]byte("Nonexistent"), nil) + logger.EXPECT().Debugf("Status of virtual machine: %s", "Nonexistent") + }, + }, + { + name: "undefined VM", + wantErr: errors.New("unrecognized system status"), + groups: func(ctrl *gomock.Controller) []*dependency.Group { + return nil + }, + mockSvc: func( + lcc *mocks.LimaCmdCreator, + logger *mocks.Logger, + lca *mocks.LimaConfigApplier, + ctrl *gomock.Controller, + ) { + getVMStatusC := mocks.NewCommand(ctrl) + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) + getVMStatusC.EXPECT().Output().Return([]byte("Undefined"), nil) + logger.EXPECT().Debugf("Status of virtual machine: %s", "Undefined") + }, + }, { name: "unknown VM status", wantErr: errors.New("unrecognized system status"), @@ -123,4 +159,4 @@ func TestStatusVMAction_run(t *testing.T) { assert.Equal(t, err, tc.wantErr) }) } -} \ No newline at end of file +} diff --git a/pkg/lima/lima.go b/pkg/lima/lima.go index 73d248e4e..28c485914 100644 --- a/pkg/lima/lima.go +++ b/pkg/lima/lima.go @@ -40,7 +40,7 @@ func GetVMStatus(creator command.LimaCmdCreator, logger flog.Logger, instanceNam func toVMStatus(status string, logger flog.Logger) (VMStatus, error) { logger.Debugf("Status of virtual machine: %s", status) switch status { - case "": + case "", "Nonexistent": return Nonexistent, nil case "Running": return Running, nil From f01df3e17e83e32477b74e13589a5fb3dbf319f5 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Fri, 23 Dec 2022 12:29:09 +0100 Subject: [PATCH 12/19] tests: Add runAdapter Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine_status_test.go | 44 ++++++++++++++++++++---- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/cmd/finch/virtual_machine_status_test.go b/cmd/finch/virtual_machine_status_test.go index e60ec8394..ca87fb3d7 100644 --- a/cmd/finch/virtual_machine_status_test.go +++ b/cmd/finch/virtual_machine_status_test.go @@ -20,13 +20,13 @@ func TestNewStatusVMCommand(t *testing.T) { assert.Equal(t, cmd.Name(), "status") } -func TestStatusVMAction_run(t *testing.T) { +func TestStatusVMAction_runAdapter(t *testing.T) { t.Parallel() testCases := []struct { name string - wantErr error - groups func(*gomock.Controller) []*dependency.Group + command *cobra.Command + args []string mockSvc func( *mocks.LimaCmdCreator, *mocks.Logger, @@ -35,11 +35,41 @@ func TestStatusVMAction_run(t *testing.T) { ) }{ { - name: "running VM", - wantErr: nil, - groups: func(ctrl *gomock.Controller) []*dependency.Group { - return nil + name: "should get nonexistent vm status", + command: &cobra.Command{ + Use: "status", + }, + args: []string{}, + mockSvc: func( + lcc *mocks.LimaCmdCreator, + logger *mocks.Logger, + lca *mocks.LimaConfigApplier, + ctrl *gomock.Controller, + ) { + getVMStatusC := mocks.NewCommand(ctrl) + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) + getVMStatusC.EXPECT().Output().Return([]byte("Nonexistent"), nil) + logger.EXPECT().Debugf("Status of virtual machine: %s", "Nonexistent") }, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + ctrl := gomock.NewController(t) + logger := mocks.NewLogger(ctrl) + lcc := mocks.NewLimaCmdCreator(ctrl) + lca := mocks.NewLimaConfigApplier(ctrl) + + tc.mockSvc(lcc, logger, lca, ctrl) + + assert.NoError(t, newStatusVMAction(lcc, logger).runAdapter(tc.command, tc.args)) + }) + } +} mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, From 6ddd344862dbe947d12d5dd9875eb0a37f88c9cd Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Fri, 23 Dec 2022 12:30:56 +0100 Subject: [PATCH 13/19] test: remove unused group case field Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine_status_test.go | 48 +++++++++++++----------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/cmd/finch/virtual_machine_status_test.go b/cmd/finch/virtual_machine_status_test.go index ca87fb3d7..af94fa6ec 100644 --- a/cmd/finch/virtual_machine_status_test.go +++ b/cmd/finch/virtual_machine_status_test.go @@ -8,8 +8,8 @@ import ( "testing" "github.com/golang/mock/gomock" - "github.com/runfinch/finch/pkg/dependency" "github.com/runfinch/finch/pkg/mocks" + "github.com/spf13/cobra" "github.com/stretchr/testify/assert" ) @@ -51,7 +51,7 @@ func TestStatusVMAction_runAdapter(t *testing.T) { getVMStatusC.EXPECT().Output().Return([]byte("Nonexistent"), nil) logger.EXPECT().Debugf("Status of virtual machine: %s", "Nonexistent") }, - }, + }, } for _, tc := range testCases { @@ -70,6 +70,25 @@ func TestStatusVMAction_runAdapter(t *testing.T) { }) } } + +func TestStatusVMAction_run(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + wantErr error + statusOutput string + mockSvc func( + *mocks.LimaCmdCreator, + *mocks.Logger, + *mocks.LimaConfigApplier, + *gomock.Controller, + ) + }{ + { + name: "running VM", + wantErr: nil, + statusOutput: "Running", mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -83,11 +102,9 @@ func TestStatusVMAction_runAdapter(t *testing.T) { }, }, { - name: "stopped VM", - wantErr: nil, - groups: func(ctrl *gomock.Controller) []*dependency.Group { - return nil - }, + name: "stopped VM", + wantErr: nil, + statusOutput: "Stopped", mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -101,11 +118,9 @@ func TestStatusVMAction_runAdapter(t *testing.T) { }, }, { - name: "nonExistent VM", - wantErr: nil, - groups: func(ctrl *gomock.Controller) []*dependency.Group { - return nil - }, + name: "nonExistent VM", + wantErr: nil, + statusOutput: "Nonexistent", mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -121,9 +136,6 @@ func TestStatusVMAction_runAdapter(t *testing.T) { { name: "undefined VM", wantErr: errors.New("unrecognized system status"), - groups: func(ctrl *gomock.Controller) []*dependency.Group { - return nil - }, mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -139,9 +151,6 @@ func TestStatusVMAction_runAdapter(t *testing.T) { { name: "unknown VM status", wantErr: errors.New("unrecognized system status"), - groups: func(ctrl *gomock.Controller) []*dependency.Group { - return nil - }, mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -157,9 +166,6 @@ func TestStatusVMAction_runAdapter(t *testing.T) { { name: "status command returns an error", wantErr: errors.New("get status error"), - groups: func(ctrl *gomock.Controller) []*dependency.Group { - return nil - }, mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, From 47e8f8fba0a13f8d03f51937b77af64d6601fea6 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Mon, 2 Jan 2023 19:20:00 +0100 Subject: [PATCH 14/19] refactor: remove unnecessary nonexistent case Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- pkg/lima/lima.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/lima/lima.go b/pkg/lima/lima.go index 28c485914..73d248e4e 100644 --- a/pkg/lima/lima.go +++ b/pkg/lima/lima.go @@ -40,7 +40,7 @@ func GetVMStatus(creator command.LimaCmdCreator, logger flog.Logger, instanceNam func toVMStatus(status string, logger flog.Logger) (VMStatus, error) { logger.Debugf("Status of virtual machine: %s", status) switch status { - case "", "Nonexistent": + case "": return Nonexistent, nil case "Running": return Running, nil From 58ef76b1b38212645248f46dc0f604bd22fa230b Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Mon, 2 Jan 2023 19:21:25 +0100 Subject: [PATCH 15/19] tests: adjust nonexistent cases Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine_status.go | 4 +--- cmd/finch/virtual_machine_status_test.go | 10 +++++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/cmd/finch/virtual_machine_status.go b/cmd/finch/virtual_machine_status.go index c13cd51c3..4098adc3b 100644 --- a/cmd/finch/virtual_machine_status.go +++ b/cmd/finch/virtual_machine_status.go @@ -51,9 +51,7 @@ func (sva *statusVMAction) run() error { case lima.Stopped: fmt.Println("Stopped") return nil - case lima.Unknown: - return fmt.Errorf("instance state of %q is unknown", limaInstanceName) default: - return fmt.Errorf("instance state of %q is undefined", limaInstanceName) + return fmt.Errorf("instance state of %q is unknown", limaInstanceName) } } diff --git a/cmd/finch/virtual_machine_status_test.go b/cmd/finch/virtual_machine_status_test.go index af94fa6ec..d668a0cfd 100644 --- a/cmd/finch/virtual_machine_status_test.go +++ b/cmd/finch/virtual_machine_status_test.go @@ -48,8 +48,8 @@ func TestStatusVMAction_runAdapter(t *testing.T) { ) { getVMStatusC := mocks.NewCommand(ctrl) lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) - getVMStatusC.EXPECT().Output().Return([]byte("Nonexistent"), nil) - logger.EXPECT().Debugf("Status of virtual machine: %s", "Nonexistent") + getVMStatusC.EXPECT().Output().Return([]byte(""), nil) + logger.EXPECT().Debugf("Status of virtual machine: %s", "") }, }, } @@ -120,7 +120,7 @@ func TestStatusVMAction_run(t *testing.T) { { name: "nonExistent VM", wantErr: nil, - statusOutput: "Nonexistent", + statusOutput: "", mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -129,8 +129,8 @@ func TestStatusVMAction_run(t *testing.T) { ) { getVMStatusC := mocks.NewCommand(ctrl) lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) - getVMStatusC.EXPECT().Output().Return([]byte("Nonexistent"), nil) - logger.EXPECT().Debugf("Status of virtual machine: %s", "Nonexistent") + getVMStatusC.EXPECT().Output().Return([]byte(""), nil) + logger.EXPECT().Debugf("Status of virtual machine: %s", "") }, }, { From 39e4b107bcef43307ce3a3f14bb3cfc7b950a750 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Wed, 11 Jan 2023 20:29:55 +0100 Subject: [PATCH 16/19] fix: dependency injection to get stdout output Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine.go | 3 ++- cmd/finch/virtual_machine_status.go | 16 +++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/cmd/finch/virtual_machine.go b/cmd/finch/virtual_machine.go index adeb05594..ef280dee3 100644 --- a/cmd/finch/virtual_machine.go +++ b/cmd/finch/virtual_machine.go @@ -5,6 +5,7 @@ package main import ( "fmt" + "os" "strings" "github.com/runfinch/finch/pkg/command" @@ -40,7 +41,7 @@ func newVirtualMachineCommand( newStartVMCommand(limaCmdCreator, logger, optionalDepGroups, lca, nca, fs, fp.LimaSSHPrivateKeyPath()), newStopVMCommand(limaCmdCreator, logger), newRemoveVMCommand(limaCmdCreator, logger), - newStatusVMCommand(limaCmdCreator, logger), + newStatusVMCommand(limaCmdCreator, logger, os.Stdout), newInitVMCommand(limaCmdCreator, logger, optionalDepGroups, lca, nca, fp.BaseYamlFilePath(), fs, fp.LimaSSHPrivateKeyPath()), ) diff --git a/cmd/finch/virtual_machine_status.go b/cmd/finch/virtual_machine_status.go index 4098adc3b..93818b611 100644 --- a/cmd/finch/virtual_machine_status.go +++ b/cmd/finch/virtual_machine_status.go @@ -5,6 +5,7 @@ package main import ( "fmt" + "io" "github.com/runfinch/finch/pkg/command" "github.com/runfinch/finch/pkg/flog" @@ -13,11 +14,11 @@ import ( "github.com/spf13/cobra" ) -func newStatusVMCommand(limaCmdCreator command.LimaCmdCreator, logger flog.Logger) *cobra.Command { +func newStatusVMCommand(limaCmdCreator command.LimaCmdCreator, logger flog.Logger, stdout io.Writer) *cobra.Command { statusVMCommand := &cobra.Command{ Use: "status", Short: "Status of the virtual machine", - RunE: newStatusVMAction(limaCmdCreator, logger).runAdapter, + RunE: newStatusVMAction(limaCmdCreator, logger, stdout).runAdapter, } return statusVMCommand @@ -26,10 +27,11 @@ func newStatusVMCommand(limaCmdCreator command.LimaCmdCreator, logger flog.Logge type statusVMAction struct { creator command.LimaCmdCreator logger flog.Logger + stdout io.Writer } -func newStatusVMAction(creator command.LimaCmdCreator, logger flog.Logger) *statusVMAction { - return &statusVMAction{creator: creator, logger: logger} +func newStatusVMAction(creator command.LimaCmdCreator, logger flog.Logger, stdout io.Writer) *statusVMAction { + return &statusVMAction{creator: creator, logger: logger, stdout: stdout} } func (sva *statusVMAction) runAdapter(cmd *cobra.Command, args []string) error { @@ -43,13 +45,13 @@ func (sva *statusVMAction) run() error { } switch status { case lima.Running: - fmt.Println("Running") + fmt.Fprintln(sva.stdout, "Running") return nil case lima.Nonexistent: - fmt.Println("Nonexistent") + fmt.Fprintln(sva.stdout, "Nonexistent") return nil case lima.Stopped: - fmt.Println("Stopped") + fmt.Fprintln(sva.stdout, "Stopped") return nil default: return fmt.Errorf("instance state of %q is unknown", limaInstanceName) From 2f687006d348f6e35521096f1e9b3b9b96edbe85 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Wed, 11 Jan 2023 20:30:38 +0100 Subject: [PATCH 17/19] tests: add status output tests Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine_status_test.go | 53 ++++++++++++++---------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/cmd/finch/virtual_machine_status_test.go b/cmd/finch/virtual_machine_status_test.go index d668a0cfd..34cd1e31c 100644 --- a/cmd/finch/virtual_machine_status_test.go +++ b/cmd/finch/virtual_machine_status_test.go @@ -4,6 +4,7 @@ package main import ( + "bytes" "errors" "testing" @@ -16,7 +17,7 @@ import ( func TestNewStatusVMCommand(t *testing.T) { t.Parallel() - cmd := newStatusVMCommand(nil, nil) + cmd := newStatusVMCommand(nil, nil, nil) assert.Equal(t, cmd.Name(), "status") } @@ -61,24 +62,25 @@ func TestStatusVMAction_runAdapter(t *testing.T) { ctrl := gomock.NewController(t) logger := mocks.NewLogger(ctrl) + stdout := bytes.Buffer{} lcc := mocks.NewLimaCmdCreator(ctrl) lca := mocks.NewLimaConfigApplier(ctrl) tc.mockSvc(lcc, logger, lca, ctrl) - assert.NoError(t, newStatusVMAction(lcc, logger).runAdapter(tc.command, tc.args)) + assert.NoError(t, newStatusVMAction(lcc, logger, &stdout).runAdapter(tc.command, tc.args)) }) } } func TestStatusVMAction_run(t *testing.T) { - t.Parallel() + // t.Parallel() testCases := []struct { - name string - wantErr error - statusOutput string - mockSvc func( + name string + wantErr error + wantStatusOutput string + mockSvc func( *mocks.LimaCmdCreator, *mocks.Logger, *mocks.LimaConfigApplier, @@ -86,9 +88,9 @@ func TestStatusVMAction_run(t *testing.T) { ) }{ { - name: "running VM", - wantErr: nil, - statusOutput: "Running", + name: "running VM", + wantErr: nil, + wantStatusOutput: "Running\n", mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -102,9 +104,9 @@ func TestStatusVMAction_run(t *testing.T) { }, }, { - name: "stopped VM", - wantErr: nil, - statusOutput: "Stopped", + name: "stopped VM", + wantErr: nil, + wantStatusOutput: "Stopped\n", mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -118,9 +120,9 @@ func TestStatusVMAction_run(t *testing.T) { }, }, { - name: "nonExistent VM", - wantErr: nil, - statusOutput: "", + name: "nonExistent VM", + wantErr: nil, + wantStatusOutput: "Nonexistent\n", mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -134,8 +136,9 @@ func TestStatusVMAction_run(t *testing.T) { }, }, { - name: "undefined VM", - wantErr: errors.New("unrecognized system status"), + name: "undefined VM", + wantErr: errors.New("unrecognized system status"), + wantStatusOutput: "", mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -149,8 +152,9 @@ func TestStatusVMAction_run(t *testing.T) { }, }, { - name: "unknown VM status", - wantErr: errors.New("unrecognized system status"), + name: "unknown VM status", + wantErr: errors.New("unrecognized system status"), + wantStatusOutput: "", mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -164,8 +168,9 @@ func TestStatusVMAction_run(t *testing.T) { }, }, { - name: "status command returns an error", - wantErr: errors.New("get status error"), + name: "status command returns an error", + wantErr: errors.New("get status error"), + wantStatusOutput: "", mockSvc: func( lcc *mocks.LimaCmdCreator, logger *mocks.Logger, @@ -186,13 +191,15 @@ func TestStatusVMAction_run(t *testing.T) { ctrl := gomock.NewController(t) logger := mocks.NewLogger(ctrl) + stdout := bytes.Buffer{} lcc := mocks.NewLimaCmdCreator(ctrl) lca := mocks.NewLimaConfigApplier(ctrl) tc.mockSvc(lcc, logger, lca, ctrl) - err := newStatusVMAction(lcc, logger).run() + err := newStatusVMAction(lcc, logger, &stdout).run() assert.Equal(t, err, tc.wantErr) + assert.Equal(t, tc.wantStatusOutput, stdout.String()) }) } } From 406e760d33418bf09e2f8ce4b2519376c18e4943 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Sun, 15 Jan 2023 16:20:22 +0100 Subject: [PATCH 18/19] tests: add e2e tests Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine_status_test.go | 5 +++-- e2e/virtual_machine_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cmd/finch/virtual_machine_status_test.go b/cmd/finch/virtual_machine_status_test.go index 34cd1e31c..f1eff42ac 100644 --- a/cmd/finch/virtual_machine_status_test.go +++ b/cmd/finch/virtual_machine_status_test.go @@ -9,9 +9,10 @@ import ( "testing" "github.com/golang/mock/gomock" - "github.com/runfinch/finch/pkg/mocks" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" + + "github.com/runfinch/finch/pkg/mocks" ) func TestNewStatusVMCommand(t *testing.T) { @@ -74,7 +75,7 @@ func TestStatusVMAction_runAdapter(t *testing.T) { } func TestStatusVMAction_run(t *testing.T) { - // t.Parallel() + t.Parallel() testCases := []struct { name string diff --git a/e2e/virtual_machine_test.go b/e2e/virtual_machine_test.go index efb02bf82..b57bee569 100644 --- a/e2e/virtual_machine_test.go +++ b/e2e/virtual_machine_test.go @@ -5,6 +5,7 @@ package e2e import ( "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" "github.com/runfinch/common-tests/command" "github.com/runfinch/common-tests/option" ) @@ -18,6 +19,10 @@ var testVirtualMachine = func(o *option.Option) { command.New(o, virtualMachineRootCmd, "remove").WithoutSuccessfulExit().Run() }) + ginkgo.It("should get running status", func() { + gomega.Expect(command.StdoutStr(o, virtualMachineRootCmd, "status")).To(gomega.Equal("Running")) + }) + ginkgo.It("should be able to stop the virtual machine", func() { command.Run(o, "images") command.New(o, virtualMachineRootCmd, "stop").WithTimeoutInSeconds(60).Run() @@ -31,6 +36,10 @@ var testVirtualMachine = func(o *option.Option) { command.New(o, virtualMachineRootCmd, "init").WithoutSuccessfulExit().Run() }) + ginkgo.It("should get stopped status", func() { + gomega.Expect(command.StdoutStr(o, virtualMachineRootCmd, "status")).To(gomega.Equal("Stopped")) + }) + ginkgo.It("should be able to start the virtual machine", func() { command.New(o, virtualMachineRootCmd, "start").WithTimeoutInSeconds(120).Run() command.Run(o, "images") @@ -49,6 +58,10 @@ var testVirtualMachine = func(o *option.Option) { command.New(o, virtualMachineRootCmd, "stop").WithoutSuccessfulExit().Run() }) + ginkgo.It("should get nonexistent status", func() { + gomega.Expect(command.StdoutStr(o, virtualMachineRootCmd, "status")).To(gomega.Equal("Nonexistent")) + }) + ginkgo.It("should be able to init", func() { command.New(o, virtualMachineRootCmd, "init").WithTimeoutInSeconds(600).Run() command.Run(o, "images") From b2b5b722eaa921b1dd07d8fa3728bf8f6afbe7a3 Mon Sep 17 00:00:00 2001 From: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> Date: Tue, 17 Jan 2023 08:16:58 +0100 Subject: [PATCH 19/19] tests: remove already covered case Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com> --- cmd/finch/virtual_machine_status_test.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/cmd/finch/virtual_machine_status_test.go b/cmd/finch/virtual_machine_status_test.go index f1eff42ac..9c62afdbc 100644 --- a/cmd/finch/virtual_machine_status_test.go +++ b/cmd/finch/virtual_machine_status_test.go @@ -136,22 +136,6 @@ func TestStatusVMAction_run(t *testing.T) { logger.EXPECT().Debugf("Status of virtual machine: %s", "") }, }, - { - name: "undefined VM", - wantErr: errors.New("unrecognized system status"), - wantStatusOutput: "", - mockSvc: func( - lcc *mocks.LimaCmdCreator, - logger *mocks.Logger, - lca *mocks.LimaConfigApplier, - ctrl *gomock.Controller, - ) { - getVMStatusC := mocks.NewCommand(ctrl) - lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) - getVMStatusC.EXPECT().Output().Return([]byte("Undefined"), nil) - logger.EXPECT().Debugf("Status of virtual machine: %s", "Undefined") - }, - }, { name: "unknown VM status", wantErr: errors.New("unrecognized system status"),