From fb4b62bc6987ed786da8f1a351ea8200206be941 Mon Sep 17 00:00:00 2001 From: Ziwen Ning Date: Mon, 20 Feb 2023 10:27:56 -0800 Subject: [PATCH] fix: parse --add-host special ip with equal sign (#229) https://github.com/runfinch/finch/issues/209 *Description of changes:* parse --add-host special ip with equal sign, like `--add-host=name:host-gateway` *Testing done:* unit tests and e2e tests - [ X ] I've reviewed the guidance in CONTRIBUTING.md #### License Acceptance By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. Signed-off-by: Ziwen Ning --- cmd/finch/nerdctl.go | 10 ++++++-- cmd/finch/nerdctl_test.go | 51 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/cmd/finch/nerdctl.go b/cmd/finch/nerdctl.go index bf3abfc59..33d50fd41 100644 --- a/cmd/finch/nerdctl.go +++ b/cmd/finch/nerdctl.go @@ -112,8 +112,14 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { } skip = shouldSkip fileEnvs = append(fileEnvs, addEnvs...) - case arg == "--add-host": - args[i+1] = resolveIP(args[i+1], nc.logger) + 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) diff --git a/cmd/finch/nerdctl_test.go b/cmd/finch/nerdctl_test.go index a4313dc80..f9c7040c1 100644 --- a/cmd/finch/nerdctl_test.go +++ b/cmd/finch/nerdctl_test.go @@ -337,7 +337,7 @@ func TestNerdctlCommand_run(t *testing.T) { }, }, { - name: "with --add-host flag and special IP", + name: "with --add-host flag and special IP by space", cmdName: "run", args: []string{"--rm", "--add-host", "name:host-gateway", "alpine:latest"}, wantErr: nil, @@ -361,7 +361,7 @@ func TestNerdctlCommand_run(t *testing.T) { }, }, { - name: "with --add-host flag but without using special IP", + 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, @@ -406,6 +406,53 @@ func TestNerdctlCommand_run(t *testing.T) { 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") + logger.EXPECT().Debugf(`Resolving special IP "host-gateway" to %q for host %q`, "192.168.5.2", "name") + c := mocks.NewCommand(ctrl) + lcc.EXPECT().Create("shell", limaInstanceName, 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") + c := mocks.NewCommand(ctrl) + lcc.EXPECT().Create("shell", limaInstanceName, nerdctlCmdName, "run", + "--rm", "--add-host=name:0.0.0.0", "alpine:latest").Return(c) + c.EXPECT().Run() + }, + }, { name: "with --help flag", cmdName: "pull",