|
| 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/afero" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + |
| 17 | + "github.com/runfinch/finch/pkg/config" |
| 18 | + "github.com/runfinch/finch/pkg/mocks" |
| 19 | +) |
| 20 | + |
| 21 | +func TestNewSettingsMCommand(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + |
| 24 | + cmd := newSettingsVMCommand(nil, nil, nil, nil) |
| 25 | + assert.Equal(t, cmd.Name(), "settings") |
| 26 | +} |
| 27 | + |
| 28 | +func TestSettingsVMAction_runAdapter(t *testing.T) { |
| 29 | + t.Parallel() |
| 30 | + |
| 31 | + testCases := []struct { |
| 32 | + name string |
| 33 | + wantErr error |
| 34 | + command *cobra.Command |
| 35 | + args []string |
| 36 | + mockSvc func( |
| 37 | + *mocks.LimaConfigApplier, |
| 38 | + afero.Fs, |
| 39 | + ) |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "should configure the instance for valid CPU and memory values", |
| 43 | + wantErr: nil, |
| 44 | + command: &cobra.Command{ |
| 45 | + Use: "settings", |
| 46 | + }, |
| 47 | + args: []string{ |
| 48 | + "--cpus=1", |
| 49 | + "--memory=2GiB", |
| 50 | + }, |
| 51 | + mockSvc: func( |
| 52 | + lca *mocks.LimaConfigApplier, |
| 53 | + fs afero.Fs, |
| 54 | + ) { |
| 55 | + finchConfigPath := "/config.yaml" |
| 56 | + data := "cpus: 2\nmemory: 6GiB" |
| 57 | + require.NoError(t, afero.WriteFile(fs, finchConfigPath, []byte(data), 0o600)) |
| 58 | + |
| 59 | + lca.EXPECT().GetFinchConfigPath().Return(finchConfigPath) |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "should configure the instance for valid CPU value", |
| 64 | + wantErr: nil, |
| 65 | + command: &cobra.Command{ |
| 66 | + Use: "settings", |
| 67 | + }, |
| 68 | + args: []string{ |
| 69 | + "--cpus=1", |
| 70 | + }, |
| 71 | + mockSvc: func( |
| 72 | + lca *mocks.LimaConfigApplier, |
| 73 | + fs afero.Fs, |
| 74 | + ) { |
| 75 | + finchConfigPath := "/config.yaml" |
| 76 | + data := "cpus: 2\nmemory: 6GiB" |
| 77 | + require.NoError(t, afero.WriteFile(fs, finchConfigPath, []byte(data), 0o600)) |
| 78 | + |
| 79 | + lca.EXPECT().GetFinchConfigPath().Return(finchConfigPath) |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "should configure the instance for valid memory value", |
| 84 | + wantErr: nil, |
| 85 | + command: &cobra.Command{ |
| 86 | + Use: "settings", |
| 87 | + }, |
| 88 | + args: []string{ |
| 89 | + "--memory=2GiB", |
| 90 | + }, |
| 91 | + mockSvc: func( |
| 92 | + lca *mocks.LimaConfigApplier, |
| 93 | + fs afero.Fs, |
| 94 | + ) { |
| 95 | + finchConfigPath := "/config.yaml" |
| 96 | + data := "cpus: 2\nmemory: 6GiB" |
| 97 | + require.NoError(t, afero.WriteFile(fs, finchConfigPath, []byte(data), 0o600)) |
| 98 | + |
| 99 | + lca.EXPECT().GetFinchConfigPath().Return(finchConfigPath) |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + for _, tc := range testCases { |
| 105 | + tc := tc |
| 106 | + t.Run(tc.name, func(t *testing.T) { |
| 107 | + t.Parallel() |
| 108 | + |
| 109 | + ctrl := gomock.NewController(t) |
| 110 | + logger := mocks.NewLogger(ctrl) |
| 111 | + lca := mocks.NewLimaConfigApplier(ctrl) |
| 112 | + fs := afero.NewMemMapFs() |
| 113 | + stdout := bytes.Buffer{} |
| 114 | + |
| 115 | + tc.mockSvc(lca, fs) |
| 116 | + |
| 117 | + cmd := newSettingsVMCommand(logger, lca, fs, &stdout) |
| 118 | + cmd.SetArgs(tc.args) |
| 119 | + err := cmd.Execute() |
| 120 | + assert.Equal(t, err, tc.wantErr) |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestSettingsVMAction_run(t *testing.T) { |
| 126 | + t.Parallel() |
| 127 | + |
| 128 | + testCases := []struct { |
| 129 | + name string |
| 130 | + wantErr error |
| 131 | + wantStatusOutput string |
| 132 | + mockSvc func( |
| 133 | + *mocks.LimaConfigApplier, |
| 134 | + afero.Fs, |
| 135 | + ) |
| 136 | + cpus int |
| 137 | + memory string |
| 138 | + }{ |
| 139 | + { |
| 140 | + name: "should update vm settings", |
| 141 | + wantErr: nil, |
| 142 | + wantStatusOutput: "Configurations have been successfully updated.\n", |
| 143 | + mockSvc: func( |
| 144 | + lca *mocks.LimaConfigApplier, |
| 145 | + fs afero.Fs, |
| 146 | + ) { |
| 147 | + finchConfigPath := "/config.yaml" |
| 148 | + data := "cpus: 2\nmemory: 6GiB" |
| 149 | + require.NoError(t, afero.WriteFile(fs, finchConfigPath, []byte(data), 0o600)) |
| 150 | + |
| 151 | + lca.EXPECT().GetFinchConfigPath().Return(finchConfigPath) |
| 152 | + }, |
| 153 | + cpus: 1, |
| 154 | + memory: "2GiB", |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "should return an error if the configuration of CPU or memory is invalid", |
| 158 | + wantErr: errors.New("the number of CPUs or the amount of memory should be at least one valid value"), |
| 159 | + wantStatusOutput: "", |
| 160 | + mockSvc: func( |
| 161 | + lca *mocks.LimaConfigApplier, |
| 162 | + fs afero.Fs, |
| 163 | + ) { |
| 164 | + finchConfigPath := "/config.yaml" |
| 165 | + data := "cpus: 2\nmemory: 6GiB" |
| 166 | + require.NoError(t, afero.WriteFile(fs, finchConfigPath, []byte(data), 0o600)) |
| 167 | + |
| 168 | + lca.EXPECT().GetFinchConfigPath().Return(finchConfigPath) |
| 169 | + }, |
| 170 | + cpus: 0, |
| 171 | + memory: "", |
| 172 | + }, |
| 173 | + { |
| 174 | + name: "should return an error if the configuration of CPU or memory is invalid", |
| 175 | + wantErr: errors.New("the number of CPUs or the amount of memory should be at least one valid value"), |
| 176 | + wantStatusOutput: "", |
| 177 | + mockSvc: func( |
| 178 | + lca *mocks.LimaConfigApplier, |
| 179 | + fs afero.Fs, |
| 180 | + ) { |
| 181 | + finchConfigPath := "/config.yaml" |
| 182 | + data := "cpus: 2\nmemory: 6GiB" |
| 183 | + require.NoError(t, afero.WriteFile(fs, finchConfigPath, []byte(data), 0o600)) |
| 184 | + |
| 185 | + lca.EXPECT().GetFinchConfigPath().Return(finchConfigPath) |
| 186 | + }, |
| 187 | + cpus: 2, |
| 188 | + memory: "6GiB", |
| 189 | + }, |
| 190 | + } |
| 191 | + |
| 192 | + for _, tc := range testCases { |
| 193 | + tc := tc |
| 194 | + t.Run(tc.name, func(t *testing.T) { |
| 195 | + t.Parallel() |
| 196 | + |
| 197 | + ctrl := gomock.NewController(t) |
| 198 | + logger := mocks.NewLogger(ctrl) |
| 199 | + lca := mocks.NewLimaConfigApplier(ctrl) |
| 200 | + fs := afero.NewMemMapFs() |
| 201 | + stdout := bytes.Buffer{} |
| 202 | + opts := config.VMConfigOpts{ |
| 203 | + CPUs: tc.cpus, |
| 204 | + Memory: tc.memory, |
| 205 | + } |
| 206 | + |
| 207 | + tc.mockSvc(lca, fs) |
| 208 | + |
| 209 | + err := newSettingsVMAction(logger, lca, fs, &stdout).run(opts) |
| 210 | + assert.Equal(t, err, tc.wantErr) |
| 211 | + assert.Equal(t, tc.wantStatusOutput, stdout.String()) |
| 212 | + }) |
| 213 | + } |
| 214 | +} |
0 commit comments