github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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  	"sync"
    13  
    14  	"code.cloudfoundry.org/cli/actor/versioncheck"
    15  
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gexec"
    19  )
    20  
    21  func IsVersionMet(minVersion string) bool {
    22  	version := matchMajorAPIVersion(minVersion)
    23  	ok, err := versioncheck.IsMinimumAPIVersionMet(version, minVersion)
    24  	Expect(err).ToNot(HaveOccurred())
    25  
    26  	return ok
    27  }
    28  
    29  type UAAVersion struct {
    30  	App struct {
    31  		Version string `json:"version"`
    32  	} `json:"app"`
    33  }
    34  
    35  func (v UAAVersion) Version() string {
    36  	return v.App.Version
    37  }
    38  
    39  func IsUAAVersionAtLeast(minVersion string) bool {
    40  	info := fetchAPIVersion()
    41  	uaaUrl := fmt.Sprintf("%s/info", info.Links.UAA.Href)
    42  	tr := &http.Transport{
    43  		TLSClientConfig: &tls.Config{InsecureSkipVerify: len(skipSSLValidation()) > 0},
    44  	}
    45  	req, _ := http.NewRequest("GET", uaaUrl, nil)
    46  	req.Header.Add("Accept", "application/json")
    47  	client := &http.Client{Transport: tr}
    48  	resp, err := client.Do(req)
    49  	Expect(err).ToNot(HaveOccurred())
    50  
    51  	defer resp.Body.Close()
    52  
    53  	if resp.StatusCode == http.StatusOK {
    54  		bodyBytes, err2 := ioutil.ReadAll(resp.Body)
    55  		Expect(err2).ToNot(HaveOccurred())
    56  
    57  		version := &UAAVersion{}
    58  
    59  		err3 := json.Unmarshal(bodyBytes, &version)
    60  		Expect(err3).ToNot(HaveOccurred())
    61  		currentUaaVersion := version.Version()
    62  		ok, err := versioncheck.IsMinimumAPIVersionMet(currentUaaVersion, minVersion)
    63  		Expect(err).ToNot(HaveOccurred())
    64  		return ok
    65  	}
    66  	Expect(resp.StatusCode).To(Equal(http.StatusOK))
    67  	return false
    68  }
    69  
    70  func SkipIfUAAVersionLessThan(version string) {
    71  	if !IsUAAVersionAtLeast(version) {
    72  		Skip(fmt.Sprintf("Test requires UAA version at least %s", version))
    73  	}
    74  }
    75  
    76  func SkipIfUAAVersionAtLeast(version string) {
    77  	if IsUAAVersionAtLeast(version) {
    78  		Skip(fmt.Sprintf("Test requires UAA version less than %s", version))
    79  	}
    80  }
    81  
    82  func matchMajorAPIVersion(minVersion string) string {
    83  	version := GetAPIVersionV2()
    84  	if strings.HasPrefix(minVersion, "3") {
    85  		version = getAPIVersionV3()
    86  	}
    87  	return version
    88  }
    89  
    90  func GetAPIVersionV2() string {
    91  	return fetchAPIVersion().Links.CloudContollerV2.Meta.Version
    92  }
    93  
    94  func SkipIfVersionLessThan(minVersion string) {
    95  	if ignoreAPIVersion() {
    96  		return
    97  	}
    98  
    99  	version := matchMajorAPIVersion(minVersion)
   100  	if !IsVersionMet(minVersion) {
   101  		Skip(fmt.Sprintf("minimum version %s not met by API version %s", minVersion, version))
   102  	}
   103  }
   104  
   105  func SkipIfVersionAtLeast(maxVersion string) {
   106  	version := matchMajorAPIVersion(maxVersion)
   107  
   108  	if IsVersionMet(maxVersion) {
   109  		Skip(fmt.Sprintf("maximum version %s exceeded by API version %s", maxVersion, version))
   110  	}
   111  }
   112  
   113  func ignoreAPIVersion() bool {
   114  	ignoreEnv := os.Getenv("CF_INT_IGNORE_API_VERSION_CHECK")
   115  	if ignoreEnv == "" {
   116  		return false
   117  	}
   118  
   119  	ignoreBool, err := strconv.ParseBool(ignoreEnv)
   120  	return ignoreBool || err != nil
   121  }
   122  
   123  type ccRoot struct {
   124  	Links struct {
   125  		CloudContollerV2 struct {
   126  			Meta struct {
   127  				Version string
   128  			}
   129  		} `json:"cloud_controller_v2"`
   130  
   131  		CloudContollerV3 struct {
   132  			Meta struct {
   133  				Version string
   134  			}
   135  		} `json:"cloud_controller_v3"`
   136  
   137  		UAA struct {
   138  			Href string `json:"href"`
   139  		} `json:"uaa"`
   140  	} `json:"links"`
   141  }
   142  
   143  var cacheLock sync.Mutex
   144  var CcRootCache *ccRoot
   145  
   146  func fetchAPIVersion() ccRoot {
   147  	cacheLock.Lock()
   148  	defer cacheLock.Unlock()
   149  	if CcRootCache == nil {
   150  		session := CF("curl", "/")
   151  		Eventually(session).Should(Exit(0))
   152  		var result ccRoot
   153  		err := json.Unmarshal(session.Out.Contents(), &result)
   154  		Expect(err).ToNot(HaveOccurred())
   155  		CcRootCache = &result
   156  	}
   157  	return *CcRootCache
   158  }
   159  
   160  func getAPIVersionV3() string {
   161  	return fetchAPIVersion().Links.CloudContollerV3.Meta.Version
   162  }
   163  
   164  func SkipIfNoRoutingAPI() {
   165  	// TODO: #161159794 remove this function and check a nicer error message when available
   166  	var response struct {
   167  		RoutingEndpoint string `json:"routing_endpoint"`
   168  	}
   169  	Curl(&response, "/v2/info")
   170  
   171  	if response.RoutingEndpoint == "" {
   172  		Skip("Test requires routing endpoint on /v2/info")
   173  	}
   174  }