21943: Add test for files appearing multiple times in output
[arvados.git] / services / keep-web / s3aws_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package keepweb
6
7 import (
8         "bytes"
9         "context"
10         "errors"
11         "io/ioutil"
12
13         "git.arvados.org/arvados.git/sdk/go/arvadostest"
14         "github.com/aws/aws-sdk-go-v2/aws"
15         "github.com/aws/aws-sdk-go-v2/config"
16         "github.com/aws/aws-sdk-go-v2/credentials"
17         "github.com/aws/aws-sdk-go-v2/service/s3"
18         check "gopkg.in/check.v1"
19 )
20
21 func (s *IntegrationSuite) TestS3AWSSDK(c *check.C) {
22         stage := s.s3setup(c)
23         defer stage.teardown(c)
24
25         cfg, err := config.LoadDefaultConfig(context.TODO(),
26                 func(o *config.LoadOptions) error {
27                         o.Credentials = credentials.StaticCredentialsProvider{
28                                 Value: aws.Credentials{
29                                         AccessKeyID:     arvadostest.ActiveTokenUUID,
30                                         SecretAccessKey: arvadostest.ActiveToken,
31                                         Source:          "test suite configuration",
32                                 },
33                         }
34                         o.EndpointResolverWithOptions = aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
35                                 if service == "S3" {
36                                         return aws.Endpoint{
37                                                 URL:               s.testServer.URL,
38                                                 HostnameImmutable: true,
39                                                 SigningRegion:     "test-region",
40                                                 Source:            aws.EndpointSourceCustom,
41                                         }, nil
42                                 }
43                                 // else, use default
44                                 return aws.Endpoint{}, &aws.EndpointNotFoundError{Err: errors.New("endpoint not overridden")}
45                         })
46                         return nil
47                 })
48         c.Assert(err, check.IsNil)
49         client := s3.NewFromConfig(cfg, func(o *s3.Options) {
50                 o.Region = "test-region"
51                 o.UsePathStyle = true
52         })
53         resp, err := client.ListObjectsV2(context.Background(), &s3.ListObjectsV2Input{
54                 Bucket:            aws.String(arvadostest.FooCollection),
55                 MaxKeys:           aws.Int32(100),
56                 Prefix:            aws.String(""),
57                 ContinuationToken: nil,
58         })
59         c.Assert(err, check.IsNil)
60         c.Check(resp.Contents, check.HasLen, 1)
61         for _, key := range resp.Contents {
62                 c.Check(*key.Key, check.Equals, "foo")
63         }
64
65         p := make([]byte, 100000000)
66         for i := range p {
67                 p[i] = byte('a')
68         }
69         _, err = client.PutObject(context.Background(), &s3.PutObjectInput{
70                 Body:        bytes.NewReader(p),
71                 Bucket:      aws.String(stage.collbucket.Name),
72                 ContentType: aws.String("application/octet-stream"),
73                 Key:         aws.String("aaaa"),
74         })
75         c.Assert(err, check.IsNil)
76
77         getresp, err := client.GetObject(context.Background(), &s3.GetObjectInput{
78                 Bucket: aws.String(stage.collbucket.Name),
79                 Key:    aws.String("aaaa"),
80         })
81         c.Assert(err, check.IsNil)
82         getdata, err := ioutil.ReadAll(getresp.Body)
83         c.Assert(err, check.IsNil)
84         c.Check(bytes.Equal(getdata, p), check.Equals, true)
85 }