github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/integration/helpers/version.go (about)

     1  package helpers
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"code.cloudfoundry.org/cli/actor/versioncheck"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/gexec"
    15  )
    16  
    17  func IsVersionMet(minVersion string) bool {
    18  	version := matchMajorAPIVersion(minVersion)
    19  	ok, err := versioncheck.IsMinimumAPIVersionMet(version, minVersion)
    20  	Expect(err).ToNot(HaveOccurred())
    21  
    22  	return ok
    23  }
    24  
    25  func matchMajorAPIVersion(minVersion string) string {
    26  	version := getAPIVersionV2()
    27  	if strings.HasPrefix(minVersion, "3") {
    28  		version = getAPIVersionV3()
    29  	}
    30  	return version
    31  }
    32  
    33  func SkipIfVersionLessThan(minVersion string) {
    34  	if ignoreAPIVersion() {
    35  		return
    36  	}
    37  
    38  	version := matchMajorAPIVersion(minVersion)
    39  	if !IsVersionMet(minVersion) {
    40  		Skip(fmt.Sprintf("minimum version %s not met by API version %s", minVersion, version))
    41  	}
    42  }
    43  
    44  func SkipIfVersionAtLeast(maxVersion string) {
    45  	version := matchMajorAPIVersion(maxVersion)
    46  
    47  	if IsVersionMet(maxVersion) {
    48  		Skip(fmt.Sprintf("maximum version %s exceeded by API version %s", maxVersion, version))
    49  	}
    50  }
    51  
    52  func ignoreAPIVersion() bool {
    53  	ignoreEnv := os.Getenv("CF_INT_IGNORE_API_VERSION_CHECK")
    54  	if ignoreEnv == "" {
    55  		return false
    56  	}
    57  
    58  	ignoreBool, err := strconv.ParseBool(ignoreEnv)
    59  	return ignoreBool || err != nil
    60  }
    61  
    62  type ccRoot struct {
    63  	Links struct {
    64  		CloudContollerV2 struct {
    65  			Meta struct {
    66  				Version string
    67  			}
    68  		} `json:"cloud_controller_v2"`
    69  
    70  		CloudContollerV3 struct {
    71  			Meta struct {
    72  				Version string
    73  			}
    74  		} `json:"cloud_controller_v3"`
    75  	} `json:"links"`
    76  }
    77  
    78  func getAPIVersionV2() string {
    79  	return fetchAPIVersion().Links.CloudContollerV2.Meta.Version
    80  }
    81  
    82  func getAPIVersionV3() string {
    83  	return fetchAPIVersion().Links.CloudContollerV3.Meta.Version
    84  }
    85  
    86  // TODO: Look into caching this
    87  func fetchAPIVersion() ccRoot {
    88  	session := CF("curl", "/")
    89  	Eventually(session).Should(Exit(0))
    90  
    91  	var cc ccRoot
    92  	err := json.Unmarshal(session.Out.Contents(), &cc)
    93  	Expect(err).ToNot(HaveOccurred())
    94  	return cc
    95  }