1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/cloudFoundryCreateService_test.go

228 lines
9.4 KiB
Go
Raw Normal View History

Implementing cfCreateService in Golang (#1649) * Adapted documentation * Adapted documentation * Adapted documentation * Adapted documentation * Adapted documentation * Added CFDeleteServiceKeys * Added ServiceKey deletion tests * added cfServiceKeys flag explanation to documentation * removed trailing spaces from documentation * resolving conflicts * Changed deletion message an variable naming * Changed tests * Changed tests * Changed tests * Changed tests * Changed CloudFoundryDeleteServiceOptions to options * Changed CloudFoundryDeleteServiceOptions to options * Minor changes * Minor changes * Changed variable naming * Changed error handling * Changed error handling and logging * Changed documentation * Simplified code * Fixed CodeClimate issues * Changed from returning err to nil where no errur returned needed * Add cloudFoundryCreateServiceKey Go Step * Changed Groovy File * Changed aliases * Removed unneccessary parts * Minor changes * Minor changes * Adapted documentation * Adapted tests * Adapted Groovy File * Changed Groovy file * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Removed Groovy Tests for cfCreateServiceKey * Minor changes * Added ATC Check YAML * Added ATC Check generated files * Added test class * Added abapEnvironmentRunATCCheck * Minor changes * Minor changes * Changed groovy * Minor changes * Changed groovy * Changed groovy * Minor changes * Adapted Groovy imports * Adapted Groovy imports * Adapted Groovy imports * Adapted Groovy * Getting ATC results * Changed error message * changed groovy * removed trailing spaces * Added login check * Minor changes * Added step to whitelistScriptReference * Added ATC error message handling * Added groovy file * Added step to groovy tests * corrected metadata file * Debugging * Debugging * Added yaml config parameter for ATC run * Adapted file location of ATC run config to jenkins specific location * Implementing universal pipeline logic for finding yaml config regardless of pipeline * Changed error handling for reading config yaml file * Changed atcrunconfig alias * minor changes * Minor changes * Minor changes * Changed back to dynamic file reading * Minor changes * filepath changes * Removing CF Login * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Minor changes * Removed whitespaces * Added CF functions unit tests * Added invalid parameter handling * Removed package and SC flag * Minor changes * Changed tests * Changed tests * Changed tests * Minor changes * Changed tests * removed unnecessary logout * Added documentation * Changed docu * Changed docu * Changed docu * Changed docu * Changed docu * Changed docu * Changed docu * Changed docu * Changed docu * Changed docu * Removed trailing spaces * Added newline at end of file * code climate fixes * code climate fixes * code climate fixes * Minor changes * Minor changes * Minor changes * Changed tests * Test changes * Splitted Cloud Foundry functions into two classes * Removed two steps from whtielistScriptReference * removed atcrunConfig alias * issue fixes * Changed docu * Changed docu * Changed docu * Removed trailing spaced from docu * Changed docu * Go generator run * Issue fixes * Remove unnecessary imports Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * Update whitelistScript Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * Adding piperutils for writing xml file * Persisting ATC Results with piperutils * Set failonMissingReports to true * Refactoring for CodeClimate * Changed result file name * Changed credentials aliases * changing secret name * Removing trailing spaces * Added secret name and alias to docu * PR commit * Go generator * Code Climate fixes * Code Climate fixes * Code Climate fixes * Removed existing groovy tests * Added cfCreateService to fieldRelatedWhiteList * Remarks * added file checking * tests adapted * Changed to execRunner * Removed workingDir definition * Removed workingDir definition * Removed workingDir definition * Added default * Added aliases * Changing to CFUtils Exec Runner * Change defaults * Removed trailing spaces * Refactoring and test changes * Added manifest file default & re-arranged defer func * TestFiles creation added * Changed alias values * Corrected defer logout err return Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com> Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> Co-authored-by: Daniel Mieg <56156797+DanielMieg@users.noreply.github.com>
2020-08-19 10:17:27 +02:00
package cmd
import (
"io/ioutil"
"os"
"testing"
"github.com/SAP/jenkins-library/pkg/cloudfoundry"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/stretchr/testify/assert"
)
func cfMockCleanup(m *mock.ExecMockRunner) {
m.ShouldFailOnCommand = map[string]error{}
m.StdoutReturn = map[string]string{}
m.Calls = []mock.ExecCall{}
}
func TestCloudFoundryCreateService(t *testing.T) {
m := &mock.ExecMockRunner{}
cf := cloudfoundry.CFUtils{Exec: m}
var telemetryData telemetry.CustomData
t.Run("Create service: no broker, no config, no tags", func(t *testing.T) {
defer cfMockCleanup(m)
config := cloudFoundryCreateServiceOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
CfService: "testService",
CfServiceInstanceName: "testName",
CfServicePlan: "testPlan",
}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
if assert.NoError(t, error) {
assert.Equal(t, []mock.ExecCall{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"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
m.Calls)
}
})
t.Run("Create service: only tags", func(t *testing.T) {
defer cfMockCleanup(m)
config := cloudFoundryCreateServiceOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
CfService: "testService",
CfServiceInstanceName: "testName",
CfServicePlan: "testPlan",
CfServiceTags: "testTag, testTag2",
}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
if assert.NoError(t, error) {
assert.Equal(t, []mock.ExecCall{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"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "-t", "testTag, testTag2"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
m.Calls)
}
})
t.Run("Create service: only broker", func(t *testing.T) {
defer cfMockCleanup(m)
config := cloudFoundryCreateServiceOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
CfService: "testService",
CfServiceInstanceName: "testName",
CfServicePlan: "testPlan",
CfServiceBroker: "testBroker",
}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
if assert.NoError(t, error) {
assert.Equal(t, []mock.ExecCall{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"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "-b", "testBroker"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
m.Calls)
}
})
t.Run("Create service: only config", func(t *testing.T) {
defer cfMockCleanup(m)
config := cloudFoundryCreateServiceOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
CfService: "testService",
CfServiceInstanceName: "testName",
CfServicePlan: "testPlan",
CfCreateServiceConfig: "testConfig.json",
}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
if assert.NoError(t, error) {
assert.Equal(t, []mock.ExecCall{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"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "-c", "testConfig.json"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
m.Calls)
}
})
t.Run("Create service: failure, no config", func(t *testing.T) {
defer cfMockCleanup(m)
config := cloudFoundryCreateServiceOptions{}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
assert.EqualError(t, error, "Error while logging in: Failed to login to Cloud Foundry: Parameters missing. Please provide the Cloud Foundry Endpoint, Org, Space, Username and Password")
})
t.Run("Create service: variable substitution in-line", func(t *testing.T) {
defer cfMockCleanup(m)
dir, err := ioutil.TempDir("", "test variable substitution")
if err != nil {
t.Fatal("Failed to create temporary directory")
}
oldCWD, _ := os.Getwd()
_ = os.Chdir(dir)
// clean up tmp dir
defer func() {
_ = os.Chdir(oldCWD)
_ = os.RemoveAll(dir)
}()
manifestFileString := `
---
create-services:
- name: ((name))
broker: "testBroker"
plan: "testPlan"
- name: ((name2))
broker: "testBroker"
plan: "testPlan"
- name: "test3"
broker: "testBroker"
plan: "testPlan"`
manifestFileStringBody := []byte(manifestFileString)
err = ioutil.WriteFile("manifestTest.yml", manifestFileStringBody, 0644)
var manifestVariables = []string{"name1=Test1", "name2=Test2"}
config := cloudFoundryCreateServiceOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
ServiceManifest: "manifestTest.yml",
ManifestVariables: manifestVariables,
}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
if assert.NoError(t, error) {
assert.Equal(t, []mock.ExecCall{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"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "manifestTest.yml", "--var", "name1=Test1", "--var", "name2=Test2"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
m.Calls)
}
})
t.Run("Create service: variable substitution with variable substitution manifest file", func(t *testing.T) {
defer cfMockCleanup(m)
dir, err := ioutil.TempDir("", "test variable substitution")
if err != nil {
t.Fatal("Failed to create temporary directory")
}
oldCWD, _ := os.Getwd()
_ = os.Chdir(dir)
// clean up tmp dir
defer func() {
_ = os.Chdir(oldCWD)
_ = os.RemoveAll(dir)
}()
varsFileString := `name: test1
name2: test2`
manifestFileString := `
---
create-services:
- name: ((name))
broker: "testBroker"
plan: "testPlan"
- name: ((name2))
broker: "testBroker"
plan: "testPlan"
- name: "test3"
broker: "testBroker"
plan: "testPlan"`
varsFileStringBody := []byte(varsFileString)
manifestFileStringBody := []byte(manifestFileString)
err = ioutil.WriteFile("varsTest.yml", varsFileStringBody, 0644)
err = ioutil.WriteFile("varsTest2.yml", varsFileStringBody, 0644)
err = ioutil.WriteFile("manifestTest.yml", manifestFileStringBody, 0644)
var manifestVariablesFiles = []string{"varsTest.yml", "varsTest2.yml"}
config := cloudFoundryCreateServiceOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
ServiceManifest: "manifestTest.yml",
ManifestVariablesFiles: manifestVariablesFiles,
}
error := runCloudFoundryCreateService(&config, &telemetryData, cf)
if assert.NoError(t, error) {
assert.Equal(t, []mock.ExecCall{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"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "manifestTest.yml", "--vars-file", "varsTest.yml", "--vars-file", "varsTest2.yml"}},
mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}},
m.Calls)
}
})
}