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"
|
|
|
|
|
|
|
|
"zotregistry.io/zot/pkg/log" //nolint:go-staticcheck
|
2023-03-28 12:20:09 -05:00
|
|
|
"zotregistry.io/zot/pkg/meta/version"
|
2023-01-16 09:01:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWrapperErrors(t *testing.T) {
|
|
|
|
const (
|
|
|
|
endpoint = "http://localhost:4566"
|
|
|
|
region = "us-east-2"
|
|
|
|
)
|
|
|
|
|
|
|
|
uuid, err := guuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
repoMetaTablename := "RepoMetadataTable" + uuid.String()
|
|
|
|
manifestDataTablename := "ManifestDataTable" + uuid.String()
|
2023-03-10 13:37:29 -05:00
|
|
|
indexDataTablename := "IndexDataTable" + 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"
|
|
|
|
|
|
|
|
customResolver := aws.EndpointResolverWithOptionsFunc(
|
|
|
|
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
|
|
|
|
return aws.Endpoint{
|
|
|
|
PartitionID: "aws",
|
|
|
|
URL: badEndpoint,
|
|
|
|
SigningRegion: region,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(region),
|
|
|
|
config.WithEndpointResolverWithOptions(customResolver))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
dynamoWrapper := DynamoDB{
|
2023-01-16 09:01:35 -05:00
|
|
|
Client: dynamodb.NewFromConfig(cfg),
|
|
|
|
RepoMetaTablename: repoMetaTablename,
|
|
|
|
ManifestDataTablename: manifestDataTablename,
|
2023-03-10 13:37:29 -05:00
|
|
|
IndexDataTablename: indexDataTablename,
|
2023-01-16 09:01:35 -05:00
|
|
|
VersionTablename: versionTablename,
|
2023-04-24 13:13:15 -05:00
|
|
|
UserDataTablename: userDataTablename,
|
2023-07-07 11:27:10 -05:00
|
|
|
APIKeyTablename: apiKeyTablename,
|
2023-01-16 09:01:35 -05:00
|
|
|
Patches: version.GetDynamoDBPatches(),
|
|
|
|
Log: log.Logger{Logger: zerolog.New(os.Stdout)},
|
|
|
|
}
|
|
|
|
|
|
|
|
// The table creation should fail as the endpoint is not configured correctly
|
|
|
|
err = dynamoWrapper.createRepoMetaTable()
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
err = dynamoWrapper.createManifestDataTable()
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
err = dynamoWrapper.createIndexDataTable()
|
|
|
|
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
|
|
|
|
|
|
|
err = dynamoWrapper.createAPIKeyTable()
|
|
|
|
So(err, ShouldNotBeNil)
|
2023-01-16 09:01:35 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Delete table errors", t, func() {
|
|
|
|
customResolver := aws.EndpointResolverWithOptionsFunc(
|
|
|
|
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
|
|
|
|
return aws.Endpoint{
|
|
|
|
PartitionID: "aws",
|
|
|
|
URL: endpoint,
|
|
|
|
SigningRegion: region,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(region),
|
|
|
|
config.WithEndpointResolverWithOptions(customResolver))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
dynamoWrapper := DynamoDB{
|
2023-01-16 09:01:35 -05:00
|
|
|
Client: dynamodb.NewFromConfig(cfg),
|
|
|
|
RepoMetaTablename: repoMetaTablename,
|
|
|
|
ManifestDataTablename: manifestDataTablename,
|
|
|
|
VersionTablename: versionTablename,
|
2023-03-10 13:37:29 -05:00
|
|
|
IndexDataTablename: indexDataTablename,
|
2023-04-24 13:13:15 -05:00
|
|
|
UserDataTablename: userDataTablename,
|
2023-01-16 09:01:35 -05:00
|
|
|
Patches: version.GetDynamoDBPatches(),
|
|
|
|
Log: log.Logger{Logger: zerolog.New(os.Stdout)},
|
|
|
|
}
|
|
|
|
|
|
|
|
// The tables were not created so delete calls fail, but dynamoWrapper should not error
|
|
|
|
err = dynamoWrapper.deleteRepoMetaTable()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = dynamoWrapper.deleteManifestDataTable()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
}
|