github.com/SAP/jenkins-library@v1.362.0/cmd/azureBlobUpload_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	"fmt"
    10  	"io/fs"
    11  	"log"
    12  	"net/http"
    13  	"os"
    14  	"testing"
    15  
    16  	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  type mockAzureContainerAPI func(blobName string) (*azblob.BlockBlobClient, error)
    21  
    22  func (m mockAzureContainerAPI) NewBlockBlobClient(blobName string) (*azblob.BlockBlobClient, error) {
    23  	return m(blobName)
    24  }
    25  
    26  func mockAzureContainerClient(t *testing.T, fail bool) azureContainerAPI {
    27  	return mockAzureContainerAPI(func(blobName string) (*azblob.BlockBlobClient, error) {
    28  		t.Helper()
    29  		if fail {
    30  			return nil, fmt.Errorf("error containerClient")
    31  		}
    32  		return &azblob.BlockBlobClient{}, nil
    33  	})
    34  }
    35  
    36  func uploadFuncMock(ctx context.Context, api *azblob.BlockBlobClient, file *os.File, o azblob.UploadOption) (*http.Response, error) {
    37  	return &http.Response{}, nil
    38  }
    39  
    40  func TestRunAzureBlobUpload(t *testing.T) {
    41  	t.Parallel()
    42  
    43  	t.Run("positive tests", func(t *testing.T) {
    44  		t.Parallel()
    45  
    46  		t.Run("happy path", func(t *testing.T) {
    47  			t.Parallel()
    48  			// create temporary file
    49  			f, err := os.CreateTemp("", "tmpfile-")
    50  			if err != nil {
    51  				log.Fatal(err)
    52  			}
    53  			defer f.Close()
    54  			defer os.Remove(f.Name())
    55  			data := []byte("test test test")
    56  			if _, err := f.Write(data); err != nil {
    57  				log.Fatal(err)
    58  			}
    59  			// initialization
    60  			config := azureBlobUploadOptions{
    61  				FilePath: f.Name(),
    62  			}
    63  			container := mockAzureContainerClient(t, false)
    64  			// test
    65  			err = executeUpload(&config, container, uploadFuncMock)
    66  			// assert
    67  			assert.NoError(t, err)
    68  		})
    69  	})
    70  
    71  	t.Run("negative tests", func(t *testing.T) {
    72  		t.Parallel()
    73  
    74  		t.Run("error path", func(t *testing.T) {
    75  			t.Parallel()
    76  			// initialization
    77  			config := azureBlobUploadOptions{
    78  				FilePath: "nonExistingFilepath",
    79  			}
    80  			container := mockAzureContainerClient(t, false)
    81  			// test
    82  			err := executeUpload(&config, container, uploadFuncMock)
    83  			// assert
    84  			assert.IsType(t, &fs.PathError{}, errors.Unwrap(err))
    85  		})
    86  
    87  		t.Run("error containerClient", func(t *testing.T) {
    88  			t.Parallel()
    89  			// create temporary file
    90  			f, err := os.CreateTemp("", "tmpfile-")
    91  			if err != nil {
    92  				log.Fatal(err)
    93  			}
    94  			defer f.Close()
    95  			defer os.Remove(f.Name())
    96  			data := []byte("test test test")
    97  			if _, err := f.Write(data); err != nil {
    98  				log.Fatal(err)
    99  			}
   100  			// initialization
   101  			config := azureBlobUploadOptions{
   102  				FilePath: f.Name(),
   103  			}
   104  			container := mockAzureContainerClient(t, true)
   105  			// test
   106  			err = executeUpload(&config, container, uploadFuncMock)
   107  			// assert
   108  			assert.EqualError(t, err, "Could not instantiate Azure blockBlobClient from Azure Container Client: error containerClient")
   109  		})
   110  
   111  		t.Run("error credentials", func(t *testing.T) {
   112  			t.Parallel()
   113  			// initialization
   114  			config := azureBlobUploadOptions{
   115  				JSONCredentialsAzure: `{
   116  					"account_name": "name",
   117  					"container_name": "container"
   118  				  }`,
   119  				FilePath: "nonExistingFilepath",
   120  			}
   121  			// test
   122  			_, err := setup(&config)
   123  			// assert
   124  			assert.EqualError(t, err, "Azure credentials are not valid: Key: 'azureCredentials.SASToken' Error:Field validation for 'SASToken' failed on the 'required' tag")
   125  		})
   126  
   127  		t.Run("error JSONStruct", func(t *testing.T) {
   128  			t.Parallel()
   129  			// initialization
   130  			config := azureBlobUploadOptions{
   131  				JSONCredentialsAzure: `faulty json`,
   132  			}
   133  			// test
   134  			_, err := setup(&config)
   135  			// assert
   136  			assert.EqualError(t, err, "Could not read JSONCredentialsAzure: invalid character 'u' in literal false (expecting 'l')")
   137  		})
   138  	})
   139  }