github.com/arunkumar7540/cli@v6.45.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/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, "--no-start")).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 path := filepath.Join(tempDir, fmt.Sprintf("%s_manifest.yml", appName)) 109 Eventually(session).Should(helpers.SayPath("Manifest file created successfully at %s", path)) 110 Eventually(session).Should(Say("OK")) 111 Eventually(session).Should(Exit(0)) 112 113 createdFile, err := ioutil.ReadFile(manifestFilePath) 114 Expect(err).ToNot(HaveOccurred()) 115 Expect(createdFile).To(MatchRegexp("---")) 116 Expect(createdFile).To(MatchRegexp("applications:")) 117 Expect(createdFile).To(MatchRegexp("name: %s", appName)) 118 }) 119 120 When("the -p flag is provided", func() { 121 When("the specified file is a directory", func() { 122 It("displays a file creation error", func() { 123 session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest", appName, "-p", tempDir) 124 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)) 125 Eventually(session).Should(Say("FAILED")) 126 Eventually(session.Err).Should(Say("Error creating manifest file: open %s: is a directory", regexp.QuoteMeta(tempDir))) 127 128 Eventually(session).Should(Exit(1)) 129 }) 130 }) 131 132 When("the specified path is invalid", func() { 133 It("displays a file creation error", func() { 134 invalidPath := filepath.Join(tempDir, "invalid", "path.yml") 135 session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest", appName, "-p", invalidPath) 136 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)) 137 Eventually(session).Should(Say("FAILED")) 138 Eventually(session.Err).Should(Say("Error creating manifest file: open %s:.*", regexp.QuoteMeta(invalidPath))) 139 140 Eventually(session).Should(Exit(1)) 141 }) 142 }) 143 144 When("the specified file does not exist", func() { 145 var newFile string 146 147 BeforeEach(func() { 148 newFile = filepath.Join(tempDir, "new-file.yml") 149 }) 150 151 It("creates the manifest in the file", func() { 152 session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest", appName, "-p", newFile) 153 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)) 154 Eventually(session).Should(Say("Manifest file created successfully at %s", helpers.ConvertPathToRegularExpression(newFile))) 155 Eventually(session).Should(Say("OK")) 156 Eventually(session).Should(Exit(0)) 157 158 createdFile, err := ioutil.ReadFile(newFile) 159 Expect(err).ToNot(HaveOccurred()) 160 Expect(createdFile).To(MatchRegexp("---")) 161 Expect(createdFile).To(MatchRegexp("applications:")) 162 Expect(createdFile).To(MatchRegexp("name: %s", appName)) 163 }) 164 }) 165 166 When("the specified file exists", func() { 167 var existingFile string 168 169 BeforeEach(func() { 170 existingFile = filepath.Join(tempDir, "some-file") 171 f, err := os.Create(existingFile) 172 Expect(err).ToNot(HaveOccurred()) 173 Expect(f.Close()).To(Succeed()) 174 }) 175 176 It("overrides the previous file with the new manifest", func() { 177 session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: tempDir}, "create-app-manifest", appName, "-p", existingFile) 178 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)) 179 Eventually(session).Should(Say("Manifest file created successfully at %s", helpers.ConvertPathToRegularExpression(existingFile))) 180 Eventually(session).Should(Say("OK")) 181 Eventually(session).Should(Exit(0)) 182 183 createdFile, err := ioutil.ReadFile(existingFile) 184 Expect(err).ToNot(HaveOccurred()) 185 Expect(createdFile).To(MatchRegexp("---")) 186 Expect(createdFile).To(MatchRegexp("applications:")) 187 Expect(createdFile).To(MatchRegexp("name: %s", appName)) 188 }) 189 }) 190 }) 191 }) 192 }) 193 })