github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/experimental/v3_create_package_command_test.go (about) 1 package experimental 2 3 import ( 4 "io/ioutil" 5 "os" 6 "regexp" 7 8 "code.cloudfoundry.org/cli/integration/helpers" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/gbytes" 12 . "github.com/onsi/gomega/gexec" 13 . "github.com/onsi/gomega/ghttp" 14 ) 15 16 var _ = Describe("v3-create-package command", func() { 17 var ( 18 orgName string 19 spaceName string 20 appName string 21 ) 22 23 BeforeEach(func() { 24 orgName = helpers.NewOrgName() 25 spaceName = helpers.NewSpaceName() 26 appName = helpers.PrefixedRandomName("app") 27 }) 28 29 Describe("help", func() { 30 Context("when --help flag is set", func() { 31 It("Displays command usage to output", func() { 32 session := helpers.CF("v3-create-package", "--help") 33 Eventually(session).Should(Say("NAME:")) 34 Eventually(session).Should(Say("v3-create-package - Uploads a V3 Package")) 35 Eventually(session).Should(Say("USAGE:")) 36 Eventually(session).Should(Say("cf v3-create-package APP_NAME \\[-p APP_PATH \\| --docker-image \\[REGISTRY_HOST:PORT/\\]IMAGE\\[:TAG\\]\\]")) 37 Eventually(session).Should(Say("OPTIONS:")) 38 Eventually(session).Should(Say("--docker-image, -o\\s+Docker image to use \\(e\\.g\\. user/docker-image-name\\)")) 39 Eventually(session).Should(Say("-p\\s+Path to app directory or to a zip file of the contents of the app directory")) 40 Eventually(session).Should(Exit(0)) 41 }) 42 }) 43 }) 44 45 Context("when the app name is not provided", func() { 46 It("tells the user that the app name is required, prints help text, and exits 1", func() { 47 session := helpers.CF("v3-create-package") 48 49 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided")) 50 Eventually(session).Should(Say("NAME:")) 51 Eventually(session).Should(Exit(1)) 52 }) 53 }) 54 55 It("displays the experimental warning", func() { 56 session := helpers.CF("v3-create-package", appName) 57 Eventually(session).Should(Say("This command is in EXPERIMENTAL stage and may change without notice")) 58 Eventually(session).Should(Exit()) 59 }) 60 61 Context("when the -p flag is not given an arg", func() { 62 It("tells the user that the flag requires an arg, prints help text, and exits 1", func() { 63 session := helpers.CF("v3-create-package", appName, "-p") 64 65 Eventually(session.Err).Should(Say("Incorrect Usage: expected argument for flag `-p'")) 66 Eventually(session).Should(Say("NAME:")) 67 Eventually(session).Should(Exit(1)) 68 }) 69 }) 70 71 Context("when the -p flag path does not exist", func() { 72 It("tells the user that the flag requires an arg, prints help text, and exits 1", func() { 73 session := helpers.CF("v3-create-package", appName, "-p", "path/that/does/not/exist") 74 75 Eventually(session.Err).Should(Say("Incorrect Usage: The specified path 'path/that/does/not/exist' does not exist.")) 76 Eventually(session).Should(Say("NAME:")) 77 Eventually(session).Should(Exit(1)) 78 }) 79 }) 80 81 Context("when the environment is not setup correctly", func() { 82 Context("when no API endpoint is set", func() { 83 BeforeEach(func() { 84 helpers.UnsetAPI() 85 }) 86 87 It("fails with no API endpoint set message", func() { 88 session := helpers.CF("v3-create-package", appName) 89 Eventually(session).Should(Say("FAILED")) 90 Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint.")) 91 Eventually(session).Should(Exit(1)) 92 }) 93 }) 94 95 Context("when the v3 api does not exist", func() { 96 var server *Server 97 98 BeforeEach(func() { 99 server = helpers.StartAndTargetServerWithoutV3API() 100 }) 101 102 AfterEach(func() { 103 server.Close() 104 }) 105 106 It("fails with error message that the minimum version is not met", func() { 107 session := helpers.CF("v3-create-package", appName) 108 Eventually(session).Should(Say("FAILED")) 109 Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\.")) 110 Eventually(session).Should(Exit(1)) 111 }) 112 }) 113 114 Context("when the v3 api version is lower than the minimum version", func() { 115 var server *Server 116 117 BeforeEach(func() { 118 server = helpers.StartAndTargetServerWithV3Version("3.0.0") 119 }) 120 121 AfterEach(func() { 122 server.Close() 123 }) 124 125 It("fails with error message that the minimum version is not met", func() { 126 session := helpers.CF("v3-create-package", appName) 127 Eventually(session).Should(Say("FAILED")) 128 Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\.")) 129 Eventually(session).Should(Exit(1)) 130 }) 131 }) 132 133 Context("when not logged in", func() { 134 BeforeEach(func() { 135 helpers.LogoutCF() 136 }) 137 138 It("fails with not logged in message", func() { 139 session := helpers.CF("v3-create-package", appName) 140 Eventually(session).Should(Say("FAILED")) 141 Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in.")) 142 Eventually(session).Should(Exit(1)) 143 }) 144 }) 145 146 Context("when there is no org set", func() { 147 BeforeEach(func() { 148 helpers.LogoutCF() 149 helpers.LoginCF() 150 }) 151 152 It("fails with no targeted org error message", func() { 153 session := helpers.CF("v3-create-package", appName) 154 Eventually(session).Should(Say("FAILED")) 155 Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org.")) 156 Eventually(session).Should(Exit(1)) 157 }) 158 }) 159 160 Context("when there is no space set", func() { 161 BeforeEach(func() { 162 helpers.LogoutCF() 163 helpers.LoginCF() 164 helpers.TargetOrg(ReadOnlyOrg) 165 }) 166 167 It("fails with no targeted space error message", func() { 168 session := helpers.CF("v3-create-package", appName) 169 Eventually(session).Should(Say("FAILED")) 170 Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space.")) 171 Eventually(session).Should(Exit(1)) 172 }) 173 }) 174 }) 175 176 Context("when the environment is set up correctly", func() { 177 BeforeEach(func() { 178 setupCF(orgName, spaceName) 179 }) 180 181 AfterEach(func() { 182 helpers.QuickDeleteOrg(orgName) 183 }) 184 185 Context("when the app does not exist", func() { 186 It("returns a not found error", func() { 187 session := helpers.CF("v3-create-package", appName) 188 userName, _ := helpers.GetCredentials() 189 Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 190 Eventually(session.Err).Should(Say("App %s not found", appName)) 191 Eventually(session).Should(Say("FAILED")) 192 Eventually(session).Should(Exit(1)) 193 }) 194 }) 195 196 Context("when the app exists", func() { 197 BeforeEach(func() { 198 Eventually(helpers.CF("v3-create-app", appName)).Should(Exit(0)) 199 }) 200 201 It("creates the package", func() { 202 session := helpers.CF("v3-create-package", appName) 203 userName, _ := helpers.GetCredentials() 204 Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 205 Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex)) 206 Eventually(session).Should(Say("OK")) 207 Eventually(session).Should(Exit(0)) 208 }) 209 210 Context("when the --docker-image flag is provided", func() { 211 Context("when the docker-image exists", func() { 212 It("creates the package", func() { 213 session := helpers.CF("v3-create-package", appName, "--docker-image", PublicDockerImage) 214 userName, _ := helpers.GetCredentials() 215 Eventually(session).Should(Say("Creating docker package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 216 Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex)) 217 Eventually(session).Should(Say("OK")) 218 Eventually(session).Should(Exit(0)) 219 }) 220 }) 221 }) 222 223 Context("when the -p flag is provided", func() { 224 Context("when the path is a directory", func() { 225 Context("when the directory contains files", func() { 226 It("creates and uploads the package from the directory", func() { 227 helpers.WithHelloWorldApp(func(appDir string) { 228 session := helpers.CF("v3-create-package", appName, "-p", appDir) 229 userName, _ := helpers.GetCredentials() 230 231 Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 232 Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex)) 233 Eventually(session).Should(Say("OK")) 234 Eventually(session).Should(Exit(0)) 235 }) 236 }) 237 }) 238 239 Context("when the directory is empty", func() { 240 var emptyDir string 241 242 BeforeEach(func() { 243 var err error 244 emptyDir, err = ioutil.TempDir("", "integration-push-path-empty") 245 Expect(err).ToNot(HaveOccurred()) 246 }) 247 248 AfterEach(func() { 249 Expect(os.RemoveAll(emptyDir)).ToNot(HaveOccurred()) 250 }) 251 252 It("returns an error", func() { 253 session := helpers.CF("v3-create-package", appName, "-p", emptyDir) 254 // TODO: Modify this after changing code if necessary 255 Eventually(session.Err).Should(Say("No app files found in '%s'", regexp.QuoteMeta(emptyDir))) 256 Eventually(session).Should(Exit(1)) 257 }) 258 }) 259 }) 260 261 Context("when the path is a zip file", func() { 262 Context("pushing a zip file", func() { 263 var archive string 264 265 BeforeEach(func() { 266 helpers.WithHelloWorldApp(func(appDir string) { 267 tmpfile, err := ioutil.TempFile("", "package-archive-integration") 268 Expect(err).ToNot(HaveOccurred()) 269 archive = tmpfile.Name() 270 Expect(tmpfile.Close()) 271 272 err = helpers.Zipit(appDir, archive, "") 273 Expect(err).ToNot(HaveOccurred()) 274 }) 275 }) 276 277 AfterEach(func() { 278 Expect(os.RemoveAll(archive)).ToNot(HaveOccurred()) 279 }) 280 281 It("creates and uploads the package from the zip file", func() { 282 session := helpers.CF("v3-create-package", appName, "-p", archive) 283 284 userName, _ := helpers.GetCredentials() 285 286 Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 287 Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex)) 288 Eventually(session).Should(Say("OK")) 289 Eventually(session).Should(Exit(0)) 290 }) 291 }) 292 }) 293 294 Context("when the path is a symlink to a directory", func() { 295 var symlinkPath string 296 297 BeforeEach(func() { 298 tempFile, err := ioutil.TempFile("", "symlink-") 299 Expect(err).ToNot(HaveOccurred()) 300 Expect(tempFile.Close()).To(Succeed()) 301 302 symlinkPath = tempFile.Name() 303 Expect(os.Remove(symlinkPath)).To(Succeed()) 304 }) 305 306 AfterEach(func() { 307 Expect(os.Remove(symlinkPath)).To(Succeed()) 308 }) 309 310 It("creates and uploads the package from the directory", func() { 311 helpers.WithHelloWorldApp(func(appDir string) { 312 Expect(os.Symlink(appDir, symlinkPath)).To(Succeed()) 313 314 session := helpers.CF("v3-create-package", appName, "-p", symlinkPath) 315 userName, _ := helpers.GetCredentials() 316 317 Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 318 Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex)) 319 Eventually(session).Should(Say("OK")) 320 Eventually(session).Should(Exit(0)) 321 }) 322 }) 323 }) 324 }) 325 326 Context("when the path is a symlink to a zip file", func() { 327 var ( 328 archive string 329 symlinkPath string 330 ) 331 332 BeforeEach(func() { 333 helpers.WithHelloWorldApp(func(appDir string) { 334 tmpfile, err := ioutil.TempFile("", "package-archive-integration") 335 Expect(err).ToNot(HaveOccurred()) 336 archive = tmpfile.Name() 337 Expect(tmpfile.Close()) 338 339 err = helpers.Zipit(appDir, archive, "") 340 Expect(err).ToNot(HaveOccurred()) 341 }) 342 343 tempFile, err := ioutil.TempFile("", "symlink-to-archive-") 344 Expect(err).ToNot(HaveOccurred()) 345 Expect(tempFile.Close()).To(Succeed()) 346 347 symlinkPath = tempFile.Name() 348 Expect(os.Remove(symlinkPath)).To(Succeed()) 349 Expect(os.Symlink(archive, symlinkPath)).To(Succeed()) 350 }) 351 352 AfterEach(func() { 353 Expect(os.Remove(archive)).To(Succeed()) 354 Expect(os.Remove(symlinkPath)).To(Succeed()) 355 }) 356 357 It("creates and uploads the package from the zip file", func() { 358 session := helpers.CF("v3-create-package", appName, "-p", symlinkPath) 359 360 userName, _ := helpers.GetCredentials() 361 362 Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 363 Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex)) 364 Eventually(session).Should(Say("OK")) 365 Eventually(session).Should(Exit(0)) 366 }) 367 }) 368 369 Context("when the -o and -p flags are provided together", func() { 370 It("displays an error and exits 1", func() { 371 helpers.WithHelloWorldApp(func(appDir string) { 372 session := helpers.CF("v3-create-package", appName, "-o", PublicDockerImage, "-p", appDir) 373 Eventually(session).Should(Say("FAILED")) 374 Eventually(session.Err).Should(Say("Incorrect Usage: The following arguments cannot be used together: --docker-image, -o, -p")) 375 Eventually(session).Should(Say("NAME:")) 376 Eventually(session).Should(Exit(1)) 377 }) 378 }) 379 }) 380 }) 381 }) 382 })