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

     1  package cmd
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/SAP/jenkins-library/pkg/cloudfoundry"
     9  	"github.com/SAP/jenkins-library/pkg/mock"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestRunAbapEnvironmentCreateSystem(t *testing.T) {
    14  	m := &mock.ExecMockRunner{}
    15  	cf := cloudfoundry.CFUtils{Exec: m}
    16  	u := &uuidMock{}
    17  
    18  	t.Run("Create service with generated manifest", func(t *testing.T) {
    19  		defer cfMockCleanup(m)
    20  		config := abapEnvironmentCreateSystemOptions{
    21  			CfAPIEndpoint:     "https://api.endpoint.com",
    22  			CfOrg:             "testOrg",
    23  			CfSpace:           "testSpace",
    24  			Username:          "testUser",
    25  			Password:          "testPassword",
    26  			CfService:         "testService",
    27  			CfServiceInstance: "testName",
    28  			CfServicePlan:     "testPlan",
    29  		}
    30  		wd, _ := os.Getwd()
    31  		err := runAbapEnvironmentCreateSystem(&config, nil, cf, u)
    32  		if assert.NoError(t, err) {
    33  			assert.Equal(t, []mock.ExecCall{
    34  				{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}},
    35  				{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", wd + "/generated_service_manifest-my-uuid.yml"}},
    36  				{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
    37  				m.Calls)
    38  		}
    39  	})
    40  
    41  	t.Run("Create service with mainfest", func(t *testing.T) {
    42  		defer cfMockCleanup(m)
    43  		config := abapEnvironmentCreateSystemOptions{
    44  			CfAPIEndpoint:   "https://api.endpoint.com",
    45  			CfOrg:           "testOrg",
    46  			CfSpace:         "testSpace",
    47  			Username:        "testUser",
    48  			Password:        "testPassword",
    49  			ServiceManifest: "customManifest.yml",
    50  		}
    51  
    52  		dir, err := ioutil.TempDir("", "test variable substitution")
    53  		if err != nil {
    54  			t.Fatal("Failed to create temporary directory")
    55  		}
    56  		oldCWD, _ := os.Getwd()
    57  		_ = os.Chdir(dir)
    58  		// clean up tmp dir
    59  		defer func() {
    60  			_ = os.Chdir(oldCWD)
    61  			_ = os.RemoveAll(dir)
    62  		}()
    63  
    64  		manifestFileString := `
    65  		---
    66  		create-services:
    67  		- name:   ((name))
    68  		  broker: "testBroker"
    69  		  plan:   "testPlan"
    70  
    71  		- name:   ((name2))
    72  		  broker: "testBroker"
    73  		  plan:   "testPlan"
    74  
    75  		- name:   "test3"
    76  		  broker: "testBroker"
    77  		  plan:   "testPlan"`
    78  
    79  		manifestFileStringBody := []byte(manifestFileString)
    80  		err = ioutil.WriteFile("customManifest.yml", manifestFileStringBody, 0644)
    81  
    82  		err = runAbapEnvironmentCreateSystem(&config, nil, cf, u)
    83  		if assert.NoError(t, err) {
    84  			assert.Equal(t, []mock.ExecCall{
    85  				{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}},
    86  				{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "customManifest.yml"}},
    87  				{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
    88  				m.Calls)
    89  		}
    90  	})
    91  }
    92  
    93  func TestManifestGeneration(t *testing.T) {
    94  
    95  	t.Run("Create service with generated manifest", func(t *testing.T) {
    96  		config := abapEnvironmentCreateSystemOptions{
    97  			CfAPIEndpoint:                  "https://api.endpoint.com",
    98  			CfOrg:                          "testOrg",
    99  			CfSpace:                        "testSpace",
   100  			Username:                       "testUser",
   101  			Password:                       "testPassword",
   102  			CfService:                      "testService",
   103  			CfServiceInstance:              "testName",
   104  			CfServicePlan:                  "testPlan",
   105  			AbapSystemAdminEmail:           "user@example.com",
   106  			AbapSystemID:                   "H02",
   107  			AbapSystemIsDevelopmentAllowed: true,
   108  			AbapSystemSizeOfPersistence:    4,
   109  			AbapSystemSizeOfRuntime:        4,
   110  			AddonDescriptorFileName:        "addon.yml",
   111  		}
   112  
   113  		dir, err := ioutil.TempDir("", "test variable substitution")
   114  		if err != nil {
   115  			t.Fatal("Failed to create temporary directory")
   116  		}
   117  		oldCWD, _ := os.Getwd()
   118  		_ = os.Chdir(dir)
   119  		// clean up tmp dir
   120  		defer func() {
   121  			_ = os.Chdir(oldCWD)
   122  			_ = os.RemoveAll(dir)
   123  		}()
   124  
   125  		addonYML := `addonProduct: myProduct
   126  addonVersion: 1.2.3
   127  repositories:
   128    - name: '/DMO/REPO'
   129  `
   130  
   131  		addonYMLBytes := []byte(addonYML)
   132  		err = ioutil.WriteFile("addon.yml", addonYMLBytes, 0644)
   133  
   134  		expectedResult := `create-services:
   135  - broker: testService
   136    name: testName
   137    parameters: '{"admin_email":"user@example.com","is_development_allowed":true,"sapsystemname":"H02","size_of_persistence":4,"size_of_runtime":4}'
   138    plan: testPlan
   139  `
   140  
   141  		resultBytes, err := generateManifestYAML(&config)
   142  
   143  		if assert.NoError(t, err) {
   144  			result := string(resultBytes)
   145  			assert.Equal(t, expectedResult, result, "Result not as expected")
   146  		}
   147  	})
   148  
   149  	t.Run("Create service with generated manifest - with addon", func(t *testing.T) {
   150  		config := abapEnvironmentCreateSystemOptions{
   151  			CfAPIEndpoint:                  "https://api.endpoint.com",
   152  			CfOrg:                          "testOrg",
   153  			CfSpace:                        "testSpace",
   154  			Username:                       "testUser",
   155  			Password:                       "testPassword",
   156  			CfService:                      "testService",
   157  			CfServiceInstance:              "testName",
   158  			CfServicePlan:                  "testPlan",
   159  			AbapSystemAdminEmail:           "user@example.com",
   160  			AbapSystemID:                   "H02",
   161  			AbapSystemIsDevelopmentAllowed: true,
   162  			AbapSystemSizeOfPersistence:    4,
   163  			AbapSystemSizeOfRuntime:        4,
   164  			AddonDescriptorFileName:        "addon.yml",
   165  			IncludeAddon:                   true,
   166  		}
   167  
   168  		dir, err := ioutil.TempDir("", "test variable substitution")
   169  		if err != nil {
   170  			t.Fatal("Failed to create temporary directory")
   171  		}
   172  		oldCWD, _ := os.Getwd()
   173  		_ = os.Chdir(dir)
   174  		// clean up tmp dir
   175  		defer func() {
   176  			_ = os.Chdir(oldCWD)
   177  			_ = os.RemoveAll(dir)
   178  		}()
   179  
   180  		addonYML := `addonProduct: myProduct
   181  addonVersion: 1.2.3
   182  repositories:
   183    - name: '/DMO/REPO'
   184  `
   185  
   186  		addonYMLBytes := []byte(addonYML)
   187  		err = ioutil.WriteFile("addon.yml", addonYMLBytes, 0644)
   188  
   189  		expectedResult := `create-services:
   190  - broker: testService
   191    name: testName
   192    parameters: '{"admin_email":"user@example.com","is_development_allowed":true,"sapsystemname":"H02","size_of_persistence":4,"size_of_runtime":4,"addon_product_name":"myProduct","addon_product_version":"1.2.3","parent_saas_appname":"addon_test"}'
   193    plan: testPlan
   194  `
   195  
   196  		resultBytes, err := generateManifestYAML(&config)
   197  
   198  		if assert.NoError(t, err) {
   199  			result := string(resultBytes)
   200  			assert.Equal(t, expectedResult, result, "Result not as expected")
   201  		}
   202  	})
   203  
   204  	t.Run("Test IsDevelopmentAllowed", func(t *testing.T) {
   205  		config := abapEnvironmentCreateSystemOptions{
   206  			CfAPIEndpoint:                  "https://api.endpoint.com",
   207  			CfOrg:                          "testOrg",
   208  			CfSpace:                        "testSpace",
   209  			Username:                       "testUser",
   210  			Password:                       "testPassword",
   211  			CfService:                      "testService",
   212  			CfServiceInstance:              "testName",
   213  			CfServicePlan:                  "testPlan",
   214  			AbapSystemAdminEmail:           "user@example.com",
   215  			AbapSystemID:                   "H02",
   216  			AbapSystemIsDevelopmentAllowed: true,
   217  			AbapSystemSizeOfPersistence:    4,
   218  			AbapSystemSizeOfRuntime:        4,
   219  			AddonDescriptorFileName:        "addon.yml",
   220  		}
   221  
   222  		dir, err := ioutil.TempDir("", "test variable substitution")
   223  		if err != nil {
   224  			t.Fatal("Failed to create temporary directory")
   225  		}
   226  		oldCWD, _ := os.Getwd()
   227  		_ = os.Chdir(dir)
   228  		// clean up tmp dir
   229  		defer func() {
   230  			_ = os.Chdir(oldCWD)
   231  			_ = os.RemoveAll(dir)
   232  		}()
   233  
   234  		addonYML := `addonProduct: myProduct
   235  addonVersion: 1.2.3
   236  repositories:
   237    - name: '/DMO/REPO'
   238  `
   239  
   240  		addonYMLBytes := []byte(addonYML)
   241  		err = ioutil.WriteFile("addon.yml", addonYMLBytes, 0644)
   242  
   243  		expectedResult := `create-services:
   244  - broker: testService
   245    name: testName
   246    parameters: '{"admin_email":"user@example.com","is_development_allowed":true,"sapsystemname":"H02","size_of_persistence":4,"size_of_runtime":4}'
   247    plan: testPlan
   248  `
   249  
   250  		resultBytes, err := generateManifestYAML(&config)
   251  
   252  		if assert.NoError(t, err) {
   253  			result := string(resultBytes)
   254  			assert.Equal(t, expectedResult, result, "Result not as expected")
   255  		}
   256  	})
   257  
   258  	t.Run("Create service with generated manifest - with addon", func(t *testing.T) {
   259  		config := abapEnvironmentCreateSystemOptions{
   260  			CfAPIEndpoint:                  "https://api.endpoint.com",
   261  			CfOrg:                          "testOrg",
   262  			CfSpace:                        "testSpace",
   263  			Username:                       "testUser",
   264  			Password:                       "testPassword",
   265  			CfService:                      "testService",
   266  			CfServiceInstance:              "testName",
   267  			CfServicePlan:                  "testPlan",
   268  			AbapSystemAdminEmail:           "user@example.com",
   269  			AbapSystemID:                   "H02",
   270  			AbapSystemIsDevelopmentAllowed: false,
   271  			AbapSystemSizeOfPersistence:    4,
   272  			AbapSystemSizeOfRuntime:        4,
   273  			AddonDescriptorFileName:        "addon.yml",
   274  			IncludeAddon:                   true,
   275  		}
   276  
   277  		dir, err := ioutil.TempDir("", "test variable substitution")
   278  		if err != nil {
   279  			t.Fatal("Failed to create temporary directory")
   280  		}
   281  		oldCWD, _ := os.Getwd()
   282  		_ = os.Chdir(dir)
   283  		// clean up tmp dir
   284  		defer func() {
   285  			_ = os.Chdir(oldCWD)
   286  			_ = os.RemoveAll(dir)
   287  		}()
   288  
   289  		addonYML := `addonProduct: myProduct
   290  addonVersion: 1.2.3
   291  repositories:
   292    - name: '/DMO/REPO'
   293  `
   294  
   295  		addonYMLBytes := []byte(addonYML)
   296  		err = ioutil.WriteFile("addon.yml", addonYMLBytes, 0644)
   297  
   298  		expectedResult := `create-services:
   299  - broker: testService
   300    name: testName
   301    parameters: '{"admin_email":"user@example.com","is_development_allowed":false,"sapsystemname":"H02","size_of_persistence":4,"size_of_runtime":4,"addon_product_name":"myProduct","addon_product_version":"1.2.3","parent_saas_appname":"addon_test"}'
   302    plan: testPlan
   303  `
   304  
   305  		resultBytes, err := generateManifestYAML(&config)
   306  
   307  		if assert.NoError(t, err) {
   308  			result := string(resultBytes)
   309  			assert.Equal(t, expectedResult, result, "Result not as expected")
   310  		}
   311  	})
   312  }
   313  
   314  type uuidMock struct {
   315  }
   316  
   317  func (u *uuidMock) getUUID() string {
   318  	return "my-uuid"
   319  }