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