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