Skip to content

Commit

Permalink
Use filepath instead of path when possible (#1073)
Browse files Browse the repository at this point in the history
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 <jschnake@vmware.com>
  • Loading branch information
johnSchnake authored Jan 17, 2020
1 parent 2f2bb1f commit c927a9f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
9 changes: 5 additions & 4 deletions cmd/sonobuoy/app/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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))
}
}

Expand Down
36 changes: 18 additions & 18 deletions pkg/worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"net/http/httptest"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -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"))
})
}
})
Expand All @@ -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"))
})
})
}
Expand All @@ -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"))
})
})
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit c927a9f

Please sign in to comment.