github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/experimental/v3_set_droplet_command_test.go (about)

     1  package experimental
     2  
     3  import (
     4  	"regexp"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     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("v3-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  		Context("when --help flag is set", func() {
    29  			It("Displays command usage to output", func() {
    30  				session := helpers.CF("v3-set-droplet", "--help")
    31  
    32  				Eventually(session).Should(Say("NAME:"))
    33  				Eventually(session).Should(Say("v3-set-droplet - Set the droplet used to run an app"))
    34  				Eventually(session).Should(Say("USAGE:"))
    35  				Eventually(session).Should(Say("cf v3-set-droplet APP_NAME -d DROPLET_GUID"))
    36  				Eventually(session).Should(Say("OPTIONS:"))
    37  				Eventually(session).Should(Say("--droplet-guid, -d\\s+The guid of the droplet to use"))
    38  
    39  				Eventually(session).Should(Exit(0))
    40  			})
    41  		})
    42  	})
    43  
    44  	Context("when the app name is not provided", func() {
    45  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    46  			session := helpers.CF("v3-set-droplet", "-d", "some-droplet-guid")
    47  
    48  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    49  			Eventually(session).Should(Say("NAME:"))
    50  			Eventually(session).Should(Exit(1))
    51  		})
    52  	})
    53  
    54  	It("displays the experimental warning", func() {
    55  		session := helpers.CF("v3-set-droplet", appName, "--droplet-guid", "some-droplet-guid")
    56  		Eventually(session).Should(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    57  		Eventually(session).Should(Exit())
    58  	})
    59  
    60  	Context("when the package GUID flag is missing", func() {
    61  		It("displays incorrect usage", func() {
    62  			session := helpers.CF("v3-set-droplet", "some-app")
    63  
    64  			Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `-d, --droplet-guid' was not specified"))
    65  			Eventually(session).Should(Say("NAME:"))
    66  
    67  			Eventually(session).Should(Exit(1))
    68  		})
    69  	})
    70  
    71  	Context("when the environment is not setup correctly", func() {
    72  		Context("when no API endpoint is set", func() {
    73  			BeforeEach(func() {
    74  				helpers.UnsetAPI()
    75  			})
    76  
    77  			It("fails with no API endpoint set message", func() {
    78  				session := helpers.CF("v3-set-droplet", appName, "--droplet-guid", "some-droplet-guid")
    79  				Eventually(session).Should(Say("FAILED"))
    80  				Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
    81  				Eventually(session).Should(Exit(1))
    82  			})
    83  		})
    84  
    85  		Context("when the v3 api does not exist", func() {
    86  			var server *Server
    87  
    88  			BeforeEach(func() {
    89  				server = helpers.StartAndTargetServerWithoutV3API()
    90  			})
    91  
    92  			AfterEach(func() {
    93  				server.Close()
    94  			})
    95  
    96  			It("fails with error message that the minimum version is not met", func() {
    97  				session := helpers.CF("v3-set-droplet", appName, "--droplet-guid", "some-droplet-guid")
    98  				Eventually(session).Should(Say("FAILED"))
    99  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\."))
   100  				Eventually(session).Should(Exit(1))
   101  			})
   102  		})
   103  
   104  		Context("when the v3 api version is lower than the minimum version", func() {
   105  			var server *Server
   106  
   107  			BeforeEach(func() {
   108  				server = helpers.StartAndTargetServerWithV3Version("3.0.0")
   109  			})
   110  
   111  			AfterEach(func() {
   112  				server.Close()
   113  			})
   114  
   115  			It("fails with error message that the minimum version is not met", func() {
   116  				session := helpers.CF("v3-set-droplet", appName, "--droplet-guid", "some-droplet-guid")
   117  				Eventually(session).Should(Say("FAILED"))
   118  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\."))
   119  				Eventually(session).Should(Exit(1))
   120  			})
   121  		})
   122  
   123  		Context("when not logged in", func() {
   124  			BeforeEach(func() {
   125  				helpers.LogoutCF()
   126  			})
   127  
   128  			It("fails with not logged in message", func() {
   129  				session := helpers.CF("v3-set-droplet", appName, "--droplet-guid", "some-droplet-guid")
   130  				Eventually(session).Should(Say("FAILED"))
   131  				Eventually(session.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\."))
   132  				Eventually(session).Should(Exit(1))
   133  			})
   134  		})
   135  
   136  		Context("when there is no org set", func() {
   137  			BeforeEach(func() {
   138  				helpers.LogoutCF()
   139  				helpers.LoginCF()
   140  			})
   141  
   142  			It("fails with no org targeted error message", func() {
   143  				session := helpers.CF("v3-set-droplet", appName, "--droplet-guid", "some-droplet-guid")
   144  				Eventually(session).Should(Say("FAILED"))
   145  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\."))
   146  				Eventually(session).Should(Exit(1))
   147  			})
   148  		})
   149  
   150  		Context("when there is no space set", func() {
   151  			BeforeEach(func() {
   152  				helpers.LogoutCF()
   153  				helpers.LoginCF()
   154  				helpers.TargetOrg(ReadOnlyOrg)
   155  			})
   156  
   157  			It("fails with no space targeted error message", func() {
   158  				session := helpers.CF("v3-set-droplet", appName, "--droplet-guid", "some-droplet-guid")
   159  				Eventually(session).Should(Say("FAILED"))
   160  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\."))
   161  				Eventually(session).Should(Exit(1))
   162  			})
   163  		})
   164  	})
   165  
   166  	Context("when the environment is set up correctly", func() {
   167  		BeforeEach(func() {
   168  			setupCF(orgName, spaceName)
   169  		})
   170  
   171  		AfterEach(func() {
   172  			helpers.QuickDeleteOrg(orgName)
   173  		})
   174  
   175  		Context("when the app exists", func() {
   176  			var dropletGUID string
   177  
   178  			BeforeEach(func() {
   179  				var packageGUID string
   180  				Eventually(helpers.CF("v3-create-app", appName)).Should(Exit(0))
   181  
   182  				helpers.WithHelloWorldApp(func(appDir string) {
   183  					pkgSession := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "v3-create-package", appName)
   184  					Eventually(pkgSession).Should(Exit(0))
   185  					regex, err := regexp.Compile(`package guid: (.+)`)
   186  					Expect(err).ToNot(HaveOccurred())
   187  					matches := regex.FindStringSubmatch(string(pkgSession.Out.Contents()))
   188  					Expect(matches).To(HaveLen(2))
   189  
   190  					packageGUID = matches[1]
   191  				})
   192  
   193  				stageSession := helpers.CF("v3-stage", appName, "--package-guid", packageGUID)
   194  				Eventually(stageSession).Should(Exit(0))
   195  
   196  				regex, err := regexp.Compile(`droplet guid:\s+(.+)`)
   197  				Expect(err).ToNot(HaveOccurred())
   198  				matches := regex.FindStringSubmatch(string(stageSession.Out.Contents()))
   199  				Expect(matches).To(HaveLen(2))
   200  
   201  				dropletGUID = matches[1]
   202  			})
   203  
   204  			It("sets the droplet for the app", func() {
   205  				userName, _ := helpers.GetCredentials()
   206  
   207  				session := helpers.CF("v3-set-droplet", appName, "-d", dropletGUID)
   208  				Eventually(session).Should(Say("Setting app %s to droplet %s in org %s / space %s as %s\\.\\.\\.", appName, dropletGUID, orgName, spaceName, userName))
   209  				Eventually(session).Should(Say("OK"))
   210  
   211  				Eventually(session).Should(Exit(0))
   212  			})
   213  
   214  			Context("when the app does not exist", func() {
   215  				It("displays app not found and exits 1", func() {
   216  					invalidAppName := "invalid-app-name"
   217  					session := helpers.CF("v3-set-droplet", invalidAppName, "-d", dropletGUID)
   218  					userName, _ := helpers.GetCredentials()
   219  
   220  					Eventually(session).Should(Say("Setting app %s to droplet %s in org %s / space %s as %s\\.\\.\\.", invalidAppName, dropletGUID, orgName, spaceName, userName))
   221  					Eventually(session.Err).Should(Say("App %s not found", invalidAppName))
   222  					Eventually(session).Should(Say("FAILED"))
   223  
   224  					Eventually(session).Should(Exit(1))
   225  				})
   226  			})
   227  
   228  			Context("when the droplet does not exist", func() {
   229  				It("displays droplet not found and exits 1", func() {
   230  					invalidDropletGUID := "some-droplet-guid"
   231  					session := helpers.CF("v3-set-droplet", appName, "-d", invalidDropletGUID)
   232  					userName, _ := helpers.GetCredentials()
   233  
   234  					Eventually(session).Should(Say("Setting app %s to droplet %s in org %s / space %s as %s\\.\\.\\.", appName, invalidDropletGUID, orgName, spaceName, userName))
   235  					Eventually(session.Err).Should(Say("Unable to assign droplet: Unable to assign current droplet\\. Ensure the droplet exists and belongs to this app\\."))
   236  					Eventually(session).Should(Say("FAILED"))
   237  					Eventually(session).Should(Exit(1))
   238  				})
   239  			})
   240  		})
   241  	})
   242  })