Skip to content

Commit

Permalink
cluster/pd: implement PDScaleScript.ConfigToFile
Browse files Browse the repository at this point in the history
  • Loading branch information
9547 committed Mar 30, 2021
1 parent ca18569 commit 450b25d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/cluster/template/scripts/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
21 changes: 21 additions & 0 deletions pkg/cluster/template/scripts/pd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
package scripts

import (
"os"
"path"
"strings"
"testing"

Expand All @@ -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)
}

0 comments on commit 450b25d

Please sign in to comment.