Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include positions filename in the error when YAML unmarshal fails. #1311

Merged
merged 2 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Unmarshal(dst interface{}, sources ...Source) error {

for _, source := range sources {
if err := source(dst); err != nil {
return errors.Wrap(err, "sourcing")
return err
}
}
return nil
Expand Down
9 changes: 6 additions & 3 deletions pkg/cfg/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"flag"
"io/ioutil"

yaml "gopkg.in/yaml.v2"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

// JSON returns a Source that opens the supplied `.json` file and loads it.
Expand All @@ -20,7 +21,8 @@ func JSON(f *string) Source {
return err
}

return dJSON(j)(dst)
err = dJSON(j)(dst)
return errors.Wrap(err, *f)
}
}

Expand All @@ -43,7 +45,8 @@ func YAML(f *string) Source {
return err
}

return dYAML(y)(dst)
err = dYAML(y)(dst)
return errors.Wrap(err, *f)
}
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/promtail/positions/positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package positions

import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -181,7 +182,8 @@ func (p *Positions) cleanup() {
}

func readPositionsFile(filename string) (map[string]string, error) {
buf, err := ioutil.ReadFile(filepath.Clean(filename))
cleanfn := filepath.Clean(filename)
buf, err := ioutil.ReadFile(cleanfn)
if err != nil {
if os.IsNotExist(err) {
return map[string]string{}, nil
Expand All @@ -191,7 +193,7 @@ func readPositionsFile(filename string) (map[string]string, error) {

var p File
if err := yaml.UnmarshalStrict(buf, &p); err != nil {
return nil, err
return nil, fmt.Errorf("%s: %v", cleanfn, err)
}

return p.Positions, nil
Expand Down
85 changes: 85 additions & 0 deletions pkg/promtail/positions/positions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package positions

import (
"io/ioutil"
"os"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func tempFilename(t *testing.T) string {
t.Helper()

temp, err := ioutil.TempFile("", "positions")
if err != nil {
t.Fatal("tempFilename:", err)
}
err = temp.Close()
if err != nil {
t.Fatal("tempFilename:", err)
}

name := temp.Name()
err = os.Remove(name)
if err != nil {
t.Fatal("tempFilename:", err)
}

return name
}

func TestReadPositionsOK(t *testing.T) {
temp := tempFilename(t)
defer func() {
_ = os.Remove(temp)
}()

yaml := []byte(`positions:
/tmp/random.log: "17623"
`)
err := ioutil.WriteFile(temp, yaml, 0644)
if err != nil {
t.Fatal(err)
}

pos, err := readPositionsFile(temp)
require.NoError(t, err)
require.Equal(t, "17623", pos["/tmp/random.log"])
}

func TestReadPositionsFromDir(t *testing.T) {
temp := tempFilename(t)
err := os.Mkdir(temp, 0644)
if err != nil {
t.Fatal(err)
}

defer func() {
_ = os.Remove(temp)
}()

_, err = readPositionsFile(temp)
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), temp)) // error must contain filename
}

func TestReadPositionsFromBadYaml(t *testing.T) {
temp := tempFilename(t)
defer func() {
_ = os.Remove(temp)
}()

badYaml := []byte(`positions:
/tmp/random.log: "176
`)
err := ioutil.WriteFile(temp, badYaml, 0644)
if err != nil {
t.Fatal(err)
}

_, err = readPositionsFile(temp)
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), temp)) // error must contain filename
}