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

     1  package cmd
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/SAP/jenkins-library/pkg/abaputils"
     9  	"github.com/SAP/jenkins-library/pkg/log"
    10  	"github.com/SAP/jenkins-library/pkg/mock"
    11  	"github.com/sirupsen/logrus/hooks/test"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  type abapEnvironmentCreateTagMockUtils struct {
    16  	*mock.ExecMockRunner
    17  	*mock.FilesMock
    18  }
    19  
    20  func newAbapEnvironmentCreateTagTestsUtils() abapEnvironmentCreateTagMockUtils {
    21  	utils := abapEnvironmentCreateTagMockUtils{
    22  		ExecMockRunner: &mock.ExecMockRunner{},
    23  		FilesMock:      &mock.FilesMock{},
    24  	}
    25  	return utils
    26  }
    27  
    28  func TestRunAbapEnvironmentCreateTag(t *testing.T) {
    29  
    30  	t.Run("happy path", func(t *testing.T) {
    31  
    32  		var autils = &abaputils.AUtilsMock{}
    33  		defer autils.Cleanup()
    34  		autils.ReturnedConnectionDetailsHTTP.Password = "password"
    35  		autils.ReturnedConnectionDetailsHTTP.User = "user"
    36  		autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
    37  		autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
    38  
    39  		dir, errDir := ioutil.TempDir("", "test read addon descriptor")
    40  		if errDir != nil {
    41  			t.Fatal("Failed to create temporary directory")
    42  		}
    43  		oldCWD, _ := os.Getwd()
    44  		_ = os.Chdir(dir)
    45  		// clean up tmp dir
    46  		defer func() {
    47  			_ = os.Chdir(oldCWD)
    48  			_ = os.RemoveAll(dir)
    49  		}()
    50  
    51  		body := `---
    52  addonVersion: "1.2.3"
    53  addonProduct: "/DMO/PRODUCT"
    54  repositories:
    55    - name: /DMO/SWC
    56      branch: main
    57      commitID: 1234abcd
    58      version: "4.5.6"
    59  `
    60  		file, _ := os.Create("repo.yml")
    61  		file.Write([]byte(body))
    62  		config := &abapEnvironmentCreateTagOptions{
    63  			Username:                            "dummy",
    64  			Password:                            "dummy",
    65  			Host:                                "https://test.com",
    66  			Repositories:                        "repo.yml",
    67  			TagName:                             "tag",
    68  			TagDescription:                      "desc",
    69  			GenerateTagForAddonProductVersion:   true,
    70  			GenerateTagForAddonComponentVersion: true,
    71  		}
    72  		client := &abaputils.ClientMock{
    73  			BodyList: []string{
    74  				`{"d" : { "Status" : "S" } }`,
    75  				`{"d" : { "uuid" : "abc" } }`,
    76  				`{"d" : { "Status" : "S" } }`,
    77  				`{"d" : { "uuid" : "abc" } }`,
    78  				`{"d" : { "Status" : "S" } }`,
    79  				`{"d" : { "uuid" : "abc" } }`,
    80  				`{"d" : { "empty" : "body" } }`,
    81  			},
    82  			Token:      "myToken",
    83  			StatusCode: 200,
    84  		}
    85  
    86  		_, hook := test.NewNullLogger()
    87  		log.RegisterHook(hook)
    88  
    89  		err := runAbapEnvironmentCreateTag(config, nil, autils, client)
    90  
    91  		assert.NoError(t, err, "Did not expect error")
    92  		assert.Equal(t, 3, len(hook.Entries), "Expected a different number of entries")
    93  		assert.Equal(t, `Created tag v4.5.6 for repository /DMO/SWC with commitID 1234abcd`, hook.AllEntries()[0].Message, "Expected a different message")
    94  		assert.Equal(t, `Created tag -DMO-PRODUCT-1.2.3 for repository /DMO/SWC with commitID 1234abcd`, hook.AllEntries()[1].Message, "Expected a different message")
    95  		assert.Equal(t, `Created tag tag for repository /DMO/SWC with commitID 1234abcd`, hook.AllEntries()[2].Message, "Expected a different message")
    96  		hook.Reset()
    97  	})
    98  
    99  	t.Run("backend error", func(t *testing.T) {
   100  
   101  		var autils = &abaputils.AUtilsMock{}
   102  		defer autils.Cleanup()
   103  		autils.ReturnedConnectionDetailsHTTP.Password = "password"
   104  		autils.ReturnedConnectionDetailsHTTP.User = "user"
   105  		autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
   106  		autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
   107  
   108  		dir, errDir := ioutil.TempDir("", "test read addon descriptor")
   109  		if errDir != nil {
   110  			t.Fatal("Failed to create temporary directory")
   111  		}
   112  		oldCWD, _ := os.Getwd()
   113  		_ = os.Chdir(dir)
   114  		// clean up tmp dir
   115  		defer func() {
   116  			_ = os.Chdir(oldCWD)
   117  			_ = os.RemoveAll(dir)
   118  		}()
   119  
   120  		body := `---
   121  addonVersion: "1.2.3"
   122  addonProduct: "/DMO/PRODUCT"
   123  repositories:
   124    - name: /DMO/SWC
   125      branch: main
   126      commitID: 1234abcd
   127      version: "4.5.6"
   128  `
   129  		file, _ := os.Create("repo.yml")
   130  		file.Write([]byte(body))
   131  		config := &abapEnvironmentCreateTagOptions{
   132  			Username:                            "dummy",
   133  			Password:                            "dummy",
   134  			Host:                                "https://test.com",
   135  			Repositories:                        "repo.yml",
   136  			TagName:                             "tag",
   137  			TagDescription:                      "desc",
   138  			GenerateTagForAddonProductVersion:   true,
   139  			GenerateTagForAddonComponentVersion: true,
   140  		}
   141  		client := &abaputils.ClientMock{
   142  			BodyList: []string{
   143  				`{"d" : { "Status" : "E" } }`,
   144  				`{"d" : { "uuid" : "abc" } }`,
   145  				`{"d" : { "Status" : "E" } }`,
   146  				`{"d" : { "uuid" : "abc" } }`,
   147  				`{"d" : { "Status" : "E" } }`,
   148  				`{"d" : { "uuid" : "abc" } }`,
   149  				`{"d" : { "empty" : "body" } }`,
   150  			},
   151  			Token:      "myToken",
   152  			StatusCode: 200,
   153  		}
   154  
   155  		_, hook := test.NewNullLogger()
   156  		log.RegisterHook(hook)
   157  
   158  		err := runAbapEnvironmentCreateTag(config, nil, autils, client)
   159  
   160  		assert.Error(t, err, "Did expect error")
   161  		assert.Equal(t, 4, len(hook.Entries), "Expected a different number of entries")
   162  		assert.Equal(t, `NOT created: Tag v4.5.6 for repository /DMO/SWC with commitID 1234abcd`, hook.AllEntries()[0].Message, "Expected a different message")
   163  		assert.Equal(t, `NOT created: Tag -DMO-PRODUCT-1.2.3 for repository /DMO/SWC with commitID 1234abcd`, hook.AllEntries()[1].Message, "Expected a different message")
   164  		assert.Equal(t, `NOT created: Tag tag for repository /DMO/SWC with commitID 1234abcd`, hook.AllEntries()[2].Message, "Expected a different message")
   165  		assert.Equal(t, `At least one tag has not been created`, hook.AllEntries()[3].Message, "Expected a different message")
   166  		hook.Reset()
   167  
   168  	})
   169  
   170  }
   171  
   172  func TestRunAbapEnvironmentCreateTagConfigurations(t *testing.T) {
   173  
   174  	t.Run("no repo.yml", func(t *testing.T) {
   175  
   176  		var autils = &abaputils.AUtilsMock{}
   177  		defer autils.Cleanup()
   178  		autils.ReturnedConnectionDetailsHTTP.Password = "password"
   179  		autils.ReturnedConnectionDetailsHTTP.User = "user"
   180  		autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
   181  		autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
   182  
   183  		config := &abapEnvironmentCreateTagOptions{
   184  			Username:                            "dummy",
   185  			Password:                            "dummy",
   186  			Host:                                "https://test.com",
   187  			RepositoryName:                      "/DMO/SWC",
   188  			CommitID:                            "1234abcd",
   189  			TagName:                             "tag",
   190  			TagDescription:                      "desc",
   191  			GenerateTagForAddonProductVersion:   true,
   192  			GenerateTagForAddonComponentVersion: true,
   193  		}
   194  		client := &abaputils.ClientMock{
   195  			BodyList: []string{
   196  				`{"d" : { "Status" : "S" } }`,
   197  				`{"d" : { "uuid" : "abc" } }`,
   198  				`{"d" : { "empty" : "body" } }`,
   199  			},
   200  			Token:      "myToken",
   201  			StatusCode: 200,
   202  		}
   203  
   204  		_, hook := test.NewNullLogger()
   205  		log.RegisterHook(hook)
   206  
   207  		err := runAbapEnvironmentCreateTag(config, nil, autils, client)
   208  
   209  		assert.NoError(t, err, "Did not expect error")
   210  		assert.Equal(t, 1, len(hook.Entries), "Expected a different number of entries")
   211  		assert.Equal(t, `Created tag tag for repository /DMO/SWC with commitID 1234abcd`, hook.AllEntries()[0].Message, "Expected a different message")
   212  		hook.Reset()
   213  	})
   214  
   215  	t.Run("backend error", func(t *testing.T) {
   216  
   217  		var autils = &abaputils.AUtilsMock{}
   218  		defer autils.Cleanup()
   219  		autils.ReturnedConnectionDetailsHTTP.Password = "password"
   220  		autils.ReturnedConnectionDetailsHTTP.User = "user"
   221  		autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
   222  		autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
   223  
   224  		dir, errDir := ioutil.TempDir("", "test read addon descriptor")
   225  		if errDir != nil {
   226  			t.Fatal("Failed to create temporary directory")
   227  		}
   228  		oldCWD, _ := os.Getwd()
   229  		_ = os.Chdir(dir)
   230  		// clean up tmp dir
   231  		defer func() {
   232  			_ = os.Chdir(oldCWD)
   233  			_ = os.RemoveAll(dir)
   234  		}()
   235  
   236  		body := `---
   237  addonVersion: "1.2.3"
   238  addonProduct: "/DMO/PRODUCT"
   239  repositories:
   240    - name: /DMO/SWC
   241      branch: main
   242      commitID: 1234abcd
   243      version: "4.5.6"
   244  `
   245  		file, _ := os.Create("repo.yml")
   246  		file.Write([]byte(body))
   247  		config := &abapEnvironmentCreateTagOptions{
   248  			Username:                            "dummy",
   249  			Password:                            "dummy",
   250  			Host:                                "https://test.com",
   251  			Repositories:                        "repo.yml",
   252  			RepositoryName:                      "/DMO/SWC2",
   253  			CommitID:                            "1234abcde",
   254  			TagName:                             "tag",
   255  			TagDescription:                      "desc",
   256  			GenerateTagForAddonProductVersion:   true,
   257  			GenerateTagForAddonComponentVersion: true,
   258  		}
   259  		client := &abaputils.ClientMock{
   260  			BodyList: []string{
   261  				`{"d" : { "Status" : "S" } }`,
   262  				`{"d" : { "uuid" : "abc" } }`,
   263  				`{"d" : { "Status" : "S" } }`,
   264  				`{"d" : { "uuid" : "abc" } }`,
   265  				`{"d" : { "Status" : "S" } }`,
   266  				`{"d" : { "uuid" : "abc" } }`,
   267  				`{"d" : { "Status" : "S" } }`,
   268  				`{"d" : { "uuid" : "abc" } }`,
   269  				`{"d" : { "Status" : "S" } }`,
   270  				`{"d" : { "uuid" : "abc" } }`,
   271  				`{"d" : { "empty" : "body" } }`,
   272  			},
   273  			Token:      "myToken",
   274  			StatusCode: 200,
   275  		}
   276  
   277  		err := runAbapEnvironmentCreateTag(config, nil, autils, client)
   278  
   279  		assert.Error(t, err, "Did expect error")
   280  		assert.Equal(t, "Something failed during the tag creation: Configuring the parameter repositories and the parameter repositoryName at the same time is not allowed", err.Error(), "Expected different error message")
   281  
   282  	})
   283  
   284  	t.Run("flags false", func(t *testing.T) {
   285  
   286  		var autils = &abaputils.AUtilsMock{}
   287  		defer autils.Cleanup()
   288  		autils.ReturnedConnectionDetailsHTTP.Password = "password"
   289  		autils.ReturnedConnectionDetailsHTTP.User = "user"
   290  		autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com"
   291  		autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken"
   292  
   293  		dir, errDir := ioutil.TempDir("", "test read addon descriptor")
   294  		if errDir != nil {
   295  			t.Fatal("Failed to create temporary directory")
   296  		}
   297  		oldCWD, _ := os.Getwd()
   298  		_ = os.Chdir(dir)
   299  		// clean up tmp dir
   300  		defer func() {
   301  			_ = os.Chdir(oldCWD)
   302  			_ = os.RemoveAll(dir)
   303  		}()
   304  
   305  		body := `---
   306  addonVersion: "1.2.3"
   307  addonProduct: "/DMO/PRODUCT"
   308  repositories:
   309    - name: /DMO/SWC
   310      branch: main
   311      commitID: 1234abcd
   312      version: "4.5.6"
   313  `
   314  		file, _ := os.Create("repo.yml")
   315  		file.Write([]byte(body))
   316  		config := &abapEnvironmentCreateTagOptions{
   317  			Username:                            "dummy",
   318  			Password:                            "dummy",
   319  			Host:                                "https://test.com",
   320  			Repositories:                        "repo.yml",
   321  			TagName:                             "tag",
   322  			TagDescription:                      "desc",
   323  			GenerateTagForAddonProductVersion:   false,
   324  			GenerateTagForAddonComponentVersion: false,
   325  		}
   326  		client := &abaputils.ClientMock{
   327  			BodyList: []string{
   328  				`{"d" : { "Status" : "S" } }`,
   329  				`{"d" : { "uuid" : "abc" } }`,
   330  				`{"d" : { "Status" : "S" } }`,
   331  				`{"d" : { "uuid" : "abc" } }`,
   332  				`{"d" : { "empty" : "body" } }`,
   333  			},
   334  			Token:      "myToken",
   335  			StatusCode: 200,
   336  		}
   337  
   338  		_, hook := test.NewNullLogger()
   339  		log.RegisterHook(hook)
   340  
   341  		err := runAbapEnvironmentCreateTag(config, nil, autils, client)
   342  
   343  		assert.NoError(t, err, "Did not expect error")
   344  		assert.Equal(t, 1, len(hook.Entries), "Expected a different number of entries")
   345  		assert.Equal(t, `Created tag tag for repository /DMO/SWC with commitID 1234abcd`, hook.AllEntries()[0].Message, "Expected a different message")
   346  		hook.Reset()
   347  
   348  	})
   349  }