github.com/jaylevin/jenkins-library@v1.230.4/cmd/abapEnvironmentAssemblePackages_test.go (about)

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