2020-02-25 15:33:34 +02:00
package cmd
import (
2020-06-18 17:30:17 +02:00
"github.com/SAP/jenkins-library/pkg/npm"
2020-05-05 15:54:50 +02:00
"os"
"testing"
2020-07-16 14:25:01 +02:00
"fmt"
2020-02-25 15:33:34 +02:00
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/maven"
2020-02-27 13:11:22 +02:00
"github.com/SAP/jenkins-library/pkg/mock"
2020-02-25 15:33:34 +02:00
"github.com/SAP/jenkins-library/pkg/piperutils"
2020-05-05 15:54:50 +02:00
"github.com/ghodss/yaml"
2020-02-25 15:33:34 +02:00
"github.com/stretchr/testify/assert"
)
func TestMarBuild ( t * testing . T ) {
cpe := mtaBuildCommonPipelineEnvironment { }
httpClient := piperhttp . Client { }
t . Run ( "Application name not set" , func ( t * testing . T ) {
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-02-25 15:33:34 +02:00
options := mtaBuildOptions { }
fileUtils := MtaTestFileUtilsMock { }
2020-06-18 17:30:17 +02:00
err := runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
2020-02-25 15:33:34 +02:00
assert . NotNil ( t , err )
assert . Equal ( t , "'mta.yaml' not found in project sources and 'applicationName' not provided as parameter - cannot generate 'mta.yaml' file" , err . Error ( ) )
} )
t . Run ( "Provide default npm registry" , func ( t * testing . T ) {
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-05-20 13:41:23 +02:00
e . StdoutReturn = map [ string ] string { "npm config get registry" : "undefined" }
2020-02-25 15:33:34 +02:00
options := mtaBuildOptions { ApplicationName : "myApp" , MtaBuildTool : "classic" , BuildTarget : "CF" , DefaultNpmRegistry : "https://example.org/npm" , MtarName : "myName" }
existingFiles := make ( map [ string ] string )
existingFiles [ "package.json" ] = "{\"name\": \"myName\", \"version\": \"1.2.3\"}"
fileUtils := MtaTestFileUtilsMock { existingFiles : existingFiles }
2020-06-18 17:30:17 +02:00
npmExecutor := newNpmExecutor ( & e )
npmExecutor . Options = npm . ExecutorOptions { DefaultNpmRegistry : options . DefaultNpmRegistry }
err := runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , npmExecutor )
2020-02-25 15:33:34 +02:00
assert . Nil ( t , err )
2020-08-11 15:58:39 +02:00
if assert . Len ( t , e . Calls , 3 ) { // the second (unchecked) entry is the mta call
2020-05-20 13:41:23 +02:00
assert . Equal ( t , "npm" , e . Calls [ 1 ] . Exec )
assert . Equal ( t , [ ] string { "config" , "set" , "registry" , "https://example.org/npm" } , e . Calls [ 1 ] . Params )
2020-02-25 15:33:34 +02:00
}
} )
t . Run ( "Package json does not exist" , func ( t * testing . T ) {
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-02-25 15:33:34 +02:00
options := mtaBuildOptions { ApplicationName : "myApp" }
fileUtils := MtaTestFileUtilsMock { }
2020-06-18 17:30:17 +02:00
err := runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
2020-02-25 15:33:34 +02:00
assert . NotNil ( t , err )
assert . Equal ( t , "package.json file does not exist" , err . Error ( ) )
} )
t . Run ( "Write yaml file" , func ( t * testing . T ) {
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-02-25 15:33:34 +02:00
options := mtaBuildOptions { ApplicationName : "myApp" , MtaBuildTool : "classic" , BuildTarget : "CF" , MtarName : "myName" }
existingFiles := make ( map [ string ] string )
existingFiles [ "package.json" ] = "{\"name\": \"myName\", \"version\": \"1.2.3\"}"
fileUtils := MtaTestFileUtilsMock { existingFiles : existingFiles }
2020-06-18 17:30:17 +02:00
err := runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
2020-02-25 15:33:34 +02:00
assert . Nil ( t , err )
type MtaResult struct {
Version string
ID string ` yaml:"ID,omitempty" `
Parameters map [ string ] string
Modules [ ] struct {
Name string
Type string
Parameters map [ string ] interface { }
}
}
assert . NotEmpty ( t , fileUtils . writtenFiles [ "mta.yaml" ] )
var result MtaResult
yaml . Unmarshal ( [ ] byte ( fileUtils . writtenFiles [ "mta.yaml" ] ) , & result )
assert . Equal ( t , "myName" , result . ID )
assert . Equal ( t , "1.2.3" , result . Version )
assert . Equal ( t , "myApp" , result . Modules [ 0 ] . Name )
assert . Equal ( t , result . Modules [ 0 ] . Parameters [ "version" ] , "1.2.3-${timestamp}" )
assert . Equal ( t , "myApp" , result . Modules [ 0 ] . Parameters [ "name" ] )
} )
t . Run ( "Dont write mta yaml file when already present no timestamp placeholder" , func ( t * testing . T ) {
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-02-25 15:33:34 +02:00
options := mtaBuildOptions { ApplicationName : "myApp" , MtaBuildTool : "classic" , BuildTarget : "CF" }
existingFiles := make ( map [ string ] string )
existingFiles [ "package.json" ] = "{\"name\": \"myName\", \"version\": \"1.2.3\"}"
existingFiles [ "mta.yaml" ] = "already there"
fileUtils := MtaTestFileUtilsMock { existingFiles : existingFiles }
2020-06-18 17:30:17 +02:00
_ = runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
2020-02-25 15:33:34 +02:00
assert . Empty ( t , fileUtils . writtenFiles )
} )
t . Run ( "Write mta yaml file when already present with timestamp placeholder" , func ( t * testing . T ) {
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-02-25 15:33:34 +02:00
options := mtaBuildOptions { ApplicationName : "myApp" , MtaBuildTool : "classic" , BuildTarget : "CF" }
existingFiles := make ( map [ string ] string )
existingFiles [ "package.json" ] = "{\"name\": \"myName\", \"version\": \"1.2.3\"}"
existingFiles [ "mta.yaml" ] = "already there with-${timestamp}"
fileUtils := MtaTestFileUtilsMock { existingFiles : existingFiles }
2020-06-18 17:30:17 +02:00
_ = runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
2020-02-25 15:33:34 +02:00
assert . NotEmpty ( t , fileUtils . writtenFiles [ "mta.yaml" ] )
} )
t . Run ( "Test mta build classic toolset" , func ( t * testing . T ) {
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-02-25 15:33:34 +02:00
2020-05-19 22:02:47 +02:00
options := mtaBuildOptions { ApplicationName : "myApp" , MtaBuildTool : "classic" , BuildTarget : "CF" , MtarName : "myName.mtar" }
2020-02-25 15:33:34 +02:00
cpe . mtarFilePath = ""
existingFiles := make ( map [ string ] string )
existingFiles [ "package.json" ] = "{\"name\": \"myName\", \"version\": \"1.2.3\"}"
fileUtils := MtaTestFileUtilsMock { existingFiles : existingFiles }
2020-06-18 17:30:17 +02:00
err := runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
2020-02-25 15:33:34 +02:00
assert . Nil ( t , err )
2020-08-11 15:58:39 +02:00
if assert . Len ( t , e . Calls , 2 ) {
assert . Equal ( t , "java" , e . Calls [ 1 ] . Exec )
assert . Equal ( t , [ ] string { "-jar" , "mta.jar" , "--mtar" , "myName.mtar" , "--build-target=CF" , "build" } , e . Calls [ 1 ] . Params )
2020-02-25 15:33:34 +02:00
}
assert . Equal ( t , "myName.mtar" , cpe . mtarFilePath )
} )
t . Run ( "Test mta build classic toolset, mtarName from already existing mta.yaml" , func ( t * testing . T ) {
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-02-25 15:33:34 +02:00
options := mtaBuildOptions { ApplicationName : "myApp" , MtaBuildTool : "classic" , BuildTarget : "CF" }
cpe . mtarFilePath = ""
existingFiles := make ( map [ string ] string )
existingFiles [ "package.json" ] = "{\"name\": \"myName\", \"version\": \"1.2.3\"}"
existingFiles [ "mta.yaml" ] = "ID: \"myNameFromMtar\""
fileUtils := MtaTestFileUtilsMock { existingFiles : existingFiles }
2020-06-18 17:30:17 +02:00
err := runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
2020-02-25 15:33:34 +02:00
assert . Nil ( t , err )
2020-08-11 15:58:39 +02:00
if assert . Len ( t , e . Calls , 2 ) {
assert . Equal ( t , "java" , e . Calls [ 1 ] . Exec )
assert . Equal ( t , [ ] string { "-jar" , "mta.jar" , "--mtar" , "myNameFromMtar.mtar" , "--build-target=CF" , "build" } , e . Calls [ 1 ] . Params )
2020-02-25 15:33:34 +02:00
}
} )
t . Run ( "Test mta build classic toolset with configured mta jar" , func ( t * testing . T ) {
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-02-25 15:33:34 +02:00
2020-05-19 22:02:47 +02:00
options := mtaBuildOptions { ApplicationName : "myApp" , MtaBuildTool : "classic" , BuildTarget : "CF" , MtaJarLocation : "/opt/sap/mta/lib/mta.jar" , MtarName : "myName.mtar" }
2020-02-25 15:33:34 +02:00
existingFiles := make ( map [ string ] string )
existingFiles [ "package.json" ] = "{\"name\": \"myName\", \"version\": \"1.2.3\"}"
fileUtils := MtaTestFileUtilsMock { existingFiles : existingFiles }
2020-06-18 17:30:17 +02:00
err := runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
2020-02-25 15:33:34 +02:00
assert . Nil ( t , err )
2020-08-11 15:58:39 +02:00
if assert . Len ( t , e . Calls , 2 ) {
assert . Equal ( t , "java" , e . Calls [ 1 ] . Exec )
assert . Equal ( t , [ ] string { "-jar" , "/opt/sap/mta/lib/mta.jar" , "--mtar" , "myName.mtar" , "--build-target=CF" , "build" } , e . Calls [ 1 ] . Params )
2020-02-25 15:33:34 +02:00
}
} )
t . Run ( "Mta build mbt toolset" , func ( t * testing . T ) {
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-02-25 15:33:34 +02:00
cpe . mtarFilePath = ""
2020-05-19 22:02:47 +02:00
options := mtaBuildOptions { ApplicationName : "myApp" , MtaBuildTool : "cloudMbt" , Platform : "CF" , MtarName : "myName.mtar" }
2020-02-25 15:33:34 +02:00
existingFiles := make ( map [ string ] string )
existingFiles [ "package.json" ] = "{\"name\": \"myName\", \"version\": \"1.2.3\"}"
fileUtils := MtaTestFileUtilsMock { existingFiles : existingFiles }
2020-06-18 17:30:17 +02:00
err := runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
2020-02-25 15:33:34 +02:00
assert . Nil ( t , err )
2020-08-11 15:58:39 +02:00
if assert . Len ( t , e . Calls , 2 ) {
assert . Equal ( t , "mbt" , e . Calls [ 1 ] . Exec )
assert . Equal ( t , [ ] string { "build" , "--mtar" , "myName.mtar" , "--platform" , "CF" , "--target" , "./" } , e . Calls [ 1 ] . Params )
2020-02-25 15:33:34 +02:00
}
assert . Equal ( t , "myName.mtar" , cpe . mtarFilePath )
} )
2020-07-28 17:06:17 +02:00
t . Run ( "M2Path related tests" , func ( t * testing . T ) {
t . Run ( "Mta build mbt toolset with m2Path" , func ( t * testing . T ) {
e := mock . ExecMockRunner { }
cpe . mtarFilePath = ""
options := mtaBuildOptions { ApplicationName : "myApp" , MtaBuildTool : "cloudMbt" , Platform : "CF" , MtarName : "myName.mtar" , M2Path : ".pipeline/local_repo" }
existingFiles := make ( map [ string ] string )
existingFiles [ "mta.yaml" ] = "ID: \"myNameFromMtar\""
fileUtils := MtaTestFileUtilsMock { existingFiles : existingFiles }
err := runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
assert . Nil ( t , err )
assert . Contains ( t , e . Env , "MAVEN_OPTS=-Dmaven.repo.local=/root_folder/workspace/.pipeline/local_repo" )
} )
} )
2020-02-25 15:33:34 +02:00
t . Run ( "Settings file releatd tests" , func ( t * testing . T ) {
2020-07-30 10:35:46 +02:00
var projectSettingsFile string
var globalSettingsFile string
2020-02-25 15:33:34 +02:00
defer func ( ) {
2020-07-30 10:35:46 +02:00
downloadAndCopySettingsFiles = maven . DownloadAndCopySettingsFiles
2020-02-25 15:33:34 +02:00
} ( )
2020-07-30 10:35:46 +02:00
downloadAndCopySettingsFiles = func (
globalSettings string ,
projectSettings string ,
fileUtils piperutils . FileUtils ,
httpClient maven . SettingsDownloadUtils ) error {
projectSettingsFile = projectSettings
globalSettingsFile = globalSettings
2020-02-25 15:33:34 +02:00
return nil
}
fileUtils := MtaTestFileUtilsMock { }
fileUtils . existingFiles = make ( map [ string ] string )
fileUtils . existingFiles [ "package.json" ] = "{\"name\": \"myName\", \"version\": \"1.2.3\"}"
t . Run ( "Copy global settings file" , func ( t * testing . T ) {
defer func ( ) {
2020-07-30 10:35:46 +02:00
projectSettingsFile = ""
globalSettingsFile = ""
2020-02-25 15:33:34 +02:00
} ( )
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-02-25 15:33:34 +02:00
options := mtaBuildOptions { ApplicationName : "myApp" , GlobalSettingsFile : "/opt/maven/settings.xml" , MtaBuildTool : "cloudMbt" , Platform : "CF" , MtarName : "myName" }
2020-06-18 17:30:17 +02:00
err := runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
2020-02-25 15:33:34 +02:00
assert . Nil ( t , err )
2020-07-30 10:35:46 +02:00
assert . Equal ( t , globalSettingsFile , "/opt/maven/settings.xml" )
assert . Equal ( t , projectSettingsFile , "" )
2020-02-25 15:33:34 +02:00
} )
t . Run ( "Copy project settings file" , func ( t * testing . T ) {
defer func ( ) {
2020-07-30 10:35:46 +02:00
projectSettingsFile = ""
globalSettingsFile = ""
2020-02-25 15:33:34 +02:00
} ( )
2020-02-27 13:11:22 +02:00
e := mock . ExecMockRunner { }
2020-02-25 15:33:34 +02:00
options := mtaBuildOptions { ApplicationName : "myApp" , ProjectSettingsFile : "/my/project/settings.xml" , MtaBuildTool : "cloudMbt" , Platform : "CF" , MtarName : "myName" }
2020-06-18 17:30:17 +02:00
err := runMtaBuild ( options , & cpe , & e , & fileUtils , & httpClient , newNpmExecutor ( & e ) )
2020-02-25 15:33:34 +02:00
assert . Nil ( t , err )
2020-07-30 10:35:46 +02:00
assert . Equal ( t , "/my/project/settings.xml" , projectSettingsFile )
assert . Equal ( t , "" , globalSettingsFile )
2020-02-25 15:33:34 +02:00
} )
} )
}
type MtaTestFileUtilsMock struct {
existingFiles map [ string ] string
writtenFiles map [ string ] string
copiedFiles map [ string ] string
}
func ( f * MtaTestFileUtilsMock ) FileExists ( path string ) ( bool , error ) {
if _ , ok := f . existingFiles [ path ] ; ok {
return true , nil
}
return false , nil
}
func ( f * MtaTestFileUtilsMock ) Copy ( src , dest string ) ( int64 , error ) {
if f . copiedFiles == nil {
f . copiedFiles = make ( map [ string ] string )
}
f . copiedFiles [ src ] = dest
return 0 , nil
}
func ( f * MtaTestFileUtilsMock ) FileRead ( path string ) ( [ ] byte , error ) {
return [ ] byte ( f . existingFiles [ path ] ) , nil
}
func ( f * MtaTestFileUtilsMock ) FileWrite ( path string , content [ ] byte , perm os . FileMode ) error {
if f . writtenFiles == nil {
f . writtenFiles = make ( map [ string ] string )
}
if _ , ok := f . writtenFiles [ path ] ; ok {
delete ( f . writtenFiles , path )
}
f . writtenFiles [ path ] = string ( content )
return nil
}
func ( f * MtaTestFileUtilsMock ) MkdirAll ( path string , perm os . FileMode ) error {
return nil
}
2020-06-18 17:30:17 +02:00
2020-07-28 17:06:17 +02:00
func ( f * MtaTestFileUtilsMock ) Abs ( path string ) ( string , error ) {
return "/root_folder/workspace/" + path , nil
}
2020-07-30 10:35:46 +02:00
func ( f * MtaTestFileUtilsMock ) Glob ( pattern string ) ( matches [ ] string , err error ) {
return nil , fmt . Errorf ( "not implemented. func is only present in order to fullfil the interface contract. Needs to be ajusted in case it gets used." )
}
2020-07-16 14:25:01 +02:00
func ( f * MtaTestFileUtilsMock ) Chmod ( path string , mode os . FileMode ) error {
return fmt . Errorf ( "not implemented. func is only present in order to fullfil the interface contract. Needs to be ajusted in case it gets used." )
}
2020-06-18 17:30:17 +02:00
func newNpmExecutor ( execRunner * mock . ExecMockRunner ) * npm . Execute {
utils := newNpmMockUtilsBundle ( )
utils . execRunner = execRunner
return & npm . Execute { Utils : & utils }
}