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

     1  //go:build unit
     2  // +build unit
     3  
     4  package piperutils
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  type fileMock struct {
    18  	fileMap     map[string][]byte
    19  	writeErrors map[string]error
    20  }
    21  
    22  func (f *fileMock) WriteFile(filename string, data []byte, perm os.FileMode) error {
    23  	if f.writeErrors != nil && f.writeErrors[filename] != nil {
    24  		return f.writeErrors[filename]
    25  	}
    26  	f.fileMap[filename] = data
    27  	return nil
    28  }
    29  
    30  func (f *fileMock) ReadFile(name string) ([]byte, error) {
    31  	return f.fileMap[name], nil
    32  }
    33  
    34  func (f *fileMock) FileExists(name string) bool {
    35  	return f.fileMap[name] != nil
    36  }
    37  
    38  func TestPersistReportAndLinks(t *testing.T) {
    39  	workspace := ""
    40  	t.Run("success - default", func(t *testing.T) {
    41  		files := fileMock{fileMap: map[string][]byte{}}
    42  
    43  		reports := []Path{{Target: "testFile1.json", Mandatory: true}, {Target: "testFile2.json"}}
    44  		links := []Path{{Target: "https://1234568.com/test", Name: "Weblink"}}
    45  		err := PersistReportsAndLinks("checkmarxExecuteScan", workspace, &files, reports, links)
    46  		assert.NoError(t, err)
    47  
    48  		reportsJSONPath := filepath.Join(workspace, "checkmarxExecuteScan_reports.json")
    49  		assert.True(t, files.FileExists(reportsJSONPath))
    50  
    51  		linksJSONPath := filepath.Join(workspace, "checkmarxExecuteScan_links.json")
    52  		assert.True(t, files.FileExists(linksJSONPath))
    53  
    54  		var reportsLoaded []Path
    55  		var linksLoaded []Path
    56  		reportsFileData, err := files.ReadFile(reportsJSONPath)
    57  		reportsDataString := string(reportsFileData)
    58  		println(reportsDataString)
    59  		assert.NoError(t, err, "No error expected but got one")
    60  
    61  		linksFileData, err := files.ReadFile(linksJSONPath)
    62  		linksDataString := string(linksFileData)
    63  		println(linksDataString)
    64  		assert.NoError(t, err, "No error expected but got one")
    65  		_ = json.Unmarshal(reportsFileData, &reportsLoaded)
    66  		_ = json.Unmarshal(linksFileData, &linksLoaded)
    67  
    68  		assert.Equal(t, 2, len(reportsLoaded), "wrong number of reports")
    69  		assert.Equal(t, 1, len(linksLoaded), "wrong number of links")
    70  		assert.Equal(t, true, reportsLoaded[0].Mandatory, "mandatory flag on report 1 has wrong value")
    71  		assert.Equal(t, "testFile1.json", reportsLoaded[0].Target, "target value on report 1 has wrong value")
    72  		assert.Equal(t, false, reportsLoaded[1].Mandatory, "mandatory flag on report 2 has wrong value")
    73  		assert.Equal(t, "testFile2.json", reportsLoaded[1].Target, "target value on report 1 has wrong value")
    74  		assert.Equal(t, false, linksLoaded[0].Mandatory, "mandatory flag on link 1 has wrong value")
    75  		assert.Equal(t, "https://1234568.com/test", linksLoaded[0].Target, "target value on link 1 has wrong value")
    76  		assert.Equal(t, "Weblink", linksLoaded[0].Name, "name value on link 1 has wrong value")
    77  	})
    78  
    79  	t.Run("success - empty list", func(t *testing.T) {
    80  		files := fileMock{fileMap: map[string][]byte{}}
    81  
    82  		reportsJSONPath := filepath.Join(workspace, "sonarExecuteScan_reports.json")
    83  		linksJSONPath := filepath.Join(workspace, "sonarExecuteScan_links.json")
    84  
    85  		// prepare uninitialised parameters
    86  		var reports, links []Path
    87  		require.Empty(t, reports)
    88  		require.Empty(t, links)
    89  
    90  		// test
    91  		err := PersistReportsAndLinks("sonarExecuteScan", workspace, &files, reports, links)
    92  		// assert
    93  		assert.NoError(t, err)
    94  		for _, reportFile := range []string{reportsJSONPath, linksJSONPath} {
    95  			assert.True(t, files.FileExists(reportFile))
    96  			reportsFileData, err := files.ReadFile(reportFile)
    97  			require.NoError(t, err, "No error expected but got one")
    98  			assert.Equal(t, "[]", string(reportsFileData))
    99  		}
   100  	})
   101  
   102  	t.Run("failure - write reports", func(t *testing.T) {
   103  		stepName := "checkmarxExecuteScan"
   104  		files := fileMock{
   105  			fileMap:     map[string][]byte{},
   106  			writeErrors: map[string]error{filepath.Join(workspace, fmt.Sprintf("%v_reports.json", stepName)): fmt.Errorf("write error")},
   107  		}
   108  
   109  		reports := []Path{{Target: "testFile1.json"}, {Target: "testFile2.json"}}
   110  		links := []Path{{Target: "https://1234568.com/test", Name: "Weblink"}}
   111  		err := PersistReportsAndLinks(stepName, workspace, &files, reports, links)
   112  
   113  		assert.EqualError(t, err, "failed to write reports.json: write error")
   114  	})
   115  
   116  	t.Run("failure - write links", func(t *testing.T) {
   117  		stepName := "checkmarxExecuteScan"
   118  		files := fileMock{
   119  			fileMap:     map[string][]byte{},
   120  			writeErrors: map[string]error{filepath.Join(workspace, fmt.Sprintf("%v_links.json", stepName)): fmt.Errorf("write error")},
   121  		}
   122  
   123  		reports := []Path{{Target: "testFile1.json"}, {Target: "testFile2.json"}}
   124  		links := []Path{{Target: "https://1234568.com/test", Name: "Weblink"}}
   125  		err := PersistReportsAndLinks(stepName, workspace, &files, reports, links)
   126  
   127  		assert.EqualError(t, err, "failed to write links.json: write error")
   128  	})
   129  }