Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add finch vm status command #83

Merged
merged 25 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
dca252d
Feature: Add vm status command
niklasmtj Nov 27, 2022
bce5bd6
Refactor: Update run to call internal status func
niklasmtj Nov 29, 2022
936c3ae
Merge branch 'runfinch:main' into feature/vm-status-command
niklasmtj Nov 29, 2022
c12ffe3
Merge branch 'feature/vm-status-command' of github.com:niklasmtj/finc…
niklasmtj Nov 29, 2022
49bd274
Fix: Update to stdout prints
niklasmtj Dec 2, 2022
83c2ab4
Feature: Add vm status command
niklasmtj Nov 27, 2022
5f70bbc
Refactor: Update run to call internal status func
niklasmtj Nov 29, 2022
dd613a2
Fix: Update to stdout prints
niklasmtj Dec 2, 2022
a023908
Merge branch 'feature/vm-status-command' of github.com:niklasmtj/finc…
niklasmtj Dec 2, 2022
97024a6
Move files to cmd/finch/ directory
niklasmtj Dec 2, 2022
d1cfc5f
fix: apply pr feedback
niklasmtj Dec 3, 2022
d9bcc5d
fix: typo in nonexistent vm status log
niklasmtj Dec 17, 2022
13eeaf5
tests: checkin current tests
niklasmtj Dec 17, 2022
453e2dc
Tests: Add nonexistent and undefined test
niklasmtj Dec 23, 2022
f01df3e
tests: Add runAdapter
niklasmtj Dec 23, 2022
e8ba36e
Merge branch 'main' into feature/vm-status-command
niklasmtj Dec 23, 2022
6ddd344
test: remove unused group case field
niklasmtj Dec 23, 2022
6cc9db5
Merge branch 'feature/vm-status-command' of github.com:niklasmtj/finc…
niklasmtj Dec 23, 2022
47e8f8f
refactor: remove unnecessary nonexistent case
niklasmtj Jan 2, 2023
58ef76b
tests: adjust nonexistent cases
niklasmtj Jan 2, 2023
39e4b10
fix: dependency injection to get stdout output
niklasmtj Jan 11, 2023
2f68700
tests: add status output tests
niklasmtj Jan 11, 2023
406e760
tests: add e2e tests
niklasmtj Jan 15, 2023
2965f7c
Merge branch 'main' into feature/vm-status-command
niklasmtj Jan 15, 2023
b2b5b72
tests: remove already covered case
niklasmtj Jan 17, 2023
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
1 change: 1 addition & 0 deletions cmd/finch/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
)

Expand Down
59 changes: 59 additions & 0 deletions cmd/finch/virtual_machine_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 {
status, err := lima.GetVMStatus(sva.creator, sva.logger, limaInstanceName)
if err != nil {
return err
}
switch status {
case lima.Running:
fmt.Println("Running")
return nil
case lima.Nonexistent:
fmt.Println("Nonexistent")
return nil
case lima.Stopped:
fmt.Println("Stopped")
return nil
case lima.Unknown:
niklasmtj marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("instance state of %q is unknown", limaInstanceName)
default:
return fmt.Errorf("instance state of %q is undefined", limaInstanceName)
}
}
126 changes: 126 additions & 0 deletions cmd/finch/virtual_machine_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

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"
)

func TestNewStatusVMCommand(t *testing.T) {
ningziwen marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

cmd := newStatusVMCommand(nil, nil)
assert.Equal(t, cmd.Name(), "status")
}

ningziwen marked this conversation as resolved.
Show resolved Hide resolved
func TestStatusVMAction_run(t *testing.T) {
niklasmtj marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

testCases := []struct {
ningziwen marked this conversation as resolved.
Show resolved Hide resolved
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)
})
ningziwen marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion cmd/finch/virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down