github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/v7/push/path_test.go (about) 1 package push 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path" 8 "path/filepath" 9 "runtime" 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("handle path in manifest and flag override", func() { 19 var ( 20 appName string 21 22 secondName string 23 tempDir string 24 ) 25 26 BeforeEach(func() { 27 appName = helpers.NewAppName() 28 secondName = helpers.NewAppName() 29 var err error 30 tempDir, err = ioutil.TempDir("", "simple-manifest-test") 31 Expect(err).ToNot(HaveOccurred()) 32 }) 33 34 AfterEach(func() { 35 Expect(os.RemoveAll(tempDir)).ToNot(HaveOccurred()) 36 }) 37 38 When("manifest specifies paths", func() { 39 It("pushes the apps using the relative path to the manifest specified", func() { 40 helpers.WithHelloWorldApp(func(dir string) { 41 nestedDir := filepath.Join(dir, "nested") 42 err := os.Mkdir(nestedDir, os.FileMode(0777)) 43 if err != nil { 44 Expect(err).NotTo(HaveOccurred()) 45 } 46 manifestPath := filepath.Join(nestedDir, "manifest.yml") 47 helpers.WriteManifest(manifestPath, map[string]interface{}{ 48 "applications": []map[string]interface{}{ 49 { 50 "name": appName, 51 "path": "..", 52 }, 53 { 54 "name": secondName, 55 "path": dir, 56 }, 57 }, 58 }) 59 session := helpers.CustomCF( 60 helpers.CFEnv{ 61 EnvVars: map[string]string{"CF_LOG_LEVEL": "debug"}, 62 }, 63 PushCommandName, 64 appName, 65 "-f", manifestPath, 66 ) 67 68 if runtime.GOOS == "windows" { 69 // The paths in windows logging have extra escaping that is difficult 70 // to match. Instead match on uploading the right number of files. 71 Eventually(session.Err).Should(Say("zipped_file_count=3")) 72 } else { 73 Eventually(session.Err).Should(helpers.SayPath(`msg="creating archive"\s+Path="?%s"?`, dir)) 74 } 75 Eventually(session).Should(Exit(0)) 76 }) 77 }) 78 79 When("a single path is not valid", func() { 80 It("errors", func() { 81 expandedTempDir, err := filepath.EvalSymlinks(tempDir) 82 Expect(err).NotTo(HaveOccurred()) 83 manifestPath := filepath.Join(tempDir, "manifest.yml") 84 helpers.WriteManifest(manifestPath, map[string]interface{}{ 85 "applications": []map[string]interface{}{ 86 { 87 "name": appName, 88 "path": "potato", 89 }, 90 { 91 "name": secondName, 92 "path": "/baboaboaboaobao/foo", 93 }, 94 }, 95 }) 96 session := helpers.CF(PushCommandName, appName, "-f", manifestPath) 97 Eventually(session.Err).Should(helpers.SayPath("File not found locally, make sure the file exists at given path %s", filepath.Join(expandedTempDir, "potato"))) 98 Eventually(session).Should(Exit(1)) 99 }) 100 }) 101 }) 102 103 When("manifest does not specify a path and there is no flag override", func() { 104 When("no droplet or docker specified", func() { 105 var workingDir string 106 107 BeforeEach(func() { 108 var err error 109 110 workingDir, err = os.Getwd() 111 Expect(err).ToNot(HaveOccurred()) 112 }) 113 114 AfterEach(func() { 115 err := os.Chdir(workingDir) 116 Expect(err).ToNot(HaveOccurred()) 117 }) 118 119 It("defaults to the current working directory", func() { 120 helpers.WithHelloWorldApp(func(dir string) { 121 err := os.Chdir(dir) 122 Expect(err).ToNot(HaveOccurred()) 123 124 nestedDir := filepath.Join(dir, "nested") 125 err = os.Mkdir(nestedDir, os.FileMode(0777)) 126 if err != nil { 127 Expect(err).NotTo(HaveOccurred()) 128 } 129 manifestPath := filepath.Join(nestedDir, "manifest.yml") 130 helpers.WriteManifest(manifestPath, map[string]interface{}{ 131 "applications": []map[string]interface{}{ 132 { 133 "name": appName, 134 }, 135 }, 136 }) 137 session := helpers.CustomCF( 138 helpers.CFEnv{ 139 EnvVars: map[string]string{"CF_LOG_LEVEL": "debug"}, 140 }, 141 PushCommandName, 142 appName, 143 "-f", manifestPath, 144 ) 145 146 if runtime.GOOS == "windows" { 147 // The paths in windows logging have extra escaping that is difficult 148 // to match. Instead match on uploading the right number of files. 149 Eventually(session.Err).Should(Say("zipped_file_count=3")) 150 } else { 151 Eventually(session.Err).Should(helpers.SayPath(`msg="creating archive"\s+Path="?%s"?`, dir)) 152 } 153 Eventually(session).Should(Exit(0)) 154 }) 155 }) 156 }) 157 158 When("docker is specified", func() { 159 It("it uses the docker config", func() { 160 manifestPath := filepath.Join(tempDir, "manifest.yml") 161 helpers.WriteManifest(manifestPath, map[string]interface{}{ 162 "applications": []map[string]interface{}{ 163 { 164 "name": appName, 165 "docker": map[string]string{ 166 "image": "bad-docker-image", 167 "username": "bad-docker-username", 168 }, 169 }, 170 }, 171 }) 172 session := helpers.CustomCF( 173 helpers.CFEnv{ 174 EnvVars: map[string]string{"CF_LOG_LEVEL": "debug", "CF_DOCKER_PASSWORD": "bad-docker-password"}, 175 }, 176 PushCommandName, 177 appName, 178 "-f", manifestPath, 179 ) 180 181 Eventually(session).Should(Say("docker")) 182 Eventually(session.Err).Should(Say("staging failed")) 183 Eventually(session).Should(Exit(1)) 184 }) 185 }) 186 }) 187 188 When("the -p flag is provided", func() { 189 var ( 190 appName string 191 ) 192 193 BeforeEach(func() { 194 appName = helpers.PrefixedRandomName("app") 195 }) 196 197 When("the path is a directory", func() { 198 When("the directory contains files", func() { 199 It("pushes the app from the directory", func() { 200 helpers.WithHelloWorldApp(func(appDir string) { 201 session := helpers.CF(PushCommandName, appName, "-p", appDir) 202 Eventually(session).Should(Say(`name:\s+%s`, appName)) 203 Eventually(session).Should(Say(`requested state:\s+started`)) 204 Eventually(session).Should(Exit(0)) 205 }) 206 }) 207 208 When("The manifest is in a different directory than the app's source", func() { 209 var workingDir string 210 211 BeforeEach(func() { 212 var err error 213 214 workingDir, err = os.Getwd() 215 Expect(err).ToNot(HaveOccurred()) 216 }) 217 218 AfterEach(func() { 219 err := os.Chdir(workingDir) 220 Expect(err).ToNot(HaveOccurred()) 221 }) 222 223 It("pushes the app with a relative path to the app directory", func() { 224 manifestDir := helpers.TempDirAbsolutePath("", "manifest-dir") 225 defer os.RemoveAll(manifestDir) 226 227 err := ioutil.WriteFile( 228 filepath.Join(manifestDir, "manifest.yml"), 229 []byte(fmt.Sprintf(`--- 230 applications: 231 - name: %s`, 232 appName)), 0666) 233 Expect(err).ToNot(HaveOccurred()) 234 235 helpers.WithHelloWorldApp(func(appDir string) { 236 err = os.Chdir("/") 237 Expect(err).ToNot(HaveOccurred()) 238 session := helpers.CF(PushCommandName, appName, "-p", path.Join(".", appDir), "-f", path.Join(manifestDir, "manifest.yml")) 239 Eventually(session).Should(Say(`name:\s+%s`, appName)) 240 Eventually(session).Should(Say(`requested state:\s+started`)) 241 Eventually(session).Should(Exit(0)) 242 }) 243 }) 244 }) 245 }) 246 247 When("the directory is empty", func() { 248 var emptyDir string 249 250 BeforeEach(func() { 251 sympath, err := ioutil.TempDir("", "integration-push-path-empty") 252 Expect(err).ToNot(HaveOccurred()) 253 emptyDir, err = filepath.EvalSymlinks(sympath) 254 Expect(err).ToNot(HaveOccurred()) 255 }) 256 257 AfterEach(func() { 258 Expect(os.RemoveAll(emptyDir)).ToNot(HaveOccurred()) 259 }) 260 261 It("returns an error", func() { 262 session := helpers.CF(PushCommandName, appName, "-p", emptyDir) 263 Eventually(session.Err).Should(helpers.SayPath("No app files found in '%s'", emptyDir)) 264 Eventually(session).Should(Exit(1)) 265 }) 266 }) 267 }) 268 269 When("the path is a zip file", func() { 270 Context("pushing a zip file", func() { 271 var archive string 272 273 BeforeEach(func() { 274 helpers.WithHelloWorldApp(func(appDir string) { 275 tmpfile, err := ioutil.TempFile("", "push-archive-integration") 276 Expect(err).ToNot(HaveOccurred()) 277 archive = tmpfile.Name() 278 Expect(tmpfile.Close()) 279 280 err = helpers.Zipit(appDir, archive, "") 281 Expect(err).ToNot(HaveOccurred()) 282 }) 283 }) 284 285 AfterEach(func() { 286 Expect(os.RemoveAll(archive)).ToNot(HaveOccurred()) 287 }) 288 289 It("pushes the app from the zip file", func() { 290 session := helpers.CF(PushCommandName, appName, "-p", archive) 291 292 Eventually(session).Should(Say(`name:\s+%s`, appName)) 293 Eventually(session).Should(Say(`requested state:\s+started`)) 294 Eventually(session).Should(Exit(0)) 295 }) 296 }) 297 }) 298 }) 299 })