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