github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/push_command_with_manifest_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  var _ = Describe("Push with manifest", func() {
    16  	var (
    17  		appName           string
    18  		orgName           string
    19  		tempFile          string
    20  		oldDockerPassword string
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		orgName = helpers.NewOrgName()
    25  		spaceName := helpers.NewSpaceName()
    26  		setupCF(orgName, spaceName)
    27  
    28  		appName = helpers.PrefixedRandomName("app")
    29  		f, err := ioutil.TempFile("", "combination-manifest-with-p")
    30  		Expect(err).ToNot(HaveOccurred())
    31  		Expect(f.Close()).To(Succeed())
    32  		tempFile = f.Name()
    33  
    34  		oldDockerPassword = os.Getenv("CF_DOCKER_PASSWORD")
    35  		Expect(os.Setenv("CF_DOCKER_PASSWORD", "my-docker-password")).To(Succeed())
    36  	})
    37  
    38  	AfterEach(func() {
    39  		Expect(os.Setenv("CF_DOCKER_PASSWORD", oldDockerPassword)).To(Succeed())
    40  		Expect(os.Remove(tempFile)).ToNot(HaveOccurred())
    41  
    42  		helpers.QuickDeleteOrg(orgName)
    43  	})
    44  
    45  	Context("when the specified manifest file does not exist", func() {
    46  		It("displays a path does not exist error, help, and exits 1", func() {
    47  			session := helpers.CF("push", "-f", "./non-existent-file")
    48  			Eventually(session.Err).Should(Say("Incorrect Usage: The specified path './non-existent-file' does not exist."))
    49  			Eventually(session.Out).Should(Say("NAME:"))
    50  			Eventually(session.Out).Should(Say("USAGE:"))
    51  			Eventually(session).Should(Exit(1))
    52  		})
    53  	})
    54  
    55  	Context("when the same docker property is provided via both manifest and command line", func() {
    56  		Context("when manifest contains 'docker.image' and the '--docker-image' flag is provided", func() {
    57  			BeforeEach(func() {
    58  				manifestContents := []byte(fmt.Sprintf(`
    59  ---
    60  applications:
    61  - name: %s
    62    docker:
    63      image: some-image
    64  `, appName))
    65  				Expect(ioutil.WriteFile(tempFile, manifestContents, 0666)).To(Succeed())
    66  			})
    67  
    68  			It("overrides 'docker.image' in the manifest with the '-o' flag value", func() {
    69  				Eventually(helpers.CF("push", "-o", DockerImage, "-f", tempFile)).Should(Exit(0))
    70  
    71  				appGUID := helpers.AppGUID(appName)
    72  				session := helpers.CF("curl", fmt.Sprintf("/v2/apps/%s", appGUID))
    73  				Eventually(session.Out).Should(Say(DockerImage))
    74  				Eventually(session).Should(Exit(0))
    75  			})
    76  		})
    77  
    78  		Context("when manifest contains 'docker.username' and the '--docker-username' flag is provided", func() {
    79  			var buffer *Buffer
    80  
    81  			BeforeEach(func() {
    82  				buffer = NewBuffer()
    83  				_, err := buffer.Write([]byte("n\n"))
    84  				Expect(err).NotTo(HaveOccurred())
    85  
    86  				manifestContents := []byte(fmt.Sprintf(`
    87  ---
    88  applications:
    89  - name: %s
    90    docker:
    91      image: some-image
    92      username: some-user
    93  `, appName))
    94  				Expect(ioutil.WriteFile(tempFile, manifestContents, 0666)).To(Succeed())
    95  			})
    96  
    97  			It("overrides 'docker.username' in the manifest with the '--docker-username' flag value", func() {
    98  				Eventually(helpers.CFWithStdin(buffer, "push", "--docker-username", "some-other-user", "-f", tempFile)).Should(Exit())
    99  
   100  				appGUID := helpers.AppGUID(appName)
   101  				session := helpers.CF("curl", fmt.Sprintf("/v2/apps/%s", appGUID))
   102  				Eventually(session.Out).Should(Say("some-other-user"))
   103  				Eventually(session).Should(Exit(0))
   104  			})
   105  		})
   106  	})
   107  
   108  	Context("when the docker password is set in the environment", func() {
   109  		Context("when the docker image is provided via the command line and docker username is provided via the manifest", func() {
   110  			BeforeEach(func() {
   111  				manifestContents := []byte(fmt.Sprintf(`
   112  ---
   113  applications:
   114  - name: %s
   115    docker:
   116      username: some-other-user
   117  `, appName))
   118  				Expect(ioutil.WriteFile(tempFile, manifestContents, 0666)).To(Succeed())
   119  			})
   120  
   121  			It("pushes the app using the docker image from command line and username from manifest", func() {
   122  				Eventually(helpers.CF("push", "-o", DockerImage, "-f", tempFile)).Should(Exit())
   123  
   124  				appGUID := helpers.AppGUID(appName)
   125  				session := helpers.CF("curl", fmt.Sprintf("/v2/apps/%s", appGUID))
   126  				Eventually(session.Out).Should(Say(DockerImage))
   127  				Eventually(session.Out).Should(Say("some-other-user"))
   128  				Eventually(session).Should(Exit(0))
   129  			})
   130  		})
   131  
   132  		Context("when the docker image is provided via the manifest and docker username is provided via the command line", func() {
   133  			var buffer *Buffer
   134  
   135  			BeforeEach(func() {
   136  				buffer = NewBuffer()
   137  				_, err := buffer.Write([]byte("my-voice-is-my-passport\n"))
   138  				Expect(err).NotTo(HaveOccurred())
   139  
   140  				manifestContents := []byte(fmt.Sprintf(`
   141  ---
   142  applications:
   143  - name: %s
   144    docker:
   145      image: %s
   146  `, appName, DockerImage))
   147  				Expect(ioutil.WriteFile(tempFile, manifestContents, 0666)).To(Succeed())
   148  			})
   149  
   150  			It("pushes the app using the docker image from manifest and username from command line", func() {
   151  				Eventually(helpers.CFWithStdin(buffer, "push", "--docker-username", "some-user", "-f", tempFile)).Should(Exit())
   152  
   153  				appGUID := helpers.AppGUID(appName)
   154  				session := helpers.CF("curl", fmt.Sprintf("/v2/apps/%s", appGUID))
   155  				Eventually(session.Out).Should(Say(DockerImage))
   156  				Eventually(session.Out).Should(Say("some-user"))
   157  				Eventually(session).Should(Exit(0))
   158  			})
   159  		})
   160  	})
   161  
   162  	Context("when the docker password is not set in the environment", func() {
   163  		BeforeEach(func() {
   164  			Expect(os.Unsetenv("CF_DOCKER_PASSWORD")).To(Succeed())
   165  		})
   166  
   167  		Context("when the docker username is provided via the command line", func() {
   168  			var buffer *Buffer
   169  
   170  			BeforeEach(func() {
   171  				buffer = NewBuffer()
   172  				_, err := buffer.Write([]byte("my-voice-is-my-passport\n"))
   173  				Expect(err).NotTo(HaveOccurred())
   174  			})
   175  
   176  			It("prompts the user for the docker password", func() {
   177  				session := helpers.CFWithStdin(buffer, "push", appName, "--docker-image", DockerImage, "--docker-username", "some-user")
   178  				Eventually(session).Should(Say("Environment variable CF_DOCKER_PASSWORD not set\\."))
   179  				Eventually(session).Should(Exit())
   180  			})
   181  		})
   182  
   183  		Context("when the docker username is provided via the manifest", func() {
   184  			BeforeEach(func() {
   185  				manifestContents := []byte(fmt.Sprintf(`
   186  ---
   187  applications:
   188  - name: %s
   189    docker:
   190      image: some-image
   191      username: some-user
   192  `, appName))
   193  				Expect(ioutil.WriteFile(tempFile, manifestContents, 0666)).To(Succeed())
   194  			})
   195  
   196  			It("displays an error and exits 1", func() {
   197  				session := helpers.CF("push", "-f", tempFile)
   198  				Eventually(session).Should(Say("No Docker password was provided\\. Please provide the password by setting the CF_DOCKER_PASSWORD environment variable\\."))
   199  				Eventually(session).Should(Exit(1))
   200  			})
   201  		})
   202  	})
   203  
   204  	Context("when invalid manifest properties are provided together", func() {
   205  		Context("manifest contains both 'buildpack' and 'docker.image'", func() {
   206  			BeforeEach(func() {
   207  				manifestContents := []byte(fmt.Sprintf(`
   208  ---
   209  applications:
   210  - name: %s
   211    buildpack: staticfile_buildpack
   212    docker:
   213      image: %s
   214  `, appName, DockerImage))
   215  				Expect(ioutil.WriteFile(tempFile, manifestContents, 0666)).To(Succeed())
   216  			})
   217  
   218  			It("displays an error and exits 1", func() {
   219  				session := helpers.CF("push", appName, "-f", tempFile)
   220  				Eventually(session).Should(Say("FAILED"))
   221  				Eventually(session).Should(Say("Invalid application configuration:"))
   222  				Eventually(session).Should(Say("Application %s must not be configured with both 'buildpack' and 'docker'", appName))
   223  				Eventually(session).Should(Exit(1))
   224  			})
   225  		})
   226  
   227  		Context("manifest contains both 'docker.image' and 'path'", func() {
   228  			BeforeEach(func() {
   229  				manifestContents := []byte(fmt.Sprintf(`
   230  ---
   231  applications:
   232  - name: %s
   233    path: .
   234    docker:
   235      image: %s
   236  `, appName, DockerImage))
   237  				Expect(ioutil.WriteFile(tempFile, manifestContents, 0666)).To(Succeed())
   238  			})
   239  
   240  			It("displays an error and exits 1", func() {
   241  				session := helpers.CF("push", appName, "-f", tempFile)
   242  				Eventually(session).Should(Say("FAILED"))
   243  				Eventually(session).Should(Say("Invalid application configuration:"))
   244  				Eventually(session).Should(Say("Application %s must not be configured with both 'docker' and 'path'", appName))
   245  				Eventually(session).Should(Exit(1))
   246  			})
   247  		})
   248  	})
   249  })