1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/azureBlobUpload_test.go

140 lines
3.5 KiB
Go
Raw Normal View History

//go:build unit
// +build unit
Create azureBlobUpload (#3766) * add Step azureBlobUpload * add azure sdk and unit tests * add Documentation * fix Groovy Wrapper * adopt the requested changes from awsS3Upload * fix lint tests * downgrade azure sdk to go 1.17 * multiple fixes e.g. use of temporary files for tests * fix tests * Update cmd/azureBlobUpload.go Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * Update cmd/azureBlobUpload.go Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * Update documentation/docs/steps/azureBlobUpload.md Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * Update documentation/docs/steps/azureBlobUpload.md Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * Update documentation/docs/steps/azureBlobUpload.md Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * Update documentation/docs/steps/azureBlobUpload.md Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * requested changes * use latest version of azure sdk after update to go 1.18 * change staticcheck from 1.1.0 to 1.2.0 * try to fix lint test by pre-compiling go 1.18 * fix caching for lint test * improve error handling by dividing runner * improve error handling and add validation * multiple naming fixes * add new test for unmarshalling JSON-Structs * Update cmd/azureBlobUpload_test.go Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * Update cmd/azureBlobUpload_test.go Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * Update cmd/azureBlobUpload_test.go Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * fix JSON unmarshall test * Update documentation/docs/steps/azureBlobUpload.md Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * Update cmd/azureBlobUpload_test.go Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * Update cmd/azureBlobUpload.go Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * fix uploadFunc Co-authored-by: Thorsten Duda <thorsten.duda@sap.com> Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com>
2022-06-15 09:41:02 +02:00
package cmd
import (
"context"
"errors"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/stretchr/testify/assert"
)
type mockAzureContainerAPI func(blobName string) (*azblob.BlockBlobClient, error)
func (m mockAzureContainerAPI) NewBlockBlobClient(blobName string) (*azblob.BlockBlobClient, error) {
return m(blobName)
}
func mockAzureContainerClient(t *testing.T, fail bool) azureContainerAPI {
return mockAzureContainerAPI(func(blobName string) (*azblob.BlockBlobClient, error) {
t.Helper()
if fail {
return nil, fmt.Errorf("error containerClient")
}
return &azblob.BlockBlobClient{}, nil
})
}
func uploadFuncMock(ctx context.Context, api *azblob.BlockBlobClient, file *os.File, o azblob.UploadOption) (*http.Response, error) {
return &http.Response{}, nil
}
func TestRunAzureBlobUpload(t *testing.T) {
t.Parallel()
t.Run("positive tests", func(t *testing.T) {
t.Parallel()
t.Run("happy path", func(t *testing.T) {
t.Parallel()
// create temporary file
f, err := os.CreateTemp("", "tmpfile-")
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 := azureBlobUploadOptions{
FilePath: f.Name(),
}
container := mockAzureContainerClient(t, false)
// test
err = executeUpload(&config, container, uploadFuncMock)
// assert
assert.NoError(t, err)
})
})
t.Run("negative tests", func(t *testing.T) {
t.Parallel()
t.Run("error path", func(t *testing.T) {
t.Parallel()
// initialization
config := azureBlobUploadOptions{
FilePath: "nonExistingFilepath",
}
container := mockAzureContainerClient(t, false)
// test
err := executeUpload(&config, container, uploadFuncMock)
// assert
assert.IsType(t, &fs.PathError{}, errors.Unwrap(err))
})
t.Run("error containerClient", func(t *testing.T) {
t.Parallel()
// create temporary file
f, err := os.CreateTemp("", "tmpfile-")
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 := azureBlobUploadOptions{
FilePath: f.Name(),
}
container := mockAzureContainerClient(t, true)
// test
err = executeUpload(&config, container, uploadFuncMock)
// assert
assert.EqualError(t, err, "Could not instantiate Azure blockBlobClient from Azure Container Client: error containerClient")
})
t.Run("error credentials", func(t *testing.T) {
t.Parallel()
// initialization
config := azureBlobUploadOptions{
JSONCredentialsAzure: `{
"account_name": "name",
"container_name": "container"
}`,
FilePath: "nonExistingFilepath",
}
// test
_, err := setup(&config)
// assert
assert.EqualError(t, err, "Azure credentials are not valid: Key: 'azureCredentials.SASToken' Error:Field validation for 'SASToken' failed on the 'required' tag")
})
t.Run("error JSONStruct", func(t *testing.T) {
t.Parallel()
// initialization
config := azureBlobUploadOptions{
JSONCredentialsAzure: `faulty json`,
}
// test
_, err := setup(&config)
// assert
assert.EqualError(t, err, "Could not read JSONCredentialsAzure: invalid character 'u' in literal false (expecting 'l')")
})
})
}