Skip to content

Commit

Permalink
[K10-2133] Refactor Restic GetSnapshotIDFromLog (#5322)
Browse files Browse the repository at this point in the history
* Refactor restic GetSnapshotIDFromLog

* Add restic_test

* [K10-2133] Refactor snapshot id from log func (#5324)

* Refactor GetSnapshotIDFromLog funcs

* Check backupID is same for all operations
  • Loading branch information
DeepikaDixit authored and Ilya Kislenko committed Apr 2, 2019
1 parent e220ac8 commit 0139202
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 56 deletions.
19 changes: 1 addition & 18 deletions pkg/function/backup_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package function

import (
"context"
"regexp"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/rand"
Expand Down Expand Up @@ -56,22 +55,6 @@ 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, encryptionKey string
var err error
Expand Down Expand Up @@ -116,7 +99,7 @@ func (*backupDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args m
return nil, errors.Wrapf(err, "Failed to create and upload backup")
}
// Get the snapshot ID from log
backupID := getSnapshotIDFromLog(stdout)
backupID := restic.SnapshotIDFromBackupLog(stdout)
if backupID == "" {
return nil, errors.New("Failed to parse the backup ID from logs")
}
Expand Down
15 changes: 0 additions & 15 deletions pkg/function/backup_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,3 @@ func (s *BackupDataSuite) 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))
}
}
2 changes: 1 addition & 1 deletion pkg/function/copy_volume_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func copyVolumeData(ctx context.Context, cli kubernetes.Interface, tp param.Temp
return nil, errors.Wrapf(err, "Failed to create and upload backup")
}
// Get the snapshot ID from log
backupID := getSnapshotIDFromLog(stdout)
backupID := restic.SnapshotIDFromBackupLog(stdout)
if backupID == "" {
return nil, errors.New("Failed to parse the backup ID from logs")
}
Expand Down
17 changes: 1 addition & 16 deletions pkg/function/delete_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package function

import (
"context"
"encoding/json"

"github.com/pkg/errors"

Expand Down Expand Up @@ -93,7 +92,7 @@ func (*deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args m
if err != nil {
return nil, errors.Wrapf(err, "Failed to forget data, could not get snapshotID from tag, Tag: %s", deleteTag)
}
deleteIdentifier, err = GetSnapshotIDFromLog(stdout)
deleteIdentifier, err = restic.SnapshotIDFromSnapshotLog(stdout)
if err != nil {
return nil, errors.Wrapf(err, "Failed to forget data, could not get snapshotID from tag, Tag: %s", deleteTag)
}
Expand Down Expand Up @@ -123,17 +122,3 @@ func (*deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args m
func (*deleteDataFunc) RequiredArgs() []string {
return []string{DeleteDataNamespaceArg, DeleteDataBackupArtifactPrefixArg}
}

// GetSnapshotIDFromLog gets the SnapshotID from log
func GetSnapshotIDFromLog(output string) (string, error) {
var result []map[string]interface{}
err := json.Unmarshal([]byte(output), &result)
if err != nil {
return "", errors.WithMessage(err, "Failed to unmarshall output from snapshotCommand")
}
if len(result) != 1 {
return "", errors.New("Snapshot not found")
}
snapId := result[0]["short_id"]
return snapId.(string), nil
}
33 changes: 33 additions & 0 deletions pkg/restic/restic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package restic

import (
"encoding/json"
"fmt"
"regexp"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -135,3 +137,34 @@ func GetOrCreateRepository(cli kubernetes.Interface, namespace, pod, container,
format.Log(pod, container, stderr)
return errors.Wrapf(err, "Failed to create object store backup location")
}

// SnapshotIDFromSnapshotLog gets the SnapshotID from Snapshot Command log
func SnapshotIDFromSnapshotLog(output string) (string, error) {
var result []map[string]interface{}
err := json.Unmarshal([]byte(output), &result)
if err != nil {
return "", errors.WithMessage(err, "Failed to unmarshall output from snapshotCommand")
}
if len(result) != 1 {
return "", errors.New("Snapshot not found")
}
snapId := result[0]["short_id"]
return snapId.(string), nil
}

// SnapshotIDFromBackupLog gets the SnapshotID from Backup Command log
func SnapshotIDFromBackupLog(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 ""
}
30 changes: 24 additions & 6 deletions pkg/function/delete_data_test.go → pkg/restic/restic_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package function
package restic

import (
"testing"

. "gopkg.in/check.v1"
)

type DeleteDataSuite struct {
}
type ResticDataSuite struct{}

func Test(t *testing.T) { TestingT(t) }

var _ = Suite(&DeleteDataSuite{})
var _ = Suite(&ResticDataSuite{})

func (s *DeleteDataSuite) TestGetSnapshotIDFromTag(c *C) {
func (s *ResticDataSuite) TestGetSnapshotIDFromTag(c *C) {
for _, tc := range []struct {
log string
expected string
Expand All @@ -19,9 +22,24 @@ func (s *DeleteDataSuite) TestGetSnapshotIDFromTag(c *C) {
{log: `[{"time":"2019-03-28T17:35:15.146526-07:00","hostname":"MacBook-Pro.local","username":"abc","uid":501,"gid":20,"tags":["backup123"],"id":"7c0bfeb93dd5b390a6eaf8a386ec8cb86e4631f2d96400407b529b53d979536a","short_id":"7c0bfeb9"},{"time":"2019-03-28T17:35:15.146526-07:00","hostname":"MacBook-Pro.local","username":"abc","uid":501,"gid":20,"tags":["backup123"],"id":"7c0bfeb93dd5b390a6eaf8a386ec8cb86e4631f2d96400407b529b53d979536a","short_id":"7c0bfeb9"}]`, expected: "", checker: NotNil},
{log: `null`, expected: "", checker: NotNil},
} {
id, err := GetSnapshotIDFromLog(tc.log)
id, err := SnapshotIDFromSnapshotLog(tc.log)
c.Assert(err, tc.checker)
c.Assert(id, Equals, tc.expected)

}
}

func (s *ResticDataSuite) TestGetSnapshotID(c *C) {
for _, tc := range []struct {
log string
expected string
}{
{"snapshot 1a2b3c4d saved", "1a2b3c4d"},
{"snapshot 123abcd", ""},
{"Invalid message", ""},
{"snapshot abc123\n saved", ""},
} {
id := SnapshotIDFromBackupLog(tc.log)
c.Check(id, Equals, tc.expected, Commentf("Failed for log: %s", tc.log))
}
}

0 comments on commit 0139202

Please sign in to comment.