Skip to content

Commit

Permalink
fix: parse --add-host special ip with equal sign (runfinch#229)
Browse files Browse the repository at this point in the history
runfinch#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 <[email protected]>
  • Loading branch information
ningziwen authored Feb 20, 2023
1 parent 4404caa commit fb4b62b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
10 changes: 8 additions & 2 deletions cmd/finch/nerdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 49 additions & 2 deletions cmd/finch/nerdctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit fb4b62b

Please sign in to comment.