This is an automated email from the ASF dual-hosted git repository.

miaoliyao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git


The following commit(s) were added to refs/heads/main by this push:
     new c26e67e  feat: init instance method and test it (#207)
c26e67e is described below

commit c26e67e49466bcc6de6c6da57d7f4ffd1ddc1210
Author: lltgo <[email protected]>
AuthorDate: Fri Feb 17 11:42:42 2023 +0800

    feat: init instance method and test it (#207)
---
 pitr/agent/internal/cons/error.go         | 10 ++++----
 pitr/agent/internal/pkg/opengauss.go      | 26 +++++++++++++++++++--
 pitr/agent/internal/pkg/opengauss_test.go | 38 +++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 6 deletions(-)

diff --git a/pitr/agent/internal/cons/error.go 
b/pitr/agent/internal/cons/error.go
index 189b9f9..d1feb14 100644
--- a/pitr/agent/internal/cons/error.go
+++ b/pitr/agent/internal/cons/error.go
@@ -22,8 +22,10 @@ import (
 )
 
 var (
-       Internal          = xerror.New(10000, "Internal error.")
-       InvalidHttpHeader = xerror.New(10001, "Invalid http header.")
-       DataNotFound      = xerror.New(10002, "Data not found.")
-       CmdOperateFailed  = xerror.New(10003, "Command operate failed")
+       Internal               = xerror.New(10000, "Internal error.")
+       InvalidHttpHeader      = xerror.New(10001, "Invalid http header.")
+       DataNotFound           = xerror.New(10002, "Data not found.")
+       CmdOperateFailed       = xerror.New(10003, "Command operate failed.")
+       BackupPathAlreadyExist = xerror.New(10004, "The backup path already 
exists.")
+       NoPermission           = xerror.New(10005, "No permission to operate.")
 )
diff --git a/pitr/agent/internal/pkg/opengauss.go 
b/pitr/agent/internal/pkg/opengauss.go
index 6d89b30..bcfcbec 100644
--- a/pitr/agent/internal/pkg/opengauss.go
+++ b/pitr/agent/internal/pkg/opengauss.go
@@ -21,6 +21,7 @@ import (
        "encoding/json"
        "errors"
        "fmt"
+       "strings"
 
        "github.com/dlclark/regexp2"
 
@@ -37,6 +38,8 @@ const (
        _backupFmt    = "gs_probackup backup --backup-path=%s --instance=%s 
--backup-mode=%s --pgdata=%s 2>&1"
        _showFmt      = "gs_probackup show --instance=%s --backup-path=%s 
--backup-id=%s --format=json 2>&1"
        _delBackupFmt = "gs_probackup delete --backup-path=%s --instance=%s 
--backup-id=%s 2>&1"
+       _initFmt      = "gs_probackup init --backup-path=%s 2>&1"
+       _deinitFmt    = "rm -r %s"
 )
 
 func (og *openGauss) AsyncBackup(backupPath, instanceName, backupMode, pgData 
string) (string, error) {
@@ -94,11 +97,30 @@ func (og *openGauss) delBackup(backupPath, instanceName, 
backupID string) error
        if err != nil {
                return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", 
og.shell, cmd, err)
        }
+       return nil
+}
+
+func (og *openGauss) init(backupPath string) error {
+       cmd := fmt.Sprintf(_initFmt, backupPath)
+       _, err := cmds.Exec(og.shell, cmd)
+       // already exist and it's not empty
        if errors.Is(err, cons.CmdOperateFailed) {
-               return cons.CmdOperateFailed
+               return cons.BackupPathAlreadyExist
        }
        if err != nil {
-               return fmt.Errorf("cmds.Exec[shell=%s, cmd=%s] return err=%w", 
og.shell, cmd, err)
+               return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", 
og.shell, cmd, err)
+       }
+       return nil
+}
+
+func (og *openGauss) deinit(backupPath string) error {
+       if !strings.HasPrefix(backupPath, "/home/omm/") {
+               return cons.NoPermission
+       }
+
+       cmd := fmt.Sprintf(_deinitFmt, backupPath)
+       if _, err := cmds.Exec(og.shell, cmd); err != nil {
+               return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", 
og.shell, cmd, err)
        }
        return nil
 }
diff --git a/pitr/agent/internal/pkg/opengauss_test.go 
b/pitr/agent/internal/pkg/opengauss_test.go
index a6cb516..02b33cd 100644
--- a/pitr/agent/internal/pkg/opengauss_test.go
+++ b/pitr/agent/internal/pkg/opengauss_test.go
@@ -78,4 +78,42 @@ var _ = Describe("OpenGauss,requires opengauss environment", 
func() {
                        Expect(errors.Is(err, 
cons.CmdOperateFailed)).To(BeTrue())
                })
        })
+
+       Context("Init and deinit", func() {
+               It("init backup and clean up the env", func() {
+                       og := &openGauss{
+                               shell: "/bin/sh",
+                       }
+
+                       data2 := "/home/omm/data2"
+
+                       err := og.init(data2)
+                       Expect(err).To(BeNil())
+
+                       err = og.init(data2)
+                       Expect(err).NotTo(BeNil())
+                       Expect(errors.Is(err, 
cons.BackupPathAlreadyExist)).To(BeTrue())
+
+                       err = og.deinit(data2)
+                       Expect(err).To(BeNil())
+
+                       // repeat validation
+                       err = og.init(data2)
+                       Expect(err).To(BeNil())
+                       err = og.deinit(data2)
+                       Expect(err).To(BeNil())
+
+               })
+
+               It("[/home/omm/]:no permission to operate other dirs", func() {
+                       og := &openGauss{
+                               shell: "/bin/sh",
+                       }
+
+                       data := "/home/omm2/data"
+
+                       err := og.deinit(data)
+                       Expect(errors.Is(err, cons.NoPermission)).To(BeTrue())
+               })
+       })
 })

Reply via email to