Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
92 changes: 92 additions & 0 deletions metadata/pkg/query-manager/paginator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* // Copyright 2023 The SODA Authors.
* //
* // Licensed under the Apache License, Version 2.0 (the "License");
* // you may not use this file except in compliance with the License.
* // You may obtain a copy of the License at
* //
* // http://www.apache.org/licenses/LICENSE-2.0
* //
* // Unless required by applicable law or agreed to in writing, software
* // distributed under the License is distributed on an "AS IS" BASIS,
* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* // See the License for the specific language governing permissions and
* // limitations under the License.
*
*/

package query_manager

import (
"github.com/opensds/multi-cloud/metadata/pkg/model"
"testing"
)

func TestPaginate(t *testing.T) {
tests := []struct {
unPaginatedResult []*model.MetaBackend
limit int32
offset int32
expectedResult []*model.MetaBackend
}{
{
unPaginatedResult: []*model.MetaBackend{
{Id: "1"},
{Id: "2"},
{Id: "3"},
{Id: "4"},
{Id: "5"},
},
limit: 2,
offset: 0,
expectedResult: []*model.MetaBackend{
{Id: "1"},
{Id: "2"},
},
},
{
unPaginatedResult: []*model.MetaBackend{
{Id: "1"},
{Id: "2"},
{Id: "3"},
{Id: "4"},
{Id: "5"},
},
limit: 2,
offset: 2,
expectedResult: []*model.MetaBackend{
{Id: "3"},
{Id: "4"},
},
},
{
unPaginatedResult: []*model.MetaBackend{
{Id: "1"},
{Id: "2"},
{Id: "3"},
{Id: "4"},
{Id: "5"},
},
limit: 2,
offset: 5,
expectedResult: []*model.MetaBackend{},
},
{
unPaginatedResult: []*model.MetaBackend{},
limit: 2,
offset: 0,
expectedResult: []*model.MetaBackend{},
},
}
for i, test := range tests {
result := Paginate(test.unPaginatedResult, test.limit, test.offset)
if len(result) != len(test.expectedResult) {
t.Errorf("Test case %d: expected length of result to be %d but got %d", i, len(test.expectedResult), len(result))
}
for j, r := range result {
if r.Id != test.expectedResult[j].Id {
t.Errorf("Test case %d: expected result at index %d to be %s but got %s", i, j, test.expectedResult[j].Id, r.Id)
}
}
}
}
165 changes: 165 additions & 0 deletions metadata/pkg/query-manager/validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* // Copyright 2023 The SODA Authors.
* //
* // Licensed under the Apache License, Version 2.0 (the "License");
* // you may not use this file except in compliance with the License.
* // You may obtain a copy of the License at
* //
* // http://www.apache.org/licenses/LICENSE-2.0
* //
* // Unless required by applicable law or agreed to in writing, software
* // distributed under the License is distributed on an "AS IS" BASIS,
* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* // See the License for the specific language governing permissions and
* // limitations under the License.
*
*/

package query_manager

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/opensds/multi-cloud/metadata/pkg/constants"
pb "github.com/opensds/multi-cloud/metadata/proto"
)

func TestValidateInput(t *testing.T) {
tests := []struct {
input *pb.ListMetadataRequest
want bool
err string
}{
{
input: &pb.ListMetadataRequest{
SizeOfBucketInBytes: 0,
BucketSizeOperator: "",
SizeOfObjectInBytes: 0,
ObjectSizeOperator: "",
SortOrder: constants.ASC,
Region: "us-west-2",
Type: constants.AWS_S3,
},
want: true,
err: "",
},
{
input: &pb.ListMetadataRequest{
SizeOfBucketInBytes: 0,
BucketSizeOperator: "",
SizeOfObjectInBytes: 0,
ObjectSizeOperator: "",
SortOrder: constants.ASC,
Region: "invalid-region",
Type: constants.AWS_S3,
},
want: false,
err: "Not a valid AWS_S3 region",
},
{
input: &pb.ListMetadataRequest{
SizeOfBucketInBytes: 0,
BucketSizeOperator: "",
SizeOfObjectInBytes: 0,
ObjectSizeOperator: "",
SortOrder: constants.ASC,
Region: "us-west-2",
Type: "invalid-type",
},
want: false,
err: "Not a valid cloud type",
},
{
input: &pb.ListMetadataRequest{
SizeOfBucketInBytes: 0,
BucketSizeOperator: "",
SizeOfObjectInBytes: 0,
ObjectSizeOperator: "",
SortOrder: "invalid-sort",
Region: "us-west-2",
Type: constants.AWS_S3,
},
want: false,
err: "Invalid sort order",
},
}

for _, tc := range tests {
result, _ := ValidateInput(tc.input)
assert.Equal(t, tc.want, result)
}
}

