diff --git a/pkg/function/backup_data.go b/pkg/function/backup_data.go index 1e5256cedf..6552dd3402 100644 --- a/pkg/function/backup_data.go +++ b/pkg/function/backup_data.go @@ -2,6 +2,7 @@ package function import ( "context" + "regexp" "github.com/pkg/errors" @@ -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 @@ -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 } diff --git a/pkg/function/backup_data_test.go b/pkg/function/backup_data_test.go index 5e8b121e74..9e6118af4e 100644 --- a/pkg/function/backup_data_test.go +++ b/pkg/function/backup_data_test.go @@ -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 ¶m.Profile{ @@ -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 @@ -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)) + } +} diff --git a/pkg/function/data_test.go b/pkg/function/data_test.go index 6a582e6467..bb0fa7a697 100644 --- a/pkg/function/data_test.go +++ b/pkg/function/data_test.go @@ -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) } }