2023-07-18 12:27:26 -05:00
|
|
|
package dynamodb
|
2023-01-16 09:01:35 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
|
|
|
|
guuid "github.com/gofrs/uuid"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
2024-07-29 12:32:51 -05:00
|
|
|
"zotregistry.dev/zot/pkg/log"
|
2024-01-31 23:34:07 -05:00
|
|
|
"zotregistry.dev/zot/pkg/meta/version"
|
|
|
|
tskip "zotregistry.dev/zot/pkg/test/skip"
|
2023-01-16 09:01:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWrapperErrors(t *testing.T) {
|
2023-10-05 06:34:50 -05:00
|
|
|
tskip.SkipDynamo(t)
|
|
|
|
|
|
|
|
const region = "us-east-2"
|
2024-07-29 12:32:51 -05:00
|
|
|
|
2023-10-05 06:34:50 -05:00
|
|
|
endpoint := os.Getenv("DYNAMODBMOCK_ENDPOINT")
|
2023-01-16 09:01:35 -05:00
|
|
|
|
|
|
|
uuid, err := guuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
repoMetaTablename := "RepoMetadataTable" + uuid.String()
|
2023-04-24 13:13:15 -05:00
|
|
|
userDataTablename := "UserDataTable" + uuid.String()
|
2023-07-07 11:27:10 -05:00
|
|
|
apiKeyTablename := "ApiKeyTable" + uuid.String()
|
2023-03-10 13:37:29 -05:00
|
|
|
|
2023-01-16 09:01:35 -05:00
|
|
|
versionTablename := "Version" + uuid.String()
|
|
|
|
|
|
|
|
Convey("Create table errors", t, func() {
|
|
|
|
badEndpoint := endpoint + "1"
|
|
|
|
|
2024-06-13 00:51:32 -05:00
|
|
|
customResolver := aws.EndpointResolverWithOptionsFunc( //nolint: staticcheck
|
2023-01-16 09:01:35 -05:00
|
|
|
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
|
2024-06-13 00:51:32 -05:00
|
|
|
return aws.Endpoint{ //nolint: staticcheck
|
2023-01-16 09:01:35 -05:00
|
|
|
PartitionID: "aws",
|
|
|
|
URL: badEndpoint,
|
|
|
|
SigningRegion: region,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(region),
|
2024-06-13 00:51:32 -05:00
|
|
|
config.WithEndpointResolverWithOptions(customResolver)) //nolint: staticcheck
|
2023-01-16 09:01:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
dynamoWrapper := DynamoDB{
|
2023-10-30 15:06:04 -05:00
|
|
|
Client: dynamodb.NewFromConfig(cfg),
|
|
|
|
RepoMetaTablename: repoMetaTablename,
|
|
|
|
VersionTablename: versionTablename,
|
|
|
|
UserDataTablename: userDataTablename,
|
|
|
|
APIKeyTablename: apiKeyTablename,
|
|
|
|
Patches: version.GetDynamoDBPatches(),
|
|
|
|
Log: log.Logger{Logger: zerolog.New(os.Stdout)},
|
2023-01-16 09:01:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// The table creation should fail as the endpoint is not configured correctly
|
2023-10-30 15:06:04 -05:00
|
|
|
err = dynamoWrapper.createTable(dynamoWrapper.RepoMetaTablename)
|
2023-02-27 14:23:18 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2023-01-16 09:01:35 -05:00
|
|
|
err = dynamoWrapper.createVersionTable()
|
|
|
|
So(err, ShouldNotBeNil)
|
2023-07-07 11:27:10 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = dynamoWrapper.createTable(dynamoWrapper.APIKeyTablename)
|
2023-07-07 11:27:10 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2023-01-16 09:01:35 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Delete table errors", t, func() {
|
2024-06-13 00:51:32 -05:00
|
|
|
customResolver := aws.EndpointResolverWithOptionsFunc( //nolint: staticcheck
|
2023-01-16 09:01:35 -05:00
|
|
|
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
|
2024-06-13 00:51:32 -05:00
|
|
|
return aws.Endpoint{ //nolint: staticcheck
|
2023-01-16 09:01:35 -05:00
|
|
|
PartitionID: "aws",
|
|
|
|
URL: endpoint,
|
|
|
|
SigningRegion: region,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(region),
|
2024-06-13 00:51:32 -05:00
|
|
|
config.WithEndpointResolverWithOptions(customResolver)) //nolint: staticcheck
|
2023-01-16 09:01:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
dynamoWrapper := DynamoDB{
|
2023-10-30 15:06:04 -05:00
|
|
|
Client: dynamodb.NewFromConfig(cfg),
|
|
|
|
RepoMetaTablename: repoMetaTablename,
|
|
|
|
VersionTablename: versionTablename,
|
|
|
|
UserDataTablename: userDataTablename,
|
|
|
|
Patches: version.GetDynamoDBPatches(),
|
|
|
|
Log: log.Logger{Logger: zerolog.New(os.Stdout)},
|
2023-01-16 09:01:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// The tables were not created so delete calls fail, but dynamoWrapper should not error
|
2023-10-30 15:06:04 -05:00
|
|
|
err = dynamoWrapper.deleteTable(dynamoWrapper.RepoMetaTablename)
|
2023-01-16 09:01:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
}
|