From faa3b20ae3750a5f7acf707bca486dbc62075273 Mon Sep 17 00:00:00 2001 From: TrungBui59 Date: Fri, 1 Sep 2023 19:27:44 -0400 Subject: [PATCH 1/4] Merge branch 'upstream/main' and adding test --- cmd/finch/main.go | 2 +- cmd/finch/nerdctl.go | 26 +--- cmd/finch/nerdctl_test.go | 138 ---------------------- e2e/vm/host_gateway_ip_test.go | 57 +++++++++ e2e/vm/vm_test.go | 1 + pkg/config/config.go | 4 +- pkg/config/nerdctl_config_applier.go | 12 +- pkg/config/nerdctl_config_applier_test.go | 18 ++- 8 files changed, 86 insertions(+), 172 deletions(-) create mode 100644 e2e/vm/host_gateway_ip_test.go diff --git a/cmd/finch/main.go b/cmd/finch/main.go index dfef4cbd9..1fb8f38c5 100644 --- a/cmd/finch/main.go +++ b/cmd/finch/main.go @@ -131,7 +131,7 @@ func virtualMachineCommands( logger, optionalDepGroups, config.NewLimaApplier(fc, ecc, fs, fp.LimaOverrideConfigPath(), system.NewStdLib()), - config.NewNerdctlApplier(fssh.NewDialer(), fs, fp.LimaSSHPrivateKeyPath(), system.NewStdLib().Env("USER")), + config.NewNerdctlApplier(fc, fssh.NewDialer(), fs, fp.LimaSSHPrivateKeyPath(), system.NewStdLib().Env("USER")), fp, fs, disk.NewUserDataDiskManager(lcc, ecc, &afero.OsFs{}, fp, system.NewStdLib().Env("HOME"), fc), diff --git a/cmd/finch/nerdctl.go b/cmd/finch/nerdctl.go index 776fa329e..17466e460 100644 --- a/cmd/finch/nerdctl.go +++ b/cmd/finch/nerdctl.go @@ -9,8 +9,8 @@ import ( "path/filepath" "strings" - dockerops "github.com/docker/docker/opts" - "github.com/lima-vm/lima/pkg/networks" + // dockerops "github.com/docker/docker/opts" + // "github.com/lima-vm/lima/pkg/networks". "github.com/spf13/afero" "github.com/spf13/cobra" @@ -112,15 +112,6 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { } skip = shouldSkip fileEnvs = append(fileEnvs, addEnvs...) - case strings.HasPrefix(arg, "--add-host"): - switch arg { - case "--add-host": - args[i+1] = resolveIP(args[i+1], nc.logger) - default: - resolvedIP := resolveIP(arg[11:], nc.logger) - arg = fmt.Sprintf("%s%s", arg[0:11], resolvedIP) - } - nerdctlArgs = append(nerdctlArgs, arg) default: nerdctlArgs = append(nerdctlArgs, arg) } @@ -305,19 +296,6 @@ func handleEnvFile(fs afero.Fs, systemDeps NerdctlCommandSystemDeps, arg, arg2 s return skip, envs, nil } -func resolveIP(host string, logger flog.Logger) string { - parts := strings.SplitN(host, ":", 2) - // If the IP Address is a string called "host-gateway", replace this value with the IP address that can be used to - // access host from the containers. - // TODO: make the host gateway ip configurable. - if parts[1] == dockerops.HostGatewayName { - resolvedIP := networks.SlirpGateway - logger.Debugf(`Resolving special IP "host-gateway" to %q for host %q`, resolvedIP, parts[0]) - return fmt.Sprintf("%s:%s", parts[0], resolvedIP) - } - return host -} - var nerdctlCmds = map[string]string{ "build": "Build an image from Dockerfile", "builder": "Manage builds", diff --git a/cmd/finch/nerdctl_test.go b/cmd/finch/nerdctl_test.go index 222dcaf1d..34d292c22 100644 --- a/cmd/finch/nerdctl_test.go +++ b/cmd/finch/nerdctl_test.go @@ -365,144 +365,6 @@ func TestNerdctlCommand_run(t *testing.T) { logger.EXPECT().Debugf("Status of virtual machine: %s", "Running") }, }, - { - name: "with --add-host flag and special IP by space", - cmdName: "run", - args: []string{"--rm", "--add-host", "name:host-gateway", "alpine:latest"}, - wantErr: nil, - mockSvc: func( - t *testing.T, - lcc *mocks.LimaCmdCreator, - ncsd *mocks.NerdctlCommandSystemDeps, - logger *mocks.Logger, - ctrl *gomock.Controller, - fs afero.Fs, - ) { - 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") - ncsd.EXPECT().LookupEnv("AWS_ACCESS_KEY_ID").Return("", false) - ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false) - ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false) - logger.EXPECT().Debugf(`Resolving special IP "host-gateway" to %q for host %q`, "192.168.5.2", "name") - ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false) - c := mocks.NewCommand(ctrl) - lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run", - "--rm", "--add-host", "name:192.168.5.2", "alpine:latest").Return(c) - c.EXPECT().Run() - }, - }, - { - name: "with --add-host flag but without using special IP by space", - cmdName: "run", - args: []string{"--rm", "--add-host", "name:0.0.0.0", "alpine:latest"}, - wantErr: nil, - mockSvc: func( - t *testing.T, - lcc *mocks.LimaCmdCreator, - ncsd *mocks.NerdctlCommandSystemDeps, - logger *mocks.Logger, - ctrl *gomock.Controller, - fs afero.Fs, - ) { - 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") - ncsd.EXPECT().LookupEnv("AWS_ACCESS_KEY_ID").Return("", false) - ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false) - ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false) - ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false) - c := mocks.NewCommand(ctrl) - lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run", - "--rm", "--add-host", "name:0.0.0.0", "alpine:latest").Return(c) - c.EXPECT().Run() - }, - }, - { - name: "with --add-host flag but without subsequent arg", - cmdName: "run", - args: []string{"--rm", "--add-host", "alpine:latest"}, - wantErr: errors.New("run cmd error"), - mockSvc: func( - t *testing.T, - lcc *mocks.LimaCmdCreator, - ncsd *mocks.NerdctlCommandSystemDeps, - logger *mocks.Logger, - ctrl *gomock.Controller, - fs afero.Fs, - ) { - 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") - ncsd.EXPECT().LookupEnv("AWS_ACCESS_KEY_ID").Return("", false) - ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false) - ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false) - ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false) - c := mocks.NewCommand(ctrl) - lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run", - "--rm", "--add-host", "alpine:latest").Return(c) - c.EXPECT().Run().Return(errors.New("run cmd error")) - }, - }, - { - name: "with --add-host flag and special IP by equal", - cmdName: "run", - args: []string{"--rm", "--add-host=name:host-gateway", "alpine:latest"}, - wantErr: nil, - mockSvc: func( - t *testing.T, - lcc *mocks.LimaCmdCreator, - ncsd *mocks.NerdctlCommandSystemDeps, - logger *mocks.Logger, - ctrl *gomock.Controller, - fs afero.Fs, - ) { - 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") - ncsd.EXPECT().LookupEnv("AWS_ACCESS_KEY_ID").Return("", false) - ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false) - ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false) - logger.EXPECT().Debugf(`Resolving special IP "host-gateway" to %q for host %q`, "192.168.5.2", "name") - ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false) - c := mocks.NewCommand(ctrl) - lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run", - "--rm", "--add-host=name:192.168.5.2", "alpine:latest").Return(c) - c.EXPECT().Run() - }, - }, - { - name: "with --add-host flag but without using special IP by equal", - cmdName: "run", - args: []string{"--rm", "--add-host=name:0.0.0.0", "alpine:latest"}, - wantErr: nil, - mockSvc: func( - t *testing.T, - lcc *mocks.LimaCmdCreator, - ncsd *mocks.NerdctlCommandSystemDeps, - logger *mocks.Logger, - ctrl *gomock.Controller, - fs afero.Fs, - ) { - 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") - ncsd.EXPECT().LookupEnv("AWS_ACCESS_KEY_ID").Return("", false) - ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false) - ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false) - ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false) - - c := mocks.NewCommand(ctrl) - lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run", - "--rm", "--add-host=name:0.0.0.0", "alpine:latest").Return(c) - c.EXPECT().Run() - }, - }, { name: "with multiple nested volumes", cmdName: "run", diff --git a/e2e/vm/host_gateway_ip_test.go b/e2e/vm/host_gateway_ip_test.go new file mode 100644 index 000000000..0d088e0f2 --- /dev/null +++ b/e2e/vm/host_gateway_ip_test.go @@ -0,0 +1,57 @@ +package vm + +import ( + "os/exec" + "path/filepath" + + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" + "github.com/onsi/gomega/gexec" + "github.com/runfinch/common-tests/command" + "github.com/runfinch/common-tests/option" + + "github.com/runfinch/finch/e2e" +) + +const defaultImage = "public.ecr.aws/docker/library/alpine:latest" + +var testHostGatewayIp = func(o *option.Option, installed bool) { + // it requires disk I/O so this will be serial test + ginkgo.Describe("--add-host tag with host_gateway", ginkgo.Serial, func() { + var limaConfigFilePath string + ginkgo.BeforeEach(func() { + origFinchCfg := readFile(finchConfigFilePath) + limaConfigFilePath = defaultLimaConfigFilePath + if installed { + path, err := exec.LookPath(e2e.InstalledTestSubject) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + realFinchPath, err := filepath.EvalSymlinks(path) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + limaConfigFilePath = filepath.Join(realFinchPath, "../../lima/data/_config/override.yaml") + } + origLimaCfg := readFile(limaConfigFilePath) + + ginkgo.DeferCleanup(func() { + writeFile(finchConfigFilePath, origFinchCfg) + writeFile(limaConfigFilePath, origLimaCfg) + + command.New(o, virtualMachineRootCmd, "stop").WithoutCheckingExitCode().WithTimeoutInSeconds(90).Run() + command.New(o, virtualMachineRootCmd, "start").WithTimeoutInSeconds(240).Run() + }) + }) + ginkgo.It("should add a custom host-to-IP mapping with --add-host flag: default value", func() { + startCmdSession := updateAndApplyConfig(o, []byte("memory: 4GiB\ncpus: 6")) + gomega.Expect(startCmdSession).Should(gexec.Exit(0)) + mapping := command.StdoutStr(o, "run", "--add-host", "test-host:host-gateway", defaultImage, "cat", "/etc/hosts") + gomega.Expect(mapping).Should(gomega.ContainSubstring("192.168.5.2")) + gomega.Expect(mapping).Should(gomega.ContainSubstring("test-host")) + }) + ginkgo.It("should add a custom host-to-IP mapping with --add-host flag: custom value", func() { + startCmdSession := updateAndApplyConfig(o, []byte("memory: 4GiB\ncpus: 6\nhost_gateway_ip: 192.168.31.1")) + gomega.Expect(startCmdSession).Should(gexec.Exit(0)) + mapping := command.StdoutStr(o, "run", "--add-host", "test-host:host-gateway", defaultImage, "cat", "/etc/hosts") + gomega.Expect(mapping).Should(gomega.ContainSubstring("192.168.31.1")) + gomega.Expect(mapping).Should(gomega.ContainSubstring("test-host")) + }) + }) +} diff --git a/e2e/vm/vm_test.go b/e2e/vm/vm_test.go index 02245e356..f0d9e1251 100644 --- a/e2e/vm/vm_test.go +++ b/e2e/vm/vm_test.go @@ -54,6 +54,7 @@ func TestVM(t *testing.T) { testSupportBundle(o) testCredHelper(o, *e2e.Installed, *e2e.Registry) testSoci(o, *e2e.Installed) + testHostGatewayIp(o, *e2e.Installed) }) gomega.RegisterFailHandler(ginkgo.Fail) diff --git a/pkg/config/config.go b/pkg/config/config.go index 88416b03a..f91c6e5c1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -54,7 +54,8 @@ type Finch struct { // Requires macOS 13.0 or later and an Apple Silicon (ARM64) mac. // Has no effect on systems where Rosetta 2 is not available (Intel/AMD64 macs, or macOS < 13.0). // This setting will only be applied on vm init. - Rosetta *bool `yaml:"rosetta,omitempty"` + Rosetta *bool `yaml:"rosetta,omitempty"` + HostGateway *string `yaml:"host_gateway_ip,omitempty"` } // Nerdctl is a copy from github.com/containerd/nerdctl/cmd/nerdctl/main.go @@ -72,6 +73,7 @@ type Nerdctl struct { CgroupManager string `toml:"cgroup_manager,omitempty"` InsecureRegistry bool `toml:"insecure_registry,omitempty"` HostsDir []string `toml:"hosts_dir,omitempty"` + HostGatewayIP string `toml:"host_gateway_ip,omitempty"` } // LimaConfigApplier applies lima configuration changes. diff --git a/pkg/config/nerdctl_config_applier.go b/pkg/config/nerdctl_config_applier.go index 6a1722255..3875d8332 100644 --- a/pkg/config/nerdctl_config_applier.go +++ b/pkg/config/nerdctl_config_applier.go @@ -23,6 +23,7 @@ const ( ) type nerdctlConfigApplier struct { + fc *Finch dialer fssh.Dialer fs afero.Fs privateKeyPath string @@ -33,8 +34,9 @@ var _ NerdctlConfigApplier = (*nerdctlConfigApplier)(nil) // NewNerdctlApplier creates a new NerdctlConfigApplier that // applies nerdctl configuration changes by SSHing to the lima VM to update the nerdctl configuration file in it. -func NewNerdctlApplier(dialer fssh.Dialer, fs afero.Fs, privateKeyPath, hostUser string) NerdctlConfigApplier { +func NewNerdctlApplier(fc *Finch, dialer fssh.Dialer, fs afero.Fs, privateKeyPath, hostUser string) NerdctlConfigApplier { return &nerdctlConfigApplier{ + fc: fc, dialer: dialer, fs: fs, privateKeyPath: privateKeyPath, @@ -93,7 +95,7 @@ func updateEnvironment(fs afero.Fs, user string) error { } // updateNerdctlConfig reads from the nerdctl config and updates values. -func updateNerdctlConfig(fs afero.Fs, user string, rootless bool) error { +func updateNerdctlConfig(fc *Finch, fs afero.Fs, user string, rootless bool) error { nerdctlRootlessCfgPath := fmt.Sprintf("/home/%s.linux/.config/nerdctl/nerdctl.toml", user) var cfgPath string @@ -124,6 +126,10 @@ func updateNerdctlConfig(fs afero.Fs, user string, rootless bool) error { } cfg.Namespace = nerdctlNamespace + cfg.HostGatewayIP = "192.168.5.2" + if fc.HostGateway != nil { + cfg.HostGatewayIP = *fc.HostGateway + } updatedCfg, err := toml.Marshal(cfg) if err != nil { @@ -158,7 +164,7 @@ func (nca *nerdctlConfigApplier) Apply(remoteAddr string) error { sftpFs := sftpfs.New(sftpClient) // rootless hardcoded to false for now to match our finch.yaml file - if err := updateNerdctlConfig(sftpFs, user, false); err != nil { + if err := updateNerdctlConfig(nca.fc, sftpFs, user, false); err != nil { return fmt.Errorf("failed to update the nerdctl config file: %w", err) } diff --git a/pkg/config/nerdctl_config_applier_test.go b/pkg/config/nerdctl_config_applier_test.go index 8b151a846..43e27caba 100644 --- a/pkg/config/nerdctl_config_applier_test.go +++ b/pkg/config/nerdctl_config_applier_test.go @@ -13,6 +13,7 @@ import ( "github.com/spf13/afero" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/xorcare/pointer" "github.com/runfinch/finch/pkg/mocks" ) @@ -128,7 +129,7 @@ func Test_updateNerdctlConfig(t *testing.T) { postRunCheck: func(t *testing.T, fs afero.Fs) { fileBytes, err := afero.ReadFile(fs, "/home/mock_user.linux/.config/nerdctl/nerdctl.toml") require.NoError(t, err) - assert.Equal(t, []byte(`namespace = "finch"`+"\n"), fileBytes) + assert.Equal(t, []byte(`host_gateway_ip = "192.168.31.1"`+"\n"+`namespace = "finch"`+"\n"), fileBytes) }, want: nil, }, @@ -140,7 +141,7 @@ func Test_updateNerdctlConfig(t *testing.T) { postRunCheck: func(t *testing.T, fs afero.Fs) { fileBytes, err := afero.ReadFile(fs, "/etc/nerdctl/nerdctl.toml") require.NoError(t, err) - assert.Equal(t, []byte(`namespace = "finch"`+"\n"), fileBytes) + assert.Equal(t, []byte(`host_gateway_ip = "192.168.31.1"`+"\n"+`namespace = "finch"`+"\n"), fileBytes) }, want: nil, }, @@ -156,7 +157,7 @@ func Test_updateNerdctlConfig(t *testing.T) { postRunCheck: func(t *testing.T, fs afero.Fs) { fileBytes, err := afero.ReadFile(fs, "/home/mock_user.linux/.config/nerdctl/nerdctl.toml") require.NoError(t, err) - assert.Equal(t, []byte(`namespace = "finch"`+"\n"), fileBytes) + assert.Equal(t, []byte(`host_gateway_ip = "192.168.31.1"`+"\n"+`namespace = "finch"`+"\n"), fileBytes) }, want: nil, }, @@ -179,13 +180,20 @@ func Test_updateNerdctlConfig(t *testing.T) { for _, tc := range testCases { tc := tc + fc := &Finch{ + CPUs: pointer.Int(4), + Memory: pointer.String("4GiB"), + VMType: pointer.String("qemu"), + Rosetta: pointer.Bool(false), + HostGateway: pointer.String("192.168.31.1"), + } t.Run(tc.name, func(t *testing.T) { t.Parallel() fs := afero.NewMemMapFs() tc.mockSvc(t, fs) - got := updateNerdctlConfig(fs, tc.user, tc.rootless) + got := updateNerdctlConfig(fc, fs, tc.user, tc.rootless) require.Equal(t, tc.want, got) tc.postRunCheck(t, fs) @@ -243,7 +251,7 @@ func TestNerdctlConfigApplier_Apply(t *testing.T) { d := mocks.NewDialer(ctrl) tc.mockSvc(t, fs, d) - got := NewNerdctlApplier(d, fs, tc.path, tc.hostUser).Apply(tc.remoteAddr) + got := NewNerdctlApplier(nil, d, fs, tc.path, tc.hostUser).Apply(tc.remoteAddr) assert.Equal(t, tc.want, got) }) From 410c2b77762c4cd157d5fad2a110a39e88677649 Mon Sep 17 00:00:00 2001 From: TrungBui59 Date: Sat, 2 Sep 2023 22:58:18 -0400 Subject: [PATCH 2/4] Fixing nit issue --- cmd/finch/nerdctl.go | 3 --- e2e/vm/host_gateway_ip_test.go | 1 + pkg/config/config.go | 2 +- pkg/config/nerdctl_config_applier.go | 4 ++-- pkg/config/nerdctl_config_applier_test.go | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/cmd/finch/nerdctl.go b/cmd/finch/nerdctl.go index 17466e460..a4bf45f97 100644 --- a/cmd/finch/nerdctl.go +++ b/cmd/finch/nerdctl.go @@ -9,9 +9,6 @@ import ( "path/filepath" "strings" - // dockerops "github.com/docker/docker/opts" - // "github.com/lima-vm/lima/pkg/networks". - "github.com/spf13/afero" "github.com/spf13/cobra" diff --git a/e2e/vm/host_gateway_ip_test.go b/e2e/vm/host_gateway_ip_test.go index 0d088e0f2..3ca18bc14 100644 --- a/e2e/vm/host_gateway_ip_test.go +++ b/e2e/vm/host_gateway_ip_test.go @@ -14,6 +14,7 @@ import ( ) const defaultImage = "public.ecr.aws/docker/library/alpine:latest" +const nerdctlRootlessCfgPath = "/home/root.linux/.config/nerdctl/nerdctl.toml" var testHostGatewayIp = func(o *option.Option, installed bool) { // it requires disk I/O so this will be serial test diff --git a/pkg/config/config.go b/pkg/config/config.go index f91c6e5c1..1af016c5d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -55,7 +55,7 @@ type Finch struct { // Has no effect on systems where Rosetta 2 is not available (Intel/AMD64 macs, or macOS < 13.0). // This setting will only be applied on vm init. Rosetta *bool `yaml:"rosetta,omitempty"` - HostGateway *string `yaml:"host_gateway_ip,omitempty"` + HostGatewayIp *string `yaml:"host_gateway_ip,omitempty"` } // Nerdctl is a copy from github.com/containerd/nerdctl/cmd/nerdctl/main.go diff --git a/pkg/config/nerdctl_config_applier.go b/pkg/config/nerdctl_config_applier.go index 3875d8332..2e637c1d5 100644 --- a/pkg/config/nerdctl_config_applier.go +++ b/pkg/config/nerdctl_config_applier.go @@ -127,8 +127,8 @@ func updateNerdctlConfig(fc *Finch, fs afero.Fs, user string, rootless bool) err cfg.Namespace = nerdctlNamespace cfg.HostGatewayIP = "192.168.5.2" - if fc.HostGateway != nil { - cfg.HostGatewayIP = *fc.HostGateway + if fc.HostGatewayIp != nil { + cfg.HostGatewayIP = *fc.HostGatewayIp } updatedCfg, err := toml.Marshal(cfg) diff --git a/pkg/config/nerdctl_config_applier_test.go b/pkg/config/nerdctl_config_applier_test.go index 43e27caba..8e94db8cf 100644 --- a/pkg/config/nerdctl_config_applier_test.go +++ b/pkg/config/nerdctl_config_applier_test.go @@ -185,7 +185,7 @@ func Test_updateNerdctlConfig(t *testing.T) { Memory: pointer.String("4GiB"), VMType: pointer.String("qemu"), Rosetta: pointer.Bool(false), - HostGateway: pointer.String("192.168.31.1"), + HostGatewayIp: pointer.String("192.168.31.1"), } t.Run(tc.name, func(t *testing.T) { t.Parallel() From 65921b978956c085c46a50d5f6f38bcb0575d556 Mon Sep 17 00:00:00 2001 From: TrungBui59 Date: Tue, 5 Sep 2023 19:27:34 -0400 Subject: [PATCH 3/4] Adding test for finch config file for test case Signed-off-by: TrungBui59 --- e2e/vm/host_gateway_ip_test.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/e2e/vm/host_gateway_ip_test.go b/e2e/vm/host_gateway_ip_test.go index 3ca18bc14..356553543 100644 --- a/e2e/vm/host_gateway_ip_test.go +++ b/e2e/vm/host_gateway_ip_test.go @@ -1,6 +1,7 @@ package vm import ( + "os" "os/exec" "path/filepath" @@ -9,12 +10,13 @@ import ( "github.com/onsi/gomega/gexec" "github.com/runfinch/common-tests/command" "github.com/runfinch/common-tests/option" + "gopkg.in/yaml.v3" "github.com/runfinch/finch/e2e" + "github.com/runfinch/finch/pkg/config" ) const defaultImage = "public.ecr.aws/docker/library/alpine:latest" -const nerdctlRootlessCfgPath = "/home/root.linux/.config/nerdctl/nerdctl.toml" var testHostGatewayIp = func(o *option.Option, installed bool) { // it requires disk I/O so this will be serial test @@ -43,6 +45,16 @@ var testHostGatewayIp = func(o *option.Option, installed bool) { ginkgo.It("should add a custom host-to-IP mapping with --add-host flag: default value", func() { startCmdSession := updateAndApplyConfig(o, []byte("memory: 4GiB\ncpus: 6")) gomega.Expect(startCmdSession).Should(gexec.Exit(0)) + + gomega.Expect(finchConfigFilePath).Should(gomega.BeARegularFile()) + cfgBuf, err := os.ReadFile(filepath.Clean(finchConfigFilePath)) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + var finchCfg config.Finch + err = yaml.Unmarshal(cfgBuf, &finchCfg) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + gomega.Expect(finchCfg.HostGatewayIp).Should(gomega.BeNil()) + mapping := command.StdoutStr(o, "run", "--add-host", "test-host:host-gateway", defaultImage, "cat", "/etc/hosts") gomega.Expect(mapping).Should(gomega.ContainSubstring("192.168.5.2")) gomega.Expect(mapping).Should(gomega.ContainSubstring("test-host")) @@ -50,6 +62,16 @@ var testHostGatewayIp = func(o *option.Option, installed bool) { ginkgo.It("should add a custom host-to-IP mapping with --add-host flag: custom value", func() { startCmdSession := updateAndApplyConfig(o, []byte("memory: 4GiB\ncpus: 6\nhost_gateway_ip: 192.168.31.1")) gomega.Expect(startCmdSession).Should(gexec.Exit(0)) + + gomega.Expect(finchConfigFilePath).Should(gomega.BeARegularFile()) + cfgBuf, err := os.ReadFile(filepath.Clean(finchConfigFilePath)) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + var finchCfg config.Finch + err = yaml.Unmarshal(cfgBuf, &finchCfg) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + gomega.Expect(*finchCfg.HostGatewayIp).Should(gomega.Equal("192.168.31.1")) + mapping := command.StdoutStr(o, "run", "--add-host", "test-host:host-gateway", defaultImage, "cat", "/etc/hosts") gomega.Expect(mapping).Should(gomega.ContainSubstring("192.168.31.1")) gomega.Expect(mapping).Should(gomega.ContainSubstring("test-host")) From c54020302f04512799ec841a8efb82da08f7a1a6 Mon Sep 17 00:00:00 2001 From: TrungBui59 Date: Tue, 5 Sep 2023 20:19:09 -0400 Subject: [PATCH 4/4] Fixing host_gateway_ip to follow nerdctl standard Signed-off-by: TrungBui59 --- pkg/config/config.go | 2 +- pkg/config/nerdctl_config_applier.go | 4 ++-- pkg/config/nerdctl_config_applier_test.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 1af016c5d..0a3fbef7d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -55,7 +55,7 @@ type Finch struct { // Has no effect on systems where Rosetta 2 is not available (Intel/AMD64 macs, or macOS < 13.0). // This setting will only be applied on vm init. Rosetta *bool `yaml:"rosetta,omitempty"` - HostGatewayIp *string `yaml:"host_gateway_ip,omitempty"` + HostGatewayIP *string `yaml:"host_gateway_ip,omitempty"` } // Nerdctl is a copy from github.com/containerd/nerdctl/cmd/nerdctl/main.go diff --git a/pkg/config/nerdctl_config_applier.go b/pkg/config/nerdctl_config_applier.go index 2e637c1d5..916c99d23 100644 --- a/pkg/config/nerdctl_config_applier.go +++ b/pkg/config/nerdctl_config_applier.go @@ -127,8 +127,8 @@ func updateNerdctlConfig(fc *Finch, fs afero.Fs, user string, rootless bool) err cfg.Namespace = nerdctlNamespace cfg.HostGatewayIP = "192.168.5.2" - if fc.HostGatewayIp != nil { - cfg.HostGatewayIP = *fc.HostGatewayIp + if fc.HostGatewayIP != nil { + cfg.HostGatewayIP = *fc.HostGatewayIP } updatedCfg, err := toml.Marshal(cfg) diff --git a/pkg/config/nerdctl_config_applier_test.go b/pkg/config/nerdctl_config_applier_test.go index 8e94db8cf..2ac67ec4d 100644 --- a/pkg/config/nerdctl_config_applier_test.go +++ b/pkg/config/nerdctl_config_applier_test.go @@ -185,7 +185,7 @@ func Test_updateNerdctlConfig(t *testing.T) { Memory: pointer.String("4GiB"), VMType: pointer.String("qemu"), Rosetta: pointer.Bool(false), - HostGatewayIp: pointer.String("192.168.31.1"), + HostGatewayIP: pointer.String("192.168.31.1"), } t.Run(tc.name, func(t *testing.T) { t.Parallel()