Skip to content

Commit

Permalink
Change default data directory and add checks to ensure it is writable
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronH88 committed Dec 7, 2023
1 parent f91b94c commit dc42e1e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 23 additions & 4 deletions pkg/workceptor/workceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ type workType struct {

// New constructs a new Workceptor instance.
func New(ctx context.Context, nc NetceptorForWorkceptor, dataDir string) (*Workceptor, error) {
if dataDir == "" {
dataDir = path.Join(os.TempDir(), "receptor")
}
dataDir = path.Join(dataDir, nc.NodeID())
dataDir = setDataDir(dataDir, nc)
c, cancel := context.WithCancel(ctx)
w := &Workceptor{
ctx: c,
Expand All @@ -86,6 +83,28 @@ func New(ctx context.Context, nc NetceptorForWorkceptor, dataDir string) (*Workc
// MainInstance is the global instance of Workceptor instantiated by the command-line main() function.
var MainInstance *Workceptor

// setDataDir returns a valid data directory.
func setDataDir(dataDir string, nc NetceptorForWorkceptor) string {
if _, err := os.Stat(dataDir); os.IsNotExist(err) {
nc.GetLogger().Warning("Receptor data directory provided does not exist \"%s\". Trying the default '/var/run/receptor/", dataDir)
} else {

Check failure on line 90 in pkg/workceptor/workceptor.go

View workflow job for this annotation

GitHub Actions / lint-receptor

unnecessary leading newline (whitespace)

Check failure on line 91 in pkg/workceptor/workceptor.go

View workflow job for this annotation

GitHub Actions / lint-receptor

File is not `gofumpt`-ed (gofumpt)
return path.Join(dataDir, nc.NodeID())
}

dataDir = "/var/run/receptor"
if _, err := os.Stat(dataDir); os.IsNotExist(err) {
nc.GetLogger().Warning("Receptor data directory \"%s\" does not exist. Setting tmp '/tmp/receptor/", dataDir)
} else {
return path.Join(dataDir, nc.NodeID())
}

dataDir = path.Join(os.TempDir(), "receptor")
dataDir = path.Join(dataDir, nc.NodeID())

return dataDir
}

// stdoutSize returns size of stdout, if it exists, or 0 otherwise.
func stdoutSize(unitdir string) int64 {
stat, err := os.Stat(path.Join(unitdir, "stdout"))
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestNegativeCost(t *testing.T) {

cmd.Process.Kill()
cmd.Wait()
if receptorStdOut.String() != "Error: connection cost must be positive\n" {
if !strings.Contains(receptorStdOut.String(), "Error: connection cost must be positive") {
t.Fatalf("Expected stdout: Error: connection cost must be positive, actual stdout: %s", receptorStdOut.String())
}
})
Expand Down

0 comments on commit dc42e1e

Please sign in to comment.