Skip to content

Commit

Permalink
remove os.ClearEnv, add configFromFile error handler for arbiter & re…
Browse files Browse the repository at this point in the history
…paro
  • Loading branch information
lichunzhu committed Jul 24, 2019
1 parent 255ad42 commit 827732d
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
19 changes: 17 additions & 2 deletions arbiter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"flag"
"fmt"
"os"
"strings"

"github.com/BurntSushi/toml"
"github.com/pingcap/errors"
Expand Down Expand Up @@ -190,6 +191,20 @@ func (cfg *Config) adjustConfig() error {
}

func (cfg *Config) configFromFile(path string) error {
_, err := toml.DecodeFile(path, cfg)
return errors.Trace(err)
metaData, err := toml.DecodeFile(path, cfg)

// If any items in confFile file are not mapped into the Config struct, issue
// an error and stop the server from starting.
if err != nil {
return err
}
if undecoded := metaData.Undecoded(); len(undecoded) > 0 {
var undecodedItems []string
for _, item := range undecoded {
undecodedItems = append(undecodedItems, item.String())
}
err = errors.Errorf("arbiter config file %s contained unknown configuration options: %s", path, strings.Join(undecodedItems, ", "))
}

return err
}
35 changes: 35 additions & 0 deletions arbiter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
package arbiter

import (
"bytes"
"fmt"
"github.com/BurntSushi/toml"
"io/ioutil"
"path"
"runtime"
"strings"
Expand Down Expand Up @@ -105,6 +108,38 @@ func (t *TestConfigSuite) TestParseConfig(c *check.C) {
c.Assert(strings.Contains(config.String(), listenAddr), check.IsTrue)
}

func (t *TestConfigSuite) TestParseConfigFileWithInvalidArgs(c *check.C) {
yc := struct {
LogLevel string `toml:"log-level" json:"log-level"`
ListenAddr string `toml:"addr" json:"addr"`
LogFile string `toml:"log-file" json:"log-file"`
UnrecognizedOptionTest bool `toml:"unrecognized-option-test" json:"unrecognized-option-test"`
}{
"debug",
"127.0.0.1:8251",
"/tmp/arbiter",
true,
}

var buf bytes.Buffer
e := toml.NewEncoder(&buf)
err := e.Encode(yc)
c.Assert(err, check.IsNil)

configFilename := path.Join(c.MkDir(), "arbiter_config_invalid.toml")
err = ioutil.WriteFile(configFilename, buf.Bytes(), 0644)
c.Assert(err, check.IsNil)

args := []string{
"--config",
configFilename,
}

cfg := NewConfig()
err = cfg.Parse(args)
c.Assert(err, check.ErrorMatches, ".*contained unknown configuration options: unrecognized-option-test.*")
}

func getTemplateConfigFilePath() string {
// we put the template config file in "cmd/arbiter/arbiter.toml"
_, filename, _, _ := runtime.Caller(0)
Expand Down
2 changes: 0 additions & 2 deletions drainer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"bytes"
"github.com/BurntSushi/toml"
"io/ioutil"
"os"
"path"
"testing"

Expand Down Expand Up @@ -152,7 +151,6 @@ func (t *testDrainerSuite) TestConfigParsingFileWithInvalidOptions(c *C) {
"-L", "debug",
}

os.Clearenv()
cfg := NewConfig()
err = cfg.Parse(args)
c.Assert(err, ErrorMatches, ".*contained unknown configuration options: unrecognized-option-test.*")
Expand Down
2 changes: 0 additions & 2 deletions pump/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func (s *testConfigSuite) TestConfigParsingFileFlags(c *C) {
"-L", "debug",
}

os.Clearenv()
cfg := NewConfig()
mustSuccess(c, cfg.Parse(args))
validateConfig(c, cfg)
Expand Down Expand Up @@ -149,7 +148,6 @@ func (s *testConfigSuite) TestConfigParsingFileWithInvalidArgs(c *C) {
"-L", "debug",
}

os.Clearenv()
cfg := NewConfig()
err = cfg.Parse(args)
c.Assert(err, ErrorMatches, ".*contained unknown configuration options: unrecognized-option-test.*")
Expand Down
18 changes: 16 additions & 2 deletions reparo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,22 @@ func (c *Config) adjustDoDBAndTable() {
}

func (c *Config) configFromFile(path string) error {
_, err := toml.DecodeFile(path, c)
return errors.Trace(err)
metaData, err := toml.DecodeFile(path, c)

// If any items in confFile file are not mapped into the Config struct, issue
// an error and stop the server from starting.
if err != nil {
return err
}
if undecoded := metaData.Undecoded(); len(undecoded) > 0 {
var undecodedItems []string
for _, item := range undecoded {
undecodedItems = append(undecodedItems, item.String())
}
err = errors.Errorf("reparo config file %s contained unknown configuration options: %s", path, strings.Join(undecodedItems, ", "))
}

return err
}

func (c *Config) validate() error {
Expand Down
41 changes: 41 additions & 0 deletions reparo/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
package reparo

import (
"bytes"
"fmt"
"github.com/BurntSushi/toml"
"io/ioutil"
"path"
"runtime"

Expand Down Expand Up @@ -72,6 +75,44 @@ func (s *testConfigSuite) TestAdjustDoDBAndTable(c *check.C) {
c.Assert(config.DoDBs[1], check.Equals, "test2")
}

func (s *testConfigSuite) TestParseConfigFileWithInvalidArgs(c *check.C) {
yc := struct {
Dir string `toml:"data-dir" json:"data-dir"`
StartDatetime string `toml:"start-datetime" json:"start-datetime"`
StopDatetime string `toml:"stop-datetime" json:"stop-datetime"`
StartTSO int64 `toml:"start-tso" json:"start-tso"`
StopTSO int64 `toml:"stop-tso" json:"stop-tso"`
LogFile string `toml:"log-file" json:"log-file"`
LogLevel string `toml:"log-level" json:"log-level"`
}{
"/tmp/reparo",
"",
"",
0,
0,
"tmp/reparo/reparo.log",
"debug",
}

var buf bytes.Buffer
e := toml.NewEncoder(&buf)
err := e.Encode(yc)
c.Assert(err, check.IsNil)

configFilename := path.Join(c.MkDir(), "reparo_config_invalid.toml")
err = ioutil.WriteFile(configFilename, buf.Bytes(), 0644)
c.Assert(err, check.IsNil)

args := []string{
"--config",
configFilename,
}

cfg := NewConfig()
err = cfg.Parse(args)
c.Assert(err, check.ErrorMatches, ".*contained unknown configuration options: unrecognized-option-test.*")
}

func getTemplateConfigFilePath() string {
// we put the template config file in "cmd/reapro/reparo.toml"
_, filename, _, _ := runtime.Caller(0)
Expand Down

0 comments on commit 827732d

Please sign in to comment.