github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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  func AddOrReplaceEnvironment(env []string, newEnvName string, newEnvVal string) []string {
    14  	var found bool
    15  	for i, envPair := range env {
    16  		splitENV := strings.Split(envPair, "=")
    17  		if splitENV[0] == newEnvName {
    18  			env[i] = fmt.Sprintf("%s=%s", newEnvName, newEnvVal)
    19  			found = true
    20  		}
    21  	}
    22  
    23  	if !found {
    24  		env = append(env, fmt.Sprintf("%s=%s", newEnvName, newEnvVal))
    25  	}
    26  	return env
    27  }
    28  
    29  func CheckEnvironmentTargetedCorrectly(targetedOrganizationRequired bool, targetedSpaceRequired bool, testOrg string, command ...string) {
    30  	LoginCF()
    31  
    32  	if targetedOrganizationRequired {
    33  		By("errors if org is not targeted")
    34  		session := CF(command...)
    35  		Eventually(session).Should(Say("FAILED"))
    36  		Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\."))
    37  		Eventually(session).Should(Exit(1))
    38  
    39  		if targetedSpaceRequired {
    40  			By("errors if space is not targeted")
    41  			TargetOrg(testOrg)
    42  			session := CF(command...)
    43  			Eventually(session).Should(Say("FAILED"))
    44  			Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\."))
    45  			Eventually(session).Should(Exit(1))
    46  		}
    47  	}
    48  
    49  	By("errors if user not logged in")
    50  	LogoutCF()
    51  	session := CF(command...)
    52  	Eventually(session).Should(Say("FAILED"))
    53  	Eventually(session.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\."))
    54  	Eventually(session).Should(Exit(1))
    55  
    56  	By("errors if cli not targeted")
    57  	UnsetAPI()
    58  	session = CF(command...)
    59  	Eventually(session).Should(Say("FAILED"))
    60  	Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
    61  	Eventually(session).Should(Exit(1))
    62  }