1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/tmsExport_test.go

149 lines
5.6 KiB
Go
Raw Normal View History

Tms export (#4160) * Change parameter type of nodeExtDescriptorMapping (cherry picked from commit ca7ce0485a90f9bbd04a86524ae13cfacc04601f) * Remove usage of the depricated ioutil package (cherry picked from commit 9821915b33ad4765f41288ece9163d27fbf6c031) * Fix cmd failure if neither git/commitId nor customDescription are provided (cherry picked from commit c362681e4569734744a2133beb950bdd78577d33) * Fix unit test (cherry picked from commit 53a90aabb5ca4a254e646b5912685ab2d4d11834) * Step metadata, step code generation * change type of nodeExtDescriptorMapping for export * Refactoring and export implementation * integration test * Add export step * Integration test * format * discard piper.go * Review related changes * restore piper.go * remove unused method * Extend documentation * Add parameter useGoStep to tmsUpload.groovy * Regenerate steps * Rename function * refactor constants * Add error path tests * Move some code to tms package * Move more code to tms * Combine tmsUpload, tmsUtils * Add groovy wrapper * add parameters to groovy step * add import * jenkinsUtils instance * comment namedUser logic in groovy * namedUser param * remove logic for namedUser param * Remove TMS integration tests * discard changes in tmsUpload.groovy * Remove parameters * Restore parameters * Change type of NodeExtDescriptorMapping to map[string]interface{} * tmsUpload: Change type of NodeExtDescriptorMapping to map * Resolve ioutil deprecation * Review related changes * Formatting * Review related improvements * Add tmsUtils test * Formatting tmsUtils_test * Remove parameters from groovy wrapper * Remove tmsUtils_test * Add TMS steps to fieldRelatedWhitelist * Add integration test * Add test to github_actions_integration_test_list.yml * Move test helper method * Step documentation placeholder * Remove parameter StashContent * Restore cmd/integrationArtifactTransport.go --------- Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com>
2023-03-27 16:55:29 +02:00
package cmd
import (
"os"
"strconv"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/tms"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
type tmsExportMockUtils struct {
*mock.ExecMockRunner
*mock.FilesMock
}
func newTmsExportTestsUtils() tmsExportMockUtils {
utils := tmsExportMockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
return utils
}
func (cim *communicationInstanceMock) ExportFileToNode(nodeName, fileId, description, namedUser string) (tms.NodeUploadResponseEntity, error) {
var nodeUploadResponseEntity tms.NodeUploadResponseEntity
if description != CUSTOM_DESCRIPTION || nodeName != NODE_NAME || fileId != strconv.FormatInt(FILE_ID, 10) || namedUser != NAMED_USER {
return nodeUploadResponseEntity, errors.New(INVALID_INPUT_MSG)
}
if cim.isErrorOnExportFileToNode {
return nodeUploadResponseEntity, errors.New("Something went wrong on exporting file to node")
} else {
return cim.exportFileToNodeResponse, nil
}
}
func TestRunTmsExport(t *testing.T) {
t.Parallel()
t.Run("happy path: 1. get nodes 2. get MTA ext descriptor -> nothing obtained 3. upload MTA ext descriptor to node 4. upload file 5. export file to node", func(t *testing.T) {
t.Parallel()
// init
nodes := []tms.Node{{Id: NODE_ID, Name: NODE_NAME}}
fileInfo := tms.FileInfo{Id: FILE_ID, Name: MTA_NAME}
communicationInstance := communicationInstanceMock{getNodesResponse: nodes, uploadFileResponse: fileInfo}
utils := newTmsTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
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}
config := tmsExportOptions{MtaPath: MTA_PATH_LOCAL, CustomDescription: CUSTOM_DESCRIPTION, NamedUser: NAMED_USER, NodeName: NODE_NAME, MtaVersion: MTA_VERSION, NodeExtDescriptorMapping: nodeNameExtDescriptorMapping}
// test
err := runTmsExport(config, &communicationInstance, utils)
// assert
assert.NoError(t, err)
})
t.Run("error path: error while uploading file", func(t *testing.T) {
t.Parallel()
// init
nodes := []tms.Node{{Id: NODE_ID, Name: NODE_NAME}}
communicationInstance := communicationInstanceMock{getNodesResponse: nodes, isErrorOnUploadFile: true}
utils := newTmsTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
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}
config := tmsExportOptions{MtaPath: MTA_PATH_LOCAL, CustomDescription: CUSTOM_DESCRIPTION, NamedUser: NAMED_USER, NodeName: NODE_NAME, MtaVersion: MTA_VERSION, NodeExtDescriptorMapping: nodeNameExtDescriptorMapping}
// test
err := runTmsExport(config, &communicationInstance, utils)
// assert
assert.EqualError(t, err, "failed to upload file: Something went wrong on uploading file")
})
t.Run("error path: error while uploading MTA extension descriptor to node", func(t *testing.T) {
t.Parallel()
// init
nodes := []tms.Node{{Id: NODE_ID, Name: NODE_NAME}}
communicationInstance := communicationInstanceMock{getNodesResponse: nodes, isErrorOnUploadMtaExtDescriptorToNode: true}
utils := newTmsTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
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}
config := tmsExportOptions{MtaPath: MTA_PATH_LOCAL, CustomDescription: CUSTOM_DESCRIPTION, NamedUser: NAMED_USER, NodeName: NODE_NAME, MtaVersion: MTA_VERSION, NodeExtDescriptorMapping: nodeNameExtDescriptorMapping}
// test
err := runTmsExport(config, &communicationInstance, utils)
// assert
assert.EqualError(t, err, "failed to upload MTA extension descriptor to node: Something went wrong on uploading MTA extension descriptor to node")
})
t.Run("error path: error while exporting file to node", func(t *testing.T) {
t.Parallel()
// init
nodes := []tms.Node{{Id: NODE_ID, Name: NODE_NAME}}
fileInfo := tms.FileInfo{Id: FILE_ID, Name: MTA_NAME}
communicationInstance := communicationInstanceMock{getNodesResponse: nodes, uploadFileResponse: fileInfo, isErrorOnExportFileToNode: true}
utils := newTmsTestsUtils()
utils.AddFile(MTA_PATH_LOCAL, []byte("dummy content"))
mtaYamlBytes, _ := os.ReadFile(MTA_YAML_PATH)
utils.AddFile(MTA_YAML_PATH_LOCAL, mtaYamlBytes)
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}
config := tmsExportOptions{MtaPath: MTA_PATH_LOCAL, CustomDescription: CUSTOM_DESCRIPTION, NamedUser: NAMED_USER, NodeName: NODE_NAME, MtaVersion: MTA_VERSION, NodeExtDescriptorMapping: nodeNameExtDescriptorMapping}
// test
err := runTmsExport(config, &communicationInstance, utils)
// assert
assert.EqualError(t, err, "failed to export file to node: Something went wrong on exporting file to node")
})
}