1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-29 21:57:01 +02:00
* it test

* transfer credentials

* Change parameter type of nodeExtDescriptorMapping

* Extend test

* Fix unit test

* Remove usage of the depricated ioutil package

* Fix cmd failure if neither git/commitId nor customDescription are
provided

* Extend test

* Add TMS test to the job matrix

* Map env. variable

* Remove usage of the env. TMS_UPLOAD_IT_KEY

* remove os

* update test

* use os.Gerenv

* test fix

* Update integration-tests.yml

* env mapping in it pr workflow

* print tmsServiceKey

* read env with upper case

* Update integration-tests.yml

* Update integration-tests.yml

* Update integration-tests-pr.yml

* Delete cover.out

* Remove TMS service key from environment in integration test workflow job

* Extend integration tests

* Revert change parameter type of nodeExtDescriptorMapping

* Extend tests

* Extend tests

* Remove unused method

* Change default TR description

* Add check for custom description

* Remove personal data from MTARs

* Register client secret to log as secret

* Move RegisterSecret to earlier point in runtime

* RegisterSecret for encodedUsernameColonPassword

* Update integration/integration_tms_upload_test.go

Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com>

* Use one test data directory

* Add a negative test

* fix config file name

---------

Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com>
This commit is contained in:
gerstneralex 2023-03-07 17:49:28 +01:00 committed by GitHub
parent 22f61be2c4
commit cfe21ebb7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 205 additions and 23 deletions

View File

@ -85,6 +85,7 @@ func tmsUpload(config tmsUploadOptions, telemetryData *telemetry.CustomData, inf
if err != nil {
log.Entry().WithError(err).Fatal("Failed to unmarshal TMS service key")
}
log.RegisterSecret(serviceKey.Uaa.ClientSecret)
if GeneralConfig.Verbose {
log.Entry().Info("Will be used for communication:")
@ -115,6 +116,7 @@ func runTmsUpload(config tmsUploadOptions, communicationInstance tms.Communicati
if config.CustomDescription != "" {
description = config.CustomDescription
}
namedUser := config.NamedUser
nodeName := config.NodeName
mtaVersion := config.MtaVersion

View File

@ -2,7 +2,7 @@ package cmd
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"testing"
@ -155,10 +155,10 @@ func TestRunTmsUpload(t *testing.T) {
utils := newTmsUploadTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := ioutil.ReadFile(MTA_YAML_PATH)
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
mtaExtDescriptorBytes, _ := ioutil.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
mtaExtDescriptorBytes, _ := os.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
utils.AddFile(MTA_EXT_DESCRIPTOR_PATH_LOCAL, mtaExtDescriptorBytes)
nodeNameExtDescriptorMapping := map[string]interface{}{NODE_NAME: MTA_EXT_DESCRIPTOR_PATH_LOCAL}
@ -202,10 +202,10 @@ func TestRunTmsUpload(t *testing.T) {
utils := newTmsUploadTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := ioutil.ReadFile(MTA_YAML_PATH)
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
mtaExtDescriptorBytes, _ := ioutil.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
mtaExtDescriptorBytes, _ := os.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
utils.AddFile(MTA_EXT_DESCRIPTOR_PATH_LOCAL, mtaExtDescriptorBytes)
nodeNameExtDescriptorMapping := map[string]interface{}{NODE_NAME: MTA_EXT_DESCRIPTOR_PATH_LOCAL}
@ -281,7 +281,7 @@ func TestRunTmsUpload(t *testing.T) {
utils := newTmsUploadTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := ioutil.ReadFile(INVALID_MTA_YAML_PATH)
mtaYamlBytes, _ := os.ReadFile(INVALID_MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
nodeNameExtDescriptorMapping := map[string]interface{}{NODE_NAME: MTA_EXT_DESCRIPTOR_PATH_LOCAL}
@ -303,7 +303,7 @@ func TestRunTmsUpload(t *testing.T) {
utils := newTmsUploadTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := ioutil.ReadFile(INVALID_MTA_YAML_PATH_2)
mtaYamlBytes, _ := os.ReadFile(INVALID_MTA_YAML_PATH_2)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
nodeNameExtDescriptorMapping := map[string]interface{}{NODE_NAME: MTA_EXT_DESCRIPTOR_PATH_LOCAL}
@ -329,19 +329,19 @@ func TestRunTmsUpload(t *testing.T) {
utils := newTmsUploadTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := ioutil.ReadFile(MTA_YAML_PATH)
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
mtaExtDescriptorBytes, _ := ioutil.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
mtaExtDescriptorBytes, _ := os.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
utils.AddFile(MTA_EXT_DESCRIPTOR_PATH_LOCAL, mtaExtDescriptorBytes)
invalidMtaExtDescriptorBytes, _ := ioutil.ReadFile(INVALID_MTA_EXT_DESCRIPTOR_PATH)
invalidMtaExtDescriptorBytes, _ := os.ReadFile(INVALID_MTA_EXT_DESCRIPTOR_PATH)
utils.AddFile(INVALID_MTA_EXT_DESCRIPTOR_PATH_LOCAL, invalidMtaExtDescriptorBytes)
invalidMtaExtDescriptorBytes2, _ := ioutil.ReadFile(INVALID_MTA_EXT_DESCRIPTOR_PATH_2)
invalidMtaExtDescriptorBytes2, _ := os.ReadFile(INVALID_MTA_EXT_DESCRIPTOR_PATH_2)
utils.AddFile(INVALID_MTA_EXT_DESCRIPTOR_PATH_LOCAL_2, invalidMtaExtDescriptorBytes2)
invalidMtaExtDescriptorBytes3, _ := ioutil.ReadFile(INVALID_MTA_EXT_DESCRIPTOR_PATH_3)
invalidMtaExtDescriptorBytes3, _ := os.ReadFile(INVALID_MTA_EXT_DESCRIPTOR_PATH_3)
utils.AddFile(INVALID_MTA_EXT_DESCRIPTOR_PATH_LOCAL_3, invalidMtaExtDescriptorBytes3)
nodeNameExtDescriptorMapping := map[string]interface{}{NODE_NAME: MTA_EXT_DESCRIPTOR_PATH_LOCAL, "UNEXISTING_NODE": "unexisting.mtaext", "ONE_MORE_UNEXISTING_NODE": INVALID_MTA_EXT_DESCRIPTOR_PATH_LOCAL, "ONE_MORE_UNEXISTING_NODE_2": INVALID_MTA_EXT_DESCRIPTOR_PATH_LOCAL_2, "ONE_MORE_UNEXISTING_NODE_3": INVALID_MTA_EXT_DESCRIPTOR_PATH_LOCAL_3}
@ -369,10 +369,10 @@ func TestRunTmsUpload(t *testing.T) {
utils := newTmsUploadTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := ioutil.ReadFile(MTA_YAML_PATH)
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
mtaExtDescriptorBytes, _ := ioutil.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
mtaExtDescriptorBytes, _ := os.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
utils.AddFile(MTA_EXT_DESCRIPTOR_PATH_LOCAL, mtaExtDescriptorBytes)
nodeNameExtDescriptorMapping := map[string]interface{}{NODE_NAME: MTA_EXT_DESCRIPTOR_PATH_LOCAL}
@ -396,10 +396,10 @@ func TestRunTmsUpload(t *testing.T) {
utils := newTmsUploadTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := ioutil.ReadFile(MTA_YAML_PATH)
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
mtaExtDescriptorBytes, _ := ioutil.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
mtaExtDescriptorBytes, _ := os.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
utils.AddFile(MTA_EXT_DESCRIPTOR_PATH_LOCAL, mtaExtDescriptorBytes)
nodeNameExtDescriptorMapping := map[string]interface{}{NODE_NAME: MTA_EXT_DESCRIPTOR_PATH_LOCAL}
@ -422,10 +422,10 @@ func TestRunTmsUpload(t *testing.T) {
utils := newTmsUploadTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := ioutil.ReadFile(MTA_YAML_PATH)
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
mtaExtDescriptorBytes, _ := ioutil.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
mtaExtDescriptorBytes, _ := os.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
utils.AddFile(MTA_EXT_DESCRIPTOR_PATH_LOCAL, mtaExtDescriptorBytes)
nodeNameExtDescriptorMapping := map[string]interface{}{NODE_NAME: MTA_EXT_DESCRIPTOR_PATH_LOCAL}
@ -448,10 +448,10 @@ func TestRunTmsUpload(t *testing.T) {
utils := newTmsUploadTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := ioutil.ReadFile(MTA_YAML_PATH)
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
mtaExtDescriptorBytes, _ := ioutil.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
mtaExtDescriptorBytes, _ := os.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
utils.AddFile(MTA_EXT_DESCRIPTOR_PATH_LOCAL, mtaExtDescriptorBytes)
nodeNameExtDescriptorMapping := map[string]interface{}{NODE_NAME: MTA_EXT_DESCRIPTOR_PATH_LOCAL}
@ -475,10 +475,10 @@ func TestRunTmsUpload(t *testing.T) {
utils := newTmsUploadTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := ioutil.ReadFile(MTA_YAML_PATH)
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
mtaExtDescriptorBytes, _ := ioutil.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
mtaExtDescriptorBytes, _ := os.ReadFile(MTA_EXT_DESCRIPTOR_PATH)
utils.AddFile(MTA_EXT_DESCRIPTOR_PATH_LOCAL, mtaExtDescriptorBytes)
nodeNameExtDescriptorMapping := map[string]interface{}{NODE_NAME: MTA_EXT_DESCRIPTOR_PATH_LOCAL}

View File

@ -19,6 +19,7 @@ run:
- '"TestMavenIntegration"'
- '"TestMTAIntegration"'
# - '"TestNexusIntegration"'
- '"TestTmsUploadIntegration"'
# these are light-weighted tests, so we can use only one pod to reduce resource consumption
- '"Test(Gauge|GCS|GitHub|GitOps|Influx|NPM|Piper|Python|Sonar|Vault|Karma)Integration"'

View File

@ -0,0 +1,144 @@
//go:build integration
// +build integration
// can be executed with
// go test -v -tags integration -run TestTmsUploadIntegration ./integration
package main
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
var tmsServiceKey string
func readEnv() {
//Reading TMS credentials from environment
tmsServiceKey = os.Getenv("PIPER_tmsServiceKey")
if len(tmsServiceKey) == 0 {
fmt.Println("Env. variable PIPER_tmsServiceKey is not provided")
os.Exit(1)
}
}
func TestTmsUploadIntegrationBinSuccess(t *testing.T) {
// success case: run cmd without nodeExtDescriptorMapping
readEnv()
container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
Image: "devxci/mbtci-java11-node14",
User: "root",
TestDir: []string{"testdata", "TestTmsUploadIntegration"},
Environment: map[string]string{"PIPER_tmsServiceKey": tmsServiceKey},
})
defer container.terminate(t)
err := container.whenRunningPiperCommand("tmsUpload",
"--mtaPath=scv_x.mtar",
"--nodeName=PIPER-TEST",
"--customDescription=Piper integration test",
"--mtaVersion=1.0.0",
"-v")
if err != nil {
t.Fatalf("Piper command failed %s", err)
}
container.assertHasOutput(t, "description: Piper integration test")
container.assertHasOutput(t, "tmsUpload - File uploaded successfully")
container.assertHasOutput(t, "tmsUpload - Node upload executed successfully")
container.assertHasOutput(t, "tmsUpload - SUCCESS")
}
func TestTmsUploadIntegrationBinNoDescriptionSuccess(t *testing.T) {
// success case: run cmd without --nodeExtDescriptorMapping and --customDescription
readEnv()
container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
Image: "devxci/mbtci-java11-node14",
User: "root",
TestDir: []string{"testdata", "TestTmsUploadIntegration"},
Environment: map[string]string{"PIPER_tmsServiceKey": tmsServiceKey},
})
defer container.terminate(t)
err := container.whenRunningPiperCommand("tmsUpload",
"--mtaPath=scv_x.mtar",
"--nodeName=PIPER-TEST",
"--mtaVersion=1.0.0",
"-v")
if err != nil {
t.Fatalf("Piper command failed %s", err)
}
container.assertHasOutput(t, "description: Created by Piper")
container.assertHasOutput(t, "tmsUpload - File uploaded successfully")
container.assertHasOutput(t, "tmsUpload - Node upload executed successfully")
container.assertHasOutput(t, "tmsUpload - SUCCESS")
}
func TestTmsUploadIntegrationBinFailParam(t *testing.T) {
// error case: run cmd with nodeExtDescriptorMapping
readEnv()
container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
Image: "devxci/mbtci-java11-node14",
User: "root",
TestDir: []string{"testdata", "TestTmsUploadIntegration"},
})
defer container.terminate(t)
err := container.whenRunningPiperCommand("tmsUpload",
"--mtaPath=scv_x.mtar",
"--nodeName=PIPER-TEST",
"--customDescription=Piper integration test",
"--nodeExtDescriptorMapping={\"PIPER-TEST\":\"scv_x.mtaext\", \"PIPER-PROD\":\"scv_x.mtaext\"}",
"--mtaVersion=1.0.0",
"-v")
assert.Error(t, err, "Did expect error")
container.assertHasOutput(t, "Error: unknown flag: --nodeExtDescriptorMapping")
}
func TestTmsUploadIntegrationBinFailDescription(t *testing.T) {
// error case: run cmd with invalid description
readEnv()
container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
Image: "devxci/mbtci-java11-node14",
User: "root",
TestDir: []string{"testdata", "TestTmsUploadIntegration"},
Environment: map[string]string{"PIPER_tmsServiceKey": tmsServiceKey},
})
defer container.terminate(t)
err := container.whenRunningPiperCommand("tmsUpload",
"--mtaPath=scv_x.mtar",
"--nodeName=PIPER-TEST",
"--customDescription={Bad description}")
assert.Error(t, err, "Did expect error")
container.assertHasOutput(t, "error tmsUpload - HTTP request failed with error")
container.assertHasOutput(t, "Failed to run tmsUpload step - failed to upload file to node")
}
func TestTmsUploadIntegrationYaml(t *testing.T) {
// success case: run with custom config
readEnv()
container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
Image: "devxci/mbtci-java11-node14",
User: "root",
TestDir: []string{"testdata", "TestTmsUploadIntegration"},
Environment: map[string]string{"PIPER_tmsServiceKey": tmsServiceKey},
})
defer container.terminate(t)
err := container.whenRunningPiperCommand("tmsUpload", "--customConfig=.pipeline/tms_integration_test_config.yml")
if err != nil {
t.Fatalf("Piper command failed %s", err)
}
container.assertHasOutput(t, "tmsUpload - File uploaded successfully")
container.assertHasOutput(t, "tmsUpload - MTA extension descriptor updated successfully")
container.assertHasOutput(t, "tmsUpload - Node upload executed successfully")
container.assertHasOutput(t, "tmsUpload - SUCCESS")
// test that oauth token is not exposed
container.assertHasNoOutput(t, "eyJ")
}

View File

@ -0,0 +1,12 @@
general:
verbose: true
steps:
tmsUpload:
useGoStep: true
nodeName: PIPER-TEST
mtaPath: scv_x.mtar
verbose: true
mtaVersion: 1.0.0
nodeExtDescriptorMapping:
PIPER-TEST: 'scv_x.mtaext'
PIPER-PROD: 'scv_x.mtaext'

View File

@ -0,0 +1,14 @@
_schema-version: "3.1.0"
ID: alm.pi.test.scv_x
version: 1.0.0
modules:
- name: java-hello-sap-x
type: java.tomcat
path: java/target/scv_x-1.0.0.war
properties:
API_URL: "${xs-api-url}"
SET_LOGGING_LEVEL: "{alm.pi.test: INFO}"
MEMORY_CALCULATOR_V1: true
parameters:
memory: 128MM

View File

@ -0,0 +1,8 @@
_schema-version: "3.1.0"
ID: alm.pi.test.scv_x_ext
extends: alm.pi.test.scv_x
modules:
- name: java-hello-sap-x
parameters:
memory: 256M

Binary file not shown.

View File

@ -117,7 +117,6 @@ func NewCommunicationInstance(httpClient piperHttp.Uploader, tmsUrl, uaaUrl, cli
if err != nil {
return communicationInstance, errors.Wrap(err, "Error fetching OAuth token")
}
log.RegisterSecret(token)
clientOptions.Token = token
communicationInstance.httpClient.SetOptions(clientOptions)
@ -132,6 +131,7 @@ func (communicationInstance *CommunicationInstance) getOAuthToken() (string, err
}
encodedUsernameColonPassword := b64.StdEncoding.EncodeToString([]byte(communicationInstance.clientId + ":" + communicationInstance.clientSecret))
log.RegisterSecret(encodedUsernameColonPassword)
header := http.Header{}
header.Add("Content-Type", "application/x-www-form-urlencoded")
header.Add("Authorization", "Basic "+encodedUsernameColonPassword)
@ -153,6 +153,7 @@ func (communicationInstance *CommunicationInstance) getOAuthToken() (string, err
if communicationInstance.isVerbose {
communicationInstance.logger.Info("OAuth Token retrieved successfully")
}
log.RegisterSecret(token.AccessToken)
return token.TokenType + " " + token.AccessToken, nil
}