2020-09-23 13:55:17 +02:00
package generator
2019-11-13 15:43:53 +02:00
import (
"fmt"
"testing"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/stretchr/testify/assert"
)
2020-07-16 09:10:15 +02:00
func TestCreateParameterOverview ( t * testing . T ) {
2021-10-11 15:22:24 +02:00
2020-07-16 09:10:15 +02:00
stepData := config . StepData {
Spec : config . StepSpec {
Inputs : config . StepInputs {
Resources : [ ] config . StepResources {
{ Name : "testStash" , Type : "stash" } ,
2019-11-14 14:59:31 +02:00
} ,
2020-07-16 09:10:15 +02:00
Parameters : [ ] config . StepParameters {
{ Name : "param1" } ,
2021-10-12 12:49:38 +02:00
{ Name : "param2" , Mandatory : true } ,
{ Name : "param3" , MandatoryIf : [ ] config . ParameterDependence { { Name : "param1" , Value : "param3Necessary" } } } ,
2021-10-11 15:22:24 +02:00
{ Name : "dockerImage" , Default : "testImage" } ,
2020-07-16 09:10:15 +02:00
{ Name : "stashContent" , Default : "testStash" } ,
2019-11-14 14:59:31 +02:00
} ,
} ,
2020-07-16 09:10:15 +02:00
} ,
}
2021-10-12 12:49:38 +02:00
stepParameterNames = [ ] string { "param1" , "param2" , "param3" }
2021-10-11 15:22:24 +02:00
t . Run ( "Test Step Section" , func ( t * testing . T ) {
2020-07-16 09:10:15 +02:00
2021-10-11 15:22:24 +02:00
expected := ` | Name | Mandatory | Additional information |
2020-07-16 09:10:15 +02:00
| -- -- | -- -- -- -- - | -- -- -- -- -- -- -- -- -- -- -- |
| [ param1 ] ( # param1 ) | no | |
2021-10-12 12:49:38 +02:00
| [ param2 ] ( # param2 ) | * * yes * * | |
| [ param3 ] ( # param3 ) | * * ( yes ) * * | mandatory in case of : < br / > - ` + "[ ` param1 ` ](#param1)= ` param3Necessary ` " + ` |
2021-10-11 15:22:24 +02:00
`
assert . Equal ( t , expected , createParameterOverview ( & stepData , false ) )
} )
t . Run ( "Test Execution Section" , func ( t * testing . T ) {
expected := ` | Name | Mandatory | Additional information |
| -- -- | -- -- -- -- - | -- -- -- -- -- -- -- -- -- -- -- |
| [ dockerImage ] ( # dockerimage ) | no | |
2020-07-16 11:34:02 +02:00
| [ stashContent ] ( # stashcontent ) | no | [ ! [ Jenkins only ] ( https : //img.shields.io/badge/-Jenkins%20only-yellowgreen)](#) |
2020-07-16 09:10:15 +02:00
`
2021-10-11 15:22:24 +02:00
assert . Equal ( t , expected , createParameterOverview ( & stepData , true ) )
} )
2020-07-16 09:10:15 +02:00
stepParameterNames = [ ] string { }
}
func TestParameterFurtherInfo ( t * testing . T ) {
tt := [ ] struct {
paramName string
stepParams [ ] string
stepData * config . StepData
contains string
notContains [ ] string
} {
{ paramName : "verbose" , stepParams : [ ] string { } , stepData : nil , contains : "activates debug output" } ,
{ paramName : "script" , stepParams : [ ] string { } , stepData : nil , contains : "reference to Jenkins main pipeline script" } ,
{ paramName : "noop" , stepParams : [ ] string { "noop" } , stepData : & config . StepData { Spec : config . StepSpec { Inputs : config . StepInputs { Parameters : [ ] config . StepParameters { } } } } , contains : "" } ,
{
paramName : "testCredentialId" ,
stepData : & config . StepData {
Spec : config . StepSpec {
Inputs : config . StepInputs {
Secrets : [ ] config . StepSecrets { { Name : "testCredentialId" , Type : "jenkins" } } ,
} ,
2019-11-14 14:59:31 +02:00
} ,
} ,
2020-07-16 09:10:15 +02:00
contains : "id of credentials" ,
} ,
{
paramName : "testSecret1" ,
stepParams : [ ] string { "testSecret1" } ,
stepData : & config . StepData {
Spec : config . StepSpec {
Inputs : config . StepInputs {
Parameters : [ ] config . StepParameters {
{ Name : "testSecret1" , Secret : true , ResourceRef : [ ] config . ResourceReference { { Name : "mytestSecret" , Type : "secret" } } } ,
} ,
} ,
2019-11-18 12:14:45 +02:00
} ,
2019-11-14 14:59:31 +02:00
} ,
2020-07-22 10:33:36 +02:00
contains : "credentials ([`mytestSecret`](#mytestsecret))" ,
2019-11-14 14:59:31 +02:00
} ,
2020-07-16 09:10:15 +02:00
{
paramName : "testSecret2" ,
stepParams : [ ] string { "testSecret2" } ,
stepData : & config . StepData {
Spec : config . StepSpec {
Inputs : config . StepInputs {
Parameters : [ ] config . StepParameters {
{ Name : "testSecret2" } ,
} ,
} ,
2019-11-18 12:14:45 +02:00
} ,
2019-11-14 14:59:31 +02:00
} ,
2020-07-16 09:10:15 +02:00
contains : "" ,
2019-11-14 14:59:31 +02:00
} ,
2020-07-16 09:10:15 +02:00
}
2019-11-13 15:43:53 +02:00
2020-07-16 09:10:15 +02:00
for _ , test := range tt {
stepParameterNames = test . stepParams
2021-10-11 15:22:24 +02:00
res , err := parameterFurtherInfo ( test . paramName , test . stepData , false )
assert . NoError ( t , err )
2020-07-16 09:10:15 +02:00
stepParameterNames = [ ] string { }
if len ( test . contains ) == 0 {
assert . Equalf ( t , test . contains , res , fmt . Sprintf ( "param %v" , test . paramName ) )
} else {
assert . Containsf ( t , res , test . contains , fmt . Sprintf ( "param %v" , test . paramName ) )
}
for _ , notThere := range test . notContains {
assert . NotContainsf ( t , res , notThere , fmt . Sprintf ( "param %v" , test . paramName ) )
}
2019-11-13 15:43:53 +02:00
}
}
2021-10-11 15:22:24 +02:00
func TestCheckParameterInfo ( t * testing . T ) {
t . Parallel ( )
tt := [ ] struct {
info string
stepParam bool
executionEnvironment bool
expected string
expectedErr error
} {
{ info : "step param" , stepParam : true , executionEnvironment : false , expected : "step param" , expectedErr : nil } ,
{ info : "execution param" , stepParam : false , executionEnvironment : true , expected : "execution param" , expectedErr : nil } ,
{ info : "step param in execution" , stepParam : true , executionEnvironment : true , expected : "" , expectedErr : fmt . Errorf ( "step parameter not relevant as execution environment parameter" ) } ,
{ info : "execution param in step" , stepParam : false , executionEnvironment : false , expected : "" , expectedErr : fmt . Errorf ( "execution environment parameter not relevant as step parameter" ) } ,
}
for _ , test := range tt {
result , err := checkParameterInfo ( test . info , test . stepParam , test . executionEnvironment )
if test . expectedErr == nil {
assert . NoError ( t , err )
} else {
assert . EqualError ( t , err , fmt . Sprint ( test . expectedErr ) )
}
assert . Equal ( t , test . expected , result )
}
}
2020-07-16 09:10:15 +02:00
func TestCreateParameterDetails ( t * testing . T ) {
2021-10-12 12:49:38 +02:00
t . Run ( "default" , func ( t * testing . T ) {
stepData := config . StepData {
Spec : config . StepSpec {
Inputs : config . StepInputs {
Parameters : [ ] config . StepParameters {
{
Name : "param1" ,
Aliases : [ ] config . Alias { { Name : "param1Alias" } , { Name : "paramAliasDeprecated" , Deprecated : true } } ,
Mandatory : true ,
Default : "param1Default" ,
LongDescription : "long description" ,
PossibleValues : [ ] interface { } { "val1" , "val2" } ,
Scope : [ ] string { "STEPS" } ,
Secret : true ,
Type : "string" ,
} ,
2020-07-16 09:10:15 +02:00
} ,
} ,
} ,
2021-10-12 12:49:38 +02:00
}
res := createParameterDetails ( & stepData )
assert . Contains ( t , res , "#### param1" )
assert . Contains ( t , res , "long description" )
assert . Contains ( t , res , "`param1Alias`" )
assert . Contains ( t , res , "`paramAliasDeprecated` (**deprecated**)" )
assert . Contains ( t , res , "string" )
assert . Contains ( t , res , "param1Default" )
assert . Contains ( t , res , "val1" )
assert . Contains ( t , res , "val2" )
assert . Contains ( t , res , "no" )
assert . Contains ( t , res , "**yes**" )
assert . Contains ( t , res , "steps" )
} )
t . Run ( "conditional mandatory parameters" , func ( t * testing . T ) {
stepData := config . StepData {
Spec : config . StepSpec {
Inputs : config . StepInputs {
Parameters : [ ] config . StepParameters {
{
Name : "param2" ,
MandatoryIf : [ ] config . ParameterDependence { { Name : "param1" , Value : "param1Val" } } ,
Type : "string" ,
} ,
} ,
} ,
} ,
}
res := createParameterDetails ( & stepData )
assert . Contains ( t , res , "mandatory in case of:<br />- [`param1`](#param1)=`param1Val`" )
} )
2019-11-13 15:43:53 +02:00
}
2020-07-16 09:10:15 +02:00
func TestFormatDefault ( t * testing . T ) {
tt := [ ] struct {
param config . StepParameters
stepParams [ ] string
expected string
contains [ ] string
} {
{ param : config . StepParameters { Name : "test1" } , stepParams : [ ] string { "test1" } , expected : "`$PIPER_test1` (if set)" } ,
{ param : config . StepParameters { Name : "jenkins1" } , stepParams : [ ] string { } , expected : "" } ,
{
param : config . StepParameters { Name : "test1" , Default : [ ] conditionDefault { { key : "key1" , value : "val1" , def : "def1" } , { key : "key2" , value : "val2" , def : "def2" } } } ,
stepParams : [ ] string { "test1" } ,
contains : [ ] string { "key1=`val1`: `def1`" , "key2=`val2`: `def2`" } ,
} ,
{
param : config . StepParameters { Name : "test1" , Default : [ ] interface { } { conditionDefault { key : "key1" , value : "val1" , def : "def1" } , "def2" } } ,
stepParams : [ ] string { "test1" } ,
contains : [ ] string { "key1=`val1`: `def1`" , "- `def2`" } ,
} ,
{
param : config . StepParameters { Name : "test1" , Default : map [ string ] string { "key1" : "def1" , "key2" : "def2" } } ,
stepParams : [ ] string { "test1" } ,
contains : [ ] string { "`key1`: `def1`" , "`key2`: `def2`" } ,
} ,
{ param : config . StepParameters { Name : "test1" , Default : "" } , stepParams : [ ] string { "test1" } , expected : "`''`" } ,
{ param : config . StepParameters { Name : "test1" , Default : "def1" } , stepParams : [ ] string { "test1" } , expected : "`def1`" } ,
{ param : config . StepParameters { Name : "test1" , Default : 1 } , stepParams : [ ] string { "test1" } , expected : "`1`" } ,
}
2019-11-13 15:43:53 +02:00
2020-07-16 09:10:15 +02:00
for _ , test := range tt {
if len ( test . contains ) > 0 {
res := formatDefault ( test . param , test . stepParams )
for _ , check := range test . contains {
assert . Contains ( t , res , check )
}
2019-11-14 14:59:31 +02:00
2020-07-16 09:10:15 +02:00
} else {
assert . Equal ( t , test . expected , formatDefault ( test . param , test . stepParams ) )
}
2019-11-14 14:59:31 +02:00
2020-07-16 09:10:15 +02:00
}
}
2019-11-14 14:59:31 +02:00
2020-07-16 09:10:15 +02:00
func TestAliasList ( t * testing . T ) {
tt := [ ] struct {
aliases [ ] config . Alias
expected string
contains [ ] string
} {
{ aliases : [ ] config . Alias { } , expected : "-" } ,
{ aliases : [ ] config . Alias { { Name : "alias1" } } , expected : "`alias1`" } ,
{ aliases : [ ] config . Alias { { Name : "alias1" , Deprecated : true } } , expected : "`alias1` (**deprecated**)" } ,
{ aliases : [ ] config . Alias { { Name : "alias1" } , { Name : "alias2" , Deprecated : true } } , contains : [ ] string { "- `alias1`" , "- `alias2` (**deprecated**)" } } ,
}
2019-11-14 14:59:31 +02:00
2020-07-16 09:10:15 +02:00
for _ , test := range tt {
if len ( test . contains ) > 0 {
res := aliasList ( test . aliases )
for _ , check := range test . contains {
assert . Contains ( t , res , check )
2019-11-14 14:59:31 +02:00
}
2020-07-16 09:10:15 +02:00
} else {
assert . Equal ( t , test . expected , aliasList ( test . aliases ) )
2019-11-14 14:59:31 +02:00
}
2020-07-16 09:10:15 +02:00
}
2019-11-14 14:59:31 +02:00
}
2020-07-16 09:10:15 +02:00
func TestPossibleValueList ( t * testing . T ) {
tt := [ ] struct {
possibleValues [ ] interface { }
expected string
contains [ ] string
} {
{ possibleValues : [ ] interface { } { } , expected : "" } ,
{ possibleValues : [ ] interface { } { "poss1" , 0 } , contains : [ ] string { "- `poss1`" , "- `0`" } } ,
}
2019-11-14 14:59:31 +02:00
2020-07-16 09:10:15 +02:00
for _ , test := range tt {
if len ( test . contains ) > 0 {
res := possibleValueList ( test . possibleValues )
for _ , check := range test . contains {
assert . Contains ( t , res , check )
}
} else {
assert . Equal ( t , test . expected , possibleValueList ( test . possibleValues ) )
2019-11-14 14:59:31 +02:00
}
2020-07-16 09:10:15 +02:00
}
2019-11-14 14:59:31 +02:00
}
2020-07-16 09:10:15 +02:00
func TestScopeDetails ( t * testing . T ) {
tt := [ ] struct {
scope [ ] string
contains [ ] string
} {
2020-07-16 11:34:02 +02:00
{ scope : [ ] string { "PARAMETERS" , "GENERAL" , "STEPS" , "STAGES" } , contains : [ ] string { "<li>☒ parameter</li>" , "<li>☒ general</li>" , "<li>☒ steps</li>" , "<li>☒ stages</li>" } } ,
{ scope : [ ] string { } , contains : [ ] string { "<li>☐ parameter</li>" , "<li>☐ general</li>" , "<li>☐ steps</li>" , "<li>☐ stages</li>" } } ,
2020-07-16 09:10:15 +02:00
}
2019-11-14 14:59:31 +02:00
2020-07-16 09:10:15 +02:00
for _ , test := range tt {
res := scopeDetails ( test . scope )
2019-11-14 14:59:31 +02:00
2020-07-16 09:10:15 +02:00
for _ , c := range test . contains {
assert . Contains ( t , res , c )
2019-11-14 14:59:31 +02:00
}
2020-07-16 09:10:15 +02:00
}
2019-11-14 14:59:31 +02:00
2020-07-16 09:10:15 +02:00
}
2019-11-14 14:59:31 +02:00
2020-07-16 09:10:15 +02:00
func TestResourceReferenceDetails ( t * testing . T ) {
tt := [ ] struct {
resourceRef [ ] config . ResourceReference
expected string
contains [ ] string
} {
{ resourceRef : [ ] config . ResourceReference { } , expected : "none" } ,
{
resourceRef : [ ] config . ResourceReference {
{ Name : "commonPipelineEnvironment" , Aliases : [ ] config . Alias { } , Type : "" , Param : "testParam" } ,
} ,
expected : "_commonPipelineEnvironment_:<br /> reference to: `testParam`<br />" ,
} ,
{
resourceRef : [ ] config . ResourceReference {
{ Name : "testCredentialId" , Aliases : [ ] config . Alias { } , Type : "secret" , Param : "password" } ,
} ,
2020-11-26 08:51:43 +02:00
expected : "Jenkins credential id:<br /> id: [`testCredentialId`](#testcredentialid)<br /> reference to: `password`<br />" ,
2020-07-16 09:10:15 +02:00
} ,
{
resourceRef : [ ] config . ResourceReference {
{ Name : "testCredentialId" , Aliases : [ ] config . Alias { { Name : "alias1" } , { Name : "alias2" , Deprecated : true } } , Type : "secret" , Param : "password" } ,
} ,
contains : [ ] string { " aliases:<br />" , " - `alias1`<br />" , " - `alias2` (**Deprecated**)<br />" } ,
} ,
}
2019-11-14 14:59:31 +02:00
2020-07-16 09:10:15 +02:00
for _ , test := range tt {
if len ( test . contains ) > 0 {
res := resourceReferenceDetails ( test . resourceRef )
for _ , check := range test . contains {
assert . Contains ( t , res , check )
}
} else {
assert . Equal ( t , test . expected , resourceReferenceDetails ( test . resourceRef ) )
2019-11-14 14:59:31 +02:00
}
2020-07-16 09:10:15 +02:00
}
2019-11-14 14:59:31 +02:00
}
2020-07-16 09:10:15 +02:00
func TestSortStepParameters ( t * testing . T ) {
stepData := config . StepData {
Spec : config . StepSpec {
Inputs : config . StepInputs {
Parameters : [ ] config . StepParameters {
{ Name : "ab1" , Mandatory : false } ,
{ Name : "ab3" , Mandatory : true } ,
{ Name : "ab5" , Mandatory : false } ,
{ Name : "ab6" , Mandatory : true } ,
{ Name : "ab4" , Mandatory : false } ,
{ Name : "ab2" , Mandatory : true } ,
2021-10-12 12:49:38 +02:00
{ Name : "ab7" , MandatoryIf : [ ] config . ParameterDependence { { Name : "ab1" , Value : "ab1Val1" } } } ,
2020-07-16 09:10:15 +02:00
} ,
} ,
} ,
}
2019-11-14 16:09:12 +02:00
2020-07-16 09:10:15 +02:00
t . Run ( "ignore mandatory" , func ( t * testing . T ) {
sortStepParameters ( & stepData , false )
2019-11-14 16:09:12 +02:00
2020-07-16 09:10:15 +02:00
assert . Equal ( t , "ab1" , stepData . Spec . Inputs . Parameters [ 0 ] . Name )
assert . Equal ( t , "ab2" , stepData . Spec . Inputs . Parameters [ 1 ] . Name )
assert . Equal ( t , "ab3" , stepData . Spec . Inputs . Parameters [ 2 ] . Name )
assert . Equal ( t , "ab4" , stepData . Spec . Inputs . Parameters [ 3 ] . Name )
assert . Equal ( t , "ab5" , stepData . Spec . Inputs . Parameters [ 4 ] . Name )
assert . Equal ( t , "ab6" , stepData . Spec . Inputs . Parameters [ 5 ] . Name )
2021-10-12 12:49:38 +02:00
assert . Equal ( t , "ab7" , stepData . Spec . Inputs . Parameters [ 6 ] . Name )
2019-11-14 16:09:12 +02:00
} )
2020-04-24 14:13:02 +02:00
2020-07-16 09:10:15 +02:00
t . Run ( "consider mandatory" , func ( t * testing . T ) {
sortStepParameters ( & stepData , true )
assert . Equal ( t , "ab2" , stepData . Spec . Inputs . Parameters [ 0 ] . Name )
assert . Equal ( t , "ab3" , stepData . Spec . Inputs . Parameters [ 1 ] . Name )
assert . Equal ( t , "ab6" , stepData . Spec . Inputs . Parameters [ 2 ] . Name )
2021-10-12 12:49:38 +02:00
assert . Equal ( t , "ab7" , stepData . Spec . Inputs . Parameters [ 3 ] . Name )
assert . Equal ( t , "ab1" , stepData . Spec . Inputs . Parameters [ 4 ] . Name )
assert . Equal ( t , "ab4" , stepData . Spec . Inputs . Parameters [ 5 ] . Name )
assert . Equal ( t , "ab5" , stepData . Spec . Inputs . Parameters [ 6 ] . Name )
2020-04-24 14:13:02 +02:00
} )
}