dcrd: Unable to override home directory

When building/testing in a containerized environment (such as Nix), the default home directory (/var/empty) is inaccessible. This prevents the unit tests from functioning:

dcrd> ++ go test -p 12 ./. -args --appdata /private/tmp/nix-build-dcrd-1.8.0.drv-1/tmp.G5ZHoVaIR3
dcrd> 2023-06-28 20:49:03.438 [INF] RPCS: Generating TLS certificates...
dcrd> 2023-06-28 20:49:03.457 [INF] RPCS: Done generating TLS certificates
dcrd> 2023-06-28 20:49:03.457 [INF] RPCS: Generating TLS certificates...
dcrd> 2023-06-28 20:49:03.464 [INF] RPCS: Done generating TLS certificates
dcrd> Error creating a default config file: mkdir /var/empty: file exists
dcrd> --- FAIL: TestLoadConfig (0.00s)
dcrd>     config_test.go:30: Failed to load dcrd config: loadConfig: failed to create home directory: mkdir /var/empty: file exists
dcrd> Error creating a default config file: mkdir /var/empty: file exists
dcrd> --- FAIL: TestDefaultAltDNSNames (0.00s)
dcrd>     config_test.go:41: Failed to load dcrd config: loadConfig: failed to create home directory: mkdir /var/empty: file exists
dcrd> Error creating a default config file: mkdir /var/empty: file exists
dcrd> --- FAIL: TestAltDNSNamesWithEnv (0.00s)
dcrd>     config_test.go:56: Failed to load dcrd config: loadConfig: failed to create home directory: mkdir /var/empty: file exists
dcrd> Error creating a default config file: mkdir /var/empty: file exists
dcrd> --- FAIL: TestAltDNSNamesWithArg (0.00s)
dcrd>     config_test.go:74: Failed to load dcrd config: loadConfig: failed to create home directory: mkdir /var/empty: file exists
dcrd> FAIL
dcrd> FAIL      github.com/decred/dcrd  0.190s
dcrd> FAIL

The logic in appDataDir will only consult the HOME environment variable if the lookup from /etc/passwd fails. This prevents me from overriding the value at runtime. If the HOME environment variable was consulted first, running the unit tests in a sandbox would be much easier.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 19 (17 by maintainers)

Most upvoted comments

Support for setting the directory via the DCRD_APPDATA environment variable has been merged.

$ DCRD_APPDATA=/tmp/foo/dcrd go test
2023-06-28 22:56:17.870 [INF] RPCS: Generating TLS certificates...
2023-06-28 22:56:17.898 [INF] RPCS: Done generating TLS certificates
2023-06-28 22:56:17.908 [INF] RPCS: Generating TLS certificates...
2023-06-28 22:56:17.918 [INF] RPCS: Done generating TLS certificates
PASS
ok      github.com/decred/dcrd  0.354s
$ ls /tmp/foo/dcrd
dcrd.conf  logs/