Skip to content

Commit 05227ce

Browse files
authored
fix: Correctly set and pick up environment variables (#315)
Issue #, if available: Currently the environment variable is set in `/root/.bashrc` so that it is [not easily accessible](https://superuser.com/questions/1254422/linux-sudo-env-does-not-have-environment-variable-while-root-user-has) by regular users even by specifying `sudo -E`. Normally we would access the VM by regular user. In order to pick up the environment variables, we need to store the vars in the /home/<user>.linux/.bashrc and added -E to sudo command as sudo itself won't preserve the environment variables. *Description of changes:* Added -E to sudo command when we call limactl command. Update the profile path so that env vars are written in /home/<user>.linux/.bashrc. Added e2e tests to make sure the content is written correctly into the ~/.finch/.config.json file in host system. *Testing done:* Manual testing 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: Ang Zhou <[email protected]>
1 parent 00f2124 commit 05227ce

9 files changed

+111
-31
lines changed

cmd/finch/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,12 @@ func virtualMachineCommands(
111111
fc *config.Finch,
112112
) *cobra.Command {
113113
optionalDepGroups := []*dependency.Group{vmnet.NewDependencyGroup(ecc, lcc, fs, fp, logger)}
114-
115114
return newVirtualMachineCommand(
116115
lcc,
117116
logger,
118117
optionalDepGroups,
119118
config.NewLimaApplier(fc, ecc, fs, fp.LimaOverrideConfigPath(), system.NewStdLib()),
120-
config.NewNerdctlApplier(fssh.NewDialer(), fs, fp.LimaSSHPrivateKeyPath()),
119+
config.NewNerdctlApplier(fssh.NewDialer(), fs, fp.LimaSSHPrivateKeyPath(), system.NewStdLib().Env("USER")),
121120
fp,
122121
fs,
123122
disk.NewUserDataDiskManager(lcc, ecc, &afero.OsFs{}, fp, system.NewStdLib().Env("HOME"), fc),

cmd/finch/nerdctl.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {
145145
finalArgs = append(finalArgs, "-e", fmt.Sprintf("%s=%s", key, val))
146146
}
147147
finalArgs = append(finalArgs, nerdctlArgs...)
148-
149-
limaArgs := append([]string{"shell", limaInstanceName, "sudo", nerdctlCmdName, cmdName}, finalArgs...)
148+
// Add -E to sudo command in order to preserve existing environment variables, more info:
149+
// https://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo/8633575#8633575
150+
limaArgs := append([]string{"shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, cmdName}, finalArgs...)
150151

151152
if nc.shouldReplaceForHelp(cmdName, args) {
152153
return nc.creator.RunWithReplacingStdout([]command.Replacement{{Source: "nerdctl", Target: "finch"}}, limaArgs...)

cmd/finch/nerdctl_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestNerdctlCommand_runAdaptor(t *testing.T) {
5454
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
5555
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
5656
c := mocks.NewCommand(ctrl)
57-
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "info").Return(c)
57+
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "info").Return(c)
5858
c.EXPECT().Run()
5959
},
6060
},
@@ -104,7 +104,7 @@ func TestNerdctlCommand_run(t *testing.T) {
104104
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
105105
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
106106
c := mocks.NewCommand(ctrl)
107-
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "build", "-t", "demo", ".").Return(c)
107+
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "build", "-t", "demo", ".").Return(c)
108108
c.EXPECT().Run()
109109
},
110110
},
@@ -205,7 +205,7 @@ func TestNerdctlCommand_run(t *testing.T) {
205205
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
206206
logger.EXPECT().SetLevel(flog.Debug)
207207
c := mocks.NewCommand(ctrl)
208-
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "pull", "test:tag").Return(c)
208+
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "pull", "test:tag").Return(c)
209209
c.EXPECT().Run()
210210
},
211211
},
@@ -229,7 +229,7 @@ func TestNerdctlCommand_run(t *testing.T) {
229229
c := mocks.NewCommand(ctrl)
230230
ncsd.EXPECT().LookupEnv("ARG2")
231231
ncsd.EXPECT().LookupEnv("ARG3")
232-
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "run",
232+
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
233233
"-e", "ARG1=val1", "--rm", "alpine:latest", "env").Return(c)
234234
c.EXPECT().Run()
235235
},
@@ -254,7 +254,7 @@ func TestNerdctlCommand_run(t *testing.T) {
254254
c := mocks.NewCommand(ctrl)
255255
ncsd.EXPECT().LookupEnv("ARG2")
256256
ncsd.EXPECT().LookupEnv("ARG3").Return("val3", true)
257-
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "run",
257+
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
258258
"-e", "ARG3=val3", "--rm", "alpine:latest", "env").Return(c)
259259
c.EXPECT().Run()
260260
},
@@ -283,7 +283,7 @@ func TestNerdctlCommand_run(t *testing.T) {
283283
ncsd.EXPECT().LookupEnv("ARG2")
284284
ncsd.EXPECT().LookupEnv("NOTSETARG")
285285
lcc.EXPECT().
286-
Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "run", "-e", "ARG1=val1", "--rm", "alpine:latest", "env").
286+
Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run", "-e", "ARG1=val1", "--rm", "alpine:latest", "env").
287287
Return(c)
288288
c.EXPECT().Run()
289289
},
@@ -312,7 +312,7 @@ func TestNerdctlCommand_run(t *testing.T) {
312312
ncsd.EXPECT().LookupEnv("ARG2").Return("val2", true)
313313
ncsd.EXPECT().LookupEnv("NOTSETARG")
314314
lcc.EXPECT().
315-
Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "run", "-e", "ARG2=val2", "--rm", "alpine:latest", "env").
315+
Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run", "-e", "ARG2=val2", "--rm", "alpine:latest", "env").
316316
Return(c)
317317
c.EXPECT().Run()
318318
},
@@ -355,7 +355,7 @@ func TestNerdctlCommand_run(t *testing.T) {
355355
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
356356
logger.EXPECT().Debugf(`Resolving special IP "host-gateway" to %q for host %q`, "192.168.5.2", "name")
357357
c := mocks.NewCommand(ctrl)
358-
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "run",
358+
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
359359
"--rm", "--add-host", "name:192.168.5.2", "alpine:latest").Return(c)
360360
c.EXPECT().Run()
361361
},
@@ -378,7 +378,7 @@ func TestNerdctlCommand_run(t *testing.T) {
378378
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
379379
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
380380
c := mocks.NewCommand(ctrl)
381-
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "run",
381+
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
382382
"--rm", "--add-host", "name:0.0.0.0", "alpine:latest").Return(c)
383383
c.EXPECT().Run()
384384
},
@@ -401,7 +401,7 @@ func TestNerdctlCommand_run(t *testing.T) {
401401
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
402402
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
403403
c := mocks.NewCommand(ctrl)
404-
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "run",
404+
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
405405
"--rm", "--add-host", "alpine:latest").Return(c)
406406
c.EXPECT().Run().Return(errors.New("run cmd error"))
407407
},
@@ -425,7 +425,7 @@ func TestNerdctlCommand_run(t *testing.T) {
425425
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
426426
logger.EXPECT().Debugf(`Resolving special IP "host-gateway" to %q for host %q`, "192.168.5.2", "name")
427427
c := mocks.NewCommand(ctrl)
428-
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "run",
428+
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
429429
"--rm", "--add-host=name:192.168.5.2", "alpine:latest").Return(c)
430430
c.EXPECT().Run()
431431
},
@@ -448,7 +448,7 @@ func TestNerdctlCommand_run(t *testing.T) {
448448
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
449449
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
450450
c := mocks.NewCommand(ctrl)
451-
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", nerdctlCmdName, "run",
451+
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
452452
"--rm", "--add-host=name:0.0.0.0", "alpine:latest").Return(c)
453453
c.EXPECT().Run()
454454
},
@@ -471,7 +471,7 @@ func TestNerdctlCommand_run(t *testing.T) {
471471
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
472472
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
473473
lcc.EXPECT().RunWithReplacingStdout(
474-
testStdoutRs, "shell", limaInstanceName, "sudo", nerdctlCmdName, "pull", "test:tag", "--help").Return(nil)
474+
testStdoutRs, "shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "pull", "test:tag", "--help").Return(nil)
475475
},
476476
},
477477
{
@@ -492,7 +492,7 @@ func TestNerdctlCommand_run(t *testing.T) {
492492
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
493493
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
494494
lcc.EXPECT().RunWithReplacingStdout(
495-
testStdoutRs, "shell", limaInstanceName, "sudo", nerdctlCmdName, "pull", "test:tag", "--help").
495+
testStdoutRs, "shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "pull", "test:tag", "--help").
496496
Return(fmt.Errorf("failed to replace"))
497497
},
498498
},

