18676: Merge branch 'main' into 18676-anon-token-support-v2-in-config
[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 main
6
7 import (
8         "bytes"
9         "context"
10         "io/ioutil"
11
12         "git.arvados.org/arvados.git/sdk/go/arvadostest"
13         "github.com/aws/aws-sdk-go-v2/aws"
14         "github.com/aws/aws-sdk-go-v2/aws/defaults"
15         "github.com/aws/aws-sdk-go-v2/aws/ec2metadata"
16         "github.com/aws/aws-sdk-go-v2/aws/ec2rolecreds"
17         "github.com/aws/aws-sdk-go-v2/aws/endpoints"
18         "github.com/aws/aws-sdk-go-v2/service/s3"
19         check "gopkg.in/check.v1"
20 )
21
22 func (s *IntegrationSuite) TestS3AWSSDK(c *check.C) {
23         stage := s.s3setup(c)
24         defer stage.teardown(c)
25
26         cfg := defaults.Config()
27         cfg.Credentials = aws.NewChainProvider([]aws.CredentialsProvider{
28                 aws.NewStaticCredentialsProvider(arvadostest.ActiveTokenUUID, arvadostest.ActiveToken, ""),
29                 ec2rolecreds.New(ec2metadata.New(cfg)),
30         })
31         cfg.EndpointResolver = aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
32                 if service == "s3" {
33                         return aws.Endpoint{
34                                 URL:           "http://" + s.testServer.Addr,
35                                 SigningRegion: "custom-signing-region",
36                         }, nil
37                 }
38                 return endpoints.NewDefaultResolver().ResolveEndpoint(service, region)
39         })
40         client := s3.New(cfg)
41         client.ForcePathStyle = true
42         listreq := client.ListObjectsV2Request(&s3.ListObjectsV2Input{
43                 Bucket:            aws.String(arvadostest.FooCollection),
44                 MaxKeys:           aws.Int64(100),
45                 Prefix:            aws.String(""),
46                 ContinuationToken: nil,
47         })
48         resp, err := listreq.Send(context.Background())
49         c.Assert(err, check.IsNil)
50         c.Check(resp.Contents, check.HasLen, 1)
51         for _, key := range resp.Contents {
52                 c.Check(*key.Key, check.Equals, "foo")
53         }
54
55         p := make([]byte, 100000000)
56         for i := range p {
57                 p[i] = byte('a')
58         }
59         putreq := client.PutObjectRequest(&s3.PutObjectInput{
60                 Body:        bytes.NewReader(p),
61                 Bucket:      aws.String(stage.collbucket.Name),
62                 ContentType: aws.String("application/octet-stream"),
63                 Key:         aws.String("aaaa"),
64         })
65         _, err = putreq.Send(context.Background())
66         c.Assert(err, check.IsNil)
67
68         getreq := client.GetObjectRequest(&s3.GetObjectInput{
69                 Bucket: aws.String(stage.collbucket.Name),
70                 Key:    aws.String("aaaa"),
71         })
72         getresp, err := getreq.Send(context.Background())
73         c.Assert(err, check.IsNil)
74         getdata, err := ioutil.ReadAll(getresp.Body)
75         c.Assert(err, check.IsNil)
76         c.Check(bytes.Equal(getdata, p), check.Equals, true)
77 }