func TestIsSortParamValid(t *testing.T) {
tests := []struct {
sortOrder string
expected bool
}{
{"", true},
{"ASC", true},
{"DESC", true},
{"abc", false},
}

for _, test := range tests {
result, _ := isSortParamValid(test.sortOrder)
if result != test.expected {
t.Errorf("isSortParamValid(%q) = %v, want %v", test.sortOrder, result, test.expected)
}
}
}

func TestIsSizeParamsValid(t *testing.T) {
tests := []struct {
sizeInBytes int64
operator string
expected bool
}{
{0, "", true},
{0, "gt", true},
{0, "lte", true},
{0, "eq", true},
{0, "gte", true},
{0, "lt", true},
{1, "gt", true},
{1, "lte", true},
{1, "eq", true},
{1, "gte", true},
{1, "lt", true},
{-1, "gt", false},
{-1, "lte", false},
{-1, "eq", false},
{-1, "gte", false},
{-1, "lt", false},
{1, "abc", false},
}

for _, test := range tests {
result, _ := isSizeParamsValid(test.sizeInBytes, test.operator)
if result != test.expected {
t.Errorf("isSizeParamsValid(%d, %q) = %v, want %v", test.sizeInBytes, test.operator, result, test.expected)
}
}
}

func TestIsValidRegion(t *testing.T) {
tests := []struct {
cloudType string
region string
expected bool
}{
{"AWS-S3", "us-west-1", true},
{"AWS-S3", "us-west-2", true},
{"AWS-S3", "abc", false},
{"", "", true},
{"abc", "us-west-1", false},
}

for _, test := range tests {
result, _ := isValidRegion(test.region, test.cloudType)
if result != test.expected {
t.Errorf("isValidRegion(%q, %q) = %v, want %v", test.region, test.cloudType, result, test.expected)
}
}
}
2 changes: 0 additions & 2 deletions metadata/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func (myc *S3Cred) Retrieve() (credentials.Value, error) {
}

func Sync(ctx context.Context, backend *backend.BackendDetail, in *pb.SyncMetadataRequest) error {
log.Debugln("the backend we got now....:%+v", backend)
sd, err := driver.CreateStorageDriver(backend.Type, backend)
if err != nil {
log.Errorln("failed to create driver. err:", err)
Expand All @@ -99,7 +98,6 @@ func Sync(ctx context.Context, backend *backend.BackendDetail, in *pb.SyncMetada
}

func (f *metadataService) SyncMetadata(ctx context.Context, in *pb.SyncMetadataRequest, out *pb.BaseResponse) error {
log.Infoln("received sncMetadata request in metadata service:%+v", in)
if in.Id != "" {
backend, err := utils.GetBackend(ctx, f.backendClient, in.Id)
if err != nil {
Expand Down
72 changes: 72 additions & 0 deletions metadata/pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* // Copyright 2023 The SODA Authors.
* //
* // Licensed under the Apache License, Version 2.0 (the "License");
* // you may not use this file except in compliance with the License.
* // You may obtain a copy of the License at
* //
* // http://www.apache.org/licenses/LICENSE-2.0
* //
* // Unless required by applicable law or agreed to in writing, software
* // distributed under the License is distributed on an "AS IS" BASIS,
* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* // See the License for the specific language governing permissions and
* // limitations under the License.
*
*/

package utils

import (
"github.com/opensds/multi-cloud/metadata/pkg/model"
"testing"
"time"
)

func TestGetBackends(t *testing.T) {
now := time.Now()
unPaginatedResult := []*model.MetaBackend{{
BackendName: "backend1",
Region: "region1",
Type: "type1",
NumberOfBuckets: 1,
Buckets: []*model.MetaBucket{{
Name: "bucket1",
Type: "type1",
Region: "region1",
TotalSize: 100,
NumberOfObjects: 1,
CreationDate: &now,
BucketTags: map[string]string{"key1": "value1"},
Objects: []*model.MetaObject{{
ObjectName: "object1",
LastModifiedDate: &now,
ServerSideEncryption: "true",
ExpiresDate: &now,
GrantControl: "grantControl1",
RedirectLocation: "redirect1",
ReplicationStatus: "replicated",
ObjectTags: map[string]string{"key1": "value1"},
Metadata: map[string]string{"key1": "value1"},
}},
}},
}}

protoBackends := GetBackends(unPaginatedResult)

if len(protoBackends) != 1 {
t.Errorf("Expected 1 proto backend, but got %d", len(protoBackends))
}

if protoBackends[0].BackendName != "backend1" {
t.Errorf("Expected backend name to be backend1, but got %s", protoBackends[0].BackendName)
}

if len(protoBackends[0].Buckets) != 1 {
t.Errorf("Expected 1 proto bucket, but got %d", len(protoBackends[0].Buckets))
}

if len(protoBackends[0].Buckets[0].Objects) != 1 {
t.Errorf("Expected 1 proto object, but got %d", len(protoBackends[0].Buckets[0].Objects))
}
}