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

     1  package isolated
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"regexp"
     8  
     9  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    10  
    11  	"code.cloudfoundry.org/cli/integration/helpers"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/gbytes"
    15  	. "github.com/onsi/gomega/gexec"
    16  )
    17  
    18  var _ = Describe("download-droplet command", func() {
    19  	var (
    20  		helpText func(*Session)
    21  		appName  string
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		appName = helpers.NewAppName()
    26  
    27  		helpText = func(session *Session) {
    28  			Eventually(session).Should(Say("NAME:"))
    29  			Eventually(session).Should(Say("download-droplet - Download an application droplet"))
    30  			Eventually(session).Should(Say("USAGE:"))
    31  			Eventually(session).Should(Say(`cf download-droplet APP_NAME \[--droplet DROPLET_GUID\] \[--path /path/to/droplet.tgz\]`))
    32  			Eventually(session).Should(Say("OPTIONS:"))
    33  			Eventually(session).Should(Say(`--droplet\s+The guid of the droplet to download \(default: app's current droplet\).`))
    34  			Eventually(session).Should(Say(`--path, -p\s+File path to download droplet to \(default: current working directory\).`))
    35  			Eventually(session).Should(Say("SEE ALSO:"))
    36  			Eventually(session).Should(Say("apps, droplets, push, set-droplet"))
    37  		}
    38  	})
    39  
    40  	Describe("help", func() {
    41  		When("--help flag is set", func() {
    42  			It("appears in cf help -a", func() {
    43  				session := helpers.CF("help", "-a")
    44  				Eventually(session).Should(Exit(0))
    45  				Expect(session).To(HaveCommandInCategoryWithDescription("download-droplet", "APPS", "Download an application droplet"))
    46  			})
    47  
    48  			It("Displays command usage to output", func() {
    49  				session := helpers.CF("download-droplet", "--help")
    50  				helpText(session)
    51  				Eventually(session).Should(Exit(0))
    52  			})
    53  		})
    54  	})
    55  
    56  	When("the app name is not provided", func() {
    57  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    58  			session := helpers.CF("download-droplet")
    59  
    60  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    61  			helpText(session)
    62  			Eventually(session).Should(Exit(1))
    63  		})
    64  	})
    65  
    66  	When("the environment is not setup correctly", func() {
    67  		It("fails with the appropriate errors", func() {
    68  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "download-droplet", appName)
    69  		})
    70  	})
    71  
    72  	When("the environment is setup correctly", func() {
    73  		var (
    74  			spaceName string
    75  			orgName   string
    76  			userName  string
    77  		)
    78  
    79  		BeforeEach(func() {
    80  			spaceName = helpers.NewSpaceName()
    81  			orgName = helpers.NewOrgName()
    82  
    83  			helpers.SetupCF(orgName, spaceName)
    84  			userName, _ = helpers.GetCredentials()
    85  		})
    86  
    87  		AfterEach(func() {
    88  			helpers.QuickDeleteOrg(orgName)
    89  		})
    90  
    91  		When("the app has a current droplet", func() {
    92  			var (
    93  				dropletPath string
    94  				dropletGUID string
    95  			)
    96  
    97  			BeforeEach(func() {
    98  				helpers.CreateApp(appName)
    99  
   100  				helpers.WithHelloWorldApp(func(appDir string) {
   101  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "push", appName)).Should(Exit(0))
   102  				})
   103  				dropletSession := helpers.CF("droplets", appName)
   104  				Eventually(dropletSession).Should(Exit(0))
   105  				regex := regexp.MustCompile(`(.+)\s+\(current\)`)
   106  				matches := regex.FindStringSubmatch(string(dropletSession.Out.Contents()))
   107  				Expect(matches).To(HaveLen(2))
   108  				dropletGUID = matches[1]
   109  
   110  				dir, err := os.Getwd()
   111  				Expect(err).ToNot(HaveOccurred())
   112  				dropletPath = filepath.Join(dir, "droplet_"+dropletGUID+".tgz")
   113  			})
   114  
   115  			AfterEach(func() {
   116  				os.RemoveAll(dropletPath)
   117  			})
   118  
   119  			It("downloads the droplet successfully", func() {
   120  				session := helpers.CF("download-droplet", appName)
   121  				Eventually(session).Should(Say(`Downloading current droplet for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   122  				Eventually(session).Should(helpers.SayPath(`Droplet downloaded successfully at %s`, dropletPath))
   123  				Eventually(session).Should(Say("OK"))
   124  				Eventually(session).Should(Exit(0))
   125  
   126  				_, err := os.Stat(dropletPath)
   127  				Expect(err).ToNot(HaveOccurred())
   128  			})
   129  
   130  			When("a path to a directory is provided", func() {
   131  				BeforeEach(func() {
   132  					tmpDir, err := ioutil.TempDir("", "droplets")
   133  					Expect(err).NotTo(HaveOccurred())
   134  					dropletPath = tmpDir
   135  				})
   136  
   137  				It("downloads the droplet to the given path successfully", func() {
   138  					session := helpers.CF("download-droplet", appName, "--path", dropletPath)
   139  					Eventually(session).Should(Say(`Downloading current droplet for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   140  					Eventually(session).Should(helpers.SayPath(`Droplet downloaded successfully at %s`, dropletPath))
   141  					Eventually(session).Should(Say("OK"))
   142  					Eventually(session).Should(Exit(0))
   143  
   144  					_, err := os.Stat(filepath.Join(dropletPath, "droplet_"+dropletGUID+".tgz"))
   145  					Expect(err).ToNot(HaveOccurred())
   146  				})
   147  			})
   148  
   149  			When("a path to a file is provided", func() {
   150  				BeforeEach(func() {
   151  					tmpDir, err := ioutil.TempDir("", "droplets")
   152  					Expect(err).NotTo(HaveOccurred())
   153  					dropletPath = filepath.Join(tmpDir, "my-droplet.tgz")
   154  				})
   155  
   156  				It("downloads the droplet to the given path successfully", func() {
   157  					session := helpers.CF("download-droplet", appName, "--path", dropletPath)
   158  					Eventually(session).Should(Say(`Downloading current droplet for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   159  					Eventually(session).Should(helpers.SayPath(`Droplet downloaded successfully at %s`, dropletPath))
   160  					Eventually(session).Should(Say("OK"))
   161  					Eventually(session).Should(Exit(0))
   162  
   163  					_, err := os.Stat(dropletPath)
   164  					Expect(err).ToNot(HaveOccurred())
   165  				})
   166  			})
   167  		})
   168  
   169  		When("the app does not exist", func() {
   170  			It("displays app not found and exits 1", func() {
   171  				session := helpers.CF("download-droplet", appName)
   172  
   173  				Eventually(session).Should(Say(`Downloading current droplet for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   174  				Eventually(session.Err).Should(Say("App '%s' not found", appName))
   175  				Eventually(session).Should(Say("FAILED"))
   176  
   177  				Eventually(session).Should(Exit(1))
   178  			})
   179  		})
   180  
   181  		When("the app does not have a current droplet", func() {
   182  			BeforeEach(func() {
   183  				helpers.CreateApp(appName)
   184  			})
   185  
   186  			It("displays that there is no current droplet and exits 1", func() {
   187  				session := helpers.CF("download-droplet", appName)
   188  
   189  				Eventually(session).Should(Say(`Downloading current droplet for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   190  				Eventually(session.Err).Should(Say("App '%s' does not have a current droplet.", appName))
   191  				Eventually(session).Should(Say("FAILED"))
   192  
   193  				Eventually(session).Should(Exit(1))
   194  			})
   195  		})
   196  
   197  		Context("when the droplet flag is passed", func() {
   198  			var (
   199  				dropletPath string
   200  				dropletGUID string
   201  			)
   202  
   203  			BeforeEach(func() {
   204  				helpers.CreateApp(appName)
   205  
   206  				helpers.WithHelloWorldApp(func(appDir string) {
   207  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "push", appName)).Should(Exit(0))
   208  				})
   209  				dropletSession := helpers.CF("droplets", appName)
   210  				Eventually(dropletSession).Should(Exit(0))
   211  				regex := regexp.MustCompile(`(.+)\s+\(current\)`)
   212  				matches := regex.FindStringSubmatch(string(dropletSession.Out.Contents()))
   213  				Expect(matches).To(HaveLen(2))
   214  				dropletGUID = matches[1]
   215  
   216  				dir, err := os.Getwd()
   217  				Expect(err).ToNot(HaveOccurred())
   218  				dropletPath = filepath.Join(dir, "droplet_"+dropletGUID+".tgz")
   219  
   220  				helpers.WithHelloWorldApp(func(appDir string) {
   221  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "push", appName)).Should(Exit(0))
   222  				})
   223  			})
   224  
   225  			AfterEach(func() {
   226  				os.RemoveAll(dropletPath)
   227  			})
   228  
   229  			It("downloads the droplet successfully", func() {
   230  				session := helpers.CF("download-droplet", appName, "--droplet", dropletGUID)
   231  				Eventually(session).Should(Say(`Downloading droplet %s for app %s in org %s / space %s as %s\.\.\.`, dropletGUID, appName, orgName, spaceName, userName))
   232  				Eventually(session).Should(helpers.SayPath(`Droplet downloaded successfully at %s`, dropletPath))
   233  				Eventually(session).Should(Say("OK"))
   234  
   235  				_, err := os.Stat(dropletPath)
   236  				Expect(err).ToNot(HaveOccurred())
   237  			})
   238  
   239  			When("the app does not contain a droplet with the given guid", func() {
   240  				It("downloads the droplet successfully", func() {
   241  					session := helpers.CF("download-droplet", appName, "--droplet", "bogus")
   242  					Eventually(session).Should(Say(`Downloading droplet bogus for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   243  					Eventually(session.Err).Should(Say("Droplet 'bogus' not found for app '%s'", appName))
   244  					Eventually(session).Should(Say("FAILED"))
   245  
   246  					Eventually(session).Should(Exit(1))
   247  				})
   248  			})
   249  		})
   250  	})
   251  })