github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/abapEnvironmentCreateSystem_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "os" 8 "testing" 9 10 "github.com/SAP/jenkins-library/pkg/cloudfoundry" 11 "github.com/SAP/jenkins-library/pkg/mock" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestRunAbapEnvironmentCreateSystem(t *testing.T) { 16 m := &mock.ExecMockRunner{} 17 cf := cloudfoundry.CFUtils{Exec: m} 18 u := &uuidMock{} 19 20 t.Run("Create service with cf create-service", func(t *testing.T) { 21 defer cfMockCleanup(m) 22 config := abapEnvironmentCreateSystemOptions{ 23 CfAPIEndpoint: "https://api.endpoint.com", 24 CfOrg: "testOrg", 25 CfSpace: "testSpace", 26 Username: "testUser", 27 Password: "testPassword", 28 CfService: "testService", 29 CfServiceInstance: "testName", 30 CfServicePlan: "testPlan", 31 } 32 err := runAbapEnvironmentCreateSystem(&config, nil, cf, u) 33 if assert.NoError(t, err) { 34 assert.Equal(t, []mock.ExecCall{ 35 {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, 36 {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", config.CfService, config.CfServicePlan, config.CfServiceInstance, "-c", "{\"is_development_allowed\":false}", "--wait"}}, 37 {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, 38 m.Calls) 39 } 40 }) 41 42 t.Run("Create service with mainfest", func(t *testing.T) { 43 defer cfMockCleanup(m) 44 config := abapEnvironmentCreateSystemOptions{ 45 CfAPIEndpoint: "https://api.endpoint.com", 46 CfOrg: "testOrg", 47 CfSpace: "testSpace", 48 Username: "testUser", 49 Password: "testPassword", 50 ServiceManifest: "customManifest.yml", 51 } 52 53 dir := t.TempDir() 54 oldCWD, _ := os.Getwd() 55 _ = os.Chdir(dir) 56 // clean up tmp dir 57 defer func() { 58 _ = os.Chdir(oldCWD) 59 }() 60 61 manifestFileString := ` 62 --- 63 create-services: 64 - name: ((name)) 65 broker: "testBroker" 66 plan: "testPlan" 67 68 - name: ((name2)) 69 broker: "testBroker" 70 plan: "testPlan" 71 72 - name: "test3" 73 broker: "testBroker" 74 plan: "testPlan"` 75 76 manifestFileStringBody := []byte(manifestFileString) 77 err := os.WriteFile("customManifest.yml", manifestFileStringBody, 0644) 78 79 err = runAbapEnvironmentCreateSystem(&config, nil, cf, u) 80 if assert.NoError(t, err) { 81 assert.Equal(t, []mock.ExecCall{ 82 {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, 83 {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "customManifest.yml"}}, 84 {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, 85 m.Calls) 86 } 87 }) 88 } 89 90 func TestManifestGeneration(t *testing.T) { 91 92 t.Run("Create service with addon - development", func(t *testing.T) { 93 config := abapEnvironmentCreateSystemOptions{ 94 CfAPIEndpoint: "https://api.endpoint.com", 95 CfOrg: "testOrg", 96 CfSpace: "testSpace", 97 Username: "testUser", 98 Password: "testPassword", 99 CfService: "testService", 100 CfServiceInstance: "testName", 101 CfServicePlan: "testPlan", 102 AbapSystemAdminEmail: "user@example.com", 103 AbapSystemID: "H02", 104 AbapSystemIsDevelopmentAllowed: true, 105 AbapSystemSizeOfPersistence: 4, 106 AbapSystemSizeOfRuntime: 4, 107 AddonDescriptorFileName: "addon.yml", 108 IncludeAddon: true, 109 } 110 111 dir := t.TempDir() 112 oldCWD, _ := os.Getwd() 113 _ = os.Chdir(dir) 114 // clean up tmp dir 115 defer func() { 116 _ = os.Chdir(oldCWD) 117 }() 118 119 addonYML := `addonProduct: myProduct 120 addonVersion: 1.2.3 121 repositories: 122 - name: '/DMO/REPO' 123 ` 124 125 addonYMLBytes := []byte(addonYML) 126 err := os.WriteFile("addon.yml", addonYMLBytes, 0644) 127 128 expectedResult := "{\"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\"}" 129 130 result, err := generateServiceParameterString(&config) 131 132 if assert.NoError(t, err) { 133 assert.Equal(t, expectedResult, result, "Result not as expected") 134 } 135 }) 136 137 t.Run("Test IsDevelopmentAllowed", func(t *testing.T) { 138 config := abapEnvironmentCreateSystemOptions{ 139 CfAPIEndpoint: "https://api.endpoint.com", 140 CfOrg: "testOrg", 141 CfSpace: "testSpace", 142 Username: "testUser", 143 Password: "testPassword", 144 CfService: "testService", 145 CfServiceInstance: "testName", 146 CfServicePlan: "testPlan", 147 AbapSystemAdminEmail: "user@example.com", 148 AbapSystemID: "H02", 149 AbapSystemIsDevelopmentAllowed: true, 150 AbapSystemSizeOfPersistence: 4, 151 AbapSystemSizeOfRuntime: 4, 152 AddonDescriptorFileName: "addon.yml", 153 } 154 155 dir := t.TempDir() 156 oldCWD, _ := os.Getwd() 157 _ = os.Chdir(dir) 158 // clean up tmp dir 159 defer func() { 160 _ = os.Chdir(oldCWD) 161 }() 162 163 addonYML := `addonProduct: myProduct 164 addonVersion: 1.2.3 165 repositories: 166 - name: '/DMO/REPO' 167 ` 168 169 addonYMLBytes := []byte(addonYML) 170 err := os.WriteFile("addon.yml", addonYMLBytes, 0644) 171 172 expectedResult := "{\"admin_email\":\"user@example.com\",\"is_development_allowed\":true,\"sapsystemname\":\"H02\",\"size_of_persistence\":4,\"size_of_runtime\":4}" 173 174 result, err := generateServiceParameterString(&config) 175 176 if assert.NoError(t, err) { 177 assert.Equal(t, expectedResult, result, "Result not as expected") 178 } 179 }) 180 181 t.Run("Create service with addon - no development", func(t *testing.T) { 182 183 config := abapEnvironmentCreateSystemOptions{ 184 CfAPIEndpoint: "https://api.endpoint.com", 185 CfOrg: "testOrg", 186 CfSpace: "testSpace", 187 Username: "testUser", 188 Password: "testPassword", 189 CfService: "testService", 190 CfServiceInstance: "testName", 191 CfServicePlan: "testPlan", 192 AbapSystemAdminEmail: "user@example.com", 193 AbapSystemID: "H02", 194 AbapSystemIsDevelopmentAllowed: false, 195 AbapSystemSizeOfPersistence: 4, 196 AbapSystemSizeOfRuntime: 4, 197 AddonDescriptorFileName: "addon.yml", 198 IncludeAddon: true, 199 } 200 201 dir := t.TempDir() 202 oldCWD, _ := os.Getwd() 203 _ = os.Chdir(dir) 204 // clean up tmp dir 205 defer func() { 206 _ = os.Chdir(oldCWD) 207 }() 208 209 addonYML := `addonProduct: myProduct 210 addonVersion: 1.2.3 211 repositories: 212 - name: '/DMO/REPO' 213 ` 214 215 addonYMLBytes := []byte(addonYML) 216 err := os.WriteFile("addon.yml", addonYMLBytes, 0644) 217 218 expectedResult := "{\"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\"}" 219 220 result, err := generateServiceParameterString(&config) 221 222 if assert.NoError(t, err) { 223 assert.Equal(t, expectedResult, result, "Result not as expected") 224 } 225 }) 226 } 227 228 type uuidMock struct { 229 } 230 231 func (u *uuidMock) getUUID() string { 232 return "my-uuid" 233 }