github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/gcs/reporting_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package gcs
     5  
     6  import (
     7  	"errors"
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/SAP/jenkins-library/pkg/gcs/mocks"
    14  	"github.com/bmatcuk/doublestar"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  type testFileInfo struct {
    19  	path string
    20  }
    21  
    22  func (t testFileInfo) Name() string {
    23  	return ""
    24  }
    25  
    26  func (t testFileInfo) Size() int64 {
    27  	return 0
    28  }
    29  
    30  func (t testFileInfo) Mode() os.FileMode {
    31  	return os.FileMode(0)
    32  }
    33  
    34  func (t testFileInfo) ModTime() time.Time {
    35  	return time.Time{}
    36  }
    37  
    38  func (t testFileInfo) IsDir() bool {
    39  	if strings.HasSuffix(t.path, "test2") {
    40  		return true
    41  	}
    42  	return false
    43  }
    44  
    45  func (t testFileInfo) Sys() interface{} {
    46  	return nil
    47  }
    48  
    49  type testStepConfig struct {
    50  	FirstParameter  string
    51  	SecondParameter int
    52  	ThirdParameter  string
    53  	FourthParameter bool
    54  }
    55  
    56  func TestPersistReportsToGCS(t *testing.T) {
    57  	var testCases = []struct {
    58  		testName      string
    59  		gcsFolderPath string
    60  		gcsSubFolder  string
    61  		outputParams  []ReportOutputParam
    62  		expected      []Task
    63  		detectedFiles []string
    64  		uploadFileErr error
    65  		expectedError error
    66  	}{
    67  		{
    68  			testName:      "success case",
    69  			gcsFolderPath: "test/folder/path",
    70  			gcsSubFolder:  "sub/folder",
    71  			outputParams: []ReportOutputParam{
    72  				{FilePattern: "*.json", ParamRef: "", StepResultType: "general"},
    73  				{FilePattern: "*/test*", ParamRef: "", StepResultType: ""},
    74  				{FilePattern: "*.json", ParamRef: "firstParameter", StepResultType: "general"},
    75  				{FilePattern: "", ParamRef: "secondParameter", StepResultType: "general"},
    76  				{FilePattern: "", ParamRef: "thirdParameter", StepResultType: ""},
    77  			},
    78  			expected: []Task{
    79  				{SourcePath: "asdf.json", TargetPath: "test/folder/path/general/sub/folder/asdf.json"},
    80  				{SourcePath: "folder/test1", TargetPath: "test/folder/path/sub/folder/folder/test1"},
    81  				{SourcePath: "testFolder/test3", TargetPath: "test/folder/path/sub/folder/testFolder/test3"},
    82  				{SourcePath: "report1.json", TargetPath: "test/folder/path/general/sub/folder/report1.json"},
    83  				{SourcePath: "test-report.json", TargetPath: "test/folder/path/general/sub/folder/test-report.json"},
    84  				{SourcePath: "test-report2.json", TargetPath: "test/folder/path/sub/folder/test-report2.json"},
    85  			},
    86  			detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"},
    87  			uploadFileErr: nil,
    88  			expectedError: nil,
    89  		},
    90  		{
    91  			testName:      "failed upload to GCS",
    92  			gcsFolderPath: "test/folder/path",
    93  			gcsSubFolder:  "",
    94  			outputParams: []ReportOutputParam{
    95  				{FilePattern: "*.json", ParamRef: "", StepResultType: "general"},
    96  			},
    97  			expected: []Task{
    98  				{SourcePath: "asdf.json", TargetPath: "test/folder/path/general/asdf.json"},
    99  			},
   100  			detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"},
   101  			uploadFileErr: errors.New("upload failed"),
   102  			expectedError: errors.New("failed to persist reports: upload failed"),
   103  		},
   104  		{
   105  			testName:      "failed - input parameter does not exist",
   106  			gcsFolderPath: "test/folder/path",
   107  			gcsSubFolder:  "",
   108  			outputParams: []ReportOutputParam{
   109  				{FilePattern: "", ParamRef: "missingParameter", StepResultType: "general"},
   110  			},
   111  			expected:      []Task{},
   112  			detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"},
   113  			uploadFileErr: nil,
   114  			expectedError: errors.New("there is no such input parameter as missingParameter"),
   115  		},
   116  		{
   117  			testName:      "failed - input parameter is empty",
   118  			gcsFolderPath: "test/folder/path",
   119  			outputParams: []ReportOutputParam{
   120  				{FilePattern: "", ParamRef: "emptyParameter", StepResultType: "general"},
   121  			},
   122  			expected:      []Task{},
   123  			detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"},
   124  			uploadFileErr: nil,
   125  			expectedError: errors.New("the value of the parameter emptyParameter must not be empty"),
   126  		},
   127  	}
   128  	for _, tt := range testCases {
   129  		t.Run(tt.testName, func(t *testing.T) {
   130  			inputParameters := map[string]string{
   131  				"firstParameter":  "report1.json",
   132  				"secondParameter": "test-report.json",
   133  				"thirdParameter":  "test-report2.json",
   134  				"emptyParameter":  "",
   135  			}
   136  			gcsBucketID := "testBucketID"
   137  			mockedClient := &mocks.Client{}
   138  
   139  			for _, expectation := range tt.expected {
   140  				mockedClient.Mock.On("UploadFile", gcsBucketID, expectation.SourcePath, expectation.TargetPath).Return(
   141  					func(pipelineId string, sourcePath string, targetPath string) error { return tt.uploadFileErr },
   142  				).Once()
   143  			}
   144  
   145  			searchFn := func(path string) ([]string, error) {
   146  				matchedFiles := []string{}
   147  				for _, value := range tt.detectedFiles {
   148  					match, _ := doublestar.Match(path, value)
   149  					if match {
   150  						matchedFiles = append(matchedFiles, value)
   151  					}
   152  				}
   153  				return matchedFiles, nil
   154  			}
   155  
   156  			fileInfoFn := func(name string) (os.FileInfo, error) {
   157  				return testFileInfo{name}, nil
   158  			}
   159  
   160  			err := PersistReportsToGCS(mockedClient, tt.outputParams, inputParameters, tt.gcsFolderPath, gcsBucketID, tt.gcsSubFolder, searchFn, fileInfoFn)
   161  			if tt.expectedError == nil {
   162  				assert.NoError(t, err)
   163  			} else {
   164  				assert.Equal(t, tt.expectedError.Error(), err.Error())
   165  			}
   166  
   167  			mockedClient.Mock.AssertNumberOfCalls(t, "UploadFile", len(tt.expected))
   168  			mockedClient.Mock.AssertExpectations(t)
   169  		})
   170  	}
   171  }