Skip to content

Commit

Permalink
BackupData: Capture and return snapshotID from logs (#4307)
Browse files Browse the repository at this point in the history
Parse and capture the snapshot ID from the logs.
  • Loading branch information
pavannd1 authored and Ilya Kislenko committed Oct 30, 2018
1 parent 08fed5f commit 11ff4de
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
21 changes: 21 additions & 0 deletions pkg/function/backup_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package function

import (
"context"
"regexp"

"github.com/pkg/errors"

Expand Down Expand Up @@ -52,6 +53,22 @@ func validateProfile(profile *param.Profile) error {
return nil
}

func getSnapshotIDFromLog(output string) string {
if output == "" {
return ""
}
logs := regexp.MustCompile("[\n]").Split(output, -1)
for _, l := range logs {
// Log should contain "snapshot ABC123 saved"
pattern := regexp.MustCompile(`snapshot\s(.*?)\ssaved$`)
match := pattern.FindAllStringSubmatch(l, 1)
if match != nil {
return match[0][1]
}
}
return ""
}

func (*backupDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) {
var namespace, pod, container, includePath, backupArtifactPrefix, backupIdentifier, encryptionKey string
var err error
Expand Down Expand Up @@ -97,6 +114,10 @@ func (*backupDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args m
if err != nil {
return nil, errors.Wrapf(err, "Failed to create and upload backup")
}
// Get the snapshot ID from log
if snapID := getSnapshotIDFromLog(stdout); snapID != "" {
return map[string]interface{}{"snapshotID": snapID}, nil
}
return nil, nil
}

Expand Down
21 changes: 18 additions & 3 deletions pkg/function/backup_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/kanisterio/kanister/pkg/param"
)

type ValidateProfileSuite struct {
type BackupDataSuite struct {
}

var _ = Suite(&ValidateProfileSuite{})
var _ = Suite(&BackupDataSuite{})

func newValidProfile() *param.Profile {
return &param.Profile{
Expand Down Expand Up @@ -56,7 +56,7 @@ func newInvalidProfile() *param.Profile {
}
}

func (s *ValidateProfileSuite) TestValidateProfile(c *C) {
func (s *BackupDataSuite) TestValidateProfile(c *C) {
testCases := []struct {
name string
profile *param.Profile
Expand All @@ -71,3 +71,18 @@ func (s *ValidateProfileSuite) TestValidateProfile(c *C) {
c.Check(err, tc.errChecker, Commentf("Test %s Failed", tc.name))
}
}

func (s *BackupDataSuite) TestGetSnapshotID(c *C) {
for _, tc := range []struct {
log string
expected string
}{
{"snapshot 1a2b3c4d saved", "1a2b3c4d"},
{"snapshot 123abcd", ""},
{"Invalid message", ""},
{"snapshot abc123\n saved", ""},
} {
id := getSnapshotIDFromLog(tc.log)
c.Check(id, Equals, tc.expected, Commentf("Failed for log: %s", tc.log))
}
}
5 changes: 4 additions & 1 deletion pkg/function/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ func (s *DataSuite) TestBackupRestoreData(c *C) {
phases, err := kanister.GetPhases(bp, actionName, *tp)
c.Assert(err, IsNil)
for _, p := range phases {
_, err = p.Exec(context.Background(), bp, actionName, *tp)
out, err := p.Exec(context.Background(), bp, actionName, *tp)
if out != nil {
c.Assert(out["snapshotID"], NotNil)
}
c.Assert(err, IsNil)
}
}
Expand Down

0 comments on commit 11ff4de

Please sign in to comment.