Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse normalized snapshotID #237

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading