github.com/SAP/jenkins-library@v1.362.0/cmd/abapEnvironmentAssemblePackages_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	abapbuild "github.com/SAP/jenkins-library/pkg/abap/build"
    11  	"github.com/SAP/jenkins-library/pkg/abaputils"
    12  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    13  	"github.com/SAP/jenkins-library/pkg/mock"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func testSetup(client piperhttp.Sender, buildID string) abapbuild.Build {
    18  	conn := new(abapbuild.Connector)
    19  	conn.Client = client
    20  	conn.DownloadClient = &abapbuild.DownloadClientMock{}
    21  	conn.Header = make(map[string][]string)
    22  	b := abapbuild.Build{
    23  		Connector: *conn,
    24  		BuildID:   buildID,
    25  	}
    26  	return b
    27  }
    28  
    29  func TestCheckIfFailedAndPrintLogsWithError(t *testing.T) {
    30  	t.Run("checkIfFailedAndPrintLogs with failed build", func(t *testing.T) {
    31  		var repo abaputils.Repository
    32  		b := testSetup(&abapbuild.ClMock{}, "ABIFNLDCSQPOVMXK4DNPBDRW2M")
    33  		b.RunState = abapbuild.Failed
    34  		var buildsWithRepo []buildWithRepository
    35  		bWR := buildWithRepository{
    36  			build: b,
    37  			repo:  repo,
    38  		}
    39  		buildsWithRepo = append(buildsWithRepo, bWR)
    40  		err := checkIfFailedAndPrintLogs(buildsWithRepo)
    41  		assert.Error(t, err)
    42  	})
    43  
    44  	t.Run("checkIfFailedAndPrintLogs", func(t *testing.T) {
    45  		var repo abaputils.Repository
    46  		b := testSetup(&abapbuild.ClMock{}, "ABIFNLDCSQPOVMXK4DNPBDRW2M")
    47  		b.RunState = abapbuild.Finished
    48  		var buildsWithRepo []buildWithRepository
    49  		bWR := buildWithRepository{
    50  			build: b,
    51  			repo:  repo,
    52  		}
    53  		buildsWithRepo = append(buildsWithRepo, bWR)
    54  		err := checkIfFailedAndPrintLogs(buildsWithRepo)
    55  		assert.NoError(t, err)
    56  	})
    57  }
    58  
    59  func TestStartingInvalidInput(t *testing.T) {
    60  	t.Run("Run starting with Invalid Input", func(t *testing.T) {
    61  		client := &abapbuild.ClMock{
    62  			Token: "MyToken",
    63  		}
    64  		conn := new(abapbuild.Connector)
    65  		conn.Client = client
    66  		conn.Header = make(map[string][]string)
    67  		aD := abaputils.AddonDescriptor{
    68  			Repositories: []abaputils.Repository{
    69  				{
    70  					Name:   "RepoA",
    71  					Status: "P",
    72  				},
    73  			},
    74  		}
    75  		builds, err := executeBuilds(&aD, *conn, time.Duration(0*time.Second), time.Duration(1*time.Millisecond))
    76  		assert.NoError(t, err)
    77  		assert.Equal(t, 1, len(builds))
    78  		assert.Equal(t, abapbuild.Failed, builds[0].build.RunState)
    79  	})
    80  }
    81  
    82  func TestStep(t *testing.T) {
    83  	autils := &abaputils.AUtilsMock{
    84  		ReturnedConnectionDetailsHTTP: abaputils.ConnectionDetailsHTTP{
    85  			URL: `/sap/opu/odata/BUILD/CORE_SRV`,
    86  		},
    87  	}
    88  	client := abapbuild.GetBuildMockClient()
    89  	cpe := &abapEnvironmentAssemblePackagesCommonPipelineEnvironment{}
    90  
    91  	t.Run("abapEnvironmentAssemblePackages: nothing to do", func(t *testing.T) {
    92  		config := &abapEnvironmentAssemblePackagesOptions{
    93  			AddonDescriptor:             cpeAbapAddonDescriptorPackageLocked,
    94  			MaxRuntimeInMinutes:         1,
    95  			PollIntervalsInMilliseconds: 1,
    96  		}
    97  
    98  		err := runAbapEnvironmentAssemblePackages(config, nil, autils, &mock.FilesMock{}, &client, cpe)
    99  		assert.NoError(t, err)
   100  		assert.NotContains(t, cpe.abap.addonDescriptor, `"InBuildScope"`)
   101  	})
   102  	t.Run("abapEnvironmentAssemblePackages: build", func(t *testing.T) {
   103  		config := &abapEnvironmentAssemblePackagesOptions{
   104  			AddonDescriptor:             cpeAbapAddonDescriptorPackageReserved,
   105  			MaxRuntimeInMinutes:         1,
   106  			PollIntervalsInMilliseconds: 1,
   107  		}
   108  
   109  		err := runAbapEnvironmentAssemblePackages(config, nil, autils, &mock.FilesMock{}, &client, cpe)
   110  		assert.NoError(t, err)
   111  		assert.Contains(t, cpe.abap.addonDescriptor, `SAPK-001AAINITAPC1.SAR`)
   112  		assert.Contains(t, cpe.abap.addonDescriptor, `"InBuildScope":true`)
   113  	})
   114  }
   115  
   116  var cpeAbapAddonDescriptorPackageLocked = `{
   117  	"addonProduct":"/ITAPC1/I_CURRENCZPRODUCT",
   118  	"addonVersion":"1.0.0",
   119  	"addonVersionAAK":"0001",
   120  	"addonUniqueID":"myAddonId",
   121  	"customerID":"$ID",
   122  	"AddonSpsLevel":"0000",
   123  	"AddonPatchLevel":"0000",
   124  	"TargetVectorID":"",
   125  	"repositories":[
   126  		{	"name":"/ITAPC1/I_CURRENCZ",
   127  			"tag":"whatever",
   128  			"branch":"",
   129  			"commitID":"",
   130  			"version":"1.0.0",
   131  			"versionAAK":"0001",
   132  			"PackageName":"SAPK-002AAINITAPC1",
   133  			"PackageType":"AOI",
   134  			"SpLevel":"0000",
   135  			"PatchLevel":"0000",
   136  			"PredecessorCommitID":"",
   137  			"Status":"L",
   138  			"Namespace":"/ITAPC1/",
   139  			"SarXMLFilePath":".pipeline\\commonPipelineEnvironment\\abap\\SAPK-002AAINITAPC1.SAR"
   140  		}
   141  	]
   142  }`
   143  
   144  var cpeAbapAddonDescriptorPackageReserved = `{
   145  	"addonProduct":"/ITAPC1/I_CURRENCZPRODUCT",
   146  	"addonVersion":"1.0.0",
   147  	"addonVersionAAK":"0001",
   148  	"addonUniqueID":"myAddonId",
   149  	"customerID":"$ID",
   150  	"AddonSpsLevel":"0000",
   151  	"AddonPatchLevel":"0000",
   152  	"TargetVectorID":"",
   153  	"repositories":[
   154  		{	"name":"/ITAPC1/I_CURRENCZ",
   155  			"tag":"whatever",
   156  			"branch":"",
   157  			"commitID":"",
   158  			"version":"1.0.0",
   159  			"versionAAK":"0001",
   160  			"PackageName":"SAPK-002AAINITAPC1",
   161  			"PackageType":"AOI",
   162  			"SpLevel":"0000",
   163  			"PatchLevel":"0000",
   164  			"PredecessorCommitID":"",
   165  			"Status":"P",
   166  			"Namespace":"/ITAPC1/",
   167  			"SarXMLFilePath":""
   168  		}
   169  	]
   170  }`