github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/integration/helpers/version.go (about) 1 package helpers 2 3 import ( 4 "crypto/tls" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "os" 10 "strconv" 11 "strings" 12 13 "code.cloudfoundry.org/cli/actor/versioncheck" 14 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 . "github.com/onsi/gomega/gexec" 18 ) 19 20 func IsVersionMet(minVersion string) bool { 21 version := matchMajorAPIVersion(minVersion) 22 ok, err := versioncheck.IsMinimumAPIVersionMet(version, minVersion) 23 Expect(err).ToNot(HaveOccurred()) 24 25 return ok 26 } 27 28 type UAAVersion struct { 29 App struct { 30 Version string `json:"version"` 31 } `json:"app"` 32 } 33 34 func (v UAAVersion) Version() string { 35 return v.App.Version 36 } 37 38 func IsUAAVersionAtLeast(minVersion string) bool { 39 info := fetchAPIVersion() 40 uaaUrl := fmt.Sprintf("%s/info", info.Links.UAA.Href) 41 tr := &http.Transport{ 42 TLSClientConfig: &tls.Config{InsecureSkipVerify: len(skipSSLValidation()) > 0}, 43 } 44 req, _ := http.NewRequest("GET", uaaUrl, nil) 45 req.Header.Add("Accept", "application/json") 46 client := &http.Client{Transport: tr} 47 resp, err := client.Do(req) 48 Expect(err).ToNot(HaveOccurred()) 49 50 defer resp.Body.Close() 51 52 if resp.StatusCode == http.StatusOK { 53 bodyBytes, err2 := ioutil.ReadAll(resp.Body) 54 Expect(err2).ToNot(HaveOccurred()) 55 56 version := &UAAVersion{} 57 58 err3 := json.Unmarshal(bodyBytes, &version) 59 Expect(err3).ToNot(HaveOccurred()) 60 currentUaaVersion := version.Version() 61 ok, err := versioncheck.IsMinimumAPIVersionMet(currentUaaVersion, minVersion) 62 Expect(err).ToNot(HaveOccurred()) 63 return ok 64 } 65 Expect(resp.StatusCode).To(Equal(http.StatusOK)) 66 return false 67 } 68 69 func SkipIfUAAVersionLessThan(version string) { 70 if !IsUAAVersionAtLeast(version) { 71 Skip(fmt.Sprintf("Test requires UAA version at least %s", version)) 72 } 73 } 74 75 func SkipIfUAAVersionAtLeast(version string) { 76 if IsUAAVersionAtLeast(version) { 77 Skip(fmt.Sprintf("Test requires UAA version less than %s", version)) 78 } 79 } 80 81 func matchMajorAPIVersion(minVersion string) string { 82 version := GetAPIVersionV2() 83 if strings.HasPrefix(minVersion, "3") { 84 version = getAPIVersionV3() 85 } 86 return version 87 } 88 89 func GetAPIVersionV2() string { 90 return fetchAPIVersion().Links.CloudContollerV2.Meta.Version 91 } 92 93 func SkipIfVersionLessThan(minVersion string) { 94 if ignoreAPIVersion() { 95 return 96 } 97 98 version := matchMajorAPIVersion(minVersion) 99 if !IsVersionMet(minVersion) { 100 Skip(fmt.Sprintf("minimum version %s not met by API version %s", minVersion, version)) 101 } 102 } 103 104 func SkipIfVersionAtLeast(maxVersion string) { 105 version := matchMajorAPIVersion(maxVersion) 106 107 if IsVersionMet(maxVersion) { 108 Skip(fmt.Sprintf("maximum version %s exceeded by API version %s", maxVersion, version)) 109 } 110 } 111 112 func ignoreAPIVersion() bool { 113 ignoreEnv := os.Getenv("CF_INT_IGNORE_API_VERSION_CHECK") 114 if ignoreEnv == "" { 115 return false 116 } 117 118 ignoreBool, err := strconv.ParseBool(ignoreEnv) 119 return ignoreBool || err != nil 120 } 121 122 type ccRoot struct { 123 Links struct { 124 CloudContollerV2 struct { 125 Meta struct { 126 Version string 127 } 128 } `json:"cloud_controller_v2"` 129 130 CloudContollerV3 struct { 131 Meta struct { 132 Version string 133 } 134 } `json:"cloud_controller_v3"` 135 136 UAA struct { 137 Href string `json:"href"` 138 } `json:"uaa"` 139 } `json:"links"` 140 } 141 142 func getAPIVersionV3() string { 143 return fetchAPIVersion().Links.CloudContollerV3.Meta.Version 144 } 145 146 // TODO: Look into caching this 147 func fetchAPIVersion() ccRoot { 148 session := CF("curl", "/") 149 Eventually(session).Should(Exit(0)) 150 151 var cc ccRoot 152 err := json.Unmarshal(session.Out.Contents(), &cc) 153 Expect(err).ToNot(HaveOccurred()) 154 return cc 155 }