github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/v6/global/create_buildpack_command_test.go (about)

     1  package global
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  	"regexp"
    10  
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    12  	"code.cloudfoundry.org/cli/integration/helpers"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  	. "github.com/onsi/gomega/gexec"
    17  	. "github.com/onsi/gomega/ghttp"
    18  )
    19  
    20  var _ = Describe("create buildpack command", func() {
    21  	var (
    22  		buildpackName string
    23  		username      string
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		buildpackName = helpers.NewBuildpackName()
    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("create-buildpack", "--help")
    34  				Eventually(session).Should(Say("NAME:"))
    35  				Eventually(session).Should(Say("create-buildpack - Create a buildpack"))
    36  				Eventually(session).Should(Say("USAGE:"))
    37  				Eventually(session).Should(Say(`cf create-buildpack BUILDPACK PATH POSITION \[--enable|--disable\]`))
    38  				Eventually(session).Should(Say("TIP:"))
    39  				Eventually(session).Should(Say("Path should be a zip file, a url to a zip file, or a local directory. Position is a positive integer, sets priority, and is sorted from lowest to highest."))
    40  				Eventually(session).Should(Say("OPTIONS:"))
    41  				Eventually(session).Should(Say(`--disable\s+Disable the buildpack from being used for staging`))
    42  				Eventually(session).Should(Say(`--enable\s+Enable the buildpack to be used for staging`))
    43  				Eventually(session).Should(Say("SEE ALSO:"))
    44  				Eventually(session).Should(Say("buildpacks, push"))
    45  				Eventually(session).Should(Exit(0))
    46  			})
    47  		})
    48  	})
    49  
    50  	When("the environment is not setup correctly", func() {
    51  		It("fails with the appropriate errors", func() {
    52  			path, err := os.Getwd()
    53  			Expect(err).ToNot(HaveOccurred())
    54  
    55  			helpers.CheckEnvironmentTargetedCorrectly(false, false, ReadOnlyOrg, "create-buildpack", "fake-buildpack", path, "1")
    56  		})
    57  	})
    58  
    59  	When("the user is logged in", func() {
    60  		BeforeEach(func() {
    61  			helpers.LoginCF()
    62  			username, _ = helpers.GetCredentials()
    63  		})
    64  		AfterEach(func() {
    65  			helpers.DeleteBuildpackIfOnOldCCAPI(buildpackName)
    66  		})
    67  
    68  		When("uploading from a directory", func() {
    69  			var buildpackDir string
    70  
    71  			AfterEach(func() {
    72  				err := os.RemoveAll(buildpackDir)
    73  				Expect(err).ToNot(HaveOccurred())
    74  			})
    75  
    76  			When("zipping the directory errors", func() {
    77  				BeforeEach(func() {
    78  					buildpackDir = "some/nonexistent/dir"
    79  				})
    80  
    81  				It("returns an error", func() {
    82  					session := helpers.CF("create-buildpack", buildpackName, buildpackDir, "1")
    83  					Eventually(session.Err).Should(Say("Incorrect Usage: The specified path 'some/nonexistent/dir' does not exist."))
    84  					Eventually(session).Should(Say("USAGE:"))
    85  					Eventually(session).Should(Exit(1))
    86  				})
    87  			})
    88  
    89  			When("zipping the directory succeeds", func() {
    90  				BeforeEach(func() {
    91  					var err error
    92  					buildpackDir, err = ioutil.TempDir("", "buildpackdir-")
    93  					Expect(err).ToNot(HaveOccurred())
    94  					file, err := ioutil.TempFile(buildpackDir, "myfile-")
    95  					defer file.Close()
    96  					Expect(err).ToNot(HaveOccurred())
    97  				})
    98  
    99  				It("successfully uploads a buildpack", func() {
   100  					session := helpers.CF("create-buildpack", buildpackName, buildpackDir, "1")
   101  					Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   102  					Eventually(session).Should(Say("OK"))
   103  					Eventually(session).Should(Say(`Uploading buildpack %s as %s\.\.\.`, buildpackName, username))
   104  					Eventually(session).Should(Say("Done uploading"))
   105  					Eventually(session).Should(Say("OK"))
   106  					Eventually(session).Should(Exit(0))
   107  				})
   108  			})
   109  
   110  			When("the specified directory is empty", func() {
   111  				BeforeEach(func() {
   112  					var err error
   113  					buildpackDir, err = ioutil.TempDir("", "empty-")
   114  					Expect(err).ToNot(HaveOccurred())
   115  				})
   116  
   117  				It("fails and reports that the directory is empty", func() {
   118  					session := helpers.CF("create-buildpack", buildpackName, buildpackDir, "1")
   119  					Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   120  					Eventually(session.Err).Should(Say("The specified path '%s' cannot be an empty directory.", regexp.QuoteMeta(buildpackDir)))
   121  					Eventually(session).Should(Say("FAILED"))
   122  					Eventually(session).Should(Exit(1))
   123  				})
   124  			})
   125  		})
   126  
   127  		When("uploading from a zip", func() {
   128  			var stacks []string
   129  
   130  			BeforeEach(func() {
   131  				stacks = helpers.EnsureMinimumNumberOfStacks(2)
   132  			})
   133  
   134  			When("specifying a valid path", func() {
   135  
   136  				When("the new buildpack is unique", func() {
   137  
   138  					When("the buildpack has an invalid name", func() {
   139  						var badBuildpackName string
   140  						BeforeEach(func() {
   141  							badBuildpackName = "periods.are.invalid"
   142  						})
   143  
   144  						It("complains that the name is invalid", func() {
   145  							helpers.BuildpackWithoutStack(func(buildpackPath string) {
   146  								session := helpers.CF("create-buildpack", badBuildpackName, buildpackPath, "1")
   147  								Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, badBuildpackName, username))
   148  								Eventually(session.Err).Should(Say("Buildpack is invalid: (?:name ){1,2}can only contain alphanumeric characters"))
   149  								Eventually(session).Should(Exit(0))
   150  							})
   151  						})
   152  					})
   153  
   154  					When("the new buildpack has a nil stack", func() {
   155  						It("successfully uploads a buildpack", func() {
   156  							helpers.BuildpackWithoutStack(func(buildpackPath string) {
   157  								session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   158  								Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   159  								Eventually(session).Should(Say("OK"))
   160  								Eventually(session).Should(Say(`Uploading buildpack %s as %s\.\.\.`, buildpackName, username))
   161  								Eventually(session).Should(Say("Done uploading"))
   162  								Eventually(session).Should(Say("OK"))
   163  								Eventually(session).Should(Exit(0))
   164  							})
   165  
   166  							session := helpers.CF("buildpacks")
   167  							Eventually(session).Should(Say(helpers.BuildpacksOutputRegex(helpers.BuildpackFields{
   168  								Name: buildpackName, Position: "1"})))
   169  							Eventually(session).Should(Exit(0))
   170  						})
   171  					})
   172  
   173  					When("the new buildpack has a valid stack", func() {
   174  						BeforeEach(func() {
   175  							helpers.SkipIfVersionLessThan(ccversion.MinVersionBuildpackStackAssociationV2)
   176  						})
   177  
   178  						It("successfully uploads a buildpack", func() {
   179  							helpers.BuildpackWithStack(func(buildpackPath string) {
   180  								session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   181  								Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   182  								Eventually(session).Should(Say("OK"))
   183  								Eventually(session).Should(Say(`Uploading buildpack %s as %s\.\.\.`, buildpackName, username))
   184  								Eventually(session).Should(Say("Done uploading"))
   185  								Eventually(session).Should(Say("OK"))
   186  								Eventually(session).Should(Exit(0))
   187  							}, stacks[0])
   188  
   189  							session := helpers.CF("buildpacks")
   190  							Eventually(session).Should(Say(helpers.BuildpacksOutputRegex(helpers.BuildpackFields{
   191  								Name: buildpackName, Stack: stacks[0], Position: "1",
   192  							})))
   193  							Eventually(session).Should(Exit(0))
   194  						})
   195  					})
   196  				})
   197  
   198  				When("the new buildpack has an invalid stack", func() {
   199  					BeforeEach(func() {
   200  						helpers.SkipIfVersionLessThan(ccversion.MinVersionBuildpackStackAssociationV2)
   201  					})
   202  
   203  					It("returns the appropriate error", func() {
   204  						helpers.BuildpackWithStack(func(buildpackPath string) {
   205  							session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   206  							Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   207  							Eventually(session).Should(Say("OK"))
   208  							Eventually(session).Should(Say(`Uploading buildpack %s as %s\.\.\.`, buildpackName, username))
   209  							Eventually(session.Err).Should(Say(`Uploaded buildpack stack \(fake-stack\) does not exist`))
   210  							Eventually(session).Should(Exit(1))
   211  						}, "fake-stack")
   212  					})
   213  				})
   214  
   215  				When("a buildpack with the same name exists", func() {
   216  					var (
   217  						existingBuildpack string
   218  					)
   219  
   220  					BeforeEach(func() {
   221  						existingBuildpack = buildpackName
   222  					})
   223  
   224  					When("the new buildpack has a nil stack", func() {
   225  						BeforeEach(func() {
   226  							helpers.SkipIfVersionLessThan(ccversion.MinVersionBuildpackStackAssociationV2)
   227  						})
   228  
   229  						When("the existing buildpack does not have a nil stack", func() {
   230  							BeforeEach(func() {
   231  								helpers.BuildpackWithStack(func(buildpackPath string) {
   232  									session := helpers.CF("create-buildpack", existingBuildpack, buildpackPath, "5")
   233  									Eventually(session).Should(Exit(0))
   234  								}, stacks[0])
   235  							})
   236  
   237  							It("successfully uploads a buildpack", func() {
   238  								helpers.BuildpackWithStack(func(buildpackPath string) {
   239  									session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   240  									Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   241  									Eventually(session).Should(Say("OK"))
   242  									Eventually(session).Should(Say(`Uploading buildpack %s as %s\.\.\.`, buildpackName, username))
   243  									Eventually(session).Should(Exit(0))
   244  								}, stacks[0])
   245  
   246  								session := helpers.CF("buildpacks")
   247  								Eventually(session).Should(Say(helpers.BuildpacksOutputRegex(helpers.BuildpackFields{
   248  									Name: buildpackName, Position: "1"})))
   249  								Eventually(session).Should(Say(helpers.BuildpacksOutputRegex(helpers.BuildpackFields{
   250  									Name: existingBuildpack, Stack: stacks[0], Position: "6"})))
   251  								Eventually(session).Should(Exit(0))
   252  							})
   253  						})
   254  
   255  						When("the existing buildpack has a nil stack", func() {
   256  							BeforeEach(func() {
   257  								helpers.BuildpackWithoutStack(func(buildpackPath string) {
   258  									session := helpers.CF("create-buildpack", existingBuildpack, buildpackPath, "5")
   259  									Eventually(session).Should(Exit(0))
   260  								})
   261  							})
   262  
   263  							It("prints a warning but exits 0", func() {
   264  								helpers.BuildpackWithoutStack(func(buildpackPath string) {
   265  									session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   266  									Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   267  									Eventually(session.Err).Should(Say("Buildpack %s already exists without a stack", buildpackName))
   268  									Eventually(session).Should(Exit(0))
   269  								})
   270  
   271  								session := helpers.CF("buildpacks")
   272  								Eventually(session).Should(Say(`%s\s+5`, existingBuildpack))
   273  								Eventually(session).Should(Exit(0))
   274  							})
   275  						})
   276  					})
   277  
   278  					When("the new buildpack has a non-nil stack", func() {
   279  						BeforeEach(func() {
   280  							helpers.SkipIfVersionLessThan(ccversion.MinVersionBuildpackStackAssociationV2)
   281  						})
   282  
   283  						When("the existing buildpack has a different non-nil stack", func() {
   284  							BeforeEach(func() {
   285  								helpers.BuildpackWithStack(func(buildpackPath string) {
   286  									session := helpers.CF("create-buildpack", existingBuildpack, buildpackPath, "5")
   287  									Eventually(session).Should(Exit(0))
   288  								}, stacks[1])
   289  							})
   290  
   291  							It("successfully uploads a buildpack", func() {
   292  								helpers.BuildpackWithStack(func(buildpackPath string) {
   293  									session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   294  									Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   295  									Eventually(session).Should(Say("OK"))
   296  									Eventually(session).Should(Say(`Uploading buildpack %s as %s\.\.\.`, buildpackName, username))
   297  									Eventually(session).Should(Say("Done uploading"))
   298  									Eventually(session).Should(Say("OK"))
   299  									Eventually(session).Should(Exit(0))
   300  								}, stacks[0])
   301  
   302  								session := helpers.CF("buildpacks")
   303  								Eventually(session).Should(Say(helpers.BuildpacksOutputRegex(helpers.BuildpackFields{
   304  									Name: buildpackName, Stack: stacks[0]})))
   305  								Eventually(session).Should(Say(helpers.BuildpacksOutputRegex(helpers.BuildpackFields{
   306  									Name: existingBuildpack, Stack: stacks[1]})))
   307  								Eventually(session).Should(Exit(0))
   308  							})
   309  						})
   310  
   311  						When("the existing buildpack has a nil stack", func() {
   312  							BeforeEach(func() {
   313  								helpers.BuildpackWithoutStack(func(buildpackPath string) {
   314  									session := helpers.CF("create-buildpack", existingBuildpack, buildpackPath, "5")
   315  									Eventually(session).Should(Exit(0))
   316  								})
   317  							})
   318  
   319  							It("prints a warning and tip but exits 0", func() {
   320  								helpers.BuildpackWithStack(func(buildpackPath string) {
   321  									session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   322  									Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   323  									Eventually(session.Err).Should(Say("Buildpack %s already exists without a stack", buildpackName))
   324  									Eventually(session).Should(Say("TIP: use 'cf buildpacks' and 'cf delete-buildpack' to delete buildpack %s without a stack", buildpackName))
   325  									Eventually(session).Should(Exit(0))
   326  								}, stacks[0])
   327  							})
   328  						})
   329  
   330  						When("the existing buildpack has the same non-nil stack", func() {
   331  							BeforeEach(func() {
   332  								helpers.BuildpackWithStack(func(buildpackPath string) {
   333  									session := helpers.CF("create-buildpack", existingBuildpack, buildpackPath, "5")
   334  									Eventually(session).Should(Exit(0))
   335  								}, stacks[0])
   336  							})
   337  
   338  							It("prints a warning but doesn't exit 1", func() {
   339  								helpers.BuildpackWithStack(func(buildpackPath string) {
   340  									session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   341  									Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   342  									Eventually(session.Err).Should(Say("The buildpack name %s is already in use for the stack %s", buildpackName, stacks[0]))
   343  									Eventually(session).Should(Say("TIP: use 'cf update-buildpack' to update this buildpack"))
   344  									Eventually(session).Should(Exit(0))
   345  								}, stacks[0])
   346  							})
   347  						})
   348  					})
   349  
   350  					When("the API doesn't support stack association", func() {
   351  						BeforeEach(func() {
   352  							helpers.SkipIfVersionAtLeast(ccversion.MinVersionBuildpackStackAssociationV2)
   353  
   354  							helpers.BuildpackWithoutStack(func(buildpackPath string) {
   355  								session := helpers.CF("create-buildpack", existingBuildpack, buildpackPath, "5")
   356  								Eventually(session).Should(Exit(0))
   357  							})
   358  						})
   359  
   360  						It("prints a warning but doesn't exit 1", func() {
   361  							helpers.BuildpackWithoutStack(func(buildpackPath string) {
   362  								session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   363  								Eventually(session.Err).Should(Say("Buildpack %s already exists", buildpackName))
   364  								Eventually(session).Should(Say("TIP: use 'cf update-buildpack' to update this buildpack"))
   365  								Eventually(session).Should(Exit(0))
   366  							})
   367  						})
   368  					})
   369  				})
   370  			})
   371  
   372  			When("specifying an invalid path", func() {
   373  				It("returns the appropriate error", func() {
   374  					session := helpers.CF("create-buildpack", buildpackName, "bogus-path", "1")
   375  
   376  					Eventually(session.Err).Should(Say("Incorrect Usage: The specified path 'bogus-path' does not exist"))
   377  					Eventually(session).Should(Say("USAGE:"))
   378  					Eventually(session).Should(Exit(1))
   379  				})
   380  			})
   381  		})
   382  
   383  		When("uploading from a URL", func() {
   384  			var buildpackURL string
   385  
   386  			When("specifying a valid URL", func() {
   387  				BeforeEach(func() {
   388  					buildpackURL = "https://github.com/cloudfoundry/binary-buildpack/releases/download/v1.0.21/binary-buildpack-v1.0.21.zip"
   389  				})
   390  
   391  				It("successfully uploads a buildpack", func() {
   392  					session := helpers.CF("create-buildpack", buildpackName, buildpackURL, "1")
   393  					Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   394  					Eventually(session).Should(Say("OK"))
   395  					Eventually(session).Should(Say(`Uploading buildpack %s as %s\.\.\.`, buildpackName, username))
   396  					Eventually(session).Should(Say("Done uploading"))
   397  					Eventually(session).Should(Say("OK"))
   398  					Eventually(session).Should(Exit(0))
   399  				})
   400  			})
   401  
   402  			When("a 4xx or 5xx HTTP response status is encountered", func() {
   403  				var server *Server
   404  
   405  				BeforeEach(func() {
   406  					server = NewServer()
   407  					// Suppresses ginkgo server logs
   408  					server.HTTPTestServer.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0)
   409  					server.AppendHandlers(
   410  						CombineHandlers(
   411  							VerifyRequest(http.MethodGet, "/"),
   412  							RespondWith(http.StatusNotFound, nil),
   413  						),
   414  					)
   415  				})
   416  
   417  				AfterEach(func() {
   418  					server.Close()
   419  				})
   420  
   421  				It("displays an appropriate error", func() {
   422  					session := helpers.CF("create-buildpack", buildpackName, server.URL(), "10")
   423  					Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   424  					Eventually(session.Err).Should(Say("Download attempt failed; server returned 404 Not Found"))
   425  					Eventually(session.Err).Should(Say(`Unable to install; buildpack is not available from the given URL\.`))
   426  					Eventually(session).Should(Say("FAILED"))
   427  					Eventually(session).Should(Exit(1))
   428  				})
   429  			})
   430  
   431  			When("specifying an invalid URL", func() {
   432  				BeforeEach(func() {
   433  					buildpackURL = "http://not-a-real-url"
   434  				})
   435  
   436  				It("returns the appropriate error", func() {
   437  					session := helpers.CF("create-buildpack", buildpackName, buildpackURL, "1")
   438  					Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   439  					Eventually(session.Err).Should(Say("Get %s: dial tcp: lookup", buildpackURL))
   440  					Eventually(session).Should(Say("FAILED"))
   441  					Eventually(session).Should(Exit(1))
   442  				})
   443  			})
   444  		})
   445  
   446  		When("specifying the position flag", func() {
   447  			When("position is positive integer", func() {
   448  				It("successfully uploads buildpack in correct position", func() {
   449  					helpers.BuildpackWithoutStack(func(buildpackPath string) {
   450  						session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "3")
   451  						Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   452  						Eventually(session).Should(Say("OK"))
   453  						Eventually(session).Should(Say(`Uploading buildpack %s as %s\.\.\.`, buildpackName, username))
   454  						Eventually(session).Should(Say("Done uploading"))
   455  						Eventually(session).Should(Say("OK"))
   456  						Eventually(session).Should(Exit(0))
   457  					})
   458  
   459  					session := helpers.CF("buildpacks")
   460  					Eventually(session).Should(Say(`%s\s+3`, buildpackName))
   461  					Eventually(session).Should(Exit(0))
   462  				})
   463  			})
   464  		})
   465  
   466  		When("using the enable/disable flags", func() {
   467  			When("specifying disable flag", func() {
   468  				It("disables buildpack", func() {
   469  					helpers.BuildpackWithoutStack(func(buildpackPath string) {
   470  						session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1", "--disable")
   471  						Eventually(session).Should(Say(`Creating buildpack %s as %s\.\.\.`, buildpackName, username))
   472  						Eventually(session).Should(Say("OK"))
   473  						Eventually(session).Should(Say(`Uploading buildpack %s as %s\.\.\.`, buildpackName, username))
   474  						Eventually(session).Should(Say("Done uploading"))
   475  						Eventually(session).Should(Say("OK"))
   476  						Eventually(session).Should(Exit(0))
   477  					})
   478  
   479  					session := helpers.CF("buildpacks")
   480  					Eventually(session).Should(Say(`%s\s+1\s+false`, buildpackName))
   481  					Eventually(session).Should(Exit(0))
   482  				})
   483  			})
   484  
   485  			When("specifying both enable and disable flags", func() {
   486  				It("returns the appropriate error", func() {
   487  					helpers.BuildpackWithoutStack(func(buildpackPath string) {
   488  						session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1", "--enable", "--disable")
   489  						Eventually(session).Should(Say("FAILED"))
   490  						Eventually(session.Err).Should(Say("Incorrect Usage: The following arguments cannot be used together: --enable, --disable"))
   491  						Eventually(session).Should(Exit(1))
   492  					})
   493  				})
   494  			})
   495  		})
   496  	})
   497  })