cmd/finch/version.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ func (va *versionAction) printVersion() error {
8888
if status != lima.Running {
8989
return errors.New("detailed version info is unavailable because VM is not running")
9090
}
91-
92-
limaArgs := []string{"shell", limaInstanceName, "sudo", "nerdctl", "version", "--format", "json"}
91+
// Add -E to sudo command in order to preserve existing environment variables, more info:
92+
// https://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo/8633575#8633575
93+
limaArgs := []string{"shell", limaInstanceName, "sudo", "-E", "nerdctl", "version", "--format", "json"}
9394
out, err := va.creator.CreateWithoutStdio(limaArgs...).Output()
9495
if err != nil {
9596
return fmt.Errorf("failed to create the nerdctl version command: %w", err)

cmd/finch/version_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ func TestVersionAction_runAdaptor(t *testing.T) {
5252
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
5353

5454
command := mocks.NewCommand(ctrl)
55-
lcc.EXPECT().CreateWithoutStdio("shell", limaInstanceName, "sudo", "nerdctl", "version", "--format", "json").Return(command)
55+
lcc.EXPECT().CreateWithoutStdio("shell", limaInstanceName, "sudo", "-E", "nerdctl", "version",
56+
"--format", "json").Return(command)
5657
//nolint: lll // Version output format is larger than 500
5758
command.EXPECT().Output().Return([]byte(`{"Client":{"Version":"v1.0.0","GitCommit":"c00780a1f5b905b09812722459c54936c9e070e6","GoVersion":"go1.19.2","Os":"linux","Arch":"arm64","Components":[{"Name":"buildctl","Version":"v0.10.5","Details":{"GitCommit":"bc26045116045516ff2427201abd299043eaf8f7"}}]},"Server":{"Components":[{"Name":"containerd","Version":"v1.6.8","Details":{"GitCommit":"9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6"}},{"Name":"runc","Version":"1.1.4","Details":{"GitCommit":"v1.1.4-0-g5fd4c4d1"}}]}}`), nil)
5859
},
@@ -98,7 +99,8 @@ func TestVersionAction_run(t *testing.T) {
9899
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
99100

100101
command := mocks.NewCommand(ctrl)
101-
lcc.EXPECT().CreateWithoutStdio("shell", limaInstanceName, "sudo", "nerdctl", "version", "--format", "json").Return(command)
102+
lcc.EXPECT().CreateWithoutStdio("shell", limaInstanceName, "sudo", "-E", "nerdctl", "version",
103+
"--format", "json").Return(command)
102104
command.EXPECT().Output().Return([]byte(`{
103105
"Client":{
104106
"Version":"v1.0.0",

e2e/vm/finch_config_file_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package vm
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"path/filepath"
10+
"time"
11+
12+
"github.com/onsi/ginkgo/v2"
13+
"github.com/onsi/gomega"
14+
"github.com/runfinch/common-tests/command"
15+
"github.com/runfinch/common-tests/ffs"
16+
"github.com/runfinch/common-tests/fnet"
17+
"github.com/runfinch/common-tests/option"
18+
)
19+
20+
// testFinchConfigFile makes sure that DOCKER_CONFIG is properly set to ~/.finch so that related information
21+
// is written to ~/.finch/config.json file.
22+
var testFinchConfigFile = func(o *option.Option) {
23+
ginkgo.Describe("finch config file", func() {
24+
ginkgo.It("should store login credentials", func() {
25+
filename := "htpasswd"
26+
registryImage := "public.ecr.aws/docker/library/registry:2"
27+
registryContainer := "auth-registry"
28+
// The htpasswd is generated by
29+
// `finch run --entrypoint htpasswd public.ecr.aws/docker/library/httpd:2 -Bbn testUser testPassword`.
30+
// We don't want to generate it on the fly because:
31+
// 1. Pulling the httpd image can take a long time, sometimes even more 10 seconds.
32+
// 2. It's unlikely that we will have to update this in the future.
33+
// 3. It's not the thing we want to validate by the functional tests. We only want the output produced by it.
34+
//nolint:gosec // This password is only used for testing purpose.
35+
htpasswd := "testUser:$2y$05$wE0sj3r9O9K9q7R0MXcfPuIerl/06L1IsxXkCuUr3QZ8lHWwicIdS"
36+
htpasswdDir := filepath.Dir(ffs.CreateTempFile(filename, htpasswd))
37+
ginkgo.DeferCleanup(os.RemoveAll, htpasswdDir)
38+
port := fnet.GetFreePort()
39+
containerID := command.StdoutStr(o, "run",
40+
"-dp", fmt.Sprintf("%d:5000", port),
41+
"--name", registryContainer,
42+
"-v", fmt.Sprintf("%s:/auth", htpasswdDir),
43+
"-e", "REGISTRY_AUTH=htpasswd",
44+
"-e", "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm",
45+
"-e", fmt.Sprintf("REGISTRY_AUTH_HTPASSWD_PATH=/auth/%s", filename),
46+
registryImage)
47+
ginkgo.DeferCleanup(command.Run, o, "rmi", registryImage)
48+
ginkgo.DeferCleanup(command.Run, o, "rm", "-f", registryContainer)
49+
for command.StdoutStr(o, "inspect", "-f", "{{.State.Running}}", containerID) != "true" {
50+
time.Sleep(1 * time.Second)
51+
}
52+
registry := fmt.Sprintf(`localhost:%d`, port)
53+
command.Run(o, "login", registry, "-u", "testUser", "-p", "testPassword")
54+
55+
homeDir, err := os.UserHomeDir()
56+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
57+
configPath := fmt.Sprintf("%s/.finch/config.json", homeDir)
58+
configContent, err := os.ReadFile(filepath.Clean(configPath))
59+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
60+
61+
gomega.Expect(string(configContent)).Should(gomega.ContainSubstring(registry))
62+
command.Run(o, "logout", registry)
63+
64+
configContent, err = os.ReadFile(filepath.Clean(configPath))
65+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
66+
gomega.Expect(string(configContent)).ShouldNot(gomega.ContainSubstring(registry))
67+
})
68+
})
69+
}

e2e/vm/vm_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func TestVM(t *testing.T) {
4545
testVMLifecycle(o)
4646
testAdditionalDisk(o)
4747
testConfig(o, *e2e.Installed)
48+
testFinchConfigFile(o)
4849
testVersion(o)
4950
testVirtualizationFrameworkAndRosetta(o, *e2e.Installed)
5051
})

pkg/config/nerdctl_config_applier.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ type nerdctlConfigApplier struct {
2626
dialer fssh.Dialer
2727
fs afero.Fs
2828
privateKeyPath string
29+
hostUser string
2930
}
3031

3132
var _ NerdctlConfigApplier = (*nerdctlConfigApplier)(nil)
3233

3334
// NewNerdctlApplier creates a new NerdctlConfigApplier that
3435
// applies nerdctl configuration changes by SSHing to the lima VM to update the nerdctl configuration file in it.
35-
func NewNerdctlApplier(dialer fssh.Dialer, fs afero.Fs, privateKeyPath string) NerdctlConfigApplier {
36+
func NewNerdctlApplier(dialer fssh.Dialer, fs afero.Fs, privateKeyPath, hostUser string) NerdctlConfigApplier {
3637
return &nerdctlConfigApplier{
3738
dialer: dialer,
3839
fs: fs,
3940
privateKeyPath: privateKeyPath,
41+
hostUser: hostUser,
4042
}
4143
}
4244

@@ -46,13 +48,16 @@ func NewNerdctlApplier(dialer fssh.Dialer, fs afero.Fs, privateKeyPath string) N
4648
// The [GNU docs for Bash] explain how these files work together in more details.
4749
// The default location of DOCKER_CONFIG is ~/.docker/config.json. This config change sets the location to
4850
// ~/.finch/config.json, but from the perspective of macOS (/Users/<user>/.finch/config.json).
51+
// The reason that we don't set environment variables inside /root/.bashrc is that the vars inside it are
52+
// not able to be picked up even if we specify `sudo -E`. We have to switch to root user in order to access them, while
53+
// normally we would access the VM as the regular user.
4954
// For more information on the variable, see the registry nerdctl docs.
5055
//
5156
// [GNU docs for Bash]: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
5257
//
5358
// [registry nerdctl docs]: https://github.com/containerd/nerdctl/blob/master/docs/registry.md
5459
func updateEnvironment(fs afero.Fs, user string) error {
55-
profileFilePath := "/root/.bashrc"
60+
profileFilePath := fmt.Sprintf("/home/%s.linux/.bashrc", user)
5661
profBuf, err := afero.ReadFile(fs, profileFilePath)
5762
if err != nil {
5863
return fmt.Errorf("failed to read config file: %w", err)
@@ -139,7 +144,7 @@ func (nca *nerdctlConfigApplier) Apply(remoteAddr string) error {
139144
return fmt.Errorf("failed to update the nerdctl config file: %w", err)
140145
}
141146

142-
if err := updateEnvironment(sftpFs, user); err != nil {
147+
if err := updateEnvironment(sftpFs, nca.hostUser); err != nil {
143148
return fmt.Errorf("failed to update the user's .profile file: %w", err)
144149
}
145150
return nil

pkg/config/nerdctl_config_applier_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ func Test_updateEnvironment(t *testing.T) {
4242
name: "happy path",
4343
user: "mock_user",
4444
mockSvc: func(t *testing.T, fs afero.Fs) {
45-
require.NoError(t, afero.WriteFile(fs, "/root/.bashrc", []byte(""), 0o644))
45+
require.NoError(t, afero.WriteFile(fs, "/home/mock_user.linux/.bashrc", []byte(""), 0o644))
4646
},
4747
postRunCheck: func(t *testing.T, fs afero.Fs) {
48-
fileBytes, err := afero.ReadFile(fs, "/root/.bashrc")
48+
fileBytes, err := afero.ReadFile(fs, "/home/mock_user.linux/.bashrc")
4949
require.NoError(t, err)
5050
assert.Equal(t, []byte("\n"+`export DOCKER_CONFIG="/Users/mock_user/.finch"`+"\n"), fileBytes)
5151
},
@@ -59,14 +59,14 @@ func Test_updateEnvironment(t *testing.T) {
5959
t,
6060
afero.WriteFile(
6161
fs,
62-
"/root/.bashrc",
62+
"/home/mock_user.linux/.bashrc",
6363
[]byte(`export DOCKER_CONFIG="/Users/mock_user/.finch"`),
6464
0o644,
6565
),
6666
)
6767
},
6868
postRunCheck: func(t *testing.T, fs afero.Fs) {
69-
fileBytes, err := afero.ReadFile(fs, "/root/.bashrc")
69+
fileBytes, err := afero.ReadFile(fs, "/home/mock_user.linux/.bashrc")
7070
require.NoError(t, err)
7171
assert.Equal(t, []byte(`export DOCKER_CONFIG="/Users/mock_user/.finch"`), fileBytes)
7272
},
@@ -79,7 +79,7 @@ func Test_updateEnvironment(t *testing.T) {
7979
postRunCheck: func(t *testing.T, fs afero.Fs) {},
8080
want: fmt.Errorf(
8181
"failed to read config file: %w",
82-
&fs.PathError{Op: "open", Path: "/root/.bashrc", Err: errors.New("file does not exist")},
82+
&fs.PathError{Op: "open", Path: "/home/mock_user.linux/.bashrc", Err: errors.New("file does not exist")},
8383
),
8484
},
8585
}
@@ -190,6 +190,7 @@ func TestNerdctlConfigApplier_Apply(t *testing.T) {
190190
testCases := []struct {
191191
name string
192192
path string
193+
hostUser string
193194
remoteAddr string
194195
mockSvc func(t *testing.T, fs afero.Fs, d *mocks.Dialer)
195196
want error
@@ -198,6 +199,7 @@ func TestNerdctlConfigApplier_Apply(t *testing.T) {
198199
name: "private key path doesn't exist",
199200
path: "/private-key",
200201
remoteAddr: "",
202+
hostUser: "mock-host-user",
201203
mockSvc: func(t *testing.T, fs afero.Fs, d *mocks.Dialer) {
202204
},
203205
want: fmt.Errorf(
@@ -232,7 +234,7 @@ func TestNerdctlConfigApplier_Apply(t *testing.T) {
232234
d := mocks.NewDialer(ctrl)
233235

234236
tc.mockSvc(t, fs, d)
235-
got := NewNerdctlApplier(d, fs, tc.path).Apply(tc.remoteAddr)
237+
got := NewNerdctlApplier(d, fs, tc.path, tc.hostUser).Apply(tc.remoteAddr)
236238

237239
assert.Equal(t, tc.want, got)
238240
})

0 commit comments

Comments
 (0)