github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/shared/experimental/v3_set_droplet_command_test.go (about)

     1  package experimental
     2  
     3  import (
     4  	"regexp"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     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  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("v3-set-droplet 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-set-droplet", "--help")
    32  
    33  				Eventually(session).Should(Say("NAME:"))
    34  				Eventually(session).Should(Say("v3-set-droplet - Set the droplet used to run an app"))
    35  				Eventually(session).Should(Say("USAGE:"))
    36  				Eventually(session).Should(Say("cf v3-set-droplet APP_NAME -d DROPLET_GUID"))
    37  				Eventually(session).Should(Say("OPTIONS:"))
    38  				Eventually(session).Should(Say(`--droplet-guid, -d\s+The guid of the droplet to use`))
    39  
    40  				Eventually(session).Should(Exit(0))
    41  			})
    42  		})
    43  	})
    44  
    45  	When("the app name is not provided", func() {
    46  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    47  			session := helpers.CF("v3-set-droplet", "-d", "some-droplet-guid")
    48  
    49  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    50  			Eventually(session).Should(Say("NAME:"))
    51  			Eventually(session).Should(Exit(1))
    52  		})
    53  	})
    54  
    55  	It("displays the experimental warning", func() {
    56  		session := helpers.CF("v3-set-droplet", appName, "--droplet-guid", "some-droplet-guid")
    57  		Eventually(session.Err).Should(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    58  		Eventually(session).Should(Exit())
    59  	})
    60  
    61  	When("the package GUID flag is missing", func() {
    62  		It("displays incorrect usage", func() {
    63  			session := helpers.CF("v3-set-droplet", "some-app")
    64  
    65  			Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `-d, --droplet-guid' was not specified"))
    66  			Eventually(session).Should(Say("NAME:"))
    67  
    68  			Eventually(session).Should(Exit(1))
    69  		})
    70  	})
    71  
    72  	When("the environment is not setup correctly", func() {
    73  		When("the v3 api version is lower than the minimum version", func() {
    74  			var server *Server
    75  
    76  			BeforeEach(func() {
    77  				server = helpers.StartAndTargetServerWithAPIVersions(helpers.DefaultV2Version, ccversion.MinV3ClientVersion)
    78  			})
    79  
    80  			AfterEach(func() {
    81  				server.Close()
    82  			})
    83  
    84  			It("fails with error message that the minimum version is not met", func() {
    85  				session := helpers.CF("v3-set-droplet", appName, "--droplet-guid", "some-droplet-guid")
    86  				Eventually(session).Should(Say("FAILED"))
    87  				Eventually(session.Err).Should(Say(`This command requires CF API version 3\.27\.0 or higher\.`))
    88  				Eventually(session).Should(Exit(1))
    89  			})
    90  		})
    91  
    92  		It("fails with the appropriate errors", func() {
    93  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "v3-set-droplet", appName, "--droplet-guid", "some-droplet-guid")
    94  		})
    95  	})
    96  
    97  	When("the environment is set up correctly", func() {
    98  		BeforeEach(func() {
    99  			helpers.SetupCF(orgName, spaceName)
   100  		})
   101  
   102  		AfterEach(func() {
   103  			helpers.QuickDeleteOrg(orgName)
   104  		})
   105  
   106  		When("the app exists", func() {
   107  			var dropletGUID string
   108  
   109  			BeforeEach(func() {
   110  				var packageGUID string
   111  				Eventually(helpers.CF("v3-create-app", appName)).Should(Exit(0))
   112  
   113  				helpers.WithHelloWorldApp(func(appDir string) {
   114  					pkgSession := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "v3-create-package", appName)
   115  					Eventually(pkgSession).Should(Exit(0))
   116  					regex, err := regexp.Compile(`package guid: (.+)`)
   117  					Expect(err).ToNot(HaveOccurred())
   118  					matches := regex.FindStringSubmatch(string(pkgSession.Out.Contents()))
   119  					Expect(matches).To(HaveLen(2))
   120  
   121  					packageGUID = matches[1]
   122  				})
   123  
   124  				stageSession := helpers.CF("v3-stage", appName, "--package-guid", packageGUID)
   125  				Eventually(stageSession).Should(Exit(0))
   126  
   127  				regex, err := regexp.Compile(`droplet guid:\s+(.+)`)
   128  				Expect(err).ToNot(HaveOccurred())
   129  				matches := regex.FindStringSubmatch(string(stageSession.Out.Contents()))
   130  				Expect(matches).To(HaveLen(2))
   131  
   132  				dropletGUID = matches[1]
   133  			})
   134  
   135  			It("sets the droplet for the app", func() {
   136  				userName, _ := helpers.GetCredentials()
   137  
   138  				session := helpers.CF("v3-set-droplet", appName, "-d", dropletGUID)
   139  				Eventually(session).Should(Say(`Setting app %s to droplet %s in org %s / space %s as %s\.\.\.`, appName, dropletGUID, orgName, spaceName, userName))
   140  				Eventually(session).Should(Say("OK"))
   141  
   142  				Eventually(session).Should(Exit(0))
   143  			})
   144  
   145  			When("the app does not exist", func() {
   146  				It("displays app not found and exits 1", func() {
   147  					invalidAppName := "invalid-app-name"
   148  					session := helpers.CF("v3-set-droplet", invalidAppName, "-d", dropletGUID)
   149  					userName, _ := helpers.GetCredentials()
   150  
   151  					Eventually(session).Should(Say(`Setting app %s to droplet %s in org %s / space %s as %s\.\.\.`, invalidAppName, dropletGUID, orgName, spaceName, userName))
   152  					Eventually(session.Err).Should(Say("App %s not found", invalidAppName))
   153  					Eventually(session).Should(Say("FAILED"))
   154  
   155  					Eventually(session).Should(Exit(1))
   156  				})
   157  			})
   158  
   159  			When("the droplet does not exist", func() {
   160  				It("displays droplet not found and exits 1", func() {
   161  					invalidDropletGUID := "some-droplet-guid"
   162  					session := helpers.CF("v3-set-droplet", appName, "-d", invalidDropletGUID)
   163  					userName, _ := helpers.GetCredentials()
   164  
   165  					Eventually(session).Should(Say(`Setting app %s to droplet %s in org %s / space %s as %s\.\.\.`, appName, invalidDropletGUID, orgName, spaceName, userName))
   166  					Eventually(session.Err).Should(Say(`Unable to assign droplet: Unable to assign current droplet\. Ensure the droplet exists and belongs to this app\.`))
   167  					Eventually(session).Should(Say("FAILED"))
   168  					Eventually(session).Should(Exit(1))
   169  				})
   170  			})
   171  		})
   172  	})
   173  })