github.com/xgoffin/jenkins-library@v1.154.0/cmd/abapEnvironmentCloneGitRepo_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/SAP/jenkins-library/pkg/abaputils"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestCloneStep(t *testing.T) {
    14  	t.Run("Run Step - Successful", func(t *testing.T) {
    15  		var autils = abaputils.AUtilsMock{}
    16  		defer autils.Cleanup()
    17  		autils.ReturnedConnectionDetailsHTTP.Password = "password"
    18  		autils.ReturnedConnectionDetailsHTTP.User = "user"
    19  		autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
    20  		autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
    21  
    22  		dir, errDir := ioutil.TempDir("", "test read addon descriptor")
    23  		if errDir != nil {
    24  			t.Fatal("Failed to create temporary directory")
    25  		}
    26  		oldCWD, _ := os.Getwd()
    27  		_ = os.Chdir(dir)
    28  		// clean up tmp dir
    29  		defer func() {
    30  			_ = os.Chdir(oldCWD)
    31  			_ = os.RemoveAll(dir)
    32  		}()
    33  
    34  		body := `---
    35  repositories:
    36  - name: /DMO/REPO_A
    37    tag: v-1.0.1-build-0001
    38    branch: branchA
    39    version: 1.0.1
    40  - name: /DMO/REPO_B
    41    tag: rel-2.1.1-build-0001
    42    branch: branchB
    43    version: 2.1.1
    44  `
    45  		file, _ := os.Create("filename.yaml")
    46  		file.Write([]byte(body))
    47  
    48  		config := abapEnvironmentCloneGitRepoOptions{
    49  			CfAPIEndpoint:     "https://api.endpoint.com",
    50  			CfOrg:             "testOrg",
    51  			CfSpace:           "testSpace",
    52  			CfServiceInstance: "testInstance",
    53  			CfServiceKeyName:  "testServiceKey",
    54  			Username:          "testUser",
    55  			Password:          "testPassword",
    56  			RepositoryName:    "testRepo1",
    57  			BranchName:        "testBranch1",
    58  			Repositories:      "filename.yaml",
    59  		}
    60  
    61  		logResultSuccess := fmt.Sprintf(`{"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 } ] } } ] } } }`)
    62  		client := &abaputils.ClientMock{
    63  			BodyList: []string{
    64  				logResultSuccess,
    65  				`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
    66  				`{"d" : { "status" : "S" } }`,
    67  				`{"d" : { "status" : "R" } }`,
    68  				`{"d" : { "status" : "R" } }`,
    69  				`{"d" : { "status" : "R" } }`,
    70  				logResultSuccess,
    71  				`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
    72  				`{"d" : { "status" : "S" } }`,
    73  				`{"d" : { "status" : "R" } }`,
    74  				`{"d" : { "status" : "R" } }`,
    75  				`{"d" : { "status" : "R" } }`,
    76  				logResultSuccess,
    77  				`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
    78  				`{"d" : { "status" : "S" } }`,
    79  				`{"d" : { "status" : "R" } }`,
    80  				`{"d" : { "status" : "R" } }`,
    81  				`{"d" : { "status" : "R" } }`,
    82  			},
    83  			Token:      "myToken",
    84  			StatusCode: 200,
    85  		}
    86  
    87  		err := runAbapEnvironmentCloneGitRepo(&config, &autils, client)
    88  		assert.NoError(t, err, "Did not expect error")
    89  		assert.Equal(t, 0, len(client.BodyList), "Not all requests were done")
    90  	})
    91  
    92  	t.Run("Run Step - failing", func(t *testing.T) {
    93  		var autils = abaputils.AUtilsMock{}
    94  		defer autils.Cleanup()
    95  		autils.ReturnedConnectionDetailsHTTP.Password = "password"
    96  		autils.ReturnedConnectionDetailsHTTP.User = "user"
    97  		autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
    98  		autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
    99  
   100  		config := abapEnvironmentCloneGitRepoOptions{
   101  			CfAPIEndpoint:     "https://api.endpoint.com",
   102  			CfOrg:             "testOrg",
   103  			CfSpace:           "testSpace",
   104  			CfServiceInstance: "testInstance",
   105  			CfServiceKeyName:  "testServiceKey",
   106  			Username:          "testUser",
   107  			Password:          "testPassword",
   108  			RepositoryName:    "testRepo1",
   109  			BranchName:        "testBranch1",
   110  		}
   111  
   112  		client := &abaputils.ClientMock{
   113  			BodyList: []string{
   114  				`{"d" : {} }`,
   115  				`{"d" : { "status" : "R" } }`,
   116  			},
   117  			Token:      "myToken",
   118  			StatusCode: 200,
   119  		}
   120  
   121  		err := runAbapEnvironmentCloneGitRepo(&config, &autils, client)
   122  		if assert.Error(t, err, "Expected error") {
   123  			assert.Equal(t, "Clone of repository / software component 'testRepo1', branch 'testBranch1' failed on the ABAP system: Request to ABAP System not successful", err.Error(), "Expected different error message")
   124  		}
   125  
   126  	})
   127  }
   128  
   129  func TestCloneStepErrorMessages(t *testing.T) {
   130  	t.Run("Status Error", func(t *testing.T) {
   131  		var autils = abaputils.AUtilsMock{}
   132  		defer autils.Cleanup()
   133  		autils.ReturnedConnectionDetailsHTTP.Password = "password"
   134  		autils.ReturnedConnectionDetailsHTTP.User = "user"
   135  		autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
   136  		autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
   137  
   138  		dir, errDir := ioutil.TempDir("", "test read addon descriptor")
   139  		if errDir != nil {
   140  			t.Fatal("Failed to create temporary directory")
   141  		}
   142  		oldCWD, _ := os.Getwd()
   143  		_ = os.Chdir(dir)
   144  		// clean up tmp dir
   145  		defer func() {
   146  			_ = os.Chdir(oldCWD)
   147  			_ = os.RemoveAll(dir)
   148  		}()
   149  
   150  		body := `---
   151  repositories:
   152  - name: /DMO/REPO_A
   153    tag: v-1.0.1-build-0001
   154    branch: branchA
   155    version: 1.0.1
   156    commitID: ABCD1234
   157  `
   158  		file, _ := os.Create("filename.yaml")
   159  		file.Write([]byte(body))
   160  
   161  		config := abapEnvironmentCloneGitRepoOptions{
   162  			CfAPIEndpoint:     "https://api.endpoint.com",
   163  			CfOrg:             "testOrg",
   164  			CfSpace:           "testSpace",
   165  			CfServiceInstance: "testInstance",
   166  			CfServiceKeyName:  "testServiceKey",
   167  			Username:          "testUser",
   168  			Password:          "testPassword",
   169  			Repositories:      "filename.yaml",
   170  		}
   171  
   172  		logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "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 } ] } } ] } } }`)
   173  		client := &abaputils.ClientMock{
   174  			BodyList: []string{
   175  				logResultError,
   176  				`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
   177  				`{"d" : { "status" : "E" } }`,
   178  				`{"d" : { "status" : "R" } }`,
   179  				`{"d" : { "status" : "R" } }`,
   180  			},
   181  			Token:      "myToken",
   182  			StatusCode: 200,
   183  		}
   184  
   185  		err := runAbapEnvironmentCloneGitRepo(&config, &autils, client)
   186  		if assert.Error(t, err, "Expected error") {
   187  			assert.Equal(t, "Clone of repository / software component '/DMO/REPO_A', branch 'branchA', commit 'ABCD1234' failed on the ABAP System", err.Error(), "Expected different error message")
   188  		}
   189  	})
   190  
   191  	t.Run("Poll Request Error", func(t *testing.T) {
   192  		var autils = abaputils.AUtilsMock{}
   193  		defer autils.Cleanup()
   194  		autils.ReturnedConnectionDetailsHTTP.Password = "password"
   195  		autils.ReturnedConnectionDetailsHTTP.User = "user"
   196  		autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
   197  		autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
   198  
   199  		config := abapEnvironmentCloneGitRepoOptions{
   200  			CfAPIEndpoint:     "https://api.endpoint.com",
   201  			CfOrg:             "testOrg",
   202  			CfSpace:           "testSpace",
   203  			CfServiceInstance: "testInstance",
   204  			CfServiceKeyName:  "testServiceKey",
   205  			Username:          "testUser",
   206  			Password:          "testPassword",
   207  			RepositoryName:    "testRepo1",
   208  			BranchName:        "testBranch1",
   209  		}
   210  
   211  		client := &abaputils.ClientMock{
   212  			BodyList: []string{
   213  				`{"d" : {  } }`,
   214  				`{"d" : { "status" : "R" } }`,
   215  				`{"d" : { "status" : "R" } }`,
   216  			},
   217  			Token:      "myToken",
   218  			StatusCode: 200,
   219  		}
   220  
   221  		err := runAbapEnvironmentCloneGitRepo(&config, &autils, client)
   222  		if assert.Error(t, err, "Expected error") {
   223  			assert.Equal(t, "Clone of repository / software component 'testRepo1', branch 'testBranch1' failed on the ABAP system: Request to ABAP System not successful", err.Error(), "Expected different error message")
   224  		}
   225  	})
   226  
   227  	t.Run("Trigger Clone Error", func(t *testing.T) {
   228  		var autils = abaputils.AUtilsMock{}
   229  		defer autils.Cleanup()
   230  		autils.ReturnedConnectionDetailsHTTP.Password = "password"
   231  		autils.ReturnedConnectionDetailsHTTP.User = "user"
   232  		autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
   233  		autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
   234  
   235  		config := abapEnvironmentCloneGitRepoOptions{
   236  			CfAPIEndpoint:     "https://api.endpoint.com",
   237  			CfOrg:             "testOrg",
   238  			CfSpace:           "testSpace",
   239  			CfServiceInstance: "testInstance",
   240  			CfServiceKeyName:  "testServiceKey",
   241  			Username:          "testUser",
   242  			Password:          "testPassword",
   243  			RepositoryName:    "testRepo1",
   244  			BranchName:        "testBranch1",
   245  		}
   246  
   247  		client := &abaputils.ClientMock{
   248  			BodyList: []string{
   249  				`{"d" : {  } }`,
   250  				`{"d" : { "status" : "R" } }`,
   251  			},
   252  			Token:      "myToken",
   253  			StatusCode: 200,
   254  		}
   255  
   256  		err := runAbapEnvironmentCloneGitRepo(&config, &autils, client)
   257  		if assert.Error(t, err, "Expected error") {
   258  			assert.Equal(t, "Clone of repository / software component 'testRepo1', branch 'testBranch1' failed on the ABAP system: Request to ABAP System not successful", err.Error(), "Expected different error message")
   259  		}
   260  	})
   261  
   262  	t.Run("Missing file error", func(t *testing.T) {
   263  		var autils = abaputils.AUtilsMock{}
   264  		defer autils.Cleanup()
   265  		autils.ReturnedConnectionDetailsHTTP.Password = "password"
   266  		autils.ReturnedConnectionDetailsHTTP.User = "user"
   267  		autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
   268  		autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
   269  
   270  		config := abapEnvironmentCloneGitRepoOptions{
   271  			CfAPIEndpoint:     "https://api.endpoint.com",
   272  			CfOrg:             "testOrg",
   273  			CfSpace:           "testSpace",
   274  			CfServiceInstance: "testInstance",
   275  			CfServiceKeyName:  "testServiceKey",
   276  			Username:          "testUser",
   277  			Password:          "testPassword",
   278  			RepositoryName:    "testRepo1",
   279  			BranchName:        "testBranch1",
   280  			Repositories:      "filename.yaml",
   281  		}
   282  
   283  		client := &abaputils.ClientMock{
   284  			BodyList: []string{
   285  				`{"d" : {} }`,
   286  				`{"d" : { "status" : "R" } }`,
   287  			},
   288  			Token:      "myToken",
   289  			StatusCode: 200,
   290  		}
   291  
   292  		err := runAbapEnvironmentCloneGitRepo(&config, &autils, client)
   293  		if assert.Error(t, err, "Expected error") {
   294  			assert.Equal(t, "Something failed during the clone: Could not find filename.yaml", err.Error(), "Expected different error message")
   295  		}
   296  
   297  	})
   298  }