2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
2020-11-03 13:02:13 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/cloudfoundry"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRunAbapEnvironmentCreateSystem(t *testing.T) {
|
|
|
|
m := &mock.ExecMockRunner{}
|
|
|
|
cf := cloudfoundry.CFUtils{Exec: m}
|
|
|
|
u := &uuidMock{}
|
|
|
|
|
2023-03-08 10:44:00 +02:00
|
|
|
t.Run("Create service with cf create-service", func(t *testing.T) {
|
2020-11-03 13:02:13 +02:00
|
|
|
defer cfMockCleanup(m)
|
|
|
|
config := abapEnvironmentCreateSystemOptions{
|
|
|
|
CfAPIEndpoint: "https://api.endpoint.com",
|
|
|
|
CfOrg: "testOrg",
|
|
|
|
CfSpace: "testSpace",
|
|
|
|
Username: "testUser",
|
|
|
|
Password: "testPassword",
|
|
|
|
CfService: "testService",
|
|
|
|
CfServiceInstance: "testName",
|
|
|
|
CfServicePlan: "testPlan",
|
|
|
|
}
|
|
|
|
err := runAbapEnvironmentCreateSystem(&config, nil, cf, u)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, []mock.ExecCall{
|
|
|
|
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}},
|
2023-03-08 10:44:00 +02:00
|
|
|
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", config.CfService, config.CfServicePlan, config.CfServiceInstance, "-c", "{\"is_development_allowed\":false}", "--wait"}},
|
2020-11-03 13:02:13 +02:00
|
|
|
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
|
|
|
|
m.Calls)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Create service with mainfest", func(t *testing.T) {
|
|
|
|
defer cfMockCleanup(m)
|
|
|
|
config := abapEnvironmentCreateSystemOptions{
|
|
|
|
CfAPIEndpoint: "https://api.endpoint.com",
|
|
|
|
CfOrg: "testOrg",
|
|
|
|
CfSpace: "testSpace",
|
|
|
|
Username: "testUser",
|
|
|
|
Password: "testPassword",
|
|
|
|
ServiceManifest: "customManifest.yml",
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:19:12 +02:00
|
|
|
dir := t.TempDir()
|
2020-11-03 13:02:13 +02:00
|
|
|
oldCWD, _ := os.Getwd()
|
|
|
|
_ = os.Chdir(dir)
|
|
|
|
// clean up tmp dir
|
|
|
|
defer func() {
|
|
|
|
_ = os.Chdir(oldCWD)
|
|
|
|
}()
|
|
|
|
|
|
|
|
manifestFileString := `
|
|
|
|
---
|
|
|
|
create-services:
|
|
|
|
- name: ((name))
|
|
|
|
broker: "testBroker"
|
|
|
|
plan: "testPlan"
|
|
|
|
|
|
|
|
- name: ((name2))
|
|
|
|
broker: "testBroker"
|
|
|
|
plan: "testPlan"
|
|
|
|
|
|
|
|
- name: "test3"
|
|
|
|
broker: "testBroker"
|
|
|
|
plan: "testPlan"`
|
|
|
|
|
|
|
|
manifestFileStringBody := []byte(manifestFileString)
|
2023-08-16 12:57:04 +02:00
|
|
|
err := os.WriteFile("customManifest.yml", manifestFileStringBody, 0644)
|
2020-11-03 13:02:13 +02:00
|
|
|
|
|
|
|
err = runAbapEnvironmentCreateSystem(&config, nil, cf, u)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, []mock.ExecCall{
|
|
|
|
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}},
|
|
|
|
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "customManifest.yml"}},
|
|
|
|
{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
|
|
|
|
m.Calls)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManifestGeneration(t *testing.T) {
|
|
|
|
|
2023-03-08 10:44:00 +02:00
|
|
|
t.Run("Create service with addon - development", func(t *testing.T) {
|
2020-11-03 14:41:39 +02:00
|
|
|
config := abapEnvironmentCreateSystemOptions{
|
|
|
|
CfAPIEndpoint: "https://api.endpoint.com",
|
|
|
|
CfOrg: "testOrg",
|
|
|
|
CfSpace: "testSpace",
|
|
|
|
Username: "testUser",
|
|
|
|
Password: "testPassword",
|
|
|
|
CfService: "testService",
|
|
|
|
CfServiceInstance: "testName",
|
|
|
|
CfServicePlan: "testPlan",
|
|
|
|
AbapSystemAdminEmail: "user@example.com",
|
|
|
|
AbapSystemID: "H02",
|
|
|
|
AbapSystemIsDevelopmentAllowed: true,
|
|
|
|
AbapSystemSizeOfPersistence: 4,
|
|
|
|
AbapSystemSizeOfRuntime: 4,
|
|
|
|
AddonDescriptorFileName: "addon.yml",
|
|
|
|
IncludeAddon: true,
|
2020-11-03 13:02:13 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 15:19:12 +02:00
|
|
|
dir := t.TempDir()
|
2020-11-03 13:02:13 +02:00
|
|
|
oldCWD, _ := os.Getwd()
|
|
|
|
_ = os.Chdir(dir)
|
|
|
|
// clean up tmp dir
|
|
|
|
defer func() {
|
|
|
|
_ = os.Chdir(oldCWD)
|
|
|
|
}()
|
|
|
|
|
|
|
|
addonYML := `addonProduct: myProduct
|
|
|
|
addonVersion: 1.2.3
|
|
|
|
repositories:
|
|
|
|
- name: '/DMO/REPO'
|
|
|
|
`
|
|
|
|
|
|
|
|
addonYMLBytes := []byte(addonYML)
|
2023-08-16 12:57:04 +02:00
|
|
|
err := os.WriteFile("addon.yml", addonYMLBytes, 0644)
|
2020-11-03 13:02:13 +02:00
|
|
|
|
2023-03-08 10:44:00 +02:00
|
|
|
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\"}"
|
2020-11-03 13:02:13 +02:00
|
|
|
|
2023-03-08 10:44:00 +02:00
|
|
|
result, err := generateServiceParameterString(&config)
|
2020-11-03 13:02:13 +02:00
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, expectedResult, result, "Result not as expected")
|
|
|
|
}
|
|
|
|
})
|
2021-01-18 12:14:55 +02:00
|
|
|
|
|
|
|
t.Run("Test IsDevelopmentAllowed", func(t *testing.T) {
|
|
|
|
config := abapEnvironmentCreateSystemOptions{
|
|
|
|
CfAPIEndpoint: "https://api.endpoint.com",
|
|
|
|
CfOrg: "testOrg",
|
|
|
|
CfSpace: "testSpace",
|
|
|
|
Username: "testUser",
|
|
|
|
Password: "testPassword",
|
|
|
|
CfService: "testService",
|
|
|
|
CfServiceInstance: "testName",
|
|
|
|
CfServicePlan: "testPlan",
|
|
|
|
AbapSystemAdminEmail: "user@example.com",
|
|
|
|
AbapSystemID: "H02",
|
|
|
|
AbapSystemIsDevelopmentAllowed: true,
|
|
|
|
AbapSystemSizeOfPersistence: 4,
|
|
|
|
AbapSystemSizeOfRuntime: 4,
|
|
|
|
AddonDescriptorFileName: "addon.yml",
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:19:12 +02:00
|
|
|
dir := t.TempDir()
|
2021-01-18 12:14:55 +02:00
|
|
|
oldCWD, _ := os.Getwd()
|
|
|
|
_ = os.Chdir(dir)
|
|
|
|
// clean up tmp dir
|
|
|
|
defer func() {
|
|
|
|
_ = os.Chdir(oldCWD)
|
|
|
|
}()
|
|
|
|
|
|
|
|
addonYML := `addonProduct: myProduct
|
|
|
|
addonVersion: 1.2.3
|
|
|
|
repositories:
|
|
|
|
- name: '/DMO/REPO'
|
|
|
|
`
|
|
|
|
|
|
|
|
addonYMLBytes := []byte(addonYML)
|
2023-08-16 12:57:04 +02:00
|
|
|
err := os.WriteFile("addon.yml", addonYMLBytes, 0644)
|
2021-01-18 12:14:55 +02:00
|
|
|
|
2023-03-08 10:44:00 +02:00
|
|
|
expectedResult := "{\"admin_email\":\"user@example.com\",\"is_development_allowed\":true,\"sapsystemname\":\"H02\",\"size_of_persistence\":4,\"size_of_runtime\":4}"
|
2021-01-18 12:14:55 +02:00
|
|
|
|
2023-03-08 10:44:00 +02:00
|
|
|
result, err := generateServiceParameterString(&config)
|
2021-01-18 12:14:55 +02:00
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, expectedResult, result, "Result not as expected")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-03-08 10:44:00 +02:00
|
|
|
t.Run("Create service with addon - no development", func(t *testing.T) {
|
|
|
|
|
2021-01-18 12:14:55 +02:00
|
|
|
config := abapEnvironmentCreateSystemOptions{
|
|
|
|
CfAPIEndpoint: "https://api.endpoint.com",
|
|
|
|
CfOrg: "testOrg",
|
|
|
|
CfSpace: "testSpace",
|
|
|
|
Username: "testUser",
|
|
|
|
Password: "testPassword",
|
|
|
|
CfService: "testService",
|
|
|
|
CfServiceInstance: "testName",
|
|
|
|
CfServicePlan: "testPlan",
|
|
|
|
AbapSystemAdminEmail: "user@example.com",
|
|
|
|
AbapSystemID: "H02",
|
|
|
|
AbapSystemIsDevelopmentAllowed: false,
|
|
|
|
AbapSystemSizeOfPersistence: 4,
|
|
|
|
AbapSystemSizeOfRuntime: 4,
|
|
|
|
AddonDescriptorFileName: "addon.yml",
|
|
|
|
IncludeAddon: true,
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:19:12 +02:00
|
|
|
dir := t.TempDir()
|
2021-01-18 12:14:55 +02:00
|
|
|
oldCWD, _ := os.Getwd()
|
|
|
|
_ = os.Chdir(dir)
|
|
|
|
// clean up tmp dir
|
|
|
|
defer func() {
|
|
|
|
_ = os.Chdir(oldCWD)
|
|
|
|
}()
|
|
|
|
|
|
|
|
addonYML := `addonProduct: myProduct
|
|
|
|
addonVersion: 1.2.3
|
|
|
|
repositories:
|
|
|
|
- name: '/DMO/REPO'
|
|
|
|
`
|
|
|
|
|
|
|
|
addonYMLBytes := []byte(addonYML)
|
2023-08-16 12:57:04 +02:00
|
|
|
err := os.WriteFile("addon.yml", addonYMLBytes, 0644)
|
2021-01-18 12:14:55 +02:00
|
|
|
|
2023-03-08 10:44:00 +02:00
|
|
|
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\"}"
|
2021-01-18 12:14:55 +02:00
|
|
|
|
2023-03-08 10:44:00 +02:00
|
|
|
result, err := generateServiceParameterString(&config)
|
2021-01-18 12:14:55 +02:00
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, expectedResult, result, "Result not as expected")
|
|
|
|
}
|
|
|
|
})
|
2020-11-03 13:02:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type uuidMock struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *uuidMock) getUUID() string {
|
|
|
|
return "my-uuid"
|
|
|
|
}
|