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 4 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
17 changes: 15 additions & 2 deletions drainer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,21 @@ 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 {
lichunzhu marked this conversation as resolved.
Show resolved Hide resolved
if undecoded := metaData.Undecoded(); len(undecoded) > 0 {
var undecodedItems []string
for _, item := range undecoded {
undecodedItems = append(undecodedItems, item.String())
}
err = errors.New(fmt.Sprintf("config file %s contained unknown configuration options: %s", path, strings.Join(undecodedItems, ", ")))
lichunzhu marked this conversation as resolved.
Show resolved Hide resolved
}
}

return err
}

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

import (
"bytes"
"github.com/BurntSushi/toml"
kennytm marked this conversation as resolved.
Show resolved Hide resolved
"os"
"testing"

"github.com/coreos/etcd/integration"
Expand Down Expand Up @@ -118,3 +121,39 @@ 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)
suzaku marked this conversation as resolved.
Show resolved Hide resolved
err := e.Encode(yc)
c.Assert(err, IsNil)

tmpfile, err := util.CreateCfgFile(buf.Bytes(), "drainer_config")
if tmpfile != nil && len(tmpfile.Name()) > 0 {
lichunzhu marked this conversation as resolved.
Show resolved Hide resolved
defer os.Remove(tmpfile.Name())
}
c.Assert(err, IsNil)

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

os.Clearenv()
cfg := NewConfig()
err = cfg.Parse(args)
c.Assert(err, ErrorMatches, ".*contained unknown configuration options:.*")
}
22 changes: 22 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package util
import (
"context"
"fmt"
"io/ioutil"
"net"
"os"
"time"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -258,3 +260,23 @@ func AdjustDuration(v *time.Duration, defValue time.Duration) {
*v = defValue
}
}

// CreateCfgFile creates and writes b as config file to dir prefix
func CreateCfgFile(b []byte, prefix string) (*os.File, error) {
lichunzhu marked this conversation as resolved.
Show resolved Hide resolved
tmpfile, err := ioutil.TempFile("", prefix)
if err != nil {
return tmpfile, errors.Trace(err)
}

_, err = tmpfile.Write(b)
if err != nil {
return tmpfile, errors.Trace(err)
}

err = tmpfile.Close()
if err != nil {
return tmpfile, errors.Trace(err)
}

return tmpfile, nil
}
15 changes: 15 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package util
import (
"context"
"errors"
"io/ioutil"
"net"
"os"
"testing"
"time"

Expand Down Expand Up @@ -110,6 +112,19 @@ func (s *utilSuite) TestStdLogger(c *C) {
c.Assert(entrys[2].Message, Matches, ".*hola:Goodbye!.*")
}

func (s *utilSuite) TestCreateCfgFile(c *C) {
var msg = []byte("hello world!")
filename := "TestCreateCfg"
tmpfile, err := CreateCfgFile(msg, filename)
if tmpfile != nil && len(tmpfile.Name()) > 0 {
defer os.Remove(tmpfile.Name())
}
c.Assert(err, IsNil)
b, err := ioutil.ReadFile(tmpfile.Name())
c.Assert(err, IsNil)
c.Assert(string(b), Equals, string(msg))
}

type getAddrIPSuite struct{}

var _ = Suite(&getAddrIPSuite{})
Expand Down
17 changes: 15 additions & 2 deletions pump/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,21 @@ 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 {
if undecoded := metaData.Undecoded(); len(undecoded) > 0 {
var undecodedItems []string
for _, item := range undecoded {
undecodedItems = append(undecodedItems, item.String())
}
err = errors.New(fmt.Sprintf("config file %s contained unknown configuration options: %s", path, strings.Join(undecodedItems, ", ")))
}
}

return err
}

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

import (
"bytes"
"io/ioutil"
"github.com/pingcap/tidb-binlog/pkg/util"
"os"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -84,7 +84,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,7 +101,12 @@ func (s *testConfigSuite) TestConfigParsingFileFlags(c *C) {
err := e.Encode(yc)
c.Assert(err, IsNil)

tmpfile := mustCreateCfgFile(c, buf.Bytes(), "pump_config")
tmpfile, err := util.CreateCfgFile(buf.Bytes(), "pump_config")
if tmpfile != nil && len(tmpfile.Name()) > 0 {
defer os.Remove(tmpfile.Name())
}
c.Assert(err, IsNil)

defer os.Remove(tmpfile.Name())

args := []string{
Expand All @@ -116,21 +121,47 @@ func (s *testConfigSuite) TestConfigParsingFileFlags(c *C) {
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)
tmpfile, err := util.CreateCfgFile(buf.Bytes(), "pump_config")
c.Assert(err, IsNil)

_, err = tmpfile.Write(b)
mustSuccess(c, err)
defer os.Remove(tmpfile.Name())

err = tmpfile.Close()
mustSuccess(c, err)
args := []string{
"--config",
tmpfile.Name(),
"-L", "debug",
}

return tmpfile
os.Clearenv()
lichunzhu marked this conversation as resolved.
Show resolved Hide resolved
cfg := NewConfig()
err = cfg.Parse(args)
c.Assert(err, ErrorMatches, ".*contained unknown configuration options.*")
}

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

func validateConfig(c *C, cfg *Config) {
Expand Down