github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/integration/v7/push/docker_test.go (about) 1 package push 2 3 import ( 4 "io/ioutil" 5 "path/filepath" 6 7 "code.cloudfoundry.org/cli/integration/helpers" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 . "github.com/onsi/gomega/gbytes" 11 . "github.com/onsi/gomega/gexec" 12 ) 13 14 var _ = Describe("pushing docker images", func() { 15 var ( 16 appName string 17 tempDir string 18 ) 19 20 BeforeEach(func() { 21 appName = helpers.PrefixedRandomName("app") 22 var err error 23 tempDir, err = ioutil.TempDir("", "simple-manifest-test") 24 Expect(err).ToNot(HaveOccurred()) 25 }) 26 27 var AssertThatItPrintsSuccessfulDockerPushOutput = func(session *Session, dockerImage string) { 28 Eventually(session).Should(Say(`name:\s+%s`, appName)) 29 Eventually(session).Should(Say(`requested state:\s+started`)) 30 Eventually(session).Should(Say("stack:")) 31 Eventually(session).Should(Say(`docker image:\s+%s`, dockerImage)) 32 Eventually(session).Should(Exit(0)) 33 } 34 35 When("a public docker image is specified in command line", func() { 36 When("the docker image is valid", func() { 37 It("uses the specified docker image", func() { 38 session := helpers.CF(PushCommandName, appName, "-o", PublicDockerImage) 39 40 AssertThatItPrintsSuccessfulDockerPushOutput(session, PublicDockerImage) 41 }) 42 }) 43 44 When("the docker image is invalid", func() { 45 It("displays an error and exits 1", func() { 46 session := helpers.CF(PushCommandName, appName, "-o", "some-invalid-docker-image") 47 Eventually(session.Err).Should(Say("StagingError - Staging error: staging failed")) 48 Eventually(session).Should(Say("FAILED")) 49 Eventually(session).Should(Exit(1)) 50 }) 51 }) 52 }) 53 54 When("a public docker image is only provided in the manifest", func() { 55 When("the docker image is valid", func() { 56 It("uses the specified docker image", func() { 57 manifestPath := filepath.Join(tempDir, "manifest.yml") 58 59 helpers.WriteManifest(manifestPath, map[string]interface{}{ 60 "applications": []map[string]interface{}{ 61 { 62 "name": appName, 63 "docker": map[string]interface{}{ 64 "image": PublicDockerImage, 65 }, 66 }, 67 }, 68 }) 69 session := helpers.CF( 70 PushCommandName, appName, 71 "-f", manifestPath, 72 ) 73 74 AssertThatItPrintsSuccessfulDockerPushOutput(session, PublicDockerImage) 75 }) 76 }) 77 78 When("the docker image is invalid", func() { 79 It("displays an error and exits 1", func() { 80 manifestPath := filepath.Join(tempDir, "manifest.yml") 81 82 helpers.WriteManifest(manifestPath, map[string]interface{}{ 83 "applications": []map[string]interface{}{ 84 { 85 "name": appName, 86 "docker": map[string]interface{}{ 87 "image": "some-invalid-docker-image", 88 }, 89 }, 90 }, 91 }) 92 session := helpers.CF( 93 PushCommandName, appName, 94 "-f", manifestPath, 95 ) 96 97 Eventually(session.Err).Should(Say("StagingError - Staging error: staging failed")) 98 Eventually(session).Should(Say("FAILED")) 99 Eventually(session).Should(Exit(1)) 100 }) 101 }) 102 }) 103 When("a docker username and password are provided with a private image", func() { 104 var ( 105 privateDockerImage string 106 privateDockerUsername string 107 privateDockerPassword string 108 ) 109 110 BeforeEach(func() { 111 privateDockerImage, privateDockerUsername, privateDockerPassword = helpers.SkipIfPrivateDockerInfoNotSet() 112 }) 113 114 When("the docker password is provided via environment variable", func() { 115 It("uses the specified private docker image", func() { 116 session := helpers.CustomCF( 117 helpers.CFEnv{ 118 EnvVars: map[string]string{"CF_DOCKER_PASSWORD": privateDockerPassword}, 119 }, 120 PushCommandName, appName, 121 "--docker-username", privateDockerUsername, 122 "--docker-image", privateDockerImage, 123 ) 124 125 Eventually(session).Should(Say("Using docker repository password from environment variable CF_DOCKER_PASSWORD.")) 126 Consistently(session).ShouldNot(Say("Docker password")) 127 128 AssertThatItPrintsSuccessfulDockerPushOutput(session, privateDockerImage) 129 }) 130 }) 131 132 When("the docker password is not provided", func() { 133 var buffer *Buffer 134 135 BeforeEach(func() { 136 buffer = NewBuffer() 137 _, err := buffer.Write([]byte(privateDockerPassword + "\n")) 138 Expect(err).NotTo(HaveOccurred()) 139 }) 140 141 It("prompts for the docker password", func() { 142 session := helpers.CFWithStdin(buffer, 143 PushCommandName, 144 appName, 145 "--docker-username", privateDockerUsername, 146 "--docker-image", privateDockerImage, 147 ) 148 149 Eventually(session).Should(Say("Environment variable CF_DOCKER_PASSWORD not set.")) 150 Eventually(session).Should(Say("Docker password")) 151 152 AssertThatItPrintsSuccessfulDockerPushOutput(session, privateDockerImage) 153 }) 154 }) 155 }) 156 157 When("a docker username and private image are only provided in the manifest", func() { 158 var ( 159 privateDockerImage string 160 privateDockerUsername string 161 privateDockerPassword string 162 ) 163 164 BeforeEach(func() { 165 privateDockerImage, privateDockerUsername, privateDockerPassword = helpers.SkipIfPrivateDockerInfoNotSet() 166 }) 167 168 When("the docker password is provided via environment variable", func() { 169 It("uses the specified private docker image", func() { 170 manifestPath := filepath.Join(tempDir, "manifest.yml") 171 172 helpers.WriteManifest(manifestPath, map[string]interface{}{ 173 "applications": []map[string]interface{}{ 174 { 175 "name": appName, 176 "docker": map[string]interface{}{ 177 "image": privateDockerImage, 178 "username": privateDockerUsername, 179 }, 180 }, 181 }, 182 }) 183 session := helpers.CustomCF( 184 helpers.CFEnv{ 185 EnvVars: map[string]string{"CF_DOCKER_PASSWORD": privateDockerPassword}, 186 }, 187 PushCommandName, appName, 188 "-f", manifestPath, 189 ) 190 191 Eventually(session).Should(Say("Using docker repository password from environment variable CF_DOCKER_PASSWORD.")) 192 Consistently(session).ShouldNot(Say("Docker password")) 193 194 AssertThatItPrintsSuccessfulDockerPushOutput(session, privateDockerImage) 195 }) 196 }) 197 198 When("the docker password is not provided", func() { 199 var buffer *Buffer 200 201 BeforeEach(func() { 202 buffer = NewBuffer() 203 _, err := buffer.Write([]byte(privateDockerPassword + "\n")) 204 Expect(err).NotTo(HaveOccurred()) 205 }) 206 207 It("prompts for the docker password", func() { 208 manifestPath := filepath.Join(tempDir, "manifest.yml") 209 helpers.WriteManifest(manifestPath, map[string]interface{}{ 210 "applications": []map[string]interface{}{ 211 { 212 "name": appName, 213 "docker": map[string]interface{}{ 214 "image": privateDockerImage, 215 "username": privateDockerUsername, 216 }, 217 }, 218 }, 219 }) 220 session := helpers.CFWithStdin(buffer, 221 PushCommandName, 222 appName, 223 "-f", manifestPath, 224 ) 225 226 Eventually(session).Should(Say("Environment variable CF_DOCKER_PASSWORD not set.")) 227 Eventually(session).Should(Say("Docker password")) 228 229 AssertThatItPrintsSuccessfulDockerPushOutput(session, privateDockerImage) 230 }) 231 }) 232 }) 233 234 })