github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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("the v3 api version is lower than the minimum version", func() {
    84  			var server *Server
    85  
    86  			BeforeEach(func() {
    87  				server = helpers.StartAndTargetServerWithAPIVersions(helpers.DefaultV2Version, ccversion.MinV3ClientVersion)
    88  			})
    89  
    90  			AfterEach(func() {
    91  				server.Close()
    92  			})
    93  
    94  			It("fails with error message that the minimum version is not met", func() {
    95  				session := helpers.CF("v3-create-package", appName)
    96  				Eventually(session).Should(Say("FAILED"))
    97  				Eventually(session.Err).Should(Say(`This command requires CF API version 3\.27\.0 or higher\.`))
    98  				Eventually(session).Should(Exit(1))
    99  			})
   100  		})
   101  
   102  		It("fails with the appropriate errors", func() {
   103  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "v3-create-package", appName)
   104  		})
   105  	})
   106  
   107  	When("the environment is set up correctly", func() {
   108  		BeforeEach(func() {
   109  			helpers.SetupCF(orgName, spaceName)
   110  		})
   111  
   112  		AfterEach(func() {
   113  			helpers.QuickDeleteOrg(orgName)
   114  		})
   115  
   116  		When("the app does not exist", func() {
   117  			It("returns a not found error", func() {
   118  				session := helpers.CF("v3-create-package", appName)
   119  				userName, _ := helpers.GetCredentials()
   120  				Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   121  				Eventually(session.Err).Should(Say("App %s not found", appName))
   122  				Eventually(session).Should(Say("FAILED"))
   123  				Eventually(session).Should(Exit(1))
   124  			})
   125  		})
   126  
   127  		When("the app exists", func() {
   128  			BeforeEach(func() {
   129  				Eventually(helpers.CF("v3-create-app", appName)).Should(Exit(0))
   130  			})
   131  
   132  			It("creates the package", func() {
   133  				session := helpers.CF("v3-create-package", appName)
   134  				userName, _ := helpers.GetCredentials()
   135  				Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   136  				Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   137  				Eventually(session).Should(Say("OK"))
   138  				Eventually(session).Should(Exit(0))
   139  			})
   140  
   141  			When("the --docker-image flag is provided", func() {
   142  				When("the docker-image exists", func() {
   143  					It("creates the package", func() {
   144  						session := helpers.CF("v3-create-package", appName, "--docker-image", PublicDockerImage)
   145  						userName, _ := helpers.GetCredentials()
   146  						Eventually(session).Should(Say("Creating docker package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   147  						Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   148  						Eventually(session).Should(Say("OK"))
   149  						Eventually(session).Should(Exit(0))
   150  					})
   151  				})
   152  			})
   153  
   154  			When("the -p flag is provided", func() {
   155  				When("the path is a directory", func() {
   156  					When("the directory contains files", func() {
   157  						It("creates and uploads the package from the directory", func() {
   158  							helpers.WithHelloWorldApp(func(appDir string) {
   159  								session := helpers.CF("v3-create-package", appName, "-p", appDir)
   160  								userName, _ := helpers.GetCredentials()
   161  
   162  								Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   163  								Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   164  								Eventually(session).Should(Say("OK"))
   165  								Eventually(session).Should(Exit(0))
   166  							})
   167  						})
   168  					})
   169  
   170  					When("the directory is empty", func() {
   171  						var emptyDir string
   172  
   173  						BeforeEach(func() {
   174  							var err error
   175  							emptyDir, err = ioutil.TempDir("", "integration-push-path-empty")
   176  							Expect(err).ToNot(HaveOccurred())
   177  						})
   178  
   179  						AfterEach(func() {
   180  							Expect(os.RemoveAll(emptyDir)).ToNot(HaveOccurred())
   181  						})
   182  
   183  						It("returns an error", func() {
   184  							session := helpers.CF("v3-create-package", appName, "-p", emptyDir)
   185  							// TODO: Modify this after changing code if necessary
   186  							Eventually(session.Err).Should(Say("No app files found in '%s'", regexp.QuoteMeta(emptyDir)))
   187  							Eventually(session).Should(Exit(1))
   188  						})
   189  					})
   190  				})
   191  
   192  				When("the path is a zip file", func() {
   193  					Context("pushing a zip file", func() {
   194  						var archive string
   195  
   196  						BeforeEach(func() {
   197  							helpers.WithHelloWorldApp(func(appDir string) {
   198  								tmpfile, err := ioutil.TempFile("", "package-archive-integration")
   199  								Expect(err).ToNot(HaveOccurred())
   200  								archive = tmpfile.Name()
   201  								Expect(tmpfile.Close())
   202  
   203  								err = helpers.Zipit(appDir, archive, "")
   204  								Expect(err).ToNot(HaveOccurred())
   205  							})
   206  						})
   207  
   208  						AfterEach(func() {
   209  							Expect(os.RemoveAll(archive)).ToNot(HaveOccurred())
   210  						})
   211  
   212  						It("creates and uploads the package from the zip file", func() {
   213  							session := helpers.CF("v3-create-package", appName, "-p", archive)
   214  
   215  							userName, _ := helpers.GetCredentials()
   216  
   217  							Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   218  							Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   219  							Eventually(session).Should(Say("OK"))
   220  							Eventually(session).Should(Exit(0))
   221  						})
   222  					})
   223  				})
   224  
   225  				When("the path is a symlink to a directory", func() {
   226  					var symlinkPath string
   227  
   228  					BeforeEach(func() {
   229  						tempFile, err := ioutil.TempFile("", "symlink-")
   230  						Expect(err).ToNot(HaveOccurred())
   231  						Expect(tempFile.Close()).To(Succeed())
   232  
   233  						symlinkPath = tempFile.Name()
   234  						Expect(os.Remove(symlinkPath)).To(Succeed())
   235  					})
   236  
   237  					AfterEach(func() {
   238  						Expect(os.Remove(symlinkPath)).To(Succeed())
   239  					})
   240  
   241  					It("creates and uploads the package from the directory", func() {
   242  						helpers.WithHelloWorldApp(func(appDir string) {
   243  							Expect(os.Symlink(appDir, symlinkPath)).To(Succeed())
   244  
   245  							session := helpers.CF("v3-create-package", appName, "-p", symlinkPath)
   246  							userName, _ := helpers.GetCredentials()
   247  
   248  							Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   249  							Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   250  							Eventually(session).Should(Say("OK"))
   251  							Eventually(session).Should(Exit(0))
   252  						})
   253  					})
   254  				})
   255  			})
   256  
   257  			When("the path is a symlink to a zip file", func() {
   258  				var (
   259  					archive     string
   260  					symlinkPath string
   261  				)
   262  
   263  				BeforeEach(func() {
   264  					helpers.WithHelloWorldApp(func(appDir string) {
   265  						tmpfile, err := ioutil.TempFile("", "package-archive-integration")
   266  						Expect(err).ToNot(HaveOccurred())
   267  						archive = tmpfile.Name()
   268  						Expect(tmpfile.Close())
   269  
   270  						err = helpers.Zipit(appDir, archive, "")
   271  						Expect(err).ToNot(HaveOccurred())
   272  					})
   273  
   274  					tempFile, err := ioutil.TempFile("", "symlink-to-archive-")
   275  					Expect(err).ToNot(HaveOccurred())
   276  					Expect(tempFile.Close()).To(Succeed())
   277  
   278  					symlinkPath = tempFile.Name()
   279  					Expect(os.Remove(symlinkPath)).To(Succeed())
   280  					Expect(os.Symlink(archive, symlinkPath)).To(Succeed())
   281  				})
   282  
   283  				AfterEach(func() {
   284  					Expect(os.Remove(archive)).To(Succeed())
   285  					Expect(os.Remove(symlinkPath)).To(Succeed())
   286  				})
   287  
   288  				It("creates and uploads the package from the zip file", func() {
   289  					session := helpers.CF("v3-create-package", appName, "-p", symlinkPath)
   290  
   291  					userName, _ := helpers.GetCredentials()
   292  
   293  					Eventually(session).Should(Say("Uploading and creating bits package for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   294  					Eventually(session).Should(Say("package guid: %s", helpers.GUIDRegex))
   295  					Eventually(session).Should(Say("OK"))
   296  					Eventually(session).Should(Exit(0))
   297  				})
   298  			})
   299  
   300  			When("the -o and -p flags are provided together", func() {
   301  				It("displays an error and exits 1", func() {
   302  					helpers.WithHelloWorldApp(func(appDir string) {
   303  						session := helpers.CF("v3-create-package", appName, "-o", PublicDockerImage, "-p", appDir)
   304  						Eventually(session).Should(Say("FAILED"))
   305  						Eventually(session.Err).Should(Say("Incorrect Usage: The following arguments cannot be used together: --docker-image, -o, -p"))
   306  						Eventually(session).Should(Say("NAME:"))
   307  						Eventually(session).Should(Exit(1))
   308  					})
   309  				})
   310  			})
   311  		})
   312  	})
   313  })