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

Return error when loading some undefined configs from local #687

Merged
merged 13 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from 9 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
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
18 changes: 16 additions & 2 deletions drainer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,22 @@ func (c *SyncerConfig) adjustDoDBAndTable() {
}

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("drainer config file %s contained unknown configuration options: %s", path, strings.Join(undecodedItems, ", "))
}

return err
}

// validate checks whether the configuration is valid
Expand Down
37 changes: 37 additions & 0 deletions drainer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
package drainer

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

"github.com/coreos/etcd/integration"
Expand Down Expand Up @@ -118,3 +122,36 @@ func (t *testDrainerSuite) TestAdjustConfig(c *C) {
c.Assert(cfg.ListenAddr, Equals, "http://0.0.0.0:8257")
c.Assert(cfg.AdvertiseAddr, Equals, "http://192.168.15.12:8257")
}

func (t *testDrainerSuite) TestConfigParsingFileWithInvalidOptions(c *C) {
yc := struct {
DataDir string `toml:"data-dir" json:"data-dir"`
ListenAddr string `toml:"addr" json:"addr"`
AdvertiseAddr string `toml:"advertise-addr" json:"advertise-addr"`
UnrecognizedOptionTest bool `toml:"unrecognized-option-test" json:"unrecognized-option-test"`
}{
"data.drainer",
"192.168.15.10:8257",
"192.168.15.10:8257",
true,
}

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

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

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

cfg := NewConfig()
err = cfg.Parse(args)
c.Assert(err, ErrorMatches, ".*contained unknown configuration options: unrecognized-option-test.*")
}
18 changes: 16 additions & 2 deletions pump/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,22 @@ func (cfg *Config) Parse(arguments []string) 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("pump config file %s contained unknown configuration options: %s", path, strings.Join(undecodedItems, ", "))
}

return err
}

// validate checks whether the configuration is valid
Expand Down
61 changes: 43 additions & 18 deletions pump/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ package pump

import (
"bytes"
"io/ioutil"
"os"

"github.com/BurntSushi/toml"
. "github.com/pingcap/check"
"io/ioutil"
"os"
"path"
)

var _ = Suite(&testConfigSuite{})
Expand Down Expand Up @@ -75,6 +75,7 @@ func (s *testConfigSuite) TestConfigParsingEnvFlags(c *C) {
os.Setenv("PUMP_ADDR", "192.168.199.200:9000")
os.Setenv("PUMP_PD_URLS", "http://127.0.0.1:2379,http://localhost:2379")
os.Setenv("PUMP_DATA_DIR", "/tmp/pump")
defer os.Clearenv()

cfg := NewConfig()
mustSuccess(c, cfg.Parse(args))
Expand All @@ -84,7 +85,7 @@ func (s *testConfigSuite) TestConfigParsingEnvFlags(c *C) {
func (s *testConfigSuite) TestConfigParsingFileFlags(c *C) {
yc := struct {
ListenAddr string `toml:"addr" json:"addr"`
AdvertiseAddr string `toml:"advertiser-addr" json:"advertise-addr"`
AdvertiseAddr string `toml:"advertise-addr" json:"advertise-addr"`
EtcdURLs string `toml:"pd-urls" json:"pd-urls"`
BinlogDir string `toml:"data-dir" json:"data-dir"`
HeartbeatInterval uint `toml:"heartbeat-interval" json:"heartbeat-interval"`
Expand All @@ -101,36 +102,60 @@ func (s *testConfigSuite) TestConfigParsingFileFlags(c *C) {
err := e.Encode(yc)
c.Assert(err, IsNil)

tmpfile := mustCreateCfgFile(c, buf.Bytes(), "pump_config")
defer os.Remove(tmpfile.Name())
configFilename := path.Join(c.MkDir(), "pump_config.toml")
err = ioutil.WriteFile(configFilename, buf.Bytes(), 0644)
c.Assert(err, IsNil)

args := []string{
"--config",
tmpfile.Name(),
configFilename,
"-L", "debug",
}

os.Clearenv()
cfg := NewConfig()
mustSuccess(c, cfg.Parse(args))
validateConfig(c, cfg)
}

func mustSuccess(c *C, err error) {
func (s *testConfigSuite) TestConfigParsingFileWithInvalidArgs(c *C) {
yc := struct {
ListenAddr string `toml:"addr" json:"addr"`
AdvertiseAddr string `toml:"advertise-addr" json:"advertise-addr"`
EtcdURLs string `toml:"pd-urls" json:"pd-urls"`
BinlogDir string `toml:"data-dir" json:"data-dir"`
HeartbeatInterval uint `toml:"heartbeat-interval" json:"heartbeat-interval"`
UnrecognizedOptionTest bool `toml:"unrecognized-option-test" json:"unrecognized-option-test"`
}{
"192.168.199.100:8260",
"192.168.199.100:8260",
"http://192.168.199.110:2379,http://hostname:2379",
"/tmp/pump",
1500,
true,
}

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

func mustCreateCfgFile(c *C, b []byte, prefix string) *os.File {
tmpfile, err := ioutil.TempFile("", prefix)
mustSuccess(c, err)
configFilename := path.Join(c.MkDir(), "pump_config_invalid.toml")
err = ioutil.WriteFile(configFilename, buf.Bytes(), 0644)
c.Assert(err, IsNil)

_, err = tmpfile.Write(b)
mustSuccess(c, err)
args := []string{
"--config",
configFilename,
"-L", "debug",
}

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

return tmpfile
func mustSuccess(c *C, err error) {
c.Assert(err, IsNil)
}

func validateConfig(c *C, cfg *Config) {
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
43 changes: 43 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,46 @@ 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"`
UnrecognizedOptionTest bool `toml:"unrecognized-option-test" json:"unrecognized-option-test"`
}{
"/tmp/reparo",
"",
"",
0,
0,
"tmp/reparo/reparo.log",
"debug",
true,
}

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