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