From dc42e1e5aac62a6f791f4435edd65cbb95789c44 Mon Sep 17 00:00:00 2001 From: Aaron Hetherington Date: Thu, 7 Dec 2023 14:12:57 +0000 Subject: [PATCH] Change default data directory and add checks to ensure it is writable --- pkg/workceptor/workceptor.go | 27 +++++++++++++++++++++++---- tests/functional/cli/cli_test.go | 2 +- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pkg/workceptor/workceptor.go b/pkg/workceptor/workceptor.go index 0057f9a77..b80ea914e 100644 --- a/pkg/workceptor/workceptor.go +++ b/pkg/workceptor/workceptor.go @@ -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, @@ -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 { + + 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")) diff --git a/tests/functional/cli/cli_test.go b/tests/functional/cli/cli_test.go index 5f1992d17..c1481c9f5 100644 --- a/tests/functional/cli/cli_test.go +++ b/tests/functional/cli/cli_test.go @@ -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()) } })