/
user_context.go
161 lines (132 loc) · 5 KB
/
user_context.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package workflowhelpers
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/cloudfoundry-incubator/cf-test-helpers/commandstarter"
"github.com/cloudfoundry-incubator/cf-test-helpers/internal"
workflowhelpersinternal "github.com/cloudfoundry-incubator/cf-test-helpers/workflowhelpers/internal"
"github.com/onsi/ginkgo"
ginkgoconfig "github.com/onsi/ginkgo/config"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
)
type userValues interface {
Username() string
Password() string
}
type spaceValues interface {
OrganizationName() string
SpaceName() string
}
type UserContext struct {
ApiUrl string
TestSpace spaceValues
TestUser userValues
SkipSSLValidation bool
CommandStarter internal.Starter
Timeout time.Duration
// the followings are left around for CATS to use
Username string
Password string
Org string
Space string
UseClientCredentials bool
}
func cliErrorMessage(session *Session) string {
var command string
if strings.EqualFold(session.Command.Args[1], "auth") {
command = strings.Join(session.Command.Args[:2], " ")
} else {
command = strings.Join(session.Command.Args, " ")
}
return fmt.Sprintf("\n>>> [ %s ] exited with an error \n", command)
}
func apiErrorMessage(session *Session) string {
apiEndpoint := strings.Join(session.Command.Args, " ")
stdError := string(session.Err.Contents())
return fmt.Sprintf("\n>>> [ %s ] exited with an error \n\n%s\n", apiEndpoint, stdError)
}
func NewUserContext(apiUrl string, testUser userValues, testSpace spaceValues, skipSSLValidation bool, timeout time.Duration) UserContext {
var org, space string
if testSpace != nil {
org = testSpace.OrganizationName()
space = testSpace.SpaceName()
}
return UserContext{
ApiUrl: apiUrl,
Username: testUser.Username(),
Password: testUser.Password(),
TestSpace: testSpace,
TestUser: testUser,
Org: org,
Space: space,
SkipSSLValidation: skipSSLValidation,
CommandStarter: commandstarter.NewCommandStarter(),
Timeout: timeout,
}
}
func (uc UserContext) Login() {
args := []string{"api", uc.ApiUrl}
if uc.SkipSSLValidation {
args = append(args, "--skip-ssl-validation")
}
session := internal.Cf(uc.CommandStarter, args...).Wait(uc.Timeout)
EventuallyWithOffset(1, session, uc.Timeout).Should(Exit(0), apiErrorMessage(session))
redactor := internal.NewRedactor(uc.TestUser.Password())
redactingReporter := internal.NewRedactingReporter(ginkgo.GinkgoWriter, redactor)
var err error
if uc.UseClientCredentials {
err = workflowhelpersinternal.CfClientAuth(uc.CommandStarter, redactingReporter, uc.TestUser.Username(), uc.TestUser.Password(), uc.Timeout)
} else {
err = workflowhelpersinternal.CfAuth(uc.CommandStarter, redactingReporter, uc.TestUser.Username(), uc.TestUser.Password(), uc.Timeout)
}
Expect(err).NotTo(HaveOccurred())
}
func (uc UserContext) SetCfHomeDir() (string, string) {
originalCfHomeDir := os.Getenv("CF_HOME")
currentCfHomeDir, err := ioutil.TempDir("", fmt.Sprintf("cf_home_%d", ginkgoconfig.GinkgoConfig.ParallelNode))
if err != nil {
panic("Error: could not create temporary home directory: " + err.Error())
}
os.Setenv("CF_HOME", currentCfHomeDir)
return originalCfHomeDir, currentCfHomeDir
}
func (uc UserContext) TargetSpace() {
if uc.TestSpace != nil && uc.TestSpace.OrganizationName() != "" {
var session *Session
session = internal.Cf(uc.CommandStarter, "target", "-o", uc.TestSpace.OrganizationName(), "-s", uc.TestSpace.SpaceName())
EventuallyWithOffset(1, session, uc.Timeout).Should(Exit(0), cliErrorMessage(session))
}
}
func (uc UserContext) AddUserToSpace() {
username := uc.TestUser.Username()
orgName := uc.TestSpace.OrganizationName()
spaceName := uc.TestSpace.SpaceName()
spaceManager := internal.Cf(uc.CommandStarter, "set-space-role", username, orgName, spaceName, "SpaceManager")
EventuallyWithOffset(1, spaceManager, uc.Timeout).Should(Exit())
if spaceManager.ExitCode() != 0 {
ExpectWithOffset(1, spaceManager.Out).Should(gbytes.Say("not authorized"))
}
spaceDeveloper := internal.Cf(uc.CommandStarter, "set-space-role", username, orgName, spaceName, "SpaceDeveloper")
EventuallyWithOffset(1, spaceDeveloper, uc.Timeout).Should(Exit())
if spaceDeveloper.ExitCode() != 0 {
ExpectWithOffset(1, spaceDeveloper.Out).Should(gbytes.Say("not authorized"))
}
spaceAuditor := internal.Cf(uc.CommandStarter, "set-space-role", username, orgName, spaceName, "SpaceAuditor")
EventuallyWithOffset(1, spaceAuditor, uc.Timeout).Should(Exit())
if spaceAuditor.ExitCode() != 0 {
ExpectWithOffset(1, spaceAuditor.Out).Should(gbytes.Say("not authorized"))
}
}
func (uc UserContext) Logout() {
session := internal.Cf(uc.CommandStarter, "logout")
EventuallyWithOffset(1, session, uc.Timeout).Should(Exit(0), cliErrorMessage(session))
}
func (uc UserContext) UnsetCfHomeDir(originalCfHomeDir, currentCfHomeDir string) {
os.Setenv("CF_HOME", originalCfHomeDir)
os.RemoveAll(currentCfHomeDir)
}