From 5876ac3f5f0478e663eda7d6624bea2f4d7c7c22 Mon Sep 17 00:00:00 2001 From: SIGSEGV Date: Tue, 2 Feb 2021 18:55:45 +0800 Subject: [PATCH] Add custom field in global options (#1098) * Add custom field in global options Fix https://github.com/pingcap/tiup/issues/571 * Add test Co-authored-by: Allen Zhong Co-authored-by: Ti Chi Robot <71242396+ti-chi-bot@users.noreply.github.com> --- pkg/cluster/spec/spec.go | 1 + pkg/cluster/spec/spec_test.go | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 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..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" @@ -682,3 +683,41 @@ 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 + 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) +}