From 458d20666bbb2fbbe205404c2ab001849e415df3 Mon Sep 17 00:00:00 2001 From: lucklove Date: Tue, 26 Jan 2021 14:55:32 +0800 Subject: [PATCH 1/2] Add custom field in global options Fix https://github.com/pingcap/tiup/issues/571 --- pkg/cluster/spec/spec.go | 1 + pkg/cluster/spec/spec_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/cluster/spec/spec.go b/pkg/cluster/spec/spec.go index ccdc9ea404..5c9a1bbae5 100644 --- a/pkg/cluster/spec/spec.go +++ b/pkg/cluster/spec/spec.go @@ -68,6 +68,7 @@ type ( ResourceControl meta.ResourceControl `yaml:"resource_control,omitempty" validate:"resource_control:editable"` OS string `yaml:"os,omitempty" default:"linux"` Arch string `yaml:"arch,omitempty" default:"amd64"` + Custom interface{} `yaml:"custom,omitempty" validate:"custom:ignore"` } // MonitoredOptions represents the monitored node configuration diff --git a/pkg/cluster/spec/spec_test.go b/pkg/cluster/spec/spec_test.go index 964ab1f130..6e4c9ce513 100644 --- a/pkg/cluster/spec/spec_test.go +++ b/pkg/cluster/spec/spec_test.go @@ -682,3 +682,20 @@ tiflash_servers: c.Check(err, NotNil) } } + +func (s *metaSuiteTopo) TestYAMLAnchor(c *C) { + topo := Specification{} + err := yaml.UnmarshalStrict([]byte(` +global: + custom: + tidb_spec: &tidb_spec + deploy_dir: "test-deploy" + log_dir: "test-deploy/log" + +tidb_servers: + - <<: *tidb_spec + host: 172.16.5.138 +`), &topo) + c.Assert(err, IsNil) + c.Assert(topo.TiDBServers[0].LogDir, Equals, "test-deploy/log") +} From e6984d661213b36fe4eeb488527fa1488c148d63 Mon Sep 17 00:00:00 2001 From: lucklove Date: Wed, 27 Jan 2021 15:54:22 +0800 Subject: [PATCH 2/2] Add test --- pkg/cluster/spec/spec_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkg/cluster/spec/spec_test.go b/pkg/cluster/spec/spec_test.go index 6e4c9ce513..b8c7e8a939 100644 --- a/pkg/cluster/spec/spec_test.go +++ b/pkg/cluster/spec/spec_test.go @@ -15,6 +15,7 @@ package spec import ( "bytes" + "strings" "testing" "github.com/BurntSushi/toml" @@ -695,7 +696,28 @@ global: tidb_servers: - <<: *tidb_spec host: 172.16.5.138 + deploy_dir: "fake-deploy" `), &topo) c.Assert(err, IsNil) + c.Assert(topo.TiDBServers[0].Host, Equals, "172.16.5.138") + c.Assert(topo.TiDBServers[0].DeployDir, Equals, "fake-deploy") c.Assert(topo.TiDBServers[0].LogDir, Equals, "test-deploy/log") } + +func (s *metaSuiteTopo) TestYAMLAnchorWithUndeclared(c *C) { + topo := Specification{} + err := yaml.UnmarshalStrict([]byte(` +global: + custom: + tidb_spec: &tidb_spec + deploy_dir: "test-deploy" + log_dir: "test-deploy/log" + undeclared: "some stuff" + +tidb_servers: + - <<: *tidb_spec + host: 172.16.5.138 +`), &topo) + c.Assert(err, NotNil) + c.Assert(strings.Contains(err.Error(), "not found"), IsTrue) +}