github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/integration/shared/global/delete_buildpack_command_test.go (about)

     1  package global
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     5  	"code.cloudfoundry.org/cli/integration/helpers"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("delete-buildpack command", func() {
    15  	var (
    16  		buildpackName string
    17  		stacks        []string
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		helpers.LoginCF()
    22  		buildpackName = helpers.NewBuildpackName()
    23  	})
    24  
    25  	When("the --help flag is passed", func() {
    26  		It("Displays the appropriate help text", func() {
    27  			session := helpers.CF("delete-buildpack", "--help")
    28  			Eventually(session).Should(Say("NAME:"))
    29  			Eventually(session).Should(Say("delete-buildpack - Delete a buildpack"))
    30  			Eventually(session).Should(Say("\n"))
    31  			Eventually(session).Should(Say("USAGE:"))
    32  			Eventually(session).Should(Say("cf delete-buildpack BUILDPACK \\[-f] \\[-s STACK]"))
    33  			Eventually(session).Should(Say("\n"))
    34  			Eventually(session).Should(Say("OPTIONS:"))
    35  			Eventually(session).Should(Say("-f\\s+Force deletion without confirmation"))
    36  			Eventually(session).Should(Say("-s\\s+Specify stack to disambiguate buildpacks with the same name. Required when buildpack name is ambiguous"))
    37  			Eventually(session).Should(Say("\n"))
    38  			Eventually(session).Should(Say("SEE ALSO:"))
    39  			Eventually(session).Should(Say("buildpacks"))
    40  			Eventually(session).Should(Exit(0))
    41  		})
    42  	})
    43  
    44  	When("the buildpack name is not provided", func() {
    45  		It("displays an error and help", func() {
    46  			session := helpers.CF("delete-buildpack")
    47  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `BUILDPACK` was not provided"))
    48  			Eventually(session).Should(Say("USAGE"))
    49  			Eventually(session).Should(Exit(1))
    50  		})
    51  	})
    52  
    53  	When("the buildpack doesn't exist", func() {
    54  		When("the user does not specify a stack", func() {
    55  			It("displays a warning and exits 0", func() {
    56  				session := helpers.CF("delete-buildpack", "-f", "nonexistent-buildpack")
    57  				Eventually(session).Should(Say("Deleting buildpack nonexistent-buildpack\\.\\.\\."))
    58  				Eventually(session).Should(Say("OK"))
    59  				Eventually(session).Should(Say("Buildpack nonexistent-buildpack does not exist."))
    60  				Eventually(session).Should(Exit(0))
    61  			})
    62  		})
    63  
    64  		When("the user specifies a stack", func() {
    65  			BeforeEach(func() {
    66  				helpers.SkipIfVersionLessThan(ccversion.MinVersionBuildpackStackAssociationV2)
    67  				stacks = helpers.FetchStacks()
    68  			})
    69  
    70  			It("displays a warning and exits 0", func() {
    71  				session := helpers.CF("delete-buildpack", "-f", "nonexistent-buildpack", "-s", stacks[0])
    72  				Eventually(session).Should(Say("Deleting buildpack nonexistent-buildpack with stack %s\\.\\.\\.", stacks[0]))
    73  				Eventually(session).Should(Say("OK"))
    74  				Eventually(session).Should(Say("Buildpack nonexistent-buildpack with stack %s not found.", stacks[0]))
    75  				Eventually(session).Should(Exit(0))
    76  			})
    77  		})
    78  	})
    79  
    80  	Context("there is exactly one buildpack with the specified name", func() {
    81  		When("the stack is specified", func() {
    82  			BeforeEach(func() {
    83  				helpers.SkipIfVersionLessThan(ccversion.MinVersionBuildpackStackAssociationV2)
    84  				stacks = helpers.FetchStacks()
    85  				helpers.BuildpackWithStack(func(buildpackPath string) {
    86  					session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
    87  					Eventually(session).Should(Exit(0))
    88  				}, stacks[0])
    89  			})
    90  
    91  			It("deletes the specified buildpack", func() {
    92  				session := helpers.CF("delete-buildpack", buildpackName, "-s", stacks[0], "-f")
    93  				Eventually(session.Out).Should(Say("OK"))
    94  				Eventually(session).Should(Exit(0))
    95  			})
    96  		})
    97  
    98  		When("the stack is not specified", func() {
    99  			It("deletes the specified buildpack", func() {
   100  				session := helpers.CF("delete-buildpack", buildpackName, "-f")
   101  				Eventually(session.Out).Should(Say("OK"))
   102  				Eventually(session).Should(Exit(0))
   103  			})
   104  		})
   105  	})
   106  
   107  	Context("there are two buildpacks with same name", func() {
   108  		BeforeEach(func() {
   109  			helpers.SkipIfVersionLessThan(ccversion.MinVersionBuildpackStackAssociationV2)
   110  			stacks = helpers.EnsureMinimumNumberOfStacks(2)
   111  		})
   112  
   113  		Context("neither buildpack has a nil stack", func() {
   114  			BeforeEach(func() {
   115  				helpers.BuildpackWithStack(func(buildpackPath string) {
   116  					session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   117  					Eventually(session).Should(Exit(0))
   118  				}, stacks[0])
   119  
   120  				helpers.BuildpackWithStack(func(buildpackPath string) {
   121  					session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   122  					Eventually(session).Should(Exit(0))
   123  				}, stacks[1])
   124  			})
   125  
   126  			It("properly handles ambiguity", func() {
   127  				By("failing when no stack specified")
   128  
   129  				session := helpers.CF("delete-buildpack", buildpackName, "-f")
   130  				Eventually(session).Should(Say("FAILED"))
   131  				Eventually(session).Should(Exit(1))
   132  
   133  				By("succeeding with warning when the buildpack name matches but the stack does not")
   134  
   135  				session = helpers.CF("delete-buildpack", buildpackName, "-s", "not-a-real-stack", "-f")
   136  				Eventually(session).Should(Say("Deleting buildpack %s with stack not-a-real-stack\\.\\.\\.", buildpackName))
   137  				Eventually(session).Should(Say("OK"))
   138  				Eventually(session).Should(Say("Buildpack %s with stack not-a-real-stack not found\\.", buildpackName))
   139  				Eventually(session).Should(Exit(0))
   140  
   141  				By("deleting the buildpack when the associated stack is specified")
   142  
   143  				session = helpers.CF("delete-buildpack", buildpackName, "-s", stacks[0], "-f")
   144  				Eventually(session).Should(Say("Deleting buildpack %s with stack %s\\.\\.\\.", buildpackName, stacks[0]))
   145  				Eventually(session).Should(Say("OK"))
   146  				Eventually(session).Should(Exit(0))
   147  
   148  				session = helpers.CF("delete-buildpack", buildpackName, "-s", stacks[1], "-f")
   149  				Eventually(session).Should(Say("Deleting buildpack %s with stack %s\\.\\.\\.", buildpackName, stacks[1]))
   150  				Eventually(session).Should(Say("OK"))
   151  				Eventually(session).Should(Exit(0))
   152  			})
   153  		})
   154  
   155  		Context("one buildpack has a nil stack", func() {
   156  			BeforeEach(func() {
   157  				helpers.BuildpackWithStack(func(buildpackPath string) {
   158  					session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   159  					Eventually(session).Should(Exit(0))
   160  				}, stacks[0])
   161  
   162  				helpers.BuildpackWithoutStack(func(buildpackPath string) {
   163  					session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   164  					Eventually(session).Should(Exit(0))
   165  				})
   166  			})
   167  
   168  			It("properly handles ambiguity", func() {
   169  				By("deleting nil buildpack when no stack specified")
   170  				session := helpers.CF("delete-buildpack", buildpackName, "-f")
   171  				Eventually(session.Out).Should(Say("OK"))
   172  				Eventually(session).Should(Exit(0))
   173  
   174  				By("deleting the remaining buildpack when no stack is specified")
   175  				session = helpers.CF("delete-buildpack", buildpackName, "-f")
   176  				Eventually(session.Out).Should(Say("OK"))
   177  				Eventually(session).Should(Exit(0))
   178  			})
   179  		})
   180  	})
   181  
   182  	When("the -f flag not is provided", func() {
   183  		var buffer *Buffer
   184  
   185  		BeforeEach(func() {
   186  			buffer = NewBuffer()
   187  
   188  			helpers.BuildpackWithoutStack(func(buildpackPath string) {
   189  				session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "1")
   190  				Eventually(session).Should(Exit(0))
   191  			})
   192  		})
   193  
   194  		When("the user enters 'y'", func() {
   195  			BeforeEach(func() {
   196  				buffer.Write([]byte("y\n"))
   197  			})
   198  
   199  			It("deletes the buildpack", func() {
   200  				session := helpers.CFWithStdin(buffer, "delete-buildpack", buildpackName)
   201  				Eventually(session).Should(Say("Deleting buildpack %s\\.\\.\\.", buildpackName))
   202  				Eventually(session).Should(Say("OK"))
   203  				Eventually(session).Should(Exit(0))
   204  			})
   205  		})
   206  
   207  		When("the user enters 'n'", func() {
   208  			BeforeEach(func() {
   209  				buffer.Write([]byte("n\n"))
   210  			})
   211  
   212  			It("does not delete the buildpack", func() {
   213  				session := helpers.CFWithStdin(buffer, "delete-buildpack", buildpackName)
   214  				Eventually(session).Should(Say("Delete cancelled"))
   215  				Eventually(session).Should(Exit(0))
   216  
   217  				session = helpers.CF("buildpacks")
   218  				Eventually(session).Should(Say(buildpackName))
   219  				Eventually(session).Should(Exit(0))
   220  			})
   221  		})
   222  
   223  		When("the user enters the default input (hits return)", func() {
   224  			BeforeEach(func() {
   225  				buffer.Write([]byte("\n"))
   226  			})
   227  
   228  			It("does not delete the buildpack", func() {
   229  				session := helpers.CFWithStdin(buffer, "delete-buildpack", buildpackName)
   230  				Eventually(session).Should(Say("Delete cancelled"))
   231  				Eventually(session).Should(Exit(0))
   232  
   233  				session = helpers.CF("buildpacks")
   234  				Eventually(session).Should(Say(buildpackName))
   235  				Eventually(session).Should(Exit(0))
   236  			})
   237  		})
   238  	})
   239  
   240  	When("the -f flag is provided", func() {
   241  		It("deletes the org", func() {
   242  			session := helpers.CF("delete-buildpack", buildpackName, "-f")
   243  			Eventually(session).Should(Say("Deleting buildpack %s\\.\\.\\.", buildpackName))
   244  			Eventually(session).Should(Say("OK"))
   245  			Eventually(session).Should(Exit(0))
   246  		})
   247  	})
   248  
   249  	When("the -s flag is provided", func() {
   250  		When("the API is less than the minimum", func() {
   251  			var server *Server
   252  
   253  			BeforeEach(func() {
   254  				server = helpers.StartAndTargetServerWithAPIVersions(ccversion.MinV2ClientVersion, ccversion.MinV3ClientVersion)
   255  			})
   256  
   257  			AfterEach(func() {
   258  				server.Close()
   259  			})
   260  
   261  			It("fails with no networking api error message", func() {
   262  				session := helpers.CF("delete-buildpack", "potato", "-s", "ahoyhoy")
   263  				Eventually(session).Should(Say("FAILED"))
   264  				Eventually(session.Err).Should(Say("Option '-s' requires CF API version %s or higher. Your target is %s.", ccversion.MinVersionBuildpackStackAssociationV2, ccversion.MinV2ClientVersion))
   265  				Eventually(session).Should(Exit(1))
   266  			})
   267  		})
   268  	})
   269  })