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