github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/integration/v7/isolated/create_package_command_test.go (about) 1 package isolated 2 3 import ( 4 "io/ioutil" 5 "os" 6 "regexp" 7 8 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 9 10 "code.cloudfoundry.org/cli/integration/helpers" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 . "github.com/onsi/gomega/gbytes" 14 . "github.com/onsi/gomega/gexec" 15 ) 16 17 var _ = Describe("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("appears in cf help -a", func() { 33 session := helpers.CF("help", "-a") 34 Eventually(session).Should(Exit(0)) 35 Expect(session).To(HaveCommandInCategoryWithDescription("create-package", "APPS", "Uploads a Package")) 36 }) 37 38 It("Displays command usage to output", func() { 39 session := helpers.CF("create-package", "--help") 40 Eventually(session).Should(Say("NAME:")) 41 Eventually(session).Should(Say("create-package - Uploads a Package")) 42 Eventually(session).Should(Say("USAGE:")) 43 Eventually(session).Should(Say(`cf create-package APP_NAME \[-p APP_PATH \| --docker-image \[REGISTRY_HOST:PORT/\]IMAGE\[:TAG\]\]`)) 44 Eventually(session).Should(Say("OPTIONS:")) 45 Eventually(session).Should(Say(`--docker-image, -o\s+Docker image to use \(e\.g\. user/docker-image-name\)`)) 46 Eventually(session).Should(Say(`-p\s+Path to app directory or to a zip file of the contents of the app directory`)) 47 Eventually(session).Should(Say("SEE ALSO:")) 48 Eventually(session).Should(Say("app, droplets, packages, push")) 49 Eventually(session).Should(Exit(0)) 50 }) 51 }) 52 }) 53 54 When("the app name is not provided", func() { 55 It("tells the user that the app name is required, prints help text, and exits 1", func() { 56 session := helpers.CF("create-package") 57 58 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided")) 59 Eventually(session).Should(Say("NAME:")) 60 Eventually(session).Should(Exit(1)) 61 }) 62 }) 63 64 When("the -p flag is not given an arg", func() { 65 It("tells the user that the flag requires an arg, prints help text, and exits 1", func() { 66 session := helpers.CF("create-package", appName, "-p") 67 68 Eventually(session.Err).Should(Say("Incorrect Usage: expected argument for flag `-p'")) 69 Eventually(session).Should(Say("NAME:")) 70 Eventually(session).Should(Exit(1)) 71 }) 72 }) 73 74 When("the -p flag path does not exist", func() { 75 It("tells the user that the flag requires an arg, prints help text, and exits 1", func() { 76 session := helpers.CF("create-package", appName, "-p", "path/that/does/not/exist") 77 78 Eventually(session.Err).Should(Say("Incorrect Usage: The specified path 'path/that/does/not/exist' does not exist.")) 79 Eventually(session).Should(Say("NAME:")) 80 Eventually(session).Should(Exit(1)) 81 }) 82 }) 83 84 When("the environment is not setup correctly", func() { 85 It("fails with the appropriate errors", func() { 86 helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "create-package", appName) 87 }) 88 }) 89 90 When("the environment is set up correctly", func() { 91 BeforeEach(func() { 92 helpers.SetupCF(orgName, spaceName) 93 }) 94 95 AfterEach(func() { 96 helpers.QuickDeleteOrg(orgName) 97 }) 98 99 When("the app does not exist", func() { 100 It("returns a not found error", func() { 101 session := helpers.CF("create-package", appName) 102 userName, _ := helpers.GetCredentials() 103 Eventually(session).Should(Say("Creating and uploading bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 104 Eventually(session.Err).Should(Say("App '%s' not found", appName)) 105 Eventually(session).Should(Say("FAILED")) 106 Eventually(session).Should(Exit(1)) 107 }) 108 }) 109 110 When("the app exists", func() { 111 When("the app is not docker", func() { 112 BeforeEach(func() { 113 Eventually(helpers.CF("create-app", appName)).Should(Exit(0)) 114 }) 115 116 It("creates the package", func() { 117 session := helpers.CF("create-package", appName) 118 userName, _ := helpers.GetCredentials() 119 Eventually(session).Should(Say("Creating and uploading bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 120 Eventually(session).Should(Say(`Package with guid '%s' has been created\.`, helpers.GUIDRegex)) 121 Eventually(session).Should(Say("OK")) 122 Eventually(session).Should(Exit(0)) 123 }) 124 When("the -p flag is provided", func() { 125 When("the path is a directory", func() { 126 When("the directory contains files", func() { 127 It("creates and uploads the package from the directory", func() { 128 helpers.WithHelloWorldApp(func(appDir string) { 129 session := helpers.CF("create-package", appName, "-p", appDir) 130 userName, _ := helpers.GetCredentials() 131 132 Eventually(session).Should(Say("Creating and uploading bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 133 Eventually(session).Should(Say(`Package with guid '%s' has been created\.`, helpers.GUIDRegex)) 134 Eventually(session).Should(Say("OK")) 135 Eventually(session).Should(Exit(0)) 136 }) 137 }) 138 }) 139 140 When("the directory is empty", func() { 141 var emptyDir string 142 143 BeforeEach(func() { 144 var err error 145 emptyDir, err = ioutil.TempDir("", "integration-push-path-empty") 146 Expect(err).ToNot(HaveOccurred()) 147 }) 148 149 AfterEach(func() { 150 Expect(os.RemoveAll(emptyDir)).ToNot(HaveOccurred()) 151 }) 152 153 It("returns an error", func() { 154 session := helpers.CF("create-package", appName, "-p", emptyDir) 155 Eventually(session.Err).Should(Say("No app files found in '%s'", regexp.QuoteMeta(emptyDir))) 156 Eventually(session).Should(Exit(1)) 157 }) 158 }) 159 }) 160 161 When("the path does *not* exist", func() { 162 It("returns an error", func() { 163 session := helpers.CF("create-package", appName, "-p", "/this/directory/for/sure/does/not/exist") 164 Eventually(session.Err).Should(Say(`The specified path '/this/directory/for/sure/does/not/exist' does not exist\.`)) 165 Eventually(session).Should(Exit(1)) 166 }) 167 }) 168 169 When("the path is a zip file", func() { 170 Context("pushing a zip file", func() { 171 var archive string 172 173 BeforeEach(func() { 174 helpers.WithHelloWorldApp(func(appDir string) { 175 tmpfile, err := ioutil.TempFile("", "package-archive-integration") 176 Expect(err).ToNot(HaveOccurred()) 177 archive = tmpfile.Name() 178 Expect(tmpfile.Close()) 179 180 err = helpers.Zipit(appDir, archive, "") 181 Expect(err).ToNot(HaveOccurred()) 182 }) 183 }) 184 185 AfterEach(func() { 186 Expect(os.RemoveAll(archive)).ToNot(HaveOccurred()) 187 }) 188 189 It("creates and uploads the package from the zip file", func() { 190 session := helpers.CF("create-package", appName, "-p", archive) 191 192 userName, _ := helpers.GetCredentials() 193 194 Eventually(session).Should(Say("Creating and uploading bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 195 Eventually(session).Should(Say(`Package with guid '%s' has been created\.`, helpers.GUIDRegex)) 196 Eventually(session).Should(Say("OK")) 197 Eventually(session).Should(Exit(0)) 198 }) 199 }) 200 }) 201 202 When("the path is a symlink to a directory", func() { 203 var symlinkPath string 204 205 BeforeEach(func() { 206 tempFile, err := ioutil.TempFile("", "symlink-") 207 Expect(err).ToNot(HaveOccurred()) 208 Expect(tempFile.Close()).To(Succeed()) 209 210 symlinkPath = tempFile.Name() 211 Expect(os.Remove(symlinkPath)).To(Succeed()) 212 }) 213 214 AfterEach(func() { 215 Expect(os.Remove(symlinkPath)).To(Succeed()) 216 }) 217 218 It("creates and uploads the package from the directory", func() { 219 helpers.WithHelloWorldApp(func(appDir string) { 220 Expect(os.Symlink(appDir, symlinkPath)).To(Succeed()) 221 222 session := helpers.CF("create-package", appName, "-p", symlinkPath) 223 userName, _ := helpers.GetCredentials() 224 225 Eventually(session).Should(Say("Creating and uploading bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 226 Eventually(session).Should(Say(`Package with guid '%s' has been created\.`, helpers.GUIDRegex)) 227 Eventually(session).Should(Say("OK")) 228 Eventually(session).Should(Exit(0)) 229 }) 230 }) 231 }) 232 }) 233 234 When("the path is a symlink to a zip file", func() { 235 var ( 236 archive string 237 symlinkPath string 238 ) 239 240 BeforeEach(func() { 241 helpers.WithHelloWorldApp(func(appDir string) { 242 tmpfile, err := ioutil.TempFile("", "package-archive-integration") 243 Expect(err).ToNot(HaveOccurred()) 244 archive = tmpfile.Name() 245 Expect(tmpfile.Close()) 246 247 err = helpers.Zipit(appDir, archive, "") 248 Expect(err).ToNot(HaveOccurred()) 249 }) 250 251 tempFile, err := ioutil.TempFile("", "symlink-to-archive-") 252 Expect(err).ToNot(HaveOccurred()) 253 Expect(tempFile.Close()).To(Succeed()) 254 255 symlinkPath = tempFile.Name() 256 Expect(os.Remove(symlinkPath)).To(Succeed()) 257 Expect(os.Symlink(archive, symlinkPath)).To(Succeed()) 258 }) 259 260 AfterEach(func() { 261 Expect(os.Remove(archive)).To(Succeed()) 262 Expect(os.Remove(symlinkPath)).To(Succeed()) 263 }) 264 265 It("creates and uploads the package from the zip file", func() { 266 session := helpers.CF("create-package", appName, "-p", symlinkPath) 267 268 userName, _ := helpers.GetCredentials() 269 270 Eventually(session).Should(Say("Creating and uploading bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 271 Eventually(session).Should(Say(`Package with guid '%s' has been created\.`, helpers.GUIDRegex)) 272 Eventually(session).Should(Say("OK")) 273 Eventually(session).Should(Exit(0)) 274 }) 275 }) 276 277 When("the -o and -p flags are provided together", func() { 278 It("displays an error and exits 1", func() { 279 helpers.WithHelloWorldApp(func(appDir string) { 280 session := helpers.CF("create-package", appName, "-o", DockerImage, "-p", appDir) 281 Eventually(session).Should(Say("FAILED")) 282 Eventually(session.Err).Should(Say("Incorrect Usage: The following arguments cannot be used together: --docker-image, -o, -p")) 283 Eventually(session).Should(Say("NAME:")) 284 Eventually(session).Should(Exit(1)) 285 }) 286 }) 287 }) 288 }) 289 }) 290 291 When("the --docker-image flag is provided", func() { 292 BeforeEach(func() { 293 Eventually(helpers.CF("create-app", appName, "--app-type", "docker")).Should(Exit(0)) 294 }) 295 When("the docker-image exists", func() { 296 It("creates the package", func() { 297 session := helpers.CF("create-package", appName, "--docker-image", DockerImage) 298 userName, _ := helpers.GetCredentials() 299 Eventually(session).Should(Say("Creating docker package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 300 Eventually(session).Should(Say(`Package with guid '%s' has been created\.`, helpers.GUIDRegex)) 301 Eventually(session).Should(Say("OK")) 302 Eventually(session).Should(Exit(0)) 303 }) 304 }) 305 }) 306 }) 307 })