forked from cloudfoundry/cli
/
version.go
189 lines (158 loc) · 4.96 KB
/
version.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package helpers
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"sync"
"code.cloudfoundry.org/cli/actor/versioncheck"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)
// IsVersionMet verifies the targeted API meets the minimum required version
func IsVersionMet(minVersion string) bool {
version := matchMajorAPIVersion(minVersion)
ok, err := versioncheck.IsMinimumAPIVersionMet(version, minVersion)
Expect(err).ToNot(HaveOccurred())
return ok
}
// UAAVersion a struct representation of the UAA version
type UAAVersion struct {
App struct {
Version string `json:"version"`
} `json:"app"`
}
// Version returns the version of the targeted UAA
func (v UAAVersion) Version() string {
return v.App.Version
}
// IsUAAVersionAtLeast returns true if the UAA version >= minVersion, false otherwise.
func IsUAAVersionAtLeast(minVersion string) bool {
info := fetchAPIVersion()
uaaUrl := fmt.Sprintf("%s/info", info.Links.UAA.Href)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: SkipSSLValidation()},
}
req, err := http.NewRequest("GET", uaaUrl, nil)
Expect(err).ToNot(HaveOccurred())
req.Header.Add("Accept", "application/json")
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
Expect(err).ToNot(HaveOccurred())
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err2 := ioutil.ReadAll(resp.Body)
Expect(err2).ToNot(HaveOccurred())
version := &UAAVersion{}
err3 := json.Unmarshal(bodyBytes, &version)
Expect(err3).ToNot(HaveOccurred())
currentUaaVersion := version.Version()
ok, err := versioncheck.IsMinimumAPIVersionMet(currentUaaVersion, minVersion)
Expect(err).ToNot(HaveOccurred())
return ok
}
Expect(resp.StatusCode).To(Equal(http.StatusOK))
return false
}
// SkipIfUAAVersionLessThan is used to skip tests if the UAA version is < the specified version
func SkipIfUAAVersionLessThan(version string) {
if !IsUAAVersionAtLeast(version) {
Skip(fmt.Sprintf("Test requires UAA version at least %s", version))
}
}
// SkipIfUAAVersionAtLeast is used to skip tests if the UAA varsion >= the specified version.
func SkipIfUAAVersionAtLeast(version string) {
if IsUAAVersionAtLeast(version) {
Skip(fmt.Sprintf("Test requires UAA version less than %s", version))
}
}
func matchMajorAPIVersion(minVersion string) string {
version := GetAPIVersionV2()
if strings.HasPrefix(minVersion, "3") {
version = getAPIVersionV3()
}
return version
}
// GetAPIVersionV2 returns the V2 api version of the targeted API
func GetAPIVersionV2() string {
return fetchAPIVersion().Links.CloudContollerV2.Meta.Version
}
// SkipIfVersionLessThan is used to skip tests if the the API version < the specified version. If
// minVersion contains the prefix 3 then the v3 version is checked, otherwise the v2 version is used.
func SkipIfVersionLessThan(minVersion string) {
if ignoreAPIVersion() {
return
}
version := matchMajorAPIVersion(minVersion)
if !IsVersionMet(minVersion) {
Skip(fmt.Sprintf("minimum version %s not met by API version %s", minVersion, version))
}
}
// SkipIfVersionLessThan is used to skip tests if the the API version >= the specified version. If
// maxVersion contains the prefix 3 then the v3 version is checked, otherwise the v2 version is used.
func SkipIfVersionAtLeast(maxVersion string) {
version := matchMajorAPIVersion(maxVersion)
if IsVersionMet(maxVersion) {
Skip(fmt.Sprintf("maximum version %s exceeded by API version %s", maxVersion, version))
}
}
func ignoreAPIVersion() bool {
ignoreEnv := os.Getenv("CF_INT_IGNORE_API_VERSION_CHECK")
if ignoreEnv == "" {
return false
}
ignoreBool, err := strconv.ParseBool(ignoreEnv)
return ignoreBool || err != nil
}
type ccRoot struct {
Links struct {
CloudContollerV2 struct {
Meta struct {
Version string
}
} `json:"cloud_controller_v2"`
CloudContollerV3 struct {
Meta struct {
Version string
}
} `json:"cloud_controller_v3"`
UAA struct {
Href string `json:"href"`
} `json:"uaa"`
} `json:"links"`
}
var cacheLock sync.Mutex
// CcRootCache is a pointer to a cache of the CC root response
var CcRootCache *ccRoot
func fetchAPIVersion() ccRoot {
cacheLock.Lock()
defer cacheLock.Unlock()
if CcRootCache == nil {
session := CF("curl", "/")
Eventually(session).Should(Exit(0))
var result ccRoot
err := json.Unmarshal(session.Out.Contents(), &result)
Expect(err).ToNot(HaveOccurred())
CcRootCache = &result
}
return *CcRootCache
}
func getAPIVersionV3() string {
return fetchAPIVersion().Links.CloudContollerV3.Meta.Version
}
// SkipIfNoRoutingAPI is used to skip tests if the routing API is not present
func SkipIfNoRoutingAPI() {
// TODO: #161159794 remove this function and check a nicer error message when available
var response struct {
RoutingEndpoint string `json:"routing_endpoint"`
}
Curl(&response, "/v2/info")
if response.RoutingEndpoint == "" {
Skip("Test requires routing endpoint on /v2/info")
}
}