-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add warning for files with '-errors'
- Loading branch information
1 parent
f9fe802
commit 86b01f4
Showing
5 changed files
with
93 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/mesosphere/dkp-cli-runtime/core/output" | ||
"github.com/spf13/afero" | ||
|
||
"github.com/mhrabovcin/troubleshoot-live/pkg/bundle" | ||
) | ||
|
||
// WarnOnErrorsFilePresence is a helper function that will issue a CLI warn | ||
// message if for given file from bundle exists a file with `-errors` suffix. | ||
// This is `troubleshoot`s way of recording errors in the bundle. | ||
// E.g. `cluster-resources/pods.json` => `cluster-resources/pods-errors.json`. | ||
func WarnOnErrorsFilePresence(b bundle.Bundle, out output.Output, path string) { | ||
ext := filepath.Ext(path) | ||
base := strings.TrimSuffix(filepath.Base(path), ext) | ||
baseDir := filepath.Dir(path) | ||
errorsPath := filepath.Join(baseDir, fmt.Sprintf("%s-errors%s", base, ext)) | ||
|
||
if ok, _ := afero.Exists(b, errorsPath); ok { | ||
out.Warnf( | ||
"The file %q suggests that there were some error when collecting data for %q. The import may not be complete.", | ||
errorsPath, path, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/mesosphere/dkp-cli-runtime/core/output" | ||
"github.com/mhrabovcin/troubleshoot-live/pkg/bundle" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWarnOnErrorsFilePresence(t *testing.T) { | ||
stdOut, stdErr, out := newOutput(0) | ||
assert.Empty(t, stdOut.String()) | ||
assert.Empty(t, stdErr.String()) | ||
fs, err := newMemFs(map[string][]byte{ | ||
"cluster-resources/pods.json": nil, | ||
"cluster-resources/pods-errors.json": nil, | ||
}) | ||
require.NoError(t, err) | ||
b := bundle.FromFs(fs) | ||
|
||
WarnOnErrorsFilePresence(b, out, "cluster-resources/pods.json") | ||
assert.Empty(t, stdOut.String()) | ||
expectedErr := `WRN The file "cluster-resources/pods-errors.json" suggests that there were ` + | ||
`some error when collecting data for "cluster-resources/pods.json"` | ||
assert.Contains(t, stdErr.String(), expectedErr) | ||
} | ||
|
||
func newOutput(verbose int) (*bytes.Buffer, *bytes.Buffer, output.Output) { | ||
stdOut := &bytes.Buffer{} | ||
stdErr := &bytes.Buffer{} | ||
return stdOut, stdErr, output.NewNonInteractiveShell(stdOut, stdErr, verbose) | ||
} | ||
|
||
func newMemFs(files map[string][]byte) (afero.Fs, error) { | ||
fs := afero.NewMemMapFs() | ||
for path, data := range files { | ||
if err := afero.WriteReader(fs, path, bytes.NewReader(data)); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return fs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters