1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/awsS3Upload_test.go
Julian Schmitt 6714794066
Create awsS3Upload (#3737)
* add Step awsS3Upload

* fix JSON Read

* fix groovy wrapper

* change credentials to secret text

* Change credentials type to token

* add cleanup for environment variables

* Add AwsS3UploadCommand to piper.go

* add documentation of awsS3Upload

* Fix JSON String Example in Documentation

* add the Upload of whole directories

* add Logging to awsS3Upload.go

* imporve Logging

* fix and improve unit tests

* fix non-existing-filepaths-Bug

* fix windows filepaths

* remove ... from logging

* change step description

* fix PR Tests

* remove redundant code

* try to run tests sequentially

* fix file.Close

* executed go mod tidy

* requested changes

* fix comments and awsCredentials

* Update documentation/docs/steps/awsS3Upload.md

Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com>

* use temporary files for unit tests

* executed go generate

Co-authored-by: ffeldmann <f.feldmann@sap.com>
Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com>
Co-authored-by: Thorsten Duda <thorsten.duda@sap.com>
2022-05-18 10:37:54 +02:00

112 lines
2.8 KiB
Go

package cmd
import (
"context"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type mockPutObjectAPI func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
func (m mockPutObjectAPI) PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
return m(ctx, params, optFns...)
}
func TestRunAwsS3Upload(t *testing.T) {
t.Parallel()
t.Run("happy path", func(t *testing.T) {
t.Parallel()
// create temporary file
f, err := os.CreateTemp("", "tmpfile-") // in Go version older than 1.17 you can use ioutil.TempFile
if err != nil {
log.Fatal(err)
}
defer f.Close()
defer os.Remove(f.Name())
data := []byte("test test test")
if _, err := f.Write(data); err != nil {
log.Fatal(err)
}
// initialization
config := awsS3UploadOptions{
FilePath: f.Name(),
}
client := mockS3Client
// test
err = runAwsS3Upload(&config, client(t, config.FilePath), "fooBucket")
// assert
assert.NoError(t, err)
})
t.Run("error path", func(t *testing.T) {
t.Parallel()
// initialization
config := awsS3UploadOptions{
FilePath: "nonExistingFilepath",
}
client := mockS3Client
// test
err := runAwsS3Upload(&config, client(t, config.FilePath), "fooBucket")
// assert
_, ok := err.(*fs.PathError)
assert.True(t, ok)
})
t.Run("error bucket", func(t *testing.T) {
t.Parallel()
// create temporary file
f, err := os.CreateTemp("", "tmpfile-") // in Go version older than 1.17 you can use ioutil.TempFile
if err != nil {
log.Fatal(err)
}
defer f.Close()
defer os.Remove(f.Name())
data := []byte("test test test")
if _, err := f.Write(data); err != nil {
log.Fatal(err)
}
// initialization
config := awsS3UploadOptions{
FilePath: f.Name(),
}
client := mockS3Client
// test
err = runAwsS3Upload(&config, client(t, config.FilePath), "errorBucket")
// assert
assert.EqualError(t, err, "expect fooBucket, got errorBucket")
})
}
func mockS3Client(t *testing.T, fileName string) S3PutObjectAPI {
return mockPutObjectAPI(func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
t.Helper()
if params.Bucket == nil {
return nil, fmt.Errorf("expect bucket to not be nil")
}
if e, a := "fooBucket", *params.Bucket; e != a {
return nil, fmt.Errorf("expect %v, got %v", e, a)
}
if params.Key == nil {
return nil, fmt.Errorf("expect key to not be nil")
}
if e, a := filepath.ToSlash(fileName), *params.Key; e != a {
return nil, fmt.Errorf("expect %v, got %v", e, a)
}
if params.Body == nil {
return nil, fmt.Errorf("expect Body / io.Reader not to be nil")
}
return &s3.PutObjectOutput{}, nil
})
}