github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/integration/experimental/v3_apply_manifest_command_test.go (about) 1 package experimental 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "regexp" 8 9 "code.cloudfoundry.org/cli/integration/helpers" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/gbytes" 13 . "github.com/onsi/gomega/gexec" 14 . "github.com/onsi/gomega/ghttp" 15 ) 16 17 var _ = Describe("v3-apply-manifest command", func() { 18 var ( 19 orgName string 20 spaceName string 21 appName string 22 manifestPath string 23 appDir string 24 ) 25 26 BeforeEach(func() { 27 orgName = helpers.NewOrgName() 28 spaceName = helpers.NewSpaceName() 29 appName = helpers.PrefixedRandomName("app") 30 appDir, _ = ioutil.TempDir("", "simple-app") 31 manifestPath = filepath.Join(appDir, "manifest.yml") 32 // Ensure the file exists at the minimum 33 helpers.WriteManifest(manifestPath, map[string]interface{}{}) 34 }) 35 36 AfterEach(func() { 37 Expect(os.RemoveAll(appDir)).ToNot(HaveOccurred()) 38 }) 39 40 Describe("help", func() { 41 Context("when --help flag is set", func() { 42 It("displays command usage to output", func() { 43 session := helpers.CF("v3-apply-manifest", "--help") 44 45 Eventually(session).Should(Say("NAME:")) 46 Eventually(session).Should(Say("v3-apply-manifest - Applies manifest properties to an application")) 47 Eventually(session).Should(Say("USAGE:")) 48 Eventually(session).Should(Say("cf v3-apply-manifest -f APP_MANIFEST_PATH")) 49 50 Eventually(session).Should(Exit(0)) 51 }) 52 }) 53 }) 54 55 Context("when the -f flag is not given an arg", func() { 56 It("tells the user that the flag requires an arg, prints help text, and exits 1", func() { 57 session := helpers.CF("v3-apply-manifest", "-f") 58 59 Eventually(session.Err).Should(Say("Incorrect Usage: expected argument for flag `-f'")) 60 Eventually(session).Should(Say("NAME:")) 61 Eventually(session).Should(Exit(1)) 62 }) 63 }) 64 65 Context("when the -f flag path does not exist", func() { 66 It("tells the user that the provided path doesn't exist, prints help text, and exits 1", func() { 67 session := helpers.CF("v3-apply-manifest", "-f", "path/that/does/not/exist") 68 69 Eventually(session.Err).Should(Say("Incorrect Usage: The specified path 'path/that/does/not/exist' does not exist.")) 70 Eventually(session).Should(Say("NAME:")) 71 Eventually(session).Should(Exit(1)) 72 }) 73 }) 74 75 Context("when the environment is not setup correctly", func() { 76 Context("when no API endpoint is set", func() { 77 BeforeEach(func() { 78 helpers.UnsetAPI() 79 }) 80 81 It("fails with no API endpoint set message", func() { 82 session := helpers.CF("v3-apply-manifest", "-f", manifestPath) 83 Eventually(session).Should(Say("FAILED")) 84 Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\.")) 85 Eventually(session).Should(Exit(1)) 86 }) 87 }) 88 89 PContext("when the v3 api does not exist", func() { 90 var server *Server 91 92 BeforeEach(func() { 93 server = helpers.StartAndTargetServerWithoutV3API() 94 }) 95 96 AfterEach(func() { 97 server.Close() 98 }) 99 100 It("fails with error message that the minimum version is not met", func() { 101 session := helpers.CF("v3-apply-manifest", "-f", manifestPath) 102 Eventually(session).Should(Say("FAILED")) 103 Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\.")) 104 Eventually(session).Should(Exit(1)) 105 }) 106 }) 107 108 Context("when the v3 api version is lower than the minimum version", func() { 109 var server *Server 110 111 BeforeEach(func() { 112 server = helpers.StartAndTargetServerWithAPIVersions(helpers.DefaultV2Version, "3.0.0") 113 }) 114 115 AfterEach(func() { 116 server.Close() 117 }) 118 119 It("fails with error message that the minimum version is not met", func() { 120 session := helpers.CF("v3-apply-manifest", "-f", manifestPath) 121 Eventually(session).Should(Say("FAILED")) 122 Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\.")) 123 Eventually(session).Should(Exit(1)) 124 }) 125 }) 126 127 Context("when not logged in", func() { 128 BeforeEach(func() { 129 helpers.LogoutCF() 130 }) 131 132 It("fails with not logged in message", func() { 133 session := helpers.CF("v3-apply-manifest", "-f", manifestPath) 134 Eventually(session.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\.")) 135 Eventually(session).Should(Say("FAILED")) 136 Eventually(session).Should(Exit(1)) 137 }) 138 }) 139 140 Context("when there is no org set", func() { 141 BeforeEach(func() { 142 helpers.LogoutCF() 143 helpers.LoginCF() 144 }) 145 146 It("fails with no org targeted error message", func() { 147 session := helpers.CF("v3-apply-manifest", "-f", manifestPath) 148 Eventually(session).Should(Say("FAILED")) 149 Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\.")) 150 Eventually(session).Should(Exit(1)) 151 }) 152 }) 153 154 Context("when there is no space set", func() { 155 BeforeEach(func() { 156 helpers.LogoutCF() 157 helpers.LoginCF() 158 helpers.TargetOrg(ReadOnlyOrg) 159 }) 160 161 It("fails with no space targeted error message", func() { 162 session := helpers.CF("v3-apply-manifest", "-f", manifestPath) 163 Eventually(session).Should(Say("FAILED")) 164 Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\.")) 165 Eventually(session).Should(Exit(1)) 166 }) 167 }) 168 }) 169 170 Context("when the environment is set up correctly", func() { 171 BeforeEach(func() { 172 helpers.SetupCF(orgName, spaceName) 173 }) 174 175 AfterEach(func() { 176 helpers.QuickDeleteOrg(orgName) 177 }) 178 179 Context("when the app exists", func() { 180 BeforeEach(func() { 181 helpers.WithHelloWorldApp(func(appDir string) { 182 Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "v3-push", appName)).Should(Exit(0)) 183 }) 184 }) 185 186 Context("when the app name in the manifest is missing", func() { 187 BeforeEach(func() { 188 helpers.WriteManifest(manifestPath, map[string]interface{}{ 189 "applications": []map[string]interface{}{ 190 { 191 "instances": 3, 192 }, 193 }, 194 }) 195 }) 196 197 It("reports an error", func() { 198 session := helpers.CF("v3-apply-manifest", "-f", manifestPath) 199 Eventually(session.Err).Should(Say("Found an application with no name specified")) 200 Eventually(session).Should(Say("FAILED")) 201 Eventually(session).Should(Exit(1)) 202 }) 203 }) 204 205 Context("when the app name in the manifest doesn't exist", func() { 206 var invalidAppName string 207 BeforeEach(func() { 208 invalidAppName = "no-such-app" 209 helpers.WriteManifest(manifestPath, map[string]interface{}{ 210 "applications": []map[string]interface{}{ 211 { 212 "name": invalidAppName, 213 "instances": 3, 214 }, 215 }, 216 }) 217 }) 218 219 It("reports an error", func() { 220 session := helpers.CF("v3-apply-manifest", "-f", manifestPath) 221 Eventually(session.Err).Should(Say("App %s not found", invalidAppName)) 222 Eventually(session).Should(Say("FAILED")) 223 224 Eventually(session).Should(Exit(1)) 225 }) 226 }) 227 228 Context("when the app name in the manifest does exist", func() { 229 Context("when the instances value is negative", func() { 230 BeforeEach(func() { 231 helpers.WriteManifest(manifestPath, map[string]interface{}{ 232 "applications": []map[string]interface{}{ 233 { 234 "name": appName, 235 "instances": -1, 236 }, 237 }, 238 }) 239 }) 240 241 It("reports an error", func() { 242 session := helpers.CF("v3-apply-manifest", "-f", manifestPath) 243 Eventually(session.Err).Should(Say("Instances must be greater than or equal to 0")) 244 Eventually(session).Should(Say("FAILED")) 245 246 Eventually(session).Should(Exit(1)) 247 }) 248 }) 249 250 Context("when the instances value is more than the space quota limit", func() { 251 BeforeEach(func() { 252 Eventually(helpers.CF("create-space-quota", "some-space-quota-name", "-a", "4")).Should(Exit(0)) 253 Eventually(helpers.CF("set-space-quota", spaceName, "some-space-quota-name")).Should(Exit(0)) 254 helpers.WriteManifest(manifestPath, map[string]interface{}{ 255 "applications": []map[string]interface{}{ 256 { 257 "name": appName, 258 "instances": 5, 259 }, 260 }, 261 }) 262 }) 263 264 It("reports an error", func() { 265 session := helpers.CF("v3-apply-manifest", "-f", manifestPath) 266 Eventually(session.Err).Should(Say("memory space_quota_exceeded, app_instance_limit space_app_instance_limit_exceeded")) 267 Eventually(session).Should(Say("FAILED")) 268 269 Eventually(session).Should(Exit(1)) 270 }) 271 }) 272 273 Context("when instances are specified correctly", func() { 274 BeforeEach(func() { 275 helpers.WriteManifest(manifestPath, map[string]interface{}{ 276 "applications": []map[string]interface{}{ 277 { 278 "name": appName, 279 "instances": 3, 280 }, 281 }, 282 }) 283 }) 284 285 It("displays the experimental warning", func() { 286 session := helpers.CF("v3-apply-manifest", "-f", manifestPath) 287 Eventually(session.Err).Should(Say("This command is in EXPERIMENTAL stage and may change without notice")) 288 Eventually(session).Should(Exit()) 289 }) 290 291 It("rescales the app", func() { 292 session := helpers.CF("app", appName) 293 userName, _ := helpers.GetCredentials() 294 Eventually(session).Should(Say("instances:\\s+%s", "1/1")) 295 Eventually(session).Should(Exit()) 296 297 session = helpers.CF("v3-apply-manifest", "-f", manifestPath) 298 Eventually(session).Should(Say("Applying manifest %s in org %s / space %s as %s...", regexp.QuoteMeta(manifestPath), orgName, spaceName, userName)) 299 Eventually(session).Should(Exit()) 300 301 session = helpers.CF("app", appName) 302 Eventually(session).Should(Say("instances:\\s+%s", "3/3")) 303 Eventually(session).Should(Exit()) 304 }) 305 }) 306 }) 307 308 }) 309 }) 310 })