github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/integration/v7/isolated/create_app_manifest_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"regexp"
     9  
    10  	"code.cloudfoundry.org/cli/integration/helpers"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/gbytes"
    14  	. "github.com/onsi/gomega/gexec"
    15  )
    16  
    17  var _ = Describe("create-app-manifest command", func() {
    18  	var appName string
    19  	var manifestFilePath string
    20  	var tempDir string
    21  
    22  	BeforeEach(func() {
    23  		appName = helpers.NewAppName()
    24  		var err error
    25  		tempDir, err = ioutil.TempDir("", "create-manifest")
    26  		Expect(err).ToNot(HaveOccurred())
    27  
    28  		manifestFilePath = filepath.Join(tempDir, fmt.Sprintf("%s_manifest.yml", appName))
    29  	})
    30  
    31  	AfterEach(func() {
    32  		os.RemoveAll(tempDir)
    33  	})
    34  
    35  	Context("Help", func() {
    36  		It("displays the help information", func() {
    37  			session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest", "--help")
    38  			Eventually(session).Should(Say("NAME:"))
    39  			Eventually(session).Should(Say("create-app-manifest - Create an app manifest for an app that has been pushed successfully"))
    40  			Eventually(session).Should(Say("USAGE:"))
    41  			Eventually(session).Should(Say(`cf create-app-manifest APP_NAME \[-p \/path\/to\/<app-name>_manifest\.yml\]`))
    42  			Eventually(session).Should(Say(""))
    43  			Eventually(session).Should(Say("OPTIONS:"))
    44  			Eventually(session).Should(Say("-p      Specify a path for file creation. If path not specified, manifest file is created in current working directory."))
    45  			Eventually(session).Should(Say("SEE ALSO:"))
    46  			Eventually(session).Should(Say("apps, push"))
    47  
    48  			Eventually(session).Should(Exit(0))
    49  		})
    50  	})
    51  
    52  	When("the environment is not setup correctly", func() {
    53  		It("fails with the appropriate errors", func() {
    54  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "create-app-manifest", "some-app-name")
    55  		})
    56  	})
    57  
    58  	When("app name not provided", func() {
    59  		It("displays a usage error", func() {
    60  			session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest")
    61  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    62  			Eventually(session).Should(Say("USAGE:"))
    63  
    64  			Eventually(session).Should(Exit(1))
    65  		})
    66  	})
    67  
    68  	When("the environment is setup correctly", func() {
    69  		var (
    70  			orgName   string
    71  			spaceName string
    72  			userName  string
    73  		)
    74  
    75  		BeforeEach(func() {
    76  			orgName = helpers.NewOrgName()
    77  			spaceName = helpers.NewSpaceName()
    78  
    79  			helpers.SetupCF(orgName, spaceName)
    80  			userName, _ = helpers.GetCredentials()
    81  		})
    82  
    83  		AfterEach(func() {
    84  			helpers.QuickDeleteOrg(orgName)
    85  		})
    86  
    87  		When("the app does not exist", func() {
    88  			It("displays an app not found error", func() {
    89  				session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest", appName)
    90  				Eventually(session).Should(Say(`Creating an app manifest from current settings of app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
    91  				Eventually(session.Err).Should(Say("App %s not found", appName))
    92  				Eventually(session).Should(Say("FAILED"))
    93  
    94  				Eventually(session).Should(Exit(1))
    95  			})
    96  		})
    97  
    98  		When("the app exists", func() {
    99  			BeforeEach(func() {
   100  				helpers.WithHelloWorldApp(func(appDir string) {
   101  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "push", appName)).Should(Exit(0))
   102  				})
   103  			})
   104  
   105  			It("creates the manifest", func() {
   106  				session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest", appName)
   107  				Eventually(session).Should(Say(`Creating an app manifest from current settings of app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   108  				Eventually(session).Should(Say("Manifest file created successfully at %s", helpers.OSAgnosticPath(tempDir, "%s_manifest.yml", appName)))
   109  				Eventually(session).Should(Say("OK"))
   110  				Eventually(session).Should(Exit(0))
   111  
   112  				createdFile, err := ioutil.ReadFile(manifestFilePath)
   113  				Expect(err).ToNot(HaveOccurred())
   114  				Expect(createdFile).To(MatchRegexp("---"))
   115  				Expect(createdFile).To(MatchRegexp("applications:"))
   116  				Expect(createdFile).To(MatchRegexp("name: %s", appName))
   117  			})
   118  
   119  			When("the -p flag is provided", func() {
   120  				When("the specified file is a directory", func() {
   121  					It("displays a file creation error", func() {
   122  						session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest", appName, "-p", tempDir)
   123  						Eventually(session).Should(Say(`Creating an app manifest from current settings of app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   124  						Eventually(session).Should(Say("FAILED"))
   125  						Eventually(session.Err).Should(Say("Error creating manifest file: open %s: is a directory", regexp.QuoteMeta(tempDir)))
   126  
   127  						Eventually(session).Should(Exit(1))
   128  					})
   129  				})
   130  
   131  				When("the specified path is invalid", func() {
   132  					It("displays a file creation error", func() {
   133  						invalidPath := filepath.Join(tempDir, "invalid", "path.yml")
   134  						session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest", appName, "-p", invalidPath)
   135  						Eventually(session).Should(Say(`Creating an app manifest from current settings of app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   136  						Eventually(session).Should(Say("FAILED"))
   137  						Eventually(session.Err).Should(Say("Error creating manifest file: open %s:.*", regexp.QuoteMeta(invalidPath)))
   138  
   139  						Eventually(session).Should(Exit(1))
   140  					})
   141  				})
   142  
   143  				When("the specified file does not exist", func() {
   144  					var newFile string
   145  
   146  					BeforeEach(func() {
   147  						newFile = filepath.Join(tempDir, "new-file.yml")
   148  					})
   149  
   150  					It("creates the manifest in the file", func() {
   151  						session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest", appName, "-p", newFile)
   152  						Eventually(session).Should(Say(`Creating an app manifest from current settings of app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   153  						Eventually(session).Should(Say("Manifest file created successfully at %s", helpers.ConvertPathToRegularExpression(newFile)))
   154  						Eventually(session).Should(Say("OK"))
   155  						Eventually(session).Should(Exit(0))
   156  
   157  						createdFile, err := ioutil.ReadFile(newFile)
   158  						Expect(err).ToNot(HaveOccurred())
   159  						Expect(createdFile).To(MatchRegexp("---"))
   160  						Expect(createdFile).To(MatchRegexp("applications:"))
   161  						Expect(createdFile).To(MatchRegexp("name: %s", appName))
   162  					})
   163  				})
   164  
   165  				When("the specified file exists", func() {
   166  					var existingFile string
   167  
   168  					BeforeEach(func() {
   169  						existingFile = filepath.Join(tempDir, "some-file")
   170  						f, err := os.Create(existingFile)
   171  						Expect(err).ToNot(HaveOccurred())
   172  						Expect(f.Close()).To(Succeed())
   173  					})
   174  
   175  					It("overrides the previous file with the new manifest", func() {
   176  						session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest", appName, "-p", existingFile)
   177  						Eventually(session).Should(Say(`Creating an app manifest from current settings of app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   178  						Eventually(session).Should(Say("Manifest file created successfully at %s", helpers.ConvertPathToRegularExpression(existingFile)))
   179  						Eventually(session).Should(Say("OK"))
   180  						Eventually(session).Should(Exit(0))
   181  
   182  						createdFile, err := ioutil.ReadFile(existingFile)
   183  						Expect(err).ToNot(HaveOccurred())
   184  						Expect(createdFile).To(MatchRegexp("---"))
   185  						Expect(createdFile).To(MatchRegexp("applications:"))
   186  						Expect(createdFile).To(MatchRegexp("name: %s", appName))
   187  					})
   188  				})
   189  			})
   190  		})
   191  	})
   192  })