github.com/xgoffin/jenkins-library@v1.154.0/pkg/abaputils/manageGitRepositoryUtils_test.go (about)

     1  package abaputils
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestPollEntity(t *testing.T) {
    13  
    14  	t.Run("Test poll entity - success case", func(t *testing.T) {
    15  
    16  		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 } ] } } ] } } }`)
    17  		client := &ClientMock{
    18  			BodyList: []string{
    19  				logResultSuccess,
    20  				`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
    21  				`{"d" : { "status" : "S" } }`,
    22  				`{"d" : { "status" : "R" } }`,
    23  			},
    24  			Token:      "myToken",
    25  			StatusCode: 200,
    26  		}
    27  
    28  		options := AbapEnvironmentOptions{
    29  			CfAPIEndpoint:     "https://api.endpoint.com",
    30  			CfOrg:             "testOrg",
    31  			CfSpace:           "testSpace",
    32  			CfServiceInstance: "testInstance",
    33  			CfServiceKeyName:  "testServiceKey",
    34  			Username:          "testUser",
    35  			Password:          "testPassword",
    36  		}
    37  
    38  		config := AbapEnvironmentCheckoutBranchOptions{
    39  			AbapEnvOptions: options,
    40  			RepositoryName: "testRepo1",
    41  		}
    42  
    43  		con := ConnectionDetailsHTTP{
    44  			User:       "MY_USER",
    45  			Password:   "MY_PW",
    46  			URL:        "https://api.endpoint.com/Entity/",
    47  			XCsrfToken: "MY_TOKEN",
    48  		}
    49  		status, _ := PollEntity(config.RepositoryName, con, client, 0)
    50  		assert.Equal(t, "S", status)
    51  	})
    52  
    53  	t.Run("Test poll entity - error case", func(t *testing.T) {
    54  		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 } ] } } ] } } }`)
    55  		client := &ClientMock{
    56  			BodyList: []string{
    57  				logResultError,
    58  				`{"d" : { "EntitySets" : [ "LogOverviews" ] } }`,
    59  				`{"d" : { "status" : "E" } }`,
    60  				`{"d" : { "status" : "R" } }`,
    61  			},
    62  			Token:      "myToken",
    63  			StatusCode: 200,
    64  		}
    65  
    66  		options := AbapEnvironmentOptions{
    67  			CfAPIEndpoint:     "https://api.endpoint.com",
    68  			CfOrg:             "testOrg",
    69  			CfSpace:           "testSpace",
    70  			CfServiceInstance: "testInstance",
    71  			CfServiceKeyName:  "testServiceKey",
    72  			Username:          "testUser",
    73  			Password:          "testPassword",
    74  		}
    75  
    76  		config := AbapEnvironmentCheckoutBranchOptions{
    77  			AbapEnvOptions: options,
    78  			RepositoryName: "testRepo1",
    79  		}
    80  
    81  		con := ConnectionDetailsHTTP{
    82  			User:       "MY_USER",
    83  			Password:   "MY_PW",
    84  			URL:        "https://api.endpoint.com/Entity/",
    85  			XCsrfToken: "MY_TOKEN",
    86  		}
    87  		status, _ := PollEntity(config.RepositoryName, con, client, 0)
    88  		assert.Equal(t, "E", status)
    89  	})
    90  }
    91  
    92  func TestGetRepositories(t *testing.T) {
    93  	t.Run("Get Repositories from config - success case", func(t *testing.T) {
    94  		expectedRepositoryList := []Repository{{
    95  			Name:   "testRepo",
    96  			Branch: "testBranch",
    97  		}, {
    98  			Name:   "testRepo2",
    99  			Branch: "testBranch2",
   100  		}, {
   101  			Name:   "testRepo3",
   102  			Branch: "testBranch3",
   103  		}, {
   104  			Name:   "testRepository",
   105  			Branch: "testBranch",
   106  		}, {
   107  			Name: "testRepository",
   108  		}}
   109  
   110  		dir, err := ioutil.TempDir("", "test abap utils")
   111  		if err != nil {
   112  			t.Fatal("Failed to create temporary directory")
   113  		}
   114  		oldCWD, _ := os.Getwd()
   115  		_ = os.Chdir(dir)
   116  		// clean up tmp dir
   117  
   118  		defer func() {
   119  			_ = os.Chdir(oldCWD)
   120  			_ = os.RemoveAll(dir)
   121  		}()
   122  
   123  		manifestFileString := `
   124  repositories:
   125  - name: 'testRepo'
   126    branch: 'testBranch'
   127  - name: 'testRepo2'
   128    branch: 'testBranch2'
   129  - name: 'testRepo3'
   130    branch: 'testBranch3'`
   131  
   132  		err = ioutil.WriteFile("repositoriesTest.yml", []byte(manifestFileString), 0644)
   133  
   134  		config := RepositoriesConfig{
   135  			BranchName:      "testBranch",
   136  			RepositoryName:  "testRepository",
   137  			RepositoryNames: []string{"testRepository"},
   138  			Repositories:    "repositoriesTest.yml",
   139  		}
   140  
   141  		repositories, err := GetRepositories(&config)
   142  
   143  		assert.Equal(t, expectedRepositoryList, repositories)
   144  		assert.NoError(t, err)
   145  	})
   146  	t.Run("Get Repositories from file config - failure case", func(t *testing.T) {
   147  		expectedRepositoryList := []Repository([]Repository{})
   148  		expectedErrorMessage := "Error in config file repositoriesTest.yml, AddonDescriptor doesn't contain any repositories"
   149  
   150  		dir, err := ioutil.TempDir("", "test abap utils")
   151  		if err != nil {
   152  			t.Fatal("Failed to create temporary directory")
   153  		}
   154  		oldCWD, _ := os.Getwd()
   155  		_ = os.Chdir(dir)
   156  		// clean up tmp dir
   157  
   158  		defer func() {
   159  			_ = os.Chdir(oldCWD)
   160  			_ = os.RemoveAll(dir)
   161  		}()
   162  
   163  		manifestFileString := `
   164  repositories:
   165  - repo: 'testRepo'
   166  - repo: 'testRepo2'`
   167  
   168  		err = ioutil.WriteFile("repositoriesTest.yml", []byte(manifestFileString), 0644)
   169  
   170  		config := RepositoriesConfig{
   171  			Repositories: "repositoriesTest.yml",
   172  		}
   173  
   174  		repositories, err := GetRepositories(&config)
   175  
   176  		assert.Equal(t, expectedRepositoryList, repositories)
   177  		assert.EqualError(t, err, expectedErrorMessage)
   178  	})
   179  	t.Run("Get Repositories from config - failure case", func(t *testing.T) {
   180  		expectedRepositoryList := []Repository([]Repository{})
   181  		expectedErrorMessage := "Error in config file repositoriesTest.yml, AddonDescriptor doesn't contain any repositories"
   182  
   183  		dir, err := ioutil.TempDir("", "test  abap utils")
   184  		if err != nil {
   185  			t.Fatal("Failed to create temporary directory")
   186  		}
   187  		oldCWD, _ := os.Getwd()
   188  		_ = os.Chdir(dir)
   189  		// clean up tmp dir
   190  
   191  		defer func() {
   192  			_ = os.Chdir(oldCWD)
   193  			_ = os.RemoveAll(dir)
   194  		}()
   195  
   196  		manifestFileString := `
   197  repositories:
   198  - repo: 'testRepo'
   199  - repo: 'testRepo2'`
   200  
   201  		err = ioutil.WriteFile("repositoriesTest.yml", []byte(manifestFileString), 0644)
   202  
   203  		config := RepositoriesConfig{
   204  			Repositories: "repositoriesTest.yml",
   205  		}
   206  
   207  		repositories, err := GetRepositories(&config)
   208  
   209  		assert.Equal(t, expectedRepositoryList, repositories)
   210  		assert.EqualError(t, err, expectedErrorMessage)
   211  	})
   212  	t.Run("Get Repositories from empty config - failure case", func(t *testing.T) {
   213  		expectedRepositoryList := []Repository([]Repository{})
   214  		expectedErrorMessage := "Failed to read repository configuration: You have not specified any repository configuration. Please make sure that you have correctly specified it. For more information please read the User documentation"
   215  
   216  		config := RepositoriesConfig{}
   217  
   218  		repositories, err := GetRepositories(&config)
   219  
   220  		assert.Equal(t, expectedRepositoryList, repositories)
   221  		assert.EqualError(t, err, expectedErrorMessage)
   222  	})
   223  }
   224  
   225  func TestCreateLogStrings(t *testing.T) {
   226  	t.Run("Clone LogString Tag and Commit", func(t *testing.T) {
   227  		repo := Repository{
   228  			Name:     "/DMO/REPO",
   229  			Branch:   "main",
   230  			CommitID: "1234567",
   231  			Tag:      "myTag",
   232  		}
   233  		logString := repo.GetCloneLogString()
   234  		assert.Equal(t, "repository / software component '/DMO/REPO', branch 'main', commit '1234567'", logString, "Expected different string")
   235  	})
   236  	t.Run("Clone LogString Tag", func(t *testing.T) {
   237  		repo := Repository{
   238  			Name:   "/DMO/REPO",
   239  			Branch: "main",
   240  			Tag:    "myTag",
   241  		}
   242  		logString := repo.GetCloneLogString()
   243  		assert.Equal(t, "repository / software component '/DMO/REPO', branch 'main', tag 'myTag'", logString, "Expected different string")
   244  	})
   245  	t.Run("Pull LogString Tag and Commit", func(t *testing.T) {
   246  		repo := Repository{
   247  			Name:     "/DMO/REPO",
   248  			Branch:   "main",
   249  			CommitID: "1234567",
   250  			Tag:      "myTag",
   251  		}
   252  		logString := repo.GetPullLogString()
   253  		assert.Equal(t, "repository / software component '/DMO/REPO', commit '1234567'", logString, "Expected different string")
   254  	})
   255  	t.Run("Pull LogString Tag", func(t *testing.T) {
   256  		repo := Repository{
   257  			Name:   "/DMO/REPO",
   258  			Branch: "main",
   259  			Tag:    "myTag",
   260  		}
   261  		logString := repo.GetPullLogString()
   262  		assert.Equal(t, "repository / software component '/DMO/REPO', tag 'myTag'", logString, "Expected different string")
   263  	})
   264  }
   265  
   266  func TestCreateRequestBodies(t *testing.T) {
   267  	t.Run("Clone Body Tag and Commit", func(t *testing.T) {
   268  		repo := Repository{
   269  			Name:     "/DMO/REPO",
   270  			Branch:   "main",
   271  			CommitID: "1234567",
   272  			Tag:      "myTag",
   273  		}
   274  		body := repo.GetCloneRequestBody()
   275  		assert.Equal(t, `{"sc_name":"/DMO/REPO", "branch_name":"main", "commit_id":"1234567"}`, body, "Expected different body")
   276  	})
   277  	t.Run("Clone Body Tag", func(t *testing.T) {
   278  		repo := Repository{
   279  			Name:   "/DMO/REPO",
   280  			Branch: "main",
   281  			Tag:    "myTag",
   282  		}
   283  		body := repo.GetCloneRequestBody()
   284  		assert.Equal(t, `{"sc_name":"/DMO/REPO", "branch_name":"main", "tag_name":"myTag"}`, body, "Expected different body")
   285  	})
   286  	t.Run("Pull Body Tag and Commit", func(t *testing.T) {
   287  		repo := Repository{
   288  			Name:     "/DMO/REPO",
   289  			Branch:   "main",
   290  			CommitID: "1234567",
   291  			Tag:      "myTag",
   292  		}
   293  		body := repo.GetPullRequestBody()
   294  		assert.Equal(t, `{"sc_name":"/DMO/REPO", "commit_id":"1234567"}`, body, "Expected different body")
   295  	})
   296  	t.Run("Pull Body Tag", func(t *testing.T) {
   297  		repo := Repository{
   298  			Name:   "/DMO/REPO",
   299  			Branch: "main",
   300  			Tag:    "myTag",
   301  		}
   302  		body := repo.GetPullRequestBody()
   303  		assert.Equal(t, `{"sc_name":"/DMO/REPO", "tag_name":"myTag"}`, body, "Expected different body")
   304  	})
   305  }