github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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  // CheckEnvironmentTargetedCorrectly will confirm if the command requires an
    30  // API to be targeted and logged in to run. It can optionally check if the
    31  // command requires org and space to be targeted.
    32  func CheckEnvironmentTargetedCorrectly(targetedOrganizationRequired bool, targetedSpaceRequired bool, testOrg string, command ...string) {
    33  	LoginCF()
    34  
    35  	if targetedOrganizationRequired {
    36  		By("errors if org is not targeted")
    37  		session := CF(command...)
    38  		Eventually(session).Should(Say("FAILED"))
    39  		Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\."))
    40  		Eventually(session).Should(Exit(1))
    41  
    42  		if targetedSpaceRequired {
    43  			By("errors if space is not targeted")
    44  			TargetOrg(testOrg)
    45  			session := CF(command...)
    46  			Eventually(session).Should(Say("FAILED"))
    47  			Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\."))
    48  			Eventually(session).Should(Exit(1))
    49  		}
    50  	}
    51  
    52  	By("errors if user not logged in")
    53  	LogoutCF()
    54  	session := CF(command...)
    55  	Eventually(session).Should(Say("FAILED"))
    56  	Eventually(session.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\."))
    57  	Eventually(session).Should(Exit(1))
    58  
    59  	By("errors if cli not targeted")
    60  	UnsetAPI()
    61  	session = CF(command...)
    62  	Eventually(session).Should(Say("FAILED"))
    63  	Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
    64  	Eventually(session).Should(Exit(1))
    65  }
    66  
    67  func UnrefactoredCheckEnvironmentTargetedCorrectly(targetedOrganizationRequired bool, targetedSpaceRequired bool, testOrg string, command ...string) {
    68  	LoginCF()
    69  
    70  	if targetedOrganizationRequired {
    71  		By("errors if org is not targeted")
    72  		session := CF(command...)
    73  		Eventually(session).Should(Say("FAILED"))
    74  		Eventually(session).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\."))
    75  		Eventually(session).Should(Exit(1))
    76  
    77  		if targetedSpaceRequired {
    78  			By("errors if space is not targeted")
    79  			TargetOrg(testOrg)
    80  			session := CF(command...)
    81  			Eventually(session).Should(Say("FAILED"))
    82  			Eventually(session).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\."))
    83  			Eventually(session).Should(Exit(1))
    84  		}
    85  	}
    86  
    87  	By("errors if user not logged in")
    88  	LogoutCF()
    89  	session := CF(command...)
    90  	Eventually(session).Should(Say("FAILED"))
    91  	Eventually(session).Should(Say("Not logged in\\. Use 'cf login' to log in\\."))
    92  	Eventually(session).Should(Exit(1))
    93  
    94  	By("errors if cli not targeted")
    95  	UnsetAPI()
    96  	session = CF(command...)
    97  	Eventually(session).Should(Say("FAILED"))
    98  	Eventually(session).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
    99  	Eventually(session).Should(Exit(1))
   100  }