github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/push/symlink_test.go (about)

     1  package push
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"regexp"
     8  
     9  	"code.cloudfoundry.org/cli/integration/helpers"
    10  
    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("push with symlink path", func() {
    18  	var (
    19  		appName       string
    20  		runningDir    string
    21  		symlinkedPath string
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		appName = helpers.NewAppName()
    26  
    27  		var err error
    28  		runningDir, err = ioutil.TempDir("", "push-with-symlink")
    29  		Expect(err).ToNot(HaveOccurred())
    30  		symlinkedPath = filepath.Join(runningDir, "symlink-dir")
    31  	})
    32  
    33  	AfterEach(func() {
    34  		Expect(os.RemoveAll(runningDir)).ToNot(HaveOccurred())
    35  	})
    36  
    37  	Context("push with flag options", func() {
    38  		Context("when pushing from a symlinked current directory", func() {
    39  			It("should push with the absolute path of the app", func() {
    40  				helpers.WithHelloWorldApp(func(dir string) {
    41  					Expect(os.Symlink(dir, symlinkedPath)).ToNot(HaveOccurred())
    42  
    43  					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: symlinkedPath}, PushCommandName, appName)
    44  					Eventually(session).Should(Say("path:\\s+(\\/private)?%s", regexp.QuoteMeta(dir)))
    45  					Eventually(session).Should(Exit(0))
    46  				})
    47  			})
    48  		})
    49  
    50  		Context("when pushing a symlinked path with the '-p' flag", func() {
    51  			It("should push with the absolute path of the app", func() {
    52  				helpers.WithHelloWorldApp(func(dir string) {
    53  					Expect(os.Symlink(dir, symlinkedPath)).ToNot(HaveOccurred())
    54  
    55  					session := helpers.CF(PushCommandName, appName, "-p", symlinkedPath)
    56  					Eventually(session).Should(Say("path:\\s+(\\/private)?%s", regexp.QuoteMeta(dir)))
    57  					Eventually(session).Should(Exit(0))
    58  				})
    59  			})
    60  		})
    61  
    62  		Context("when pushing an symlinked archive with the '-p' flag", func() {
    63  			var archive string
    64  
    65  			BeforeEach(func() {
    66  				helpers.WithHelloWorldApp(func(appDir string) {
    67  					tmpfile, err := ioutil.TempFile("", "push-archive-integration")
    68  					Expect(err).ToNot(HaveOccurred())
    69  					archive = tmpfile.Name()
    70  					Expect(tmpfile.Close()).ToNot(HaveOccurred())
    71  
    72  					err = helpers.Zipit(appDir, archive, "")
    73  					Expect(err).ToNot(HaveOccurred())
    74  				})
    75  			})
    76  
    77  			AfterEach(func() {
    78  				Expect(os.RemoveAll(archive)).ToNot(HaveOccurred())
    79  			})
    80  
    81  			It("should push with the absolute path of the archive", func() {
    82  				Expect(os.Symlink(archive, symlinkedPath)).ToNot(HaveOccurred())
    83  
    84  				session := helpers.CF(PushCommandName, appName, "-p", symlinkedPath)
    85  				Eventually(session).Should(Say("path:\\s+(\\/private)?%s", regexp.QuoteMeta(archive)))
    86  				Eventually(session).Should(Exit(0))
    87  			})
    88  		})
    89  
    90  		Context("push with a single app manifest", func() {
    91  			Context("when the path property is a symlinked path", func() {
    92  				It("should push with the absolute path of the app", func() {
    93  					helpers.WithHelloWorldApp(func(dir string) {
    94  						Expect(os.Symlink(dir, symlinkedPath)).ToNot(HaveOccurred())
    95  
    96  						helpers.WriteManifest(filepath.Join(runningDir, "manifest.yml"), map[string]interface{}{
    97  							"applications": []map[string]string{
    98  								{
    99  									"name": appName,
   100  									"path": symlinkedPath,
   101  								},
   102  							},
   103  						})
   104  
   105  						session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: runningDir}, PushCommandName)
   106  						Eventually(session).Should(Say("path:\\s+(\\/private)?%s", regexp.QuoteMeta(dir)))
   107  						Eventually(session).Should(Exit(0))
   108  					})
   109  				})
   110  			})
   111  		})
   112  	})
   113  })
   114  
   115  var _ = Describe("push with symlinked resources", func() {
   116  	var (
   117  		appName string
   118  	)
   119  
   120  	BeforeEach(func() {
   121  		appName = helpers.NewAppName()
   122  	})
   123  
   124  	Context("when pushing a directory", func() {
   125  		Context("when the directory contains a symlink to a file in the directory", func() {
   126  			Context("when the file exists", func() {
   127  				It("should push the symlink", func() {
   128  					helpers.WithHelloWorldApp(func(dir string) {
   129  						targetFile := filepath.Join(dir, "targetFile")
   130  						Expect(ioutil.WriteFile(targetFile, []byte("foo bar baz"), 0777)).ToNot(HaveOccurred())
   131  						relativePath, err := filepath.Rel(dir, targetFile)
   132  
   133  						err = os.Symlink(relativePath, filepath.Join(dir, "symlinkFile"))
   134  						Expect(err).ToNot(HaveOccurred())
   135  
   136  						session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName, "--no-start")
   137  
   138  						Eventually(session).Should(Exit(0))
   139  					})
   140  
   141  					helpers.VerifyAppPackageContents(appName, "symlinkFile", "targetFile", "Staticfile", "index.html")
   142  				})
   143  			})
   144  
   145  			Context("when the file doesn't exists", func() {
   146  				It("should push the symlink", func() {
   147  					helpers.WithHelloWorldApp(func(dir string) {
   148  						tempFile, err := ioutil.TempFile(dir, "tempFile")
   149  						Expect(err).ToNot(HaveOccurred())
   150  						tempFile.Close()
   151  						relativePath, err := filepath.Rel(dir, tempFile.Name())
   152  
   153  						err = os.Symlink(relativePath, filepath.Join(dir, "symlinkFile"))
   154  						Expect(err).ToNot(HaveOccurred())
   155  						Expect(os.Remove(tempFile.Name()))
   156  
   157  						session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName, "--no-start")
   158  
   159  						Eventually(session).Should(Exit(0))
   160  					})
   161  
   162  					helpers.VerifyAppPackageContents(appName, "symlinkFile", "Staticfile", "index.html")
   163  				})
   164  			})
   165  		})
   166  
   167  		Context("when the directory contains a symlink to subdirectory in the directory", func() {
   168  			It("should push the symlink", func() {
   169  				helpers.WithHelloWorldApp(func(dir string) {
   170  					targetDir, err := ioutil.TempDir(dir, "target-dir")
   171  					Expect(err).ToNot(HaveOccurred())
   172  					relativePath, err := filepath.Rel(dir, targetDir)
   173  
   174  					err = os.Symlink(relativePath, filepath.Join(dir, "symlinkFile"))
   175  					Expect(err).ToNot(HaveOccurred())
   176  					Expect(os.RemoveAll(targetDir)).ToNot(HaveOccurred())
   177  
   178  					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName, "--no-start")
   179  
   180  					Eventually(session).Should(Exit(0))
   181  				})
   182  
   183  				helpers.VerifyAppPackageContents(appName, "symlinkFile", "Staticfile", "index.html")
   184  			})
   185  		})
   186  
   187  		PContext("when the directory contains a symlink to a file outside the directory", func() {
   188  			var targetPath string
   189  
   190  			BeforeEach(func() {
   191  				tmpFile, err := ioutil.TempFile("", "push-symlink-integration-")
   192  				Expect(err).ToNot(HaveOccurred())
   193  				tmpFile.Close()
   194  
   195  				targetPath = tmpFile.Name()
   196  			})
   197  
   198  			AfterEach(func() {
   199  				Expect(os.Remove(targetPath))
   200  			})
   201  
   202  			It("it should fail with an upload invalid error", func() {
   203  				helpers.WithHelloWorldApp(func(dir string) {
   204  					err := os.Symlink(targetPath, filepath.Join(dir, "symlinkFile"))
   205  					Expect(err).ToNot(HaveOccurred())
   206  
   207  					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName, "--no-start")
   208  
   209  					Eventually(session.Err).Should(Say("The app upload is invalid: Symlink\\(s\\) point outside of root folder"))
   210  					Eventually(session).Should(Exit(1))
   211  				})
   212  			})
   213  		})
   214  	})
   215  
   216  	Context("when pushing an archive", func() {
   217  		var archive string
   218  
   219  		AfterEach(func() {
   220  			Expect(os.RemoveAll(archive)).ToNot(HaveOccurred())
   221  		})
   222  
   223  		Context("when the archive contains a symlink to a file in the directory", func() {
   224  			BeforeEach(func() {
   225  				helpers.WithHelloWorldApp(func(appDir string) {
   226  					helpers.WithHelloWorldApp(func(appDir string) {
   227  						tmpfile, err := ioutil.TempFile("", "push-archive-integration")
   228  						Expect(err).ToNot(HaveOccurred())
   229  						archive = tmpfile.Name()
   230  						Expect(tmpfile.Close()).ToNot(HaveOccurred())
   231  
   232  						targetFile := filepath.Join(appDir, "targetFile")
   233  						Expect(ioutil.WriteFile(targetFile, []byte("some random data"), 0777)).ToNot(HaveOccurred())
   234  						relativePath, err := filepath.Rel(appDir, targetFile)
   235  
   236  						err = os.Symlink(relativePath, filepath.Join(appDir, "symlinkFile"))
   237  						Expect(err).ToNot(HaveOccurred())
   238  
   239  						err = helpers.Zipit(appDir, archive, "")
   240  						Expect(err).ToNot(HaveOccurred())
   241  					})
   242  				})
   243  			})
   244  
   245  			It("should push the symlink", func() {
   246  				session := helpers.CF(PushCommandName, appName, "--no-start", "-p", archive)
   247  
   248  				Eventually(session).Should(Exit(0))
   249  				helpers.VerifyAppPackageContents(appName, "symlinkFile", "targetFile", "Staticfile", "index.html")
   250  			})
   251  		})
   252  	})
   253  })