2023-05-03 18:02:11 +02:00
//go:build unit
// +build unit
2022-05-23 15:15:22 +02:00
package cmd
import (
2023-11-28 14:26:31 +02:00
"encoding/json"
2022-05-23 15:15:22 +02:00
"os"
"testing"
2023-11-28 14:26:31 +02:00
"time"
2022-05-23 15:15:22 +02:00
"github.com/SAP/jenkins-library/pkg/abaputils"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
)
2023-11-28 14:26:31 +02:00
var executionLogStringCreateTag string
var logResultSuccess string
func init ( ) {
logResultSuccess = ` { "d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Success", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } } `
executionLog := abaputils . LogProtocolResults {
Results : [ ] abaputils . LogProtocol {
{
ProtocolLine : 1 ,
OverviewIndex : 1 ,
Type : "LogEntry" ,
Description : "S" ,
Timestamp : "/Date(1644332299000+0000)/" ,
} ,
} ,
Count : "1" ,
}
executionLogResponse , _ := json . Marshal ( executionLog )
executionLogStringCreateTag = string ( executionLogResponse )
}
2022-05-23 15:15:22 +02:00
func TestRunAbapEnvironmentCreateTag ( t * testing . T ) {
t . Run ( "happy path" , func ( t * testing . T ) {
var autils = & abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
2022-07-12 15:19:12 +02:00
dir := t . TempDir ( )
2022-05-23 15:15:22 +02:00
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
// clean up tmp dir
defer func ( ) {
_ = os . Chdir ( oldCWD )
} ( )
body := ` -- -
addonVersion : "1.2.3"
addonProduct : "/DMO/PRODUCT"
repositories :
- name : / DMO / SWC
branch : main
commitID : 1234 abcd
version : "4.5.6"
`
file , _ := os . Create ( "repo.yml" )
2022-07-06 14:29:04 +02:00
_ , err := file . Write ( [ ] byte ( body ) )
assert . NoError ( t , err )
2022-05-23 15:15:22 +02:00
config := & abapEnvironmentCreateTagOptions {
Username : "dummy" ,
Password : "dummy" ,
Host : "https://test.com" ,
Repositories : "repo.yml" ,
TagName : "tag" ,
TagDescription : "desc" ,
GenerateTagForAddonProductVersion : true ,
GenerateTagForAddonComponentVersion : true ,
}
client := & abaputils . ClientMock {
BodyList : [ ] string {
2023-12-19 16:16:48 +02:00
` { "d" : ` + executionLogStringCreateTag + ` } ` ,
2023-11-28 14:26:31 +02:00
logResultSuccess ,
2022-05-23 15:15:22 +02:00
` { "d" : { "Status" : "S" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
2023-12-19 16:16:48 +02:00
` { "d" : ` + executionLogStringCreateTag + ` } ` ,
2023-11-28 14:26:31 +02:00
logResultSuccess ,
2022-05-23 15:15:22 +02:00
` { "d" : { "Status" : "S" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
2023-12-19 16:16:48 +02:00
` { "d" : ` + executionLogStringCreateTag + ` } ` ,
2023-11-28 14:26:31 +02:00
logResultSuccess ,
2022-05-23 15:15:22 +02:00
` { "d" : { "Status" : "S" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
` { "d" : { "empty" : "body" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
_ , hook := test . NewNullLogger ( )
log . RegisterHook ( hook )
2023-12-19 16:16:48 +02:00
apiManager := & abaputils . SoftwareComponentApiManager { Client : client , PollIntervall : 1 * time . Nanosecond , Force0510 : true }
2023-11-28 14:26:31 +02:00
err = runAbapEnvironmentCreateTag ( config , autils , apiManager )
2022-05-23 15:15:22 +02:00
assert . NoError ( t , err , "Did not expect error" )
2023-12-19 16:16:48 +02:00
assert . Equal ( t , 25 , len ( hook . Entries ) , "Expected a different number of entries" )
assert . Equal ( t , ` Created tag v4.5.6 for repository /DMO/SWC with commitID 1234abcd ` , hook . AllEntries ( ) [ 12 ] . Message , "Expected a different message" )
assert . Equal ( t , ` Created tag -DMO-PRODUCT-1.2.3 for repository /DMO/SWC with commitID 1234abcd ` , hook . AllEntries ( ) [ 18 ] . Message , "Expected a different message" )
assert . Equal ( t , ` Created tag tag for repository /DMO/SWC with commitID 1234abcd ` , hook . AllEntries ( ) [ 24 ] . Message , "Expected a different message" )
2022-05-23 15:15:22 +02:00
hook . Reset ( )
} )
t . Run ( "backend error" , func ( t * testing . T ) {
var autils = & abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
2022-07-12 15:19:12 +02:00
dir := t . TempDir ( )
2022-05-23 15:15:22 +02:00
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
// clean up tmp dir
defer func ( ) {
_ = os . Chdir ( oldCWD )
} ( )
body := ` -- -
addonVersion : "1.2.3"
addonProduct : "/DMO/PRODUCT"
repositories :
- name : / DMO / SWC
branch : main
commitID : 1234 abcd
version : "4.5.6"
`
file , _ := os . Create ( "repo.yml" )
2022-07-06 14:29:04 +02:00
_ , err := file . Write ( [ ] byte ( body ) )
assert . NoError ( t , err )
2022-05-23 15:15:22 +02:00
config := & abapEnvironmentCreateTagOptions {
Username : "dummy" ,
Password : "dummy" ,
Host : "https://test.com" ,
Repositories : "repo.yml" ,
TagName : "tag" ,
TagDescription : "desc" ,
GenerateTagForAddonProductVersion : true ,
GenerateTagForAddonComponentVersion : true ,
}
client := & abaputils . ClientMock {
BodyList : [ ] string {
2023-12-19 16:16:48 +02:00
` { "d" : ` + executionLogStringCreateTag + ` } ` ,
2023-11-28 14:26:31 +02:00
logResultSuccess ,
2022-05-23 15:15:22 +02:00
` { "d" : { "Status" : "E" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
2023-11-28 14:26:31 +02:00
` { "d" : { "empty" : "body" } } ` ,
2023-12-19 16:16:48 +02:00
` { "d" : ` + executionLogStringCreateTag + ` } ` ,
2023-11-28 14:26:31 +02:00
logResultSuccess ,
2022-05-23 15:15:22 +02:00
` { "d" : { "Status" : "E" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
2023-11-28 14:26:31 +02:00
` { "d" : { "empty" : "body" } } ` ,
2023-12-19 16:16:48 +02:00
` { "d" : ` + executionLogStringCreateTag + ` } ` ,
2023-11-28 14:26:31 +02:00
logResultSuccess ,
2022-05-23 15:15:22 +02:00
` { "d" : { "Status" : "E" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
` { "d" : { "empty" : "body" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
_ , hook := test . NewNullLogger ( )
log . RegisterHook ( hook )
2023-12-19 16:16:48 +02:00
apiManager := & abaputils . SoftwareComponentApiManager { Client : client , PollIntervall : 1 * time . Nanosecond , Force0510 : true }
2023-11-28 14:26:31 +02:00
err = runAbapEnvironmentCreateTag ( config , autils , apiManager )
2022-05-23 15:15:22 +02:00
assert . Error ( t , err , "Did expect error" )
2023-12-19 16:16:48 +02:00
assert . Equal ( t , 40 , len ( hook . Entries ) , "Expected a different number of entries" )
assert . Equal ( t , ` NOT created: Tag v4.5.6 for repository /DMO/SWC with commitID 1234abcd ` , hook . AllEntries ( ) [ 12 ] . Message , "Expected a different message" )
assert . Equal ( t , ` NOT created: Tag -DMO-PRODUCT-1.2.3 for repository /DMO/SWC with commitID 1234abcd ` , hook . AllEntries ( ) [ 25 ] . Message , "Expected a different message" )
assert . Equal ( t , ` NOT created: Tag tag for repository /DMO/SWC with commitID 1234abcd ` , hook . AllEntries ( ) [ 38 ] . Message , "Expected a different message" )
assert . Equal ( t , ` At least one tag has not been created ` , hook . AllEntries ( ) [ 39 ] . Message , "Expected a different message" )
2022-05-23 15:15:22 +02:00
hook . Reset ( )
} )
}
func TestRunAbapEnvironmentCreateTagConfigurations ( t * testing . T ) {
t . Run ( "no repo.yml" , func ( t * testing . T ) {
var autils = & abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
config := & abapEnvironmentCreateTagOptions {
Username : "dummy" ,
Password : "dummy" ,
Host : "https://test.com" ,
RepositoryName : "/DMO/SWC" ,
CommitID : "1234abcd" ,
TagName : "tag" ,
TagDescription : "desc" ,
GenerateTagForAddonProductVersion : true ,
GenerateTagForAddonComponentVersion : true ,
}
client := & abaputils . ClientMock {
BodyList : [ ] string {
2023-12-19 16:16:48 +02:00
` { "d" : ` + executionLogStringCreateTag + ` } ` ,
2023-11-28 14:26:31 +02:00
logResultSuccess ,
2022-05-23 15:15:22 +02:00
` { "d" : { "Status" : "S" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
` { "d" : { "empty" : "body" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
_ , hook := test . NewNullLogger ( )
log . RegisterHook ( hook )
2023-12-19 16:16:48 +02:00
apiManager := & abaputils . SoftwareComponentApiManager { Client : client , PollIntervall : 1 * time . Nanosecond , Force0510 : true }
2023-11-28 14:26:31 +02:00
err := runAbapEnvironmentCreateTag ( config , autils , apiManager )
2022-05-23 15:15:22 +02:00
assert . NoError ( t , err , "Did not expect error" )
2023-12-19 16:16:48 +02:00
assert . Equal ( t , 13 , len ( hook . Entries ) , "Expected a different number of entries" )
assert . Equal ( t , ` Created tag tag for repository /DMO/SWC with commitID 1234abcd ` , hook . AllEntries ( ) [ 12 ] . Message , "Expected a different message" )
2022-05-23 15:15:22 +02:00
hook . Reset ( )
} )
t . Run ( "backend error" , func ( t * testing . T ) {
var autils = & abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
2022-07-12 15:19:12 +02:00
dir := t . TempDir ( )
2022-05-23 15:15:22 +02:00
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
// clean up tmp dir
defer func ( ) {
_ = os . Chdir ( oldCWD )
} ( )
body := ` -- -
addonVersion : "1.2.3"
addonProduct : "/DMO/PRODUCT"
repositories :
- name : / DMO / SWC
branch : main
commitID : 1234 abcd
version : "4.5.6"
`
file , _ := os . Create ( "repo.yml" )
2022-07-06 14:29:04 +02:00
_ , err := file . Write ( [ ] byte ( body ) )
assert . NoError ( t , err )
2022-05-23 15:15:22 +02:00
config := & abapEnvironmentCreateTagOptions {
Username : "dummy" ,
Password : "dummy" ,
Host : "https://test.com" ,
Repositories : "repo.yml" ,
RepositoryName : "/DMO/SWC2" ,
CommitID : "1234abcde" ,
TagName : "tag" ,
TagDescription : "desc" ,
GenerateTagForAddonProductVersion : true ,
GenerateTagForAddonComponentVersion : true ,
}
client := & abaputils . ClientMock {
BodyList : [ ] string {
` { "d" : { "Status" : "S" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
` { "d" : { "Status" : "S" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
` { "d" : { "Status" : "S" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
` { "d" : { "Status" : "S" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
` { "d" : { "Status" : "S" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
` { "d" : { "empty" : "body" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
2023-12-19 16:16:48 +02:00
apiManager := & abaputils . SoftwareComponentApiManager { Client : client , PollIntervall : 1 * time . Nanosecond , Force0510 : true }
2023-11-28 14:26:31 +02:00
err = runAbapEnvironmentCreateTag ( config , autils , apiManager )
2022-05-23 15:15:22 +02:00
assert . Error ( t , err , "Did expect error" )
assert . Equal ( t , "Something failed during the tag creation: Configuring the parameter repositories and the parameter repositoryName at the same time is not allowed" , err . Error ( ) , "Expected different error message" )
} )
t . Run ( "flags false" , func ( t * testing . T ) {
var autils = & abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
2022-07-12 15:19:12 +02:00
dir := t . TempDir ( )
2022-05-23 15:15:22 +02:00
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
// clean up tmp dir
defer func ( ) {
_ = os . Chdir ( oldCWD )
_ = os . RemoveAll ( dir )
} ( )
body := ` -- -
addonVersion : "1.2.3"
addonProduct : "/DMO/PRODUCT"
repositories :
- name : / DMO / SWC
branch : main
commitID : 1234 abcd
version : "4.5.6"
`
file , _ := os . Create ( "repo.yml" )
2022-07-06 14:29:04 +02:00
_ , err := file . Write ( [ ] byte ( body ) )
assert . NoError ( t , err )
2022-05-23 15:15:22 +02:00
config := & abapEnvironmentCreateTagOptions {
Username : "dummy" ,
Password : "dummy" ,
Host : "https://test.com" ,
Repositories : "repo.yml" ,
TagName : "tag" ,
TagDescription : "desc" ,
GenerateTagForAddonProductVersion : false ,
GenerateTagForAddonComponentVersion : false ,
}
client := & abaputils . ClientMock {
BodyList : [ ] string {
` { "d" : { "Status" : "S" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
` { "d" : { "Status" : "S" } } ` ,
` { "d" : { "uuid" : "abc" } } ` ,
` { "d" : { "empty" : "body" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
_ , hook := test . NewNullLogger ( )
log . RegisterHook ( hook )
2023-12-19 16:16:48 +02:00
apiManager := & abaputils . SoftwareComponentApiManager { Client : client , PollIntervall : 1 * time . Nanosecond , Force0510 : true }
2023-11-28 14:26:31 +02:00
err = runAbapEnvironmentCreateTag ( config , autils , apiManager )
2022-05-23 15:15:22 +02:00
assert . NoError ( t , err , "Did not expect error" )
2023-12-19 16:16:48 +02:00
assert . Equal ( t , 6 , len ( hook . Entries ) , "Expected a different number of entries" )
assert . Equal ( t , ` Created tag tag for repository /DMO/SWC with commitID 1234abcd ` , hook . AllEntries ( ) [ 5 ] . Message , "Expected a different message" )
2022-05-23 15:15:22 +02:00
hook . Reset ( )
} )
}