2023-05-03 18:02:11 +02:00
//go:build unit
// +build unit
2020-03-20 19:20:52 +02:00
package cmd
import (
2020-11-10 18:14:55 +02:00
"errors"
2020-03-20 19:20:52 +02:00
"fmt"
"github.com/SAP/jenkins-library/pkg/maven"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/nexus"
"github.com/stretchr/testify/assert"
2020-11-10 18:14:55 +02:00
"net/http"
2020-06-18 17:30:17 +02:00
"os"
"path/filepath"
"strings"
2020-03-20 19:20:52 +02:00
"testing"
)
type mockUtilsBundle struct {
2020-06-15 09:47:33 +02:00
* mock . FilesMock
2020-11-10 18:14:55 +02:00
* mock . ExecMockRunner
2020-06-15 09:47:33 +02:00
mta bool
maven bool
npm bool
properties map [ string ] map [ string ] string
cpe map [ string ] string
2020-03-20 19:20:52 +02:00
}
2020-11-10 18:14:55 +02:00
func ( m * mockUtilsBundle ) DownloadFile ( url , filename string , header http . Header , cookies [ ] * http . Cookie ) error {
2020-11-11 17:32:23 +02:00
return errors . New ( "test should not download files" )
2020-11-10 18:14:55 +02:00
}
func newMockUtilsBundle ( usesMta , usesMaven , usesNpm bool ) * mockUtilsBundle {
utils := mockUtilsBundle {
FilesMock : & mock . FilesMock { } ,
ExecMockRunner : & mock . ExecMockRunner { } ,
mta : usesMta ,
maven : usesMaven ,
npm : usesNpm ,
}
2020-03-20 19:20:52 +02:00
utils . properties = map [ string ] map [ string ] string { }
utils . cpe = map [ string ] string { }
2020-11-10 18:14:55 +02:00
return & utils
2020-03-20 19:20:52 +02:00
}
2020-06-15 09:47:33 +02:00
func ( m * mockUtilsBundle ) UsesMta ( ) bool {
2020-03-20 19:20:52 +02:00
return m . mta
}
2020-06-15 09:47:33 +02:00
func ( m * mockUtilsBundle ) UsesMaven ( ) bool {
2020-03-20 19:20:52 +02:00
return m . maven
}
2020-06-15 09:47:33 +02:00
func ( m * mockUtilsBundle ) UsesNpm ( ) bool {
2020-04-11 12:56:44 +02:00
return m . npm
}
2020-03-20 19:20:52 +02:00
func ( m * mockUtilsBundle ) getEnvParameter ( path , name string ) string {
path = path + "/" + name
return m . cpe [ path ]
}
func ( m * mockUtilsBundle ) setProperty ( pomFile , expression , value string ) {
2020-06-18 17:30:17 +02:00
pomFile = strings . ReplaceAll ( pomFile , "/" , string ( os . PathSeparator ) )
pomFile = strings . ReplaceAll ( pomFile , "\\" , string ( os . PathSeparator ) )
2020-03-20 19:20:52 +02:00
pom := m . properties [ pomFile ]
if pom == nil {
pom = map [ string ] string { }
m . properties [ pomFile ] = pom
}
pom [ expression ] = value
}
2020-06-11 14:02:54 +02:00
func ( m * mockUtilsBundle ) evaluate ( options * maven . EvaluateOptions , expression string ) ( string , error ) {
pom := m . properties [ options . PomPath ]
2020-03-20 19:20:52 +02:00
if pom == nil {
2020-06-11 14:02:54 +02:00
return "" , fmt . Errorf ( "pom file '%s' not found" , options . PomPath )
2020-03-20 19:20:52 +02:00
}
value := pom [ expression ]
if value == "<empty>" {
return "" , nil
}
if value == "" {
2020-06-11 14:02:54 +02:00
return "" , fmt . Errorf ( "property '%s' not found in '%s'" , expression , options . PomPath )
2020-03-20 19:20:52 +02:00
}
return value , nil
}
type mockUploader struct {
nexus . Upload
uploadedArtifacts [ ] nexus . ArtifactDescription
}
func ( m * mockUploader ) Clear ( ) {
// Clear is called after a successful upload. Record the artifacts that are present before
// they are cleared. This way we can later peek into the set of all artifacts that were
// uploaded across multiple bundles.
m . uploadedArtifacts = append ( m . uploadedArtifacts , m . GetArtifacts ( ) ... )
m . Upload . Clear ( )
}
func createOptions ( ) nexusUploadOptions {
return nexusUploadOptions {
2020-04-11 12:56:44 +02:00
MavenRepository : "maven-releases" ,
NpmRepository : "npm-repo" ,
GroupID : "my.group.id" ,
ArtifactID : "artifact.id" ,
Version : "nexus3" ,
Url : "localhost:8081" ,
2020-03-20 19:20:52 +02:00
}
}
var testMtaYml = [ ] byte ( `
_schema - version : 2.1 .0
ID : test
version : 0.3 .0
modules :
- name : java
type : java
path : srv
` )
var testMtaYmlNoVersion = [ ] byte ( `
_schema - version : 2.1 .0
ID : test
modules :
- name : java
type : java
` )
var testPomXml = [ ] byte ( `
< project >
< modelVersion > 4.0 .0 < / modelVersion >
< groupId > com . mycompany . app < / groupId >
< artifactId > my - app < / artifactId >
< version > 1.0 < / version >
< / project >
` )
2020-04-11 12:56:44 +02:00
var testPackageJson = [ ] byte ( ` {
"name" : "npm-nexus-upload-test" ,
"version" : "1.0.0"
}
` )
2020-03-20 19:20:52 +02:00
func TestUploadMTAProjects ( t * testing . T ) {
2020-06-15 09:47:33 +02:00
t . Parallel ( )
2020-03-20 19:20:52 +02:00
t . Run ( "Uploading MTA project without groupId parameter fails" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( true , false , false )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "mta.yaml" , testMtaYml )
2020-03-20 19:20:52 +02:00
utils . cpe [ ".pipeline/commonPipelineEnvironment/mtarFilePath" ] = "test.mtar"
uploader := mockUploader { }
options := createOptions ( )
options . GroupID = ""
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . EqualError ( t , err , "the 'groupId' parameter needs to be provided for MTA projects" )
assert . Equal ( t , 0 , len ( uploader . GetArtifacts ( ) ) )
assert . Equal ( t , 0 , len ( uploader . uploadedArtifacts ) )
} )
2020-03-31 15:16:18 +02:00
t . Run ( "Uploading MTA project without artifactId parameter works" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( true , false , false )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "mta.yaml" , testMtaYml )
utils . AddFile ( "test.mtar" , [ ] byte ( "contentsOfMtar" ) )
2020-03-20 19:20:52 +02:00
utils . cpe [ ".pipeline/commonPipelineEnvironment/mtarFilePath" ] = "test.mtar"
uploader := mockUploader { }
options := createOptions ( )
options . ArtifactID = ""
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-31 15:16:18 +02:00
if assert . NoError ( t , err ) {
assert . Equal ( t , 2 , len ( uploader . uploadedArtifacts ) )
assert . Equal ( t , "test" , uploader . GetArtifactsID ( ) )
}
2020-03-20 19:20:52 +02:00
} )
t . Run ( "Uploading MTA project fails due to missing yaml file" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( true , false , false )
2020-03-20 19:20:52 +02:00
utils . cpe [ ".pipeline/commonPipelineEnvironment/mtarFilePath" ] = "test.mtar"
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . EqualError ( t , err , "could not read from required project descriptor file 'mta.yml'" )
assert . Equal ( t , 0 , len ( uploader . GetArtifacts ( ) ) )
assert . Equal ( t , 0 , len ( uploader . uploadedArtifacts ) )
} )
t . Run ( "Uploading MTA project fails due to garbage YAML content" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( true , false , false )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "mta.yaml" , [ ] byte ( "garbage" ) )
2020-03-20 19:20:52 +02:00
utils . cpe [ ".pipeline/commonPipelineEnvironment/mtarFilePath" ] = "test.mtar"
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . EqualError ( t , err ,
"failed to parse contents of the project descriptor file 'mta.yaml'" )
assert . Equal ( t , 0 , len ( uploader . GetArtifacts ( ) ) )
assert . Equal ( t , 0 , len ( uploader . uploadedArtifacts ) )
} )
t . Run ( "Uploading MTA project fails due invalid version in YAML content" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( true , false , false )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "mta.yaml" , [ ] byte ( testMtaYmlNoVersion ) )
2020-03-20 19:20:52 +02:00
utils . cpe [ ".pipeline/commonPipelineEnvironment/mtarFilePath" ] = "test.mtar"
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . EqualError ( t , err ,
"the project descriptor file 'mta.yaml' has an invalid version: version must not be empty" )
assert . Equal ( t , 0 , len ( uploader . GetArtifacts ( ) ) )
assert . Equal ( t , 0 , len ( uploader . uploadedArtifacts ) )
} )
t . Run ( "Test uploading mta.yaml project fails due to missing mtar file" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( true , false , false )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "mta.yaml" , testMtaYml )
2020-03-20 19:20:52 +02:00
utils . cpe [ ".pipeline/commonPipelineEnvironment/mtarFilePath" ] = "test.mtar"
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . EqualError ( t , err , "artifact file not found 'test.mtar'" )
assert . Equal ( t , "0.3.0" , uploader . GetArtifactsVersion ( ) )
assert . Equal ( t , "artifact.id" , uploader . GetArtifactsID ( ) )
// Check the artifacts that /would/ have been uploaded
artifacts := uploader . GetArtifacts ( )
if assert . Equal ( t , 1 , len ( artifacts ) ) {
assert . Equal ( t , "mta.yaml" , artifacts [ 0 ] . File )
assert . Equal ( t , "yaml" , artifacts [ 0 ] . Type )
}
assert . Equal ( t , 0 , len ( uploader . uploadedArtifacts ) )
} )
t . Run ( "Test uploading mta.yaml project works" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( true , false , false )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "mta.yaml" , testMtaYml )
utils . AddFile ( "test.mtar" , [ ] byte ( "contentsOfMtar" ) )
2020-03-20 19:20:52 +02:00
utils . cpe [ ".pipeline/commonPipelineEnvironment/mtarFilePath" ] = "test.mtar"
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . NoError ( t , err , "expected mta.yaml project upload to work" )
assert . Equal ( t , "0.3.0" , uploader . GetArtifactsVersion ( ) )
assert . Equal ( t , "artifact.id" , uploader . GetArtifactsID ( ) )
artifacts := uploader . uploadedArtifacts
if assert . Equal ( t , 2 , len ( artifacts ) ) {
assert . Equal ( t , "mta.yaml" , artifacts [ 0 ] . File )
assert . Equal ( t , "yaml" , artifacts [ 0 ] . Type )
assert . Equal ( t , "test.mtar" , artifacts [ 1 ] . File )
assert . Equal ( t , "mtar" , artifacts [ 1 ] . Type )
}
} )
t . Run ( "Test uploading mta.yml project works" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( true , false , false )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "mta.yml" , testMtaYml )
utils . AddFile ( "test.mtar" , [ ] byte ( "contentsOfMtar" ) )
2020-03-20 19:20:52 +02:00
utils . cpe [ ".pipeline/commonPipelineEnvironment/mtarFilePath" ] = "test.mtar"
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . NoError ( t , err , "expected mta.yml project upload to work" )
assert . Equal ( t , "0.3.0" , uploader . GetArtifactsVersion ( ) )
assert . Equal ( t , "artifact.id" , uploader . GetArtifactsID ( ) )
artifacts := uploader . uploadedArtifacts
if assert . Equal ( t , 2 , len ( artifacts ) ) {
assert . Equal ( t , "mta.yml" , artifacts [ 0 ] . File )
assert . Equal ( t , "yaml" , artifacts [ 0 ] . Type )
assert . Equal ( t , "test.mtar" , artifacts [ 1 ] . File )
assert . Equal ( t , "mtar" , artifacts [ 1 ] . Type )
}
} )
}
func TestUploadArtifacts ( t * testing . T ) {
2020-06-15 09:47:33 +02:00
t . Parallel ( )
2020-03-20 19:20:52 +02:00
t . Run ( "Uploading MTA project fails without info" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-03-20 19:20:52 +02:00
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := uploadArtifacts ( utils , & uploader , & options , false )
2020-03-20 19:20:52 +02:00
assert . EqualError ( t , err , "no group ID was provided, or could be established from project files" )
} )
t . Run ( "Uploading MTA project fails without any artifacts" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-03-20 19:20:52 +02:00
uploader := mockUploader { }
options := createOptions ( )
_ = uploader . SetInfo ( options . GroupID , "some.id" , "3.0" )
2020-11-10 18:14:55 +02:00
err := uploadArtifacts ( utils , & uploader , & options , false )
2020-03-20 19:20:52 +02:00
assert . EqualError ( t , err , "no artifacts to upload" )
} )
t . Run ( "Uploading MTA project fails for unknown reasons" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-03-20 19:20:52 +02:00
// Configure mocked execRunner to fail
2020-11-10 18:14:55 +02:00
utils . ShouldFailOnCommand = map [ string ] error { }
utils . ShouldFailOnCommand [ "mvn" ] = fmt . Errorf ( "failed" )
2020-03-20 19:20:52 +02:00
uploader := mockUploader { }
options := createOptions ( )
_ = uploader . SetInfo ( options . GroupID , "some.id" , "3.0" )
_ = uploader . AddArtifact ( nexus . ArtifactDescription {
File : "mta.yaml" ,
Type : "yaml" ,
} )
_ = uploader . AddArtifact ( nexus . ArtifactDescription {
File : "artifact.mtar" ,
Type : "yaml" ,
} )
2020-11-10 18:14:55 +02:00
err := uploadArtifacts ( utils , & uploader , & options , false )
2020-04-24 10:41:49 +02:00
assert . EqualError ( t , err , "uploading artifacts for ID 'some.id' failed: failed to run executable, command: '[mvn -Durl=http:// -DgroupId=my.group.id -Dversion=3.0 -DartifactId=some.id -Dfile=mta.yaml -Dpackaging=yaml -DgeneratePom=false -Dfiles=artifact.mtar -Dclassifiers= -Dtypes=yaml -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode " + deployGoal + "]', error: failed" )
2020-03-20 19:20:52 +02:00
} )
t . Run ( "Uploading bundle generates correct maven parameters" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-03-20 19:20:52 +02:00
uploader := mockUploader { }
options := createOptions ( )
2020-04-11 12:56:44 +02:00
_ = uploader . SetRepoURL ( "localhost:8081" , "nexus3" , "maven-releases" , "npm-repo" )
2020-03-20 19:20:52 +02:00
_ = uploader . SetInfo ( options . GroupID , "my.artifact" , "4.0" )
_ = uploader . AddArtifact ( nexus . ArtifactDescription {
File : "mta.yaml" ,
Type : "yaml" ,
} )
_ = uploader . AddArtifact ( nexus . ArtifactDescription {
File : "pom.yml" ,
Type : "pom" ,
} )
2020-11-10 18:14:55 +02:00
err := uploadArtifacts ( utils , & uploader , & options , false )
2020-03-20 19:20:52 +02:00
assert . NoError ( t , err , "expected upload as two bundles to work" )
2020-11-10 18:14:55 +02:00
assert . Equal ( t , 1 , len ( utils . Calls ) )
2020-03-20 19:20:52 +02:00
expectedParameters1 := [ ] string {
"-Durl=http://localhost:8081/repository/maven-releases/" ,
"-DgroupId=my.group.id" ,
"-Dversion=4.0" ,
"-DartifactId=my.artifact" ,
"-Dfile=mta.yaml" ,
"-Dpackaging=yaml" ,
"-DgeneratePom=false" ,
"-Dfiles=pom.yml" ,
"-Dclassifiers=" ,
"-Dtypes=pom" ,
2020-04-24 10:41:49 +02:00
"-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" ,
2020-03-20 19:20:52 +02:00
"--batch-mode" ,
deployGoal }
2020-11-10 18:14:55 +02:00
assert . Equal ( t , len ( expectedParameters1 ) , len ( utils . Calls [ 0 ] . Params ) )
assert . Equal ( t , mock . ExecCall { Exec : "mvn" , Params : expectedParameters1 } , utils . Calls [ 0 ] )
2020-03-20 19:20:52 +02:00
} )
}
2020-11-11 17:32:23 +02:00
func TestRunNexusUpload ( t * testing . T ) {
t . Parallel ( )
t . Run ( "uploading without any repos fails step" , func ( t * testing . T ) {
t . Parallel ( )
utils := newMockUtilsBundle ( false , false , true )
utils . AddFile ( "package.json" , testPackageJson )
uploader := mockUploader { }
options := nexusUploadOptions {
Url : "localhost:8081" ,
}
err := runNexusUpload ( utils , & uploader , & options )
2021-03-10 16:06:42 +02:00
assert . EqualError ( t , err , "none of the parameters 'mavenRepository' and 'npmRepository' are configured, or 'format' should be set if the 'url' already contains the repository ID" )
2020-11-11 17:32:23 +02:00
} )
2020-04-11 12:56:44 +02:00
t . Run ( "Test uploading simple npm project" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , false , true )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "package.json" , testPackageJson )
2020-04-11 12:56:44 +02:00
uploader := mockUploader { }
options := createOptions ( )
2020-09-16 11:33:03 +02:00
options . Username = "admin"
2020-04-11 12:56:44 +02:00
options . Password = "admin123"
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-04-11 12:56:44 +02:00
assert . NoError ( t , err , "expected npm upload to work" )
assert . Equal ( t , "localhost:8081/repository/npm-repo/" , uploader . GetNpmRepoURL ( ) )
2020-11-10 18:14:55 +02:00
assert . Equal ( t , mock . ExecCall { Exec : "npm" , Params : [ ] string { "publish" } } , utils . Calls [ 0 ] )
assert . Equal ( t , [ ] string { "npm_config_registry=http://localhost:8081/repository/npm-repo/" , "npm_config_email=project-piper@no-reply.com" , "npm_config__auth=YWRtaW46YWRtaW4xMjM=" } , utils . Env )
2020-04-11 12:56:44 +02:00
} )
}
2020-03-20 19:20:52 +02:00
func TestUploadMavenProjects ( t * testing . T ) {
2020-06-15 09:47:33 +02:00
t . Parallel ( )
2020-03-20 19:20:52 +02:00
t . Run ( "Uploading Maven project fails due to missing pom.xml" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-03-20 19:20:52 +02:00
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . EqualError ( t , err , "pom.xml not found" )
assert . Equal ( t , 0 , len ( uploader . uploadedArtifacts ) )
} )
t . Run ( "Test uploading Maven project with POM packaging works" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-03-20 19:20:52 +02:00
utils . setProperty ( "pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "pom.xml" , "project.groupId" , "com.mycompany.app" )
utils . setProperty ( "pom.xml" , "project.artifactId" , "my-app" )
utils . setProperty ( "pom.xml" , "project.packaging" , "pom" )
utils . setProperty ( "pom.xml" , "project.build.finalName" , "my-app-1.0" )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "pom.xml" , testPomXml )
2020-03-20 19:20:52 +02:00
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . NoError ( t , err , "expected Maven upload to work" )
assert . Equal ( t , "1.0" , uploader . GetArtifactsVersion ( ) )
assert . Equal ( t , "my-app" , uploader . GetArtifactsID ( ) )
artifacts := uploader . uploadedArtifacts
if assert . Equal ( t , 1 , len ( artifacts ) ) {
assert . Equal ( t , "pom.xml" , artifacts [ 0 ] . File )
assert . Equal ( t , "pom" , artifacts [ 0 ] . Type )
}
} )
2020-04-03 21:32:38 +02:00
t . Run ( "Test uploading Maven project with JAR packaging fails without main target" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-04-03 21:32:38 +02:00
utils . setProperty ( "pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "pom.xml" , "project.groupId" , "com.mycompany.app" )
utils . setProperty ( "pom.xml" , "project.artifactId" , "my-app" )
utils . setProperty ( "pom.xml" , "project.packaging" , "jar" )
utils . setProperty ( "pom.xml" , "project.build.finalName" , "my-app-1.0" )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "pom.xml" , testPomXml )
utils . AddDir ( "target" )
2020-04-03 21:32:38 +02:00
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-04-03 21:32:38 +02:00
assert . EqualError ( t , err , "target artifact not found for packaging 'jar'" )
assert . Equal ( t , 0 , len ( uploader . uploadedArtifacts ) )
} )
2020-03-20 19:20:52 +02:00
t . Run ( "Test uploading Maven project with JAR packaging works" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-03-20 19:20:52 +02:00
utils . setProperty ( "pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "pom.xml" , "project.groupId" , "com.mycompany.app" )
utils . setProperty ( "pom.xml" , "project.artifactId" , "my-app" )
utils . setProperty ( "pom.xml" , "project.packaging" , "jar" )
utils . setProperty ( "pom.xml" , "project.build.finalName" , "my-app-1.0" )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "pom.xml" , testPomXml )
2020-06-18 17:30:17 +02:00
utils . AddFile ( filepath . Join ( "target" , "my-app-1.0.jar" ) , [ ] byte ( "contentsOfJar" ) )
2020-03-20 19:20:52 +02:00
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . NoError ( t , err , "expected Maven upload to work" )
assert . Equal ( t , "1.0" , uploader . GetArtifactsVersion ( ) )
assert . Equal ( t , "my-app" , uploader . GetArtifactsID ( ) )
artifacts := uploader . uploadedArtifacts
if assert . Equal ( t , 2 , len ( artifacts ) ) {
assert . Equal ( t , "pom.xml" , artifacts [ 0 ] . File )
assert . Equal ( t , "pom" , artifacts [ 0 ] . Type )
2020-06-18 17:30:17 +02:00
assert . Equal ( t , filepath . Join ( "target" , "my-app-1.0.jar" ) , artifacts [ 1 ] . File )
2020-03-20 19:20:52 +02:00
assert . Equal ( t , "jar" , artifacts [ 1 ] . Type )
}
} )
t . Run ( "Test uploading Maven project with fall-back to JAR packaging works" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-03-20 19:20:52 +02:00
utils . setProperty ( "pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "pom.xml" , "project.groupId" , "com.mycompany.app" )
utils . setProperty ( "pom.xml" , "project.artifactId" , "my-app" )
utils . setProperty ( "pom.xml" , "project.packaging" , "<empty>" )
utils . setProperty ( "pom.xml" , "project.build.finalName" , "my-app-1.0" )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "pom.xml" , testPomXml )
2020-06-18 17:30:17 +02:00
utils . AddFile ( filepath . Join ( "target" , "my-app-1.0.jar" ) , [ ] byte ( "contentsOfJar" ) )
2020-03-20 19:20:52 +02:00
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . NoError ( t , err , "expected Maven upload to work" )
assert . Equal ( t , "1.0" , uploader . GetArtifactsVersion ( ) )
assert . Equal ( t , "my-app" , uploader . GetArtifactsID ( ) )
artifacts := uploader . uploadedArtifacts
if assert . Equal ( t , 2 , len ( artifacts ) ) {
assert . Equal ( t , "pom.xml" , artifacts [ 0 ] . File )
assert . Equal ( t , "pom" , artifacts [ 0 ] . Type )
2020-06-18 17:30:17 +02:00
assert . Equal ( t , filepath . Join ( "target" , "my-app-1.0.jar" ) , artifacts [ 1 ] . File )
2020-03-20 19:20:52 +02:00
assert . Equal ( t , "jar" , artifacts [ 1 ] . Type )
}
} )
t . Run ( "Test uploading Maven project with fall-back to group id from parameters works" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-03-20 19:20:52 +02:00
utils . setProperty ( "pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "pom.xml" , "project.artifactId" , "my-app" )
utils . setProperty ( "pom.xml" , "project.packaging" , "pom" )
utils . setProperty ( "pom.xml" , "project.build.finalName" , "my-app-1.0" )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "pom.xml" , testPomXml )
2020-03-20 19:20:52 +02:00
uploader := mockUploader { }
options := createOptions ( )
options . GroupID = "awesome.group"
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . NoError ( t , err , "expected Maven upload to work" )
assert . Equal ( t , "localhost:8081/repository/maven-releases/" ,
2020-04-11 12:56:44 +02:00
uploader . GetMavenRepoURL ( ) )
2020-03-20 19:20:52 +02:00
assert . Equal ( t , "1.0" , uploader . GetArtifactsVersion ( ) )
assert . Equal ( t , "my-app" , uploader . GetArtifactsID ( ) )
artifacts := uploader . uploadedArtifacts
if assert . Equal ( t , 1 , len ( artifacts ) ) {
assert . Equal ( t , "pom.xml" , artifacts [ 0 ] . File )
assert . Equal ( t , "pom" , artifacts [ 0 ] . Type )
}
} )
2020-04-03 21:32:38 +02:00
t . Run ( "Test uploading Maven project with fall-back for finalBuildName works" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-04-03 21:32:38 +02:00
utils . setProperty ( "pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "pom.xml" , "project.groupId" , "awesome.group" )
utils . setProperty ( "pom.xml" , "project.artifactId" , "my-app" )
utils . setProperty ( "pom.xml" , "project.packaging" , "jar" )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "pom.xml" , testPomXml )
2020-06-18 17:30:17 +02:00
utils . AddFile ( filepath . Join ( "target" , "my-app-1.0.jar" ) , [ ] byte ( "contentsOfJar" ) )
2020-04-03 21:32:38 +02:00
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-04-03 21:32:38 +02:00
assert . NoError ( t , err , "expected Maven upload to work" )
assert . Equal ( t , "localhost:8081/repository/maven-releases/" ,
2020-04-11 12:56:44 +02:00
uploader . GetMavenRepoURL ( ) )
2020-04-03 21:32:38 +02:00
assert . Equal ( t , "1.0" , uploader . GetArtifactsVersion ( ) )
assert . Equal ( t , "my-app" , uploader . GetArtifactsID ( ) )
artifacts := uploader . uploadedArtifacts
if assert . Equal ( t , 2 , len ( artifacts ) ) {
assert . Equal ( t , "pom.xml" , artifacts [ 0 ] . File )
assert . Equal ( t , "pom" , artifacts [ 0 ] . Type )
2020-06-18 17:30:17 +02:00
assert . Equal ( t , filepath . Join ( "target" , "my-app-1.0.jar" ) , artifacts [ 1 ] . File )
2020-04-03 21:32:38 +02:00
assert . Equal ( t , "jar" , artifacts [ 1 ] . Type )
}
} )
2020-03-20 19:20:52 +02:00
t . Run ( "Test uploading Maven project with application module and finalName works" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-03-20 19:20:52 +02:00
utils . setProperty ( "pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "pom.xml" , "project.groupId" , "com.mycompany.app" )
utils . setProperty ( "pom.xml" , "project.artifactId" , "my-app" )
utils . setProperty ( "pom.xml" , "project.packaging" , "pom" )
utils . setProperty ( "pom.xml" , "project.build.finalName" , "my-app-1.0" )
utils . setProperty ( "application/pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "application/pom.xml" , "project.groupId" , "com.mycompany.app" )
utils . setProperty ( "application/pom.xml" , "project.artifactId" , "my-app-app" )
2020-04-03 21:32:38 +02:00
utils . setProperty ( "application/pom.xml" , "project.packaging" , "war" )
2020-03-20 19:20:52 +02:00
utils . setProperty ( "application/pom.xml" , "project.build.finalName" , "final-artifact" )
2020-04-03 21:32:38 +02:00
utils . setProperty ( "integration-tests/pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "integration-tests/pom.xml" , "project.groupId" , "com.mycompany.app" )
utils . setProperty ( "integration-tests/pom.xml" , "project.artifactId" , "my-app-app-integration-tests" )
utils . setProperty ( "integration-tests/pom.xml" , "project.packaging" , "jar" )
utils . setProperty ( "integration-tests/pom.xml" , "project.build.finalName" , "final-artifact" )
utils . setProperty ( "unit-tests/pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "unit-tests/pom.xml" , "project.groupId" , "com.mycompany.app" )
utils . setProperty ( "unit-tests/pom.xml" , "project.artifactId" , "my-app-app-unit-tests" )
utils . setProperty ( "unit-tests/pom.xml" , "project.packaging" , "jar" )
utils . setProperty ( "unit-tests/pom.xml" , "project.build.finalName" , "final-artifact" )
utils . setProperty ( "performance-tests/pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "performance-tests/pom.xml" , "project.groupId" , "com.mycompany.app" )
utils . setProperty ( "performance-tests/pom.xml" , "project.artifactId" , "my-app-app" )
utils . setProperty ( "performance-tests/pom.xml" , "project.packaging" , "" )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "pom.xml" , testPomXml )
2020-06-18 17:30:17 +02:00
utils . AddFile ( filepath . Join ( "application" , "pom.xml" ) , testPomXml )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "application/target/final-artifact.war" , [ ] byte ( "contentsOfJar" ) )
utils . AddFile ( "application/target/final-artifact-classes.jar" , [ ] byte ( "contentsOfClassesJar" ) )
utils . AddFile ( "integration-tests/pom.xml" , testPomXml )
utils . AddFile ( "integration-tests/target/final-artifact-integration-tests.jar" , [ ] byte ( "contentsOfJar" ) )
utils . AddFile ( "unit-tests/pom.xml" , testPomXml )
utils . AddFile ( "unit-tests/target/final-artifact-unit-tests.jar" , [ ] byte ( "contentsOfJar" ) )
utils . AddFile ( "performance-tests/pom.xml" , testPomXml )
2020-03-20 19:20:52 +02:00
uploader := mockUploader { }
options := createOptions ( )
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . NoError ( t , err , "expected upload of maven project with application module to succeed" )
assert . Equal ( t , "1.0" , uploader . GetArtifactsVersion ( ) )
2020-06-15 09:47:33 +02:00
assert . Equal ( t , "my-app" , uploader . GetArtifactsID ( ) )
2020-03-20 19:20:52 +02:00
artifacts := uploader . uploadedArtifacts
if assert . Equal ( t , 4 , len ( artifacts ) ) {
2020-06-18 17:30:17 +02:00
assert . Equal ( t , filepath . Join ( "application" , "pom.xml" ) , artifacts [ 0 ] . File )
2020-03-20 19:20:52 +02:00
assert . Equal ( t , "pom" , artifacts [ 0 ] . Type )
2020-06-18 17:30:17 +02:00
assert . Equal ( t , filepath . Join ( "application" , "target" , "final-artifact.war" ) , artifacts [ 1 ] . File )
2020-06-15 09:47:33 +02:00
assert . Equal ( t , "war" , artifacts [ 1 ] . Type )
2020-03-20 19:20:52 +02:00
2020-06-18 17:30:17 +02:00
assert . Equal ( t , filepath . Join ( "application" , "target" , "final-artifact-classes.jar" ) , artifacts [ 2 ] . File )
2020-06-15 09:47:33 +02:00
assert . Equal ( t , "jar" , artifacts [ 2 ] . Type )
assert . Equal ( t , "pom.xml" , artifacts [ 3 ] . File )
assert . Equal ( t , "pom" , artifacts [ 3 ] . Type )
2020-03-20 19:20:52 +02:00
}
2020-11-10 18:14:55 +02:00
if assert . Equal ( t , 2 , len ( utils . Calls ) ) {
2020-03-20 19:20:52 +02:00
expectedParameters1 := [ ] string {
"-Durl=http://localhost:8081/repository/maven-releases/" ,
"-DgroupId=com.mycompany.app" ,
"-Dversion=1.0" ,
2020-06-15 09:47:33 +02:00
"-DartifactId=my-app-app" ,
2020-06-18 17:30:17 +02:00
"-Dfile=" + filepath . Join ( "application" , "pom.xml" ) ,
2020-03-20 19:20:52 +02:00
"-Dpackaging=pom" ,
2020-06-18 17:30:17 +02:00
"-Dfiles=" + filepath . Join ( "application" , "target" , "final-artifact.war" ) + "," + filepath . Join ( "application" , "target" , "final-artifact-classes.jar" ) ,
2020-06-15 09:47:33 +02:00
"-Dclassifiers=,classes" ,
"-Dtypes=war,jar" ,
2020-04-24 10:41:49 +02:00
"-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" ,
2020-03-20 19:20:52 +02:00
"--batch-mode" ,
deployGoal }
2020-11-10 18:14:55 +02:00
assert . Equal ( t , len ( expectedParameters1 ) , len ( utils . Calls [ 0 ] . Params ) )
assert . Equal ( t , mock . ExecCall { Exec : "mvn" , Params : expectedParameters1 } , utils . Calls [ 0 ] )
2020-03-20 19:20:52 +02:00
expectedParameters2 := [ ] string {
"-Durl=http://localhost:8081/repository/maven-releases/" ,
"-DgroupId=com.mycompany.app" ,
"-Dversion=1.0" ,
2020-06-15 09:47:33 +02:00
"-DartifactId=my-app" ,
"-Dfile=pom.xml" ,
2020-03-20 19:20:52 +02:00
"-Dpackaging=pom" ,
2020-04-24 10:41:49 +02:00
"-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" ,
2020-03-20 19:20:52 +02:00
"--batch-mode" ,
deployGoal }
2020-11-10 18:14:55 +02:00
assert . Equal ( t , len ( expectedParameters2 ) , len ( utils . Calls [ 1 ] . Params ) )
assert . Equal ( t , mock . ExecCall { Exec : "mvn" , Params : expectedParameters2 } , utils . Calls [ 1 ] )
2020-03-20 19:20:52 +02:00
}
} )
t . Run ( "Write credentials settings" , func ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-03-20 19:20:52 +02:00
utils . setProperty ( "pom.xml" , "project.version" , "1.0" )
utils . setProperty ( "pom.xml" , "project.groupId" , "com.mycompany.app" )
utils . setProperty ( "pom.xml" , "project.artifactId" , "my-app" )
utils . setProperty ( "pom.xml" , "project.packaging" , "pom" )
utils . setProperty ( "pom.xml" , "project.build.finalName" , "my-app-1.0" )
2020-06-15 09:47:33 +02:00
utils . AddFile ( "pom.xml" , testPomXml )
2020-03-20 19:20:52 +02:00
uploader := mockUploader { }
options := createOptions ( )
2020-09-16 11:33:03 +02:00
options . Username = "admin"
2020-03-20 19:20:52 +02:00
options . Password = "admin123"
2020-11-10 18:14:55 +02:00
err := runNexusUpload ( utils , & uploader , & options )
2020-03-20 19:20:52 +02:00
assert . NoError ( t , err , "expected Maven upload to work" )
2020-11-10 18:14:55 +02:00
assert . Equal ( t , 1 , len ( utils . Calls ) )
2020-03-20 19:20:52 +02:00
expectedParameters1 := [ ] string {
"--settings" ,
settingsPath ,
"-Durl=http://localhost:8081/repository/maven-releases/" ,
"-DgroupId=com.mycompany.app" ,
"-Dversion=1.0" ,
"-DartifactId=my-app" ,
"-DrepositoryId=" + settingsServerID ,
"-Dfile=pom.xml" ,
"-Dpackaging=pom" ,
2020-04-24 10:41:49 +02:00
"-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" ,
2020-03-20 19:20:52 +02:00
"--batch-mode" ,
deployGoal }
2020-11-10 18:14:55 +02:00
assert . Equal ( t , len ( expectedParameters1 ) , len ( utils . Calls [ 0 ] . Params ) )
assert . Equal ( t , mock . ExecCall { Exec : "mvn" , Params : expectedParameters1 } , utils . Calls [ 0 ] )
2020-03-20 19:20:52 +02:00
expectedEnv := [ ] string { "NEXUS_username=admin" , "NEXUS_password=admin123" }
2020-11-10 18:14:55 +02:00
assert . Equal ( t , 2 , len ( utils . Env ) )
assert . Equal ( t , expectedEnv , utils . Env )
2020-03-20 19:20:52 +02:00
2020-06-15 09:47:33 +02:00
assert . False ( t , utils . HasFile ( settingsPath ) )
assert . True ( t , utils . HasRemovedFile ( settingsPath ) )
2020-03-20 19:20:52 +02:00
} )
}
func TestSetupNexusCredentialsSettingsFile ( t * testing . T ) {
2020-11-11 17:32:23 +02:00
t . Parallel ( )
2020-04-11 12:56:44 +02:00
utils := newMockUtilsBundle ( false , true , false )
2020-09-16 11:33:03 +02:00
options := nexusUploadOptions { Username : "admin" , Password : "admin123" }
2020-03-20 19:20:52 +02:00
mavenOptions := maven . ExecuteOptions { }
2020-11-10 18:14:55 +02:00
settingsPath , err := setupNexusCredentialsSettingsFile ( utils , & options , & mavenOptions )
2020-03-20 19:20:52 +02:00
assert . NoError ( t , err , "expected setting up credentials settings.xml to work" )
2020-11-10 18:14:55 +02:00
assert . Equal ( t , 0 , len ( utils . Calls ) )
2020-03-20 19:20:52 +02:00
expectedEnv := [ ] string { "NEXUS_username=admin" , "NEXUS_password=admin123" }
2020-11-10 18:14:55 +02:00
assert . Equal ( t , 2 , len ( utils . Env ) )
assert . Equal ( t , expectedEnv , utils . Env )
2020-03-20 19:20:52 +02:00
assert . True ( t , settingsPath != "" )
2020-06-15 09:47:33 +02:00
assert . True ( t , utils . HasFile ( settingsPath ) )
2020-03-20 19:20:52 +02:00
}