Skip to content

Commit

Permalink
Merge branch 'runfinch:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubhranshu153 committed Sep 18, 2024
2 parents d1a1877 + c382e22 commit 31bac25
Show file tree
Hide file tree
Showing 30 changed files with 1,142 additions and 317 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
gen-code-no-diff:
strategy:
matrix:
os: [macos-latest, windows-latest]
os: [macos-latest, windows-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
Expand All @@ -64,7 +64,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest]
os: [macos-latest, windows-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Configure git CRLF settings
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ release: check-licenses all download-licenses

.PHONY: coverage
coverage:
go test $(shell go list ./... | grep -v e2e) -coverprofile=test-coverage.out
go test $(shell go list ./... | grep -v e2e | grep -v benchmark | grep -v mocks) -coverprofile=test-coverage.out
go tool cover -html=test-coverage.out

.PHONY: download-licenses
Expand Down Expand Up @@ -252,7 +252,7 @@ check-licenses:

.PHONY: test-unit
test-unit:
go test $(shell go list ./... | grep -v e2e) -shuffle on
go test $(shell go list ./... | grep -v e2e | grep -v benchmark | grep -v mocks) -shuffle on

# test-e2e assumes the VM instance doesn't exist, please make sure to remove it before running.
#
Expand Down
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/containerd/nerdctl/v2/pkg/errutil"
Expand All @@ -17,7 +15,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 @@ -35,51 +32,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
38 changes: 34 additions & 4 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,7 +84,7 @@ var newApp = func(

ncc := command.NewNerdctlCmdCreator(ecc,
logger,
fp.NerdctlConfigFilePath(finchRootPath),
fp.NerdctlConfigFilePath(),
fp.BuildkitSocketPath(),
fp.FinchDependencyBinDir(),
system.NewStdLib(),
Expand All @@ -63,7 +93,7 @@ var newApp = func(
supportBundleBuilder := support.NewBundleBuilder(
logger,
fs,
support.NewBundleConfig(fp, finchRootPath),
support.NewBundleConfig(fp, fp.FinchDir()),
fp,
ecc,
ncc,
Expand Down
101 changes: 101 additions & 0 deletions cmd/finch/main_native_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 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"

"gopkg.in/yaml.v3"

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

"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 31bac25

Please sign in to comment.