From bdf49c44091b7c7064bfd12e0e44a1efe1607a94 Mon Sep 17 00:00:00 2001 From: John Schnake Date: Fri, 17 Jan 2020 08:44:55 -0600 Subject: [PATCH] Use filepath instead of path when possible There may be more of these situations lurking around, but as we try to support Windows nodes we need to make sure that we are using filepath unless we know we need forward slashes (like in URLs). This changes a few calls to path.XYZ to filepath.XYZ and updates some tests in the same way. xref #732 Signed-off-by: John Schnake --- cmd/sonobuoy/app/worker.go | 9 +++++---- pkg/client/retrieve.go | 6 +++--- pkg/worker/worker_test.go | 36 ++++++++++++++++++------------------ 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/cmd/sonobuoy/app/worker.go b/cmd/sonobuoy/app/worker.go index 64472a101..78d472fee 100644 --- a/cmd/sonobuoy/app/worker.go +++ b/cmd/sonobuoy/app/worker.go @@ -25,17 +25,18 @@ import ( "os" "os/signal" "path" + "path/filepath" "strings" "syscall" "time" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" "github.com/vmware-tanzu/sonobuoy/pkg/errlog" "github.com/vmware-tanzu/sonobuoy/pkg/plugin" "github.com/vmware-tanzu/sonobuoy/pkg/plugin/aggregation" "github.com/vmware-tanzu/sonobuoy/pkg/worker" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" - "github.com/spf13/cobra" ) // NewCmdWorker is the cobra command that acts as the entrypoint for Sonobuoy when running @@ -163,7 +164,7 @@ func runGather(global bool) error { } go worker.RelayProgressUpdates(cfg.ProgressUpdatesPort, progressURL.String(), client) - err = worker.GatherResults(cfg.ResultsDir+"/done", resultURL.String(), client, sigHandler(plugin.GracefulShutdownPeriod*time.Second)) + err = worker.GatherResults(filepath.Join(cfg.ResultsDir, "done"), resultURL.String(), client, sigHandler(plugin.GracefulShutdownPeriod*time.Second)) return errors.Wrap(err, "gathering results") } diff --git a/pkg/client/retrieve.go b/pkg/client/retrieve.go index 05528c08e..95ad03e60 100644 --- a/pkg/client/retrieve.go +++ b/pkg/client/retrieve.go @@ -27,8 +27,8 @@ import ( "github.com/vmware-tanzu/sonobuoy/pkg/config" pluginaggregation "github.com/vmware-tanzu/sonobuoy/pkg/plugin/aggregation" - "github.com/pkg/errors" + "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/remotecommand" @@ -142,7 +142,7 @@ func UntarAll(reader io.Reader, destFile, prefix string) (filenames []string, re entrySeq++ mode := header.FileInfo().Mode() outFileName := path.Join(destFile, header.Name[len(prefix):]) - baseName := path.Dir(outFileName) + baseName := filepath.Dir(outFileName) if err := os.MkdirAll(baseName, 0755); err != nil { return filenames, err } @@ -160,7 +160,7 @@ func UntarAll(reader io.Reader, destFile, prefix string) (filenames []string, re return filenames, err } if exists { - outFileName = filepath.Join(outFileName, path.Base(header.Name)) + outFileName = filepath.Join(outFileName, filepath.Base(header.Name)) } } diff --git a/pkg/worker/worker_test.go b/pkg/worker/worker_test.go index 3931d9f66..811011d49 100644 --- a/pkg/worker/worker_test.go +++ b/pkg/worker/worker_test.go @@ -23,7 +23,7 @@ import ( "net/http/httptest" "os" "os/exec" - "path" + "path/filepath" "strings" "testing" @@ -52,14 +52,14 @@ func TestRun(t *testing.T) { } withTempDir(t, func(tmpdir string) { - ioutil.WriteFile(tmpdir+"/systemd_logs", []byte("{}"), 0755) - ioutil.WriteFile(tmpdir+"/done", []byte(tmpdir+"/systemd_logs"), 0755) - err := GatherResults(tmpdir+"/done", URL, srv.Client(), nil) + ioutil.WriteFile(filepath.Join(tmpdir, "systemd_logs"), []byte("{}"), 0755) + ioutil.WriteFile(filepath.Join(tmpdir, "done"), []byte(filepath.Join(tmpdir, "systemd_logs")), 0755) + err := GatherResults(filepath.Join(tmpdir, "done"), URL, srv.Client(), nil) if err != nil { t.Fatalf("Got error running agent: %v", err) } - ensureExists(t, path.Join(aggr.OutputDir, "systemd_logs", "results", "node1")) + ensureExists(t, filepath.Join(aggr.OutputDir, "systemd_logs", "results", "node1")) }) } }) @@ -79,14 +79,14 @@ func TestRunGlobal(t *testing.T) { } withTempDir(t, func(tmpdir string) { - ioutil.WriteFile(tmpdir+"/systemd_logs.json", []byte("{}"), 0755) - ioutil.WriteFile(tmpdir+"/done", []byte(tmpdir+"/systemd_logs.json"), 0755) - err := GatherResults(tmpdir+"/done", url, srv.Client(), nil) + ioutil.WriteFile(filepath.Join(tmpdir, "systemd_logs.json"), []byte("{}"), 0755) + ioutil.WriteFile(filepath.Join(tmpdir, "done"), []byte(filepath.Join(tmpdir, "systemd_logs.json")), 0755) + err := GatherResults(filepath.Join(tmpdir, "done"), url, srv.Client(), nil) if err != nil { t.Fatalf("Got error running agent: %v", err) } - ensureExists(t, path.Join(aggr.OutputDir, "systemd_logs", "results")) + ensureExists(t, filepath.Join(aggr.OutputDir, "systemd_logs", "results")) }) }) } @@ -104,14 +104,14 @@ func TestRunGlobal_noExtension(t *testing.T) { t.Fatalf("unexpected error getting global result url %v", err) } withTempDir(t, func(tmpdir string) { - ioutil.WriteFile(tmpdir+"/systemd_logs", []byte("{}"), 0755) - ioutil.WriteFile(tmpdir+"/done", []byte(tmpdir+"/systemd_logs"), 0755) - err := GatherResults(tmpdir+"/done", url, srv.Client(), nil) + ioutil.WriteFile(filepath.Join(tmpdir, "systemd_logs"), []byte("{}"), 0755) + ioutil.WriteFile(filepath.Join(tmpdir, "done"), []byte(filepath.Join(tmpdir, "systemd_logs")), 0755) + err := GatherResults(filepath.Join(tmpdir, "done"), url, srv.Client(), nil) if err != nil { t.Fatalf("Got error running agent: %v", err) } - ensureExists(t, path.Join(aggr.OutputDir, "systemd_logs", "results")) + ensureExists(t, filepath.Join(aggr.OutputDir, "systemd_logs", "results")) }) }) } @@ -131,7 +131,7 @@ func TestRunGlobalCleanup(t *testing.T) { } withTempDir(t, func(tmpdir string) { - err := GatherResults(tmpdir+"/done", url, srv.Client(), stopc) + err := GatherResults(filepath.Join(tmpdir, "done"), url, srv.Client(), stopc) if err != nil { t.Fatalf("Got error running agent: %v", err) } @@ -188,10 +188,10 @@ func TestRelayProgress(t *testing.T) { } } -func ensureExists(t *testing.T, filepath string) { - if _, err := os.Stat(filepath); err != nil && os.IsNotExist(err) { - t.Logf("Plugin agent ran, but couldn't find expected results at %v:", filepath) - output, _ := exec.Command("ls", "-l", path.Dir(filepath)).CombinedOutput() +func ensureExists(t *testing.T, checkPath string) { + if _, err := os.Stat(checkPath); err != nil && os.IsNotExist(err) { + t.Logf("Plugin agent ran, but couldn't find expected results at %v:", checkPath) + output, _ := exec.Command("ls", "-l", filepath.Dir(checkPath)).CombinedOutput() t.Log(string(output)) t.Fail() }