Skip to content

Commit

Permalink
Merge pull request #237 from dell/delete_snapshot_id_parsing
Browse files Browse the repository at this point in the history
fix: parse normalized snapshotID
  • Loading branch information
mdutka-dell authored Feb 16, 2024
2 parents 1b7c311 + 889cf95 commit d55a851
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
13 changes: 8 additions & 5 deletions common/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,19 +379,22 @@ func GetNormalizedSnapshotID(ctx context.Context, snapshotID, clusterName, acces
func ParseNormalizedSnapshotID(ctx context.Context, snapID string) (string, string, string, error) {
log := GetRunIDLogger(ctx)
tokens := strings.Split(snapID, SnapshotIDSeparator)
if len(tokens) < 1 {
return "", "", "", fmt.Errorf("snapshot ID '%s' cannot be split into tokens", snapID)
if len(tokens) < 1 || snapID == "" {
return "", "", "", fmt.Errorf("snapshot ID cannot be split into tokens")
}

snapshotID := tokens[0]
var clusterName, accessZone string
if len(tokens) > 1 {
if len(tokens) > 2 {
clusterName = tokens[1]
accessZone = tokens[2]
} else if len(tokens) > 1 {
clusterName = tokens[1]
accessZone = ""
}

log.Debugf("normalized snapshot ID '%s' parsed into snapshot ID '%s' and cluster name '%s'",
snapID, snapshotID, clusterName)
log.Debugf("normalized snapshot ID '%s' parsed into snapshot ID '%s', cluster name '%s' and access zone '%s'",
snapID, snapshotID, clusterName, accessZone)

return snapshotID, clusterName, accessZone, nil
}
Expand Down
55 changes: 55 additions & 0 deletions common/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.

import (
"context"
"errors"
"fmt"
"testing"

Expand Down Expand Up @@ -81,6 +82,60 @@ func TestParseNormalizedVolumeID(t *testing.T) {
assert.NotNil(t, err)
}

func TestParseNormalizedSnapshotID(t *testing.T) {
ctx := context.Background()
testCases := []struct {
input string
expectedSnapID string
expectedCluster string
expectedZone string
expectedErr error
}{
{
input: "12345",
expectedSnapID: "12345",
expectedCluster: "",
expectedZone: "",
expectedErr: nil,
},
{
input: "12345=_=_=cluster1",
expectedSnapID: "12345",
expectedCluster: "cluster1",
expectedZone: "",
expectedErr: nil,
},
{
input: "12345=_=_=cluster1=_=_=zone1",
expectedSnapID: "12345",
expectedCluster: "cluster1",
expectedZone: "zone1",
expectedErr: nil,
},
{
input: "12345=_=_=cluster1=_=_=zone1=_=_=suffix",
expectedSnapID: "12345",
expectedCluster: "cluster1",
expectedZone: "zone1",
expectedErr: nil,
},
{
input: "",
expectedSnapID: "",
expectedCluster: "",
expectedZone: "",
expectedErr: errors.New("snapshot ID cannot be split into tokens"),
},
}
for _, tc := range testCases {
snapID, clusterName, accessZone, err := ParseNormalizedSnapshotID(ctx, tc.input)
assert.Equal(t, tc.expectedSnapID, snapID)
assert.Equal(t, tc.expectedCluster, clusterName)
assert.Equal(t, tc.expectedZone, accessZone)
assert.Equal(t, tc.expectedErr, err)
}
}

func TestGetPathForVolume(t *testing.T) {
isiPath := "/ifs/data"
volName := "k8s-123456"
Expand Down

0 comments on commit d55a851

Please sign in to comment.