From 450b25d608e5b97624285f1ce714ae0503bf4134 Mon Sep 17 00:00:00 2001 From: 9547 Date: Tue, 30 Mar 2021 23:39:36 +0800 Subject: [PATCH] cluster/pd: implement PDScaleScript.ConfigToFile --- pkg/cluster/template/scripts/pd.go | 9 +++++++++ pkg/cluster/template/scripts/pd_test.go | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/pkg/cluster/template/scripts/pd.go b/pkg/cluster/template/scripts/pd.go index 70efa136d7..109a78ef9a 100644 --- a/pkg/cluster/template/scripts/pd.go +++ b/pkg/cluster/template/scripts/pd.go @@ -149,3 +149,12 @@ func NewPDScaleScript(pdScript *PDScript) *PDScaleScript { func (c *PDScaleScript) Config() ([]byte, error) { return c.configWithScript("run_pd_scale.sh.tpl") } + +// ConfigToFile write config content to specific path +func (c *PDScaleScript) ConfigToFile(file string) error { + config, err := c.Config() + if err != nil { + return err + } + return os.WriteFile(file, config, 0755) +} diff --git a/pkg/cluster/template/scripts/pd_test.go b/pkg/cluster/template/scripts/pd_test.go index 5a522dbf0f..192807592d 100644 --- a/pkg/cluster/template/scripts/pd_test.go +++ b/pkg/cluster/template/scripts/pd_test.go @@ -13,6 +13,8 @@ package scripts import ( + "os" + "path" "strings" "testing" @@ -26,16 +28,35 @@ var _ = Suite(&pdSuite{}) func Test(t *testing.T) { TestingT(t) } func (s *pdSuite) TestScaleConfig(c *C) { + confDir, err := os.MkdirTemp("", "tiup-*") + c.Assert(err, IsNil) + defer os.RemoveAll(confDir) + + conf := path.Join(confDir, "pd0.conf") pdScript := NewPDScript("pd", "1.1.1.1", "/home/deploy/pd-2379", "/home/pd-data", "/home/pd-log") pdConfig, err := pdScript.Config() c.Assert(err, IsNil) c.Assert(strings.Contains(string(pdConfig), "--initial-cluster"), IsTrue) c.Assert(strings.Contains(string(pdConfig), "--join"), IsFalse) + err = pdScript.ConfigToFile(conf) + c.Assert(err, IsNil) + content, err := os.ReadFile(conf) + c.Assert(err, IsNil) + c.Assert(strings.Contains(string(content), "--initial-cluster"), IsTrue) + c.Assert(strings.Contains(string(content), "--join"), IsFalse) + scScript := NewPDScaleScript(pdScript) scConfig, err := scScript.Config() c.Assert(err, IsNil) c.Assert(pdConfig, Not(DeepEquals), scConfig) c.Assert(strings.Contains(string(scConfig), "--initial-cluster"), IsFalse) c.Assert(strings.Contains(string(scConfig), "--join"), IsTrue) + + err = scScript.ConfigToFile(conf) + c.Assert(err, IsNil) + content, err = os.ReadFile(conf) + c.Assert(err, IsNil) + c.Assert(strings.Contains(string(content), "--initial-cluster"), IsFalse) + c.Assert(strings.Contains(string(content), "--join"), IsTrue) }