/
api.go
112 lines (97 loc) · 3.2 KB
/
api.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
package helpers
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/onsi/ginkgo"
"github.com/cloudfoundry-incubator/cf-test-helpers/cf"
"github.com/cloudfoundry-incubator/cf-test-helpers/workflowhelpers"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)
type APIResponse struct {
Pagination struct {
TotalResults int `json:"total_results"`
}
Resources []struct {
GUID string `json:"guid"`
Name string `json:"name"`
UserName string `json:"username"`
} `json:"resources"`
}
func apiCall(user workflowhelpers.UserContext, testConfig Config, endpoint string) *APIResponse {
var session *Session
var resp *APIResponse
workflowhelpers.AsUser(user, testConfig.BasicTimeout, func() {
session = cf.Cf("curl", "--fail", endpoint).Wait(testConfig.BasicTimeout)
Expect(session).To(Exit(0))
})
err := json.Unmarshal(session.Out.Contents(), &resp)
if err != nil {
return nil
}
return resp
}
func GetGUIDs(user workflowhelpers.UserContext, testConfig Config, endpoint string) []string {
var guids []string
resp := apiCall(user, testConfig, endpoint)
if resp != nil {
for _, item := range resp.Resources {
// do not select non-test resources (e.g. the default CF orgs or security groups)
name := item.Name
if name == "" {
name = item.UserName
}
if strings.HasPrefix(name, testConfig.GetNamePrefix()+"-") {
guids = append(guids, item.GUID)
}
}
}
return guids
}
func GetUserGUID(user workflowhelpers.UserContext, testConfig Config) string {
userGUIDs := GetGUIDs(user, testConfig, fmt.Sprintf("/v3/users?usernames=%s", user.Username))
if userGUIDs != nil {
Expect(len(userGUIDs)).To(Equal(1))
return userGUIDs[0]
}
return ""
}
func WaitToFail(user workflowhelpers.UserContext, testConfig Config, endpoint string) {
workflowhelpers.AsUser(user, testConfig.BasicTimeout, func() {
for exitCode := -1; exitCode <= 0; {
exitCode = cf.Cf("curl", "--fail", endpoint).Wait(testConfig.BasicTimeout).ExitCode()
}
})
}
func GetTotalResults(user workflowhelpers.UserContext, testConfig Config, endpoint string) int {
var totalResults int
resp := apiCall(user, testConfig, endpoint)
if resp != nil {
totalResults = resp.Pagination.TotalResults
}
return totalResults
}
func GetXRuntimeHeader(response []byte) float64 {
exp := regexp.MustCompile(`X-Runtime: (\d+.?\d+)`)
matches := exp.FindSubmatch(response)
ExpectWithOffset(1, matches).ToNot(BeEmpty(), "Response did not contain X-Runtime header")
runtime, err := strconv.ParseFloat(string(matches[1]), 64)
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "Runtime header could not be parsed from string to float64")
return runtime
}
func TimeCFCurl(b ginkgo.Benchmarker, timeout time.Duration, curlArguments ...string) {
exitCode, _ := TimeCFCurlReturning(b, timeout, curlArguments...)
Expect(exitCode).To(Equal(0))
}
func TimeCFCurlReturning(b ginkgo.Benchmarker, timeout time.Duration, curlArguments ...string) (int, []byte) {
var args = []string{"curl", "--fail", "-v"}
args = append(args, curlArguments...)
result := cf.Cf(args...).Wait(timeout)
runtime := GetXRuntimeHeader(result.Out.Contents())
b.RecordValue("request time", runtime)
return result.ExitCode(), result.Out.Contents()
}