1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/config/reporting_test.go
Siarhei Pazdniakou cd243ee542
feat(gcs): allow upload to gcs from steps (#3034)
* Upload reports to Google Cloud Storage bucket

* Added tests. Made fixes

* Update step generation. GCS client was moved to GeneralConfig

* Code was refactored

* Fixed issues

* Fixed issues

* Code correction due to PR comments

* Improved gcs client and integration tests

* Integrated gcp config. Updated step metadata

* Fixed issues. Added tests

* Added cpe, vault, aliases resolving for reporting parameters

* Added tests

* Uncommented DeferExitHandler. Removed useless comments

* fixed cloning of config

* Added comments for exported functions. Removed unused mock

* minor fix

* Implemented setting of report name via paramRef

* some refactoring. Writing tests

* Update pkg/config/reporting.go

* Update cmd/sonarExecuteScan_generated.go

* Apply suggestions from code review

* Update pkg/config/reporting.go

* Update pkg/config/reporting.go

* fixed removing valut secret files

* Update pkg/config/reporting.go

* restore order

* restore order

* Apply suggestions from code review

* go generate

* fixed tests

* Update resources/metadata/sonarExecuteScan.yaml

* Update resources.go

* Fixed tests. Code was regenerated

* changed somewhere gcp to gcs. Fixed one test

* move gcsSubFolder to input parameters

* fixed removing valut secret files

* minor fix in integration tests

* fix integration tests

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Sven Merk <33895725+nevskrem@users.noreply.github.com>
2021-12-15 15:07:47 +01:00

204 lines
6.2 KiB
Go

package config
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMixinReportingConfig(t *testing.T) {
gcpJsonKeyFilePath := "path/key.json"
gcsFolderPath := "test/folder/path"
gcsBucketID := "testBucketId"
config := StepConfig{
Config: map[string]interface{}{},
HookConfig: nil,
}
general := map[string]interface{}{
"gcpJsonKeyFilePath": gcpJsonKeyFilePath,
"gcsFolderPath": gcsFolderPath,
"gcsBucketId": "generalBucketId",
}
steps := map[string]interface{}{
"gcsBucketId": gcsBucketID,
"unknownConfig": "test",
}
config.mixinReportingConfig(nil, general, steps)
assert.Contains(t, config.Config, "gcpJsonKeyFilePath")
assert.Equal(t, gcpJsonKeyFilePath, config.Config["gcpJsonKeyFilePath"])
assert.Contains(t, config.Config, "gcsFolderPath")
assert.Equal(t, gcsFolderPath, config.Config["gcsFolderPath"])
assert.Contains(t, config.Config, "gcsBucketId")
assert.Equal(t, gcsBucketID, config.Config["gcsBucketId"])
assert.NotContains(t, config.Config, "unknownConfig")
}
func TestReportingParams_GetResourceParameters(t *testing.T) {
tt := []struct {
in ReportingParams
expected map[string]interface{}
}{
{
in: ReportingParams{Parameters: []StepParameters{}},
expected: map[string]interface{}{},
},
{
in: ReportingParams{Parameters: []StepParameters{
{Name: "param1"},
{Name: "param2"},
}},
expected: map[string]interface{}{},
},
{
in: ReportingParams{Parameters: []StepParameters{
{Name: "param1", ResourceRef: []ResourceReference{}},
{Name: "param2", ResourceRef: []ResourceReference{}},
}},
expected: map[string]interface{}{},
},
{
in: ReportingParams{Parameters: []StepParameters{
{Name: "param1", ResourceRef: []ResourceReference{{Name: "notAvailable", Param: "envparam1"}}},
{Name: "param2", ResourceRef: []ResourceReference{{Name: "commonPipelineEnvironment", Param: "envparam2"}}, Type: "string"},
}},
expected: map[string]interface{}{"param2": "val2"},
},
{
in: ReportingParams{Parameters: []StepParameters{
{Name: "param2", ResourceRef: []ResourceReference{{Name: "commonPipelineEnvironment", Param: "envparam2"}}, Type: "string"},
{Name: "param3", ResourceRef: []ResourceReference{{Name: "commonPipelineEnvironment", Param: "jsonList"}}, Type: "[]string"},
}},
expected: map[string]interface{}{"param2": "val2", "param3": []interface{}{"value1", "value2"}},
},
{
in: ReportingParams{Parameters: []StepParameters{
{Name: "param4", ResourceRef: []ResourceReference{{Name: "commonPipelineEnvironment", Param: "jsonKeyValue"}}, Type: "map[string]interface{}"},
}},
expected: map[string]interface{}{"param4": map[string]interface{}{"key": "value"}},
},
{
in: ReportingParams{Parameters: []StepParameters{
{Name: "param1", ResourceRef: []ResourceReference{{Name: "commonPipelineEnvironment", Param: "envparam1"}}, Type: "noString"},
{Name: "param4", ResourceRef: []ResourceReference{{Name: "commonPipelineEnvironment", Param: "jsonKeyValueString"}}, Type: "string"},
}},
expected: map[string]interface{}{"param4": "{\"key\":\"valueString\"}"},
},
}
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal("Failed to create temporary directory")
}
// clean up tmp dir
defer os.RemoveAll(dir)
cpeDir := filepath.Join(dir, "commonPipelineEnvironment")
err = os.MkdirAll(cpeDir, 0700)
if err != nil {
t.Fatal("Failed to create sub directory")
}
ioutil.WriteFile(filepath.Join(cpeDir, "envparam1"), []byte("val1"), 0700)
ioutil.WriteFile(filepath.Join(cpeDir, "envparam2"), []byte("val2"), 0700)
ioutil.WriteFile(filepath.Join(cpeDir, "jsonList.json"), []byte("[\"value1\",\"value2\"]"), 0700)
ioutil.WriteFile(filepath.Join(cpeDir, "jsonKeyValue.json"), []byte("{\"key\":\"value\"}"), 0700)
ioutil.WriteFile(filepath.Join(cpeDir, "jsonKeyValueString"), []byte("{\"key\":\"valueString\"}"), 0700)
for run, test := range tt {
t.Run(fmt.Sprintf("Run %v", run), func(t *testing.T) {
actual := test.in.GetResourceParameters(dir, "commonPipelineEnvironment")
assert.Equal(t, test.expected, actual)
})
}
}
func TestReportingParams_GetGetStepFilters(t *testing.T) {
tt := []struct {
in ReportingParams
expected StepFilters
}{
{
in: ReportingParams{Parameters: []StepParameters{}},
expected: StepFilters{},
},
{
in: ReportingParams{Parameters: []StepParameters{
{Name: "param1"},
{Name: "param2"},
}},
expected: StepFilters{
All: []string{"param1", "param2"},
General: []string{"param1", "param2"},
Steps: []string{"param1", "param2"},
Stages: []string{"param1", "param2"},
},
},
{
in: ReportingParams{Parameters: []StepParameters{
{Name: "param1"},
{Name: "param2"},
{Name: "param3"},
{Name: "param4"},
{Name: "param5"},
{Name: "param6"},
}},
expected: StepFilters{
All: []string{"param1", "param2", "param3", "param4", "param5", "param6"},
General: []string{"param1", "param2", "param3", "param4", "param5", "param6"},
Steps: []string{"param1", "param2", "param3", "param4", "param5", "param6"},
Stages: []string{"param1", "param2", "param3", "param4", "param5", "param6"},
},
},
}
for run, test := range tt {
t.Run(fmt.Sprintf("Run %v", run), func(t *testing.T) {
actual := test.in.getStepFilters()
assert.Equal(t, test.expected, actual)
})
}
}
func TestReportingParams_GetReportingFilter(t *testing.T) {
tt := []struct {
in ReportingParams
expected []string
}{
{
in: ReportingParams{Parameters: []StepParameters{}},
expected: nil,
},
{
in: ReportingParams{Parameters: []StepParameters{
{Name: "param1"},
{Name: "param2"},
}},
expected: []string{"param1", "param2"},
},
{
in: ReportingParams{Parameters: []StepParameters{
{Name: "param1"},
{Name: "param2"},
{Name: "param3"},
{Name: "param4"},
{Name: "param5"},
{Name: "param6"},
}},
expected: []string{"param1", "param2", "param3", "param4", "param5", "param6"},
},
}
for run, test := range tt {
t.Run(fmt.Sprintf("Run %v", run), func(t *testing.T) {
actual := test.in.getReportingFilter()
assert.Equal(t, test.expected, actual)
})
}
}