github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/helpers/environment.go (about) 1 package helpers 2 3 import ( 4 "fmt" 5 "strings" 6 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/gbytes" 10 . "github.com/onsi/gomega/gexec" 11 ) 12 13 // AddOrReplaceEnvironment will update environment if it already exists or will add 14 // a new environment with the given environment name and details. 15 func AddOrReplaceEnvironment(env []string, newEnvName string, newEnvVal string) []string { 16 var found bool 17 for i, envPair := range env { 18 splitENV := strings.Split(envPair, "=") 19 if splitENV[0] == newEnvName { 20 env[i] = fmt.Sprintf("%s=%s", newEnvName, newEnvVal) 21 found = true 22 } 23 } 24 25 if !found { 26 env = append(env, fmt.Sprintf("%s=%s", newEnvName, newEnvVal)) 27 } 28 return env 29 } 30 31 func CheckSpaceAndOrgTargetedCorrectly(command ...string) { 32 LoginCF() 33 34 By("errors if org and space are not targeted") 35 session := CF(command...) 36 Eventually(session).Should(Say("FAILED")) 37 Eventually(session.Out).Should(Say("No org and space targeted, use 'cf target -o ORG -s SPACE' to target an org and space")) 38 Eventually(session).Should(Exit(1)) 39 } 40 41 // CheckEnvironmentTargetedCorrectly will confirm if the command requires an 42 // API to be targeted and logged in to run. It can optionally check if the 43 // command requires org and space to be targeted. 44 func CheckEnvironmentTargetedCorrectly(targetedOrganizationRequired bool, targetedSpaceRequired bool, testOrg string, command ...string) { 45 LoginCF() 46 47 if targetedOrganizationRequired { 48 By("errors if org is not targeted") 49 session := CF(command...) 50 Eventually(session).Should(Say("FAILED")) 51 Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\.")) 52 Eventually(session).Should(Exit(1)) 53 54 if targetedSpaceRequired { 55 By("errors if space is not targeted") 56 TargetOrg(testOrg) 57 session := CF(command...) 58 Eventually(session).Should(Say("FAILED")) 59 Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\.")) 60 Eventually(session).Should(Exit(1)) 61 } 62 } 63 64 By("errors if user not logged in") 65 LogoutCF() 66 session := CF(command...) 67 Eventually(session).Should(Say("FAILED")) 68 Eventually(session.Err).Should(Say("Not logged in\\. Use 'cf login' or 'cf login --sso' to log in\\.")) 69 Eventually(session).Should(Exit(1)) 70 71 By("errors if cli not targeted") 72 UnsetAPI() 73 session = CF(command...) 74 Eventually(session).Should(Say("FAILED")) 75 Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\.")) 76 Eventually(session).Should(Exit(1)) 77 } 78 79 // UnrefactoredCheckEnvironmentTargetedCorrectly will confirm if the command requires an 80 // API to be targeted and logged in to run. It can optionally check if the 81 // command requires org and space to be targeted. 82 func UnrefactoredCheckEnvironmentTargetedCorrectly(targetedOrganizationRequired bool, targetedSpaceRequired bool, testOrg string, command ...string) { 83 LoginCF() 84 85 if targetedOrganizationRequired { 86 By("errors if org is not targeted") 87 session := CF(command...) 88 Eventually(session).Should(Say("FAILED")) 89 Eventually(session).Should(Say("No org and space targeted, use 'cf target -o ORG -s SPACE' to target an org and space")) 90 Eventually(session).Should(Exit(1)) 91 92 if targetedSpaceRequired { 93 By("errors if space is not targeted") 94 TargetOrg(testOrg) 95 session := CF(command...) 96 Eventually(session).Should(Say("FAILED")) 97 Eventually(session).Should(Say("No space targeted, use 'cf target -s' to target a space\\.")) 98 Eventually(session).Should(Exit(1)) 99 } 100 } 101 102 By("errors if user not logged in") 103 LogoutCF() 104 session := CF(command...) 105 Eventually(session).Should(Say("FAILED")) 106 Eventually(session).Should(Say("Not logged in\\. Use 'cf login' or 'cf login --sso' to log in\\.")) 107 Eventually(session).Should(Exit(1)) 108 109 By("errors if cli not targeted") 110 UnsetAPI() 111 session = CF(command...) 112 Eventually(session).Should(Say("FAILED")) 113 Eventually(session).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\.")) 114 Eventually(session).Should(Exit(1)) 115 }