Skip to content

Commit

Permalink
add unit tests for files which were refactored on Linux
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Alvarez <[email protected]>
  • Loading branch information
pendo324 committed Aug 8, 2024
1 parent 2de33f6 commit 2f650f3
Show file tree
Hide file tree
Showing 24 changed files with 1,255 additions and 269 deletions.
48 changes: 0 additions & 48 deletions cmd/finch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package main

import (
"fmt"
"io"
"os"

"github.com/spf13/afero"
Expand All @@ -16,7 +14,6 @@ import (
"github.com/runfinch/finch/pkg/config"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/fmemory"
"github.com/runfinch/finch/pkg/path"
"github.com/runfinch/finch/pkg/system"
)

Expand All @@ -33,51 +30,6 @@ func main() {
}
}

func xmain(logger flog.Logger,
ffd path.FinchFinderDeps,
fs afero.Fs,
loadCfgDeps config.LoadSystemDeps,
mem fmemory.Memory,
stdOut io.Writer,
) error {
fp, err := path.FindFinch(ffd)
if err != nil {
return fmt.Errorf("failed to find the installation path of Finch: %w", err)
}

home, err := ffd.GetUserHome()
if err != nil {
return fmt.Errorf("failed to get user home directory: %w", err)
}
finchRootPath, err := fp.FinchRootDir(ffd)
if err != nil {
return fmt.Errorf("failed to get finch root path: %w", err)
}
ecc := command.NewExecCmdCreator()
fc, err := config.Load(
fs,
fp.ConfigFilePath(finchRootPath),
logger,
loadCfgDeps,
mem,
ecc,
)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}

return newApp(
logger,
fp,
fs,
fc,
stdOut,
home,
finchRootPath,
ecc,
).Execute()
}

func initializeNerdctlCommands(
ncc command.NerdctlCmdCreator,
ecc command.Creator,
Expand Down
40 changes: 35 additions & 5 deletions cmd/finch/main_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,51 @@ import (
"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/config"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/fmemory"
"github.com/runfinch/finch/pkg/lima/wrapper"
"github.com/runfinch/finch/pkg/path"
"github.com/runfinch/finch/pkg/support"
"github.com/runfinch/finch/pkg/system"
"github.com/runfinch/finch/pkg/version"
)

func xmain(logger flog.Logger,
_ path.FinchFinderDeps,
fs afero.Fs,
loadCfgDeps config.LoadSystemDeps,
mem fmemory.Memory,
stdOut io.Writer,
) error {
fp := path.NewFinchPath()
ecc := command.NewExecCmdCreator()
fc, err := config.Load(
fs,
fp.ConfigFilePath(),
logger,
loadCfgDeps,
mem,
ecc,
)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}

return newApp(
logger,
fp,
fs,
fc,
stdOut,
ecc,
).Execute()
}

var newApp = func(
logger flog.Logger,
fp path.Finch,
fs afero.Fs,
fc *config.Finch,
stdOut io.Writer,
_,
finchRootPath string,
ecc command.Creator,
) *cobra.Command {
usage := fmt.Sprintf("%v <command>", finchRootCmd)
Expand All @@ -54,16 +84,16 @@ var newApp = func(

ncc := command.NewNerdctlCmdCreator(ecc,
logger,
fp.NerdctlConfigFilePath(finchRootPath),
fp.BuildkitSocketPath(finchRootPath),
fp.NerdctlConfigFilePath(),
fp.BuildkitSocketPath(),
fp.FinchDependencyBinDir(),
system.NewStdLib(),
)
lima := wrapper.NewLimaWrapper()
supportBundleBuilder := support.NewBundleBuilder(
logger,
fs,
support.NewBundleConfig(fp, finchRootPath),
support.NewBundleConfig(fp, fp.FinchDir()),
fp,
ecc,
ncc,
Expand Down
100 changes: 100 additions & 0 deletions cmd/finch/main_native_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//go:build linux

package main

import (
"fmt"
"os"
"testing"

"github.com/runfinch/finch/pkg/config"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/mocks"
"github.com/runfinch/finch/pkg/path"
"github.com/runfinch/finch/pkg/version"
"gopkg.in/yaml.v3"

"github.com/golang/mock/gomock"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var nativeConfigStr = ""

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

ctrl := gomock.NewController(t)
l := mocks.NewLogger(ctrl)
fp := path.Finch("")
fs := afero.NewMemMapFs()
stdOut := os.Stdout
ecc := mocks.NewCommandCreator(ctrl)

require.NoError(t, afero.WriteFile(fs, "/real/config.yaml", []byte(nativeConfigStr), 0o600))

cmd := newApp(l, fp, fs, &config.Finch{}, stdOut, ecc)

assert.Equal(t, cmd.Name(), finchRootCmd)
assert.Equal(t, cmd.Version, version.Version)
assert.Equal(t, cmd.SilenceUsage, true)
assert.Equal(t, cmd.SilenceErrors, true)
// confirm the number of command, comprised of nerdctl commands + finch commands
// one less than "remote", because there are no VM commands on native
assert.Equal(t, len(cmd.Commands()), len(nerdctlCmds)+3)

// PersistentPreRunE should set logger level to debug if the debug flag exists.
mockCmd := &cobra.Command{}
mockCmd.Flags().Bool("debug", true, "")
l.EXPECT().SetLevel(flog.Debug)

require.NoError(t, cmd.PersistentPreRunE(mockCmd, nil))
}

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

testCases := []struct {
name string
wantErr error
mockSvc func(afero.Fs)
}{
{
name: "happy path",
wantErr: nil,
mockSvc: func(fs afero.Fs) {
require.NoError(t, afero.WriteFile(fs, "/etc/finch/finch.yaml", []byte(nativeConfigStr), 0o600))
},
},
{
name: "failed to load finch config because of invalid YAML",
wantErr: fmt.Errorf("failed to load config: %w",
fmt.Errorf("failed to unmarshal config file: %w",
&yaml.TypeError{Errors: []string{"line 1: cannot unmarshal !!str `this is...` into config.Finch"}},
),
),
mockSvc: func(
fs afero.Fs,
) {
require.NoError(t, afero.WriteFile(fs, "/etc/finch/finch.yaml", []byte("this isn't YAML"), 0o600))
},
},
}

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

fs := afero.NewMemMapFs()
tc.mockSvc(fs)
err := xmain(nil, nil, fs, nil, nil, nil)
assert.Equal(t, err, tc.wantErr)
})
}
}
46 changes: 46 additions & 0 deletions cmd/finch/main_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,59 @@ import (
"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/config"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/fmemory"
"github.com/runfinch/finch/pkg/lima/wrapper"
"github.com/runfinch/finch/pkg/path"
"github.com/runfinch/finch/pkg/support"
"github.com/runfinch/finch/pkg/system"
"github.com/runfinch/finch/pkg/version"
)

func xmain(logger flog.Logger,
ffd path.FinchFinderDeps,
fs afero.Fs,
loadCfgDeps config.LoadSystemDeps,
mem fmemory.Memory,
stdOut io.Writer,
) error {
fp, err := path.FindFinch(ffd)
if err != nil {
return fmt.Errorf("failed to find the installation path of Finch: %w", err)
}

home, err := ffd.GetUserHome()
if err != nil {
return fmt.Errorf("failed to get user home directory: %w", err)
}
finchRootPath, err := fp.FinchRootDir(ffd)
if err != nil {
return fmt.Errorf("failed to get finch root path: %w", err)
}
ecc := command.NewExecCmdCreator()
fc, err := config.Load(
fs,
fp.ConfigFilePath(finchRootPath),
logger,
loadCfgDeps,
mem,
ecc,
)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}

return newApp(
logger,
fp,
fs,
fc,
stdOut,
home,
finchRootPath,
ecc,
).Execute()
}

var newApp = func(
logger flog.Logger,
fp path.Finch,
Expand Down
Loading

0 comments on commit 2f650f3

Please sign in to comment.