github.com/arunkumar7540/cli@v6.45.0+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/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  			BeforeEach(func() {
   108  				Eventually(helpers.CF("v3-create-app", appName)).Should(Exit(0))
   109  			})
   110  
   111  			It("creates the package", func() {
   112  				session := helpers.CF("v3-create-package", appName)
   113  				userName, _ := helpers.GetCredentials()
   114  				Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   115  				Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   116  				Eventually(session).Should(Say("OK"))
   117  				Eventually(session).Should(Exit(0))
   118  			})
   119  
   120  			When("the --docker-image flag is provided", func() {
   121  				When("the docker-image exists", func() {
   122  					It("creates the package", func() {
   123  						session := helpers.CF("v3-create-package", appName, "--docker-image", PublicDockerImage)
   124  						userName, _ := helpers.GetCredentials()
   125  						Eventually(session).Should(Say("Creating docker package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   126  						Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   127  						Eventually(session).Should(Say("OK"))
   128  						Eventually(session).Should(Exit(0))
   129  					})
   130  				})
   131  			})
   132  
   133  			When("the -p flag is provided", func() {
   134  				When("the path is a directory", func() {
   135  					When("the directory contains files", func() {
   136  						It("creates and uploads the package from the directory", func() {
   137  							helpers.WithHelloWorldApp(func(appDir string) {
   138  								session := helpers.CF("v3-create-package", appName, "-p", appDir)
   139  								userName, _ := helpers.GetCredentials()
   140  
   141  								Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   142  								Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   143  								Eventually(session).Should(Say("OK"))
   144  								Eventually(session).Should(Exit(0))
   145  							})
   146  						})
   147  					})
   148  
   149  					When("the directory is empty", func() {
   150  						var emptyDir string
   151  
   152  						BeforeEach(func() {
   153  							var err error
   154  							emptyDir, err = ioutil.TempDir("", "integration-push-path-empty")
   155  							Expect(err).ToNot(HaveOccurred())
   156  						})
   157  
   158  						AfterEach(func() {
   159  							Expect(os.RemoveAll(emptyDir)).ToNot(HaveOccurred())
   160  						})
   161  
   162  						It("returns an error", func() {
   163  							session := helpers.CF("v3-create-package", appName, "-p", emptyDir)
   164  							// TODO: Modify this after changing code if necessary
   165  							Eventually(session.Err).Should(Say("No app files found in '%s'", regexp.QuoteMeta(emptyDir)))
   166  							Eventually(session).Should(Exit(1))
   167  						})
   168  					})
   169  				})
   170  
   171  				When("the path is a zip file", func() {
   172  					Context("pushing a zip file", func() {
   173  						var archive string
   174  
   175  						BeforeEach(func() {
   176  							helpers.WithHelloWorldApp(func(appDir string) {
   177  								tmpfile, err := ioutil.TempFile("", "package-archive-integration")
   178  								Expect(err).ToNot(HaveOccurred())
   179  								archive = tmpfile.Name()
   180  								Expect(tmpfile.Close())
   181  
   182  								err = helpers.Zipit(appDir, archive, "")
   183  								Expect(err).ToNot(HaveOccurred())
   184  							})
   185  						})
   186  
   187  						AfterEach(func() {
   188  							Expect(os.RemoveAll(archive)).ToNot(HaveOccurred())
   189  						})
   190  
   191  						It("creates and uploads the package from the zip file", func() {
   192  							session := helpers.CF("v3-create-package", appName, "-p", archive)
   193  
   194  							userName, _ := helpers.GetCredentials()
   195  
   196  							Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   197  							Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   198  							Eventually(session).Should(Say("OK"))
   199  							Eventually(session).Should(Exit(0))
   200  						})
   201  					})
   202  				})
   203  
   204  				When("the path is a symlink to a directory", func() {
   205  					var symlinkPath string
   206  
   207  					BeforeEach(func() {
   208  						tempFile, err := ioutil.TempFile("", "symlink-")
   209  						Expect(err).ToNot(HaveOccurred())
   210  						Expect(tempFile.Close()).To(Succeed())
   211  
   212  						symlinkPath = tempFile.Name()
   213  						Expect(os.Remove(symlinkPath)).To(Succeed())
   214  					})
   215  
   216  					AfterEach(func() {
   217  						Expect(os.Remove(symlinkPath)).To(Succeed())
   218  					})
   219  
   220  					It("creates and uploads the package from the directory", func() {
   221  						helpers.WithHelloWorldApp(func(appDir string) {
   222  							Expect(os.Symlink(appDir, symlinkPath)).To(Succeed())
   223  
   224  							session := helpers.CF("v3-create-package", appName, "-p", symlinkPath)
   225  							userName, _ := helpers.GetCredentials()
   226  
   227  							Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   228  							Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   229  							Eventually(session).Should(Say("OK"))
   230  							Eventually(session).Should(Exit(0))
   231  						})
   232  					})
   233  				})
   234  			})
   235  
   236  			When("the path is a symlink to a zip file", func() {
   237  				var (
   238  					archive     string
   239  					symlinkPath string
   240  				)
   241  
   242  				BeforeEach(func() {
   243  					helpers.WithHelloWorldApp(func(appDir string) {
   244  						tmpfile, err := ioutil.TempFile("", "package-archive-integration")
   245  						Expect(err).ToNot(HaveOccurred())
   246  						archive = tmpfile.Name()
   247  						Expect(tmpfile.Close())
   248  
   249  						err = helpers.Zipit(appDir, archive, "")
   250  						Expect(err).ToNot(HaveOccurred())
   251  					})
   252  
   253  					tempFile, err := ioutil.TempFile("", "symlink-to-archive-")
   254  					Expect(err).ToNot(HaveOccurred())
   255  					Expect(tempFile.Close()).To(Succeed())
   256  
   257  					symlinkPath = tempFile.Name()
   258  					Expect(os.Remove(symlinkPath)).To(Succeed())
   259  					Expect(os.Symlink(archive, symlinkPath)).To(Succeed())
   260  				})
   261  
   262  				AfterEach(func() {
   263  					Expect(os.Remove(archive)).To(Succeed())
   264  					Expect(os.Remove(symlinkPath)).To(Succeed())
   265  				})
   266  
   267  				It("creates and uploads the package from the zip file", func() {
   268  					session := helpers.CF("v3-create-package", appName, "-p", symlinkPath)
   269  
   270  					userName, _ := helpers.GetCredentials()
   271  
   272  					Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   273  					Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   274  					Eventually(session).Should(Say("OK"))
   275  					Eventually(session).Should(Exit(0))
   276  				})
   277  			})
   278  
   279  			When("the -o and -p flags are provided together", func() {
   280  				It("displays an error and exits 1", func() {
   281  					helpers.WithHelloWorldApp(func(appDir string) {
   282  						session := helpers.CF("v3-create-package", appName, "-o", PublicDockerImage, "-p", appDir)
   283  						Eventually(session).Should(Say("FAILED"))
   284  						Eventually(session.Err).Should(Say("Incorrect Usage: The following arguments cannot be used together: --docker-image, -o, -p"))
   285  						Eventually(session).Should(Say("NAME:"))
   286  						Eventually(session).Should(Exit(1))
   287  					})
   288  				})
   289  			})
   290  		})
   291  	})
   292  })