|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "errors" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/golang/mock/gomock" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + |
| 15 | + "github.com/runfinch/finch/pkg/mocks" |
| 16 | +) |
| 17 | + |
| 18 | +func TestNewStatusVMCommand(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + cmd := newStatusVMCommand(nil, nil, nil) |
| 22 | + assert.Equal(t, cmd.Name(), "status") |
| 23 | +} |
| 24 | + |
| 25 | +func TestStatusVMAction_runAdapter(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + |
| 28 | + testCases := []struct { |
| 29 | + name string |
| 30 | + command *cobra.Command |
| 31 | + args []string |
| 32 | + mockSvc func( |
| 33 | + *mocks.LimaCmdCreator, |
| 34 | + *mocks.Logger, |
| 35 | + *mocks.LimaConfigApplier, |
| 36 | + *gomock.Controller, |
| 37 | + ) |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "should get nonexistent vm status", |
| 41 | + command: &cobra.Command{ |
| 42 | + Use: "status", |
| 43 | + }, |
| 44 | + args: []string{}, |
| 45 | + mockSvc: func( |
| 46 | + lcc *mocks.LimaCmdCreator, |
| 47 | + logger *mocks.Logger, |
| 48 | + lca *mocks.LimaConfigApplier, |
| 49 | + ctrl *gomock.Controller, |
| 50 | + ) { |
| 51 | + getVMStatusC := mocks.NewCommand(ctrl) |
| 52 | + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) |
| 53 | + getVMStatusC.EXPECT().Output().Return([]byte(""), nil) |
| 54 | + logger.EXPECT().Debugf("Status of virtual machine: %s", "") |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, tc := range testCases { |
| 60 | + tc := tc |
| 61 | + t.Run(tc.name, func(t *testing.T) { |
| 62 | + t.Parallel() |
| 63 | + |
| 64 | + ctrl := gomock.NewController(t) |
| 65 | + logger := mocks.NewLogger(ctrl) |
| 66 | + stdout := bytes.Buffer{} |
| 67 | + lcc := mocks.NewLimaCmdCreator(ctrl) |
| 68 | + lca := mocks.NewLimaConfigApplier(ctrl) |
| 69 | + |
| 70 | + tc.mockSvc(lcc, logger, lca, ctrl) |
| 71 | + |
| 72 | + assert.NoError(t, newStatusVMAction(lcc, logger, &stdout).runAdapter(tc.command, tc.args)) |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestStatusVMAction_run(t *testing.T) { |
| 78 | + t.Parallel() |
| 79 | + |
| 80 | + testCases := []struct { |
| 81 | + name string |
| 82 | + wantErr error |
| 83 | + wantStatusOutput string |
| 84 | + mockSvc func( |
| 85 | + *mocks.LimaCmdCreator, |
| 86 | + *mocks.Logger, |
| 87 | + *mocks.LimaConfigApplier, |
| 88 | + *gomock.Controller, |
| 89 | + ) |
| 90 | + }{ |
| 91 | + { |
| 92 | + name: "running VM", |
| 93 | + wantErr: nil, |
| 94 | + wantStatusOutput: "Running\n", |
| 95 | + mockSvc: func( |
| 96 | + lcc *mocks.LimaCmdCreator, |
| 97 | + logger *mocks.Logger, |
| 98 | + lca *mocks.LimaConfigApplier, |
| 99 | + ctrl *gomock.Controller, |
| 100 | + ) { |
| 101 | + getVMStatusC := mocks.NewCommand(ctrl) |
| 102 | + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) |
| 103 | + getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil) |
| 104 | + logger.EXPECT().Debugf("Status of virtual machine: %s", "Running") |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "stopped VM", |
| 109 | + wantErr: nil, |
| 110 | + wantStatusOutput: "Stopped\n", |
| 111 | + mockSvc: func( |
| 112 | + lcc *mocks.LimaCmdCreator, |
| 113 | + logger *mocks.Logger, |
| 114 | + lca *mocks.LimaConfigApplier, |
| 115 | + ctrl *gomock.Controller, |
| 116 | + ) { |
| 117 | + getVMStatusC := mocks.NewCommand(ctrl) |
| 118 | + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) |
| 119 | + getVMStatusC.EXPECT().Output().Return([]byte("Stopped"), nil) |
| 120 | + logger.EXPECT().Debugf("Status of virtual machine: %s", "Stopped") |
| 121 | + }, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "nonExistent VM", |
| 125 | + wantErr: nil, |
| 126 | + wantStatusOutput: "Nonexistent\n", |
| 127 | + mockSvc: func( |
| 128 | + lcc *mocks.LimaCmdCreator, |
| 129 | + logger *mocks.Logger, |
| 130 | + lca *mocks.LimaConfigApplier, |
| 131 | + ctrl *gomock.Controller, |
| 132 | + ) { |
| 133 | + getVMStatusC := mocks.NewCommand(ctrl) |
| 134 | + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) |
| 135 | + getVMStatusC.EXPECT().Output().Return([]byte(""), nil) |
| 136 | + logger.EXPECT().Debugf("Status of virtual machine: %s", "") |
| 137 | + }, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "unknown VM status", |
| 141 | + wantErr: errors.New("unrecognized system status"), |
| 142 | + wantStatusOutput: "", |
| 143 | + mockSvc: func( |
| 144 | + lcc *mocks.LimaCmdCreator, |
| 145 | + logger *mocks.Logger, |
| 146 | + lca *mocks.LimaConfigApplier, |
| 147 | + ctrl *gomock.Controller, |
| 148 | + ) { |
| 149 | + getVMStatusC := mocks.NewCommand(ctrl) |
| 150 | + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) |
| 151 | + getVMStatusC.EXPECT().Output().Return([]byte("Broken"), nil) |
| 152 | + logger.EXPECT().Debugf("Status of virtual machine: %s", "Broken") |
| 153 | + }, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "status command returns an error", |
| 157 | + wantErr: errors.New("get status error"), |
| 158 | + wantStatusOutput: "", |
| 159 | + mockSvc: func( |
| 160 | + lcc *mocks.LimaCmdCreator, |
| 161 | + logger *mocks.Logger, |
| 162 | + lca *mocks.LimaConfigApplier, |
| 163 | + ctrl *gomock.Controller, |
| 164 | + ) { |
| 165 | + getVMStatusC := mocks.NewCommand(ctrl) |
| 166 | + lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC) |
| 167 | + getVMStatusC.EXPECT().Output().Return([]byte("Broken"), errors.New("get status error")) |
| 168 | + }, |
| 169 | + }, |
| 170 | + } |
| 171 | + |
| 172 | + for _, tc := range testCases { |
| 173 | + tc := tc |
| 174 | + t.Run(tc.name, func(t *testing.T) { |
| 175 | + t.Parallel() |
| 176 | + |
| 177 | + ctrl := gomock.NewController(t) |
| 178 | + logger := mocks.NewLogger(ctrl) |
| 179 | + stdout := bytes.Buffer{} |
| 180 | + lcc := mocks.NewLimaCmdCreator(ctrl) |
| 181 | + lca := mocks.NewLimaConfigApplier(ctrl) |
| 182 | + |
| 183 | + tc.mockSvc(lcc, logger, lca, ctrl) |
| 184 | + |
| 185 | + err := newStatusVMAction(lcc, logger, &stdout).run() |
| 186 | + assert.Equal(t, err, tc.wantErr) |
| 187 | + assert.Equal(t, tc.wantStatusOutput, stdout.String()) |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
0 commit comments