github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/set_droplet_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"regexp"
     5  
     6  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     7  	"code.cloudfoundry.org/cli/integration/helpers"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  	. "github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("set-droplet command", func() {
    15  	var (
    16  		orgName   string
    17  		spaceName string
    18  		appName   string
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		orgName = helpers.NewOrgName()
    23  		spaceName = helpers.NewSpaceName()
    24  		appName = helpers.PrefixedRandomName("app")
    25  	})
    26  
    27  	Describe("help", func() {
    28  		When("--help flag is set", func() {
    29  			It("appears in cf help -a", func() {
    30  				session := helpers.CF("help", "-a")
    31  				Eventually(session).Should(Exit(0))
    32  				Expect(session).To(HaveCommandInCategoryWithDescription("set-droplet", "APPS", "Set the droplet used to run an app"))
    33  			})
    34  
    35  			It("Displays command usage to output", func() {
    36  				session := helpers.CF("set-droplet", "--help")
    37  
    38  				Eventually(session).Should(Say("NAME:"))
    39  				Eventually(session).Should(Say("set-droplet - Set the droplet used to run an app"))
    40  				Eventually(session).Should(Say("USAGE:"))
    41  				Eventually(session).Should(Say("cf set-droplet APP_NAME DROPLET_GUID"))
    42  				Eventually(session).Should(Say("SEE ALSO:"))
    43  				Eventually(session).Should(Say("app, create-package, droplets, packages, push, stage"))
    44  
    45  				Eventually(session).Should(Exit(0))
    46  			})
    47  		})
    48  	})
    49  
    50  	When("the app name is not provided", func() {
    51  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    52  			session := helpers.CF("set-droplet")
    53  
    54  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `APP_NAME` and `DROPLET_GUID` were not provided"))
    55  			Eventually(session).Should(Say("NAME:"))
    56  			Eventually(session).Should(Exit(1))
    57  		})
    58  	})
    59  
    60  	When("the droplet GUID arg is missing", func() {
    61  		It("displays incorrect usage", func() {
    62  			session := helpers.CF("set-droplet", "some-app")
    63  
    64  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `DROPLET_GUID` was not provided"))
    65  			Eventually(session).Should(Say("NAME:"))
    66  			Eventually(session).Should(Exit(1))
    67  		})
    68  	})
    69  
    70  	When("the environment is not setup correctly", func() {
    71  		It("fails with the appropriate errors", func() {
    72  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "set-droplet", appName, "some-droplet-guid")
    73  		})
    74  	})
    75  
    76  	When("the environment is set up correctly", func() {
    77  		BeforeEach(func() {
    78  			helpers.SetupCF(orgName, spaceName)
    79  		})
    80  
    81  		AfterEach(func() {
    82  			helpers.QuickDeleteOrg(orgName)
    83  		})
    84  
    85  		When("the app exists", func() {
    86  			var dropletGUID string
    87  
    88  			BeforeEach(func() {
    89  				var packageGUID string
    90  				Eventually(helpers.CF("create-app", appName)).Should(Exit(0))
    91  
    92  				helpers.WithHelloWorldApp(func(appDir string) {
    93  					pkgSession := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "create-package", appName)
    94  					Eventually(pkgSession).Should(Exit(0))
    95  					regex := regexp.MustCompile(`Package with guid '(.+)' has been created\.`)
    96  					matches := regex.FindStringSubmatch(string(pkgSession.Out.Contents()))
    97  					Expect(matches).To(HaveLen(2))
    98  
    99  					packageGUID = matches[1]
   100  				})
   101  
   102  				stageSession := helpers.CF("stage-package", appName, "--package-guid", packageGUID)
   103  				Eventually(stageSession).Should(Exit(0))
   104  
   105  				regex := regexp.MustCompile(`droplet guid:\s+(.+)`)
   106  				matches := regex.FindStringSubmatch(string(stageSession.Out.Contents()))
   107  				Expect(matches).To(HaveLen(2))
   108  
   109  				dropletGUID = matches[1]
   110  			})
   111  
   112  			It("sets the droplet for the app", func() {
   113  				userName, _ := helpers.GetCredentials()
   114  
   115  				session := helpers.CF("set-droplet", appName, dropletGUID)
   116  				Eventually(session).Should(Say(`Setting app %s to droplet %s in org %s / space %s as %s\.\.\.`, appName, dropletGUID, orgName, spaceName, userName))
   117  				Eventually(session).Should(Say("OK"))
   118  
   119  				Eventually(session).Should(Exit(0))
   120  			})
   121  
   122  			When("the app does not exist", func() {
   123  				It("displays app not found and exits 1", func() {
   124  					invalidAppName := "invalid-app-name"
   125  					session := helpers.CF("set-droplet", invalidAppName, dropletGUID)
   126  					userName, _ := helpers.GetCredentials()
   127  
   128  					Eventually(session).Should(Say(`Setting app %s to droplet %s in org %s / space %s as %s\.\.\.`, invalidAppName, dropletGUID, orgName, spaceName, userName))
   129  					Eventually(session.Err).Should(Say("App '%s' not found", invalidAppName))
   130  					Eventually(session).Should(Say("FAILED"))
   131  
   132  					Eventually(session).Should(Exit(1))
   133  				})
   134  			})
   135  
   136  			When("the droplet does not exist", func() {
   137  				It("displays droplet not found and exits 1", func() {
   138  					invalidDropletGUID := "some-droplet-guid"
   139  					session := helpers.CF("set-droplet", appName, invalidDropletGUID)
   140  					userName, _ := helpers.GetCredentials()
   141  
   142  					Eventually(session).Should(Say(`Setting app %s to droplet %s in org %s / space %s as %s\.\.\.`, appName, invalidDropletGUID, orgName, spaceName, userName))
   143  					Eventually(session.Err).Should(Say(`Unable to assign droplet: Unable to assign current droplet\. Ensure the droplet exists and belongs to this app\.`))
   144  					Eventually(session).Should(Say("FAILED"))
   145  					Eventually(session).Should(Exit(1))
   146  				})
   147  			})
   148  		})
   149  	})
   150  })