github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/api_version_warning_test.go (about) 1 package command_test 2 3 import ( 4 "fmt" 5 "regexp" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 8 . "code.cloudfoundry.org/cli/command" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 "code.cloudfoundry.org/cli/util/ui" 11 "code.cloudfoundry.org/cli/version" 12 "github.com/blang/semver" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 ) 17 18 var _ = Describe("version checks", func() { 19 Describe("WarnIfCLIVersionBelowAPIDefinedMinimum", func() { 20 var ( 21 testUI *ui.UI 22 fakeConfig *commandfakes.FakeConfig 23 24 executeErr error 25 26 apiVersion string 27 minCLIVersion string 28 binaryVersion string 29 ) 30 31 BeforeEach(func() { 32 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 33 fakeConfig = new(commandfakes.FakeConfig) 34 }) 35 36 JustBeforeEach(func() { 37 executeErr = WarnIfCLIVersionBelowAPIDefinedMinimum(fakeConfig, apiVersion, testUI) 38 }) 39 40 When("checking the cloud controller minimum version warning", func() { 41 When("the CLI version is less than the recommended minimum", func() { 42 BeforeEach(func() { 43 apiVersion = ccversion.MinSupportedV2ClientVersion 44 minCLIVersion = "1.0.0" 45 fakeConfig.MinCLIVersionReturns(minCLIVersion) 46 binaryVersion = "0.0.0" 47 fakeConfig.BinaryVersionReturns(binaryVersion) 48 }) 49 50 It("displays a recommendation to update the CLI version", func() { 51 Expect(executeErr).ToNot(HaveOccurred()) 52 Expect(testUI.Err).To(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion)) 53 }) 54 }) 55 }) 56 57 When("the CLI version is greater or equal to the recommended minimum", func() { 58 BeforeEach(func() { 59 apiVersion = "100.200.3" 60 minCLIVersion = "1.0.0" 61 fakeConfig.MinCLIVersionReturns(minCLIVersion) 62 binaryVersion = "1.0.0" 63 fakeConfig.BinaryVersionReturns(binaryVersion) 64 }) 65 66 It("does not display a recommendation to update the CLI version", func() { 67 Expect(executeErr).ToNot(HaveOccurred()) 68 Expect(testUI.Err).NotTo(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion)) 69 }) 70 }) 71 72 When("an error is encountered while parsing the semver versions", func() { 73 BeforeEach(func() { 74 apiVersion = "100.200.3" 75 minCLIVersion = "1.0.0" 76 fakeConfig.MinCLIVersionReturns(minCLIVersion) 77 fakeConfig.BinaryVersionReturns("&#%") 78 }) 79 80 It("does not recommend to update the CLI version", func() { 81 Expect(executeErr).To(MatchError("No Major.Minor.Patch elements found")) 82 Expect(testUI.Err).NotTo(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion)) 83 }) 84 }) 85 86 Context("version contains string", func() { 87 BeforeEach(func() { 88 apiVersion = "100.200.3" 89 minCLIVersion = "1.0.0" 90 fakeConfig.MinCLIVersionReturns(minCLIVersion) 91 binaryVersion = "1.0.0-alpha.5" 92 fakeConfig.BinaryVersionReturns(binaryVersion) 93 }) 94 95 It("parses the versions successfully and recommends to update the CLI version", func() { 96 Expect(executeErr).ToNot(HaveOccurred()) 97 Expect(testUI.Err).To(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion)) 98 }) 99 }) 100 101 Context("minimum version is empty", func() { 102 BeforeEach(func() { 103 apiVersion = "100.200.3" 104 minCLIVersion = "" 105 fakeConfig.MinCLIVersionReturns(minCLIVersion) 106 binaryVersion = "1.0.0" 107 fakeConfig.BinaryVersionReturns(binaryVersion) 108 }) 109 110 It("does not return an error", func() { 111 Expect(executeErr).ToNot(HaveOccurred()) 112 }) 113 }) 114 115 When("comparing the default versions", func() { 116 BeforeEach(func() { 117 apiVersion = "100.200.3" 118 minCLIVersion = "1.2.3" 119 fakeConfig.MinCLIVersionReturns(minCLIVersion) 120 binaryVersion = version.DefaultVersion 121 fakeConfig.BinaryVersionReturns(binaryVersion) 122 }) 123 124 It("does not return an error or print a warning", func() { 125 Expect(executeErr).ToNot(HaveOccurred()) 126 }) 127 }) 128 }) 129 130 Describe("WarnIfAPIVersionBelowSupportedMinimum", func() { 131 var ( 132 testUI *ui.UI 133 134 apiVersion string 135 ) 136 137 BeforeEach(func() { 138 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 139 }) 140 141 When("checking the cloud controller minimum version warning", func() { 142 When("checking for outdated API version", func() { 143 When("the API version is older than the minimum supported API version", func() { 144 var min semver.Version 145 BeforeEach(func() { 146 var err error 147 min, err = semver.Make(ccversion.MinSupportedV2ClientVersion) 148 Expect(err).ToNot(HaveOccurred()) 149 apiVersion = fmt.Sprintf("%d.%d.%d", min.Major, min.Minor-1, min.Patch) 150 }) 151 152 It("outputs a warning telling the user to upgrade their CF version", func() { 153 err := WarnIfAPIVersionBelowSupportedMinimum(apiVersion, testUI) 154 Expect(err).ToNot(HaveOccurred()) 155 warning := regexp.QuoteMeta("Your CF API version (%s) is no longer supported. " + 156 "Upgrade to a newer version of the API (minimum version %s). Please refer to " + 157 "https://github.com/cloudfoundry/cli/wiki/Versioning-Policy#cf-cli-minimum-supported-version") 158 Expect(testUI.Err).To(Say(warning, apiVersion, min)) 159 }) 160 }) 161 162 When("the API version is newer than the minimum supported API version", func() { 163 BeforeEach(func() { 164 min, err := semver.Make(ccversion.MinSupportedV2ClientVersion) 165 Expect(err).ToNot(HaveOccurred()) 166 apiVersion = fmt.Sprintf("%d.%d.%d", min.Major, min.Minor+1, min.Patch) 167 }) 168 169 It("continues silently", func() { 170 err := WarnIfAPIVersionBelowSupportedMinimum(apiVersion, testUI) 171 Expect(err).ToNot(HaveOccurred()) 172 Expect(testUI.Err).NotTo(Say("Your CF API version .+ is no longer supported. Upgrade to a newer version of the API .+")) 173 }) 174 }) 175 }) 176 }) 177 }) 178 })