|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package exec_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/aws/copilot-cli/e2e/internal/client" |
| 14 | + . "github.com/onsi/ginkgo" |
| 15 | + . "github.com/onsi/gomega" |
| 16 | +) |
| 17 | + |
| 18 | +var _ = Describe("exec flow", func() { |
| 19 | + Context("when creating a new app", func() { |
| 20 | + var ( |
| 21 | + initErr error |
| 22 | + ) |
| 23 | + BeforeAll(func() { |
| 24 | + _, initErr = cli.AppInit(&client.AppInitRequest{ |
| 25 | + AppName: appName, |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + It("app init succeeds", func() { |
| 30 | + Expect(initErr).NotTo(HaveOccurred()) |
| 31 | + }) |
| 32 | + |
| 33 | + It("app init creates an copilot directory and workspace file", func() { |
| 34 | + Expect("./copilot").Should(BeADirectory()) |
| 35 | + Expect("./copilot/.workspace").Should(BeAnExistingFile()) |
| 36 | + }) |
| 37 | + |
| 38 | + It("app ls includes new app", func() { |
| 39 | + Eventually(cli.AppList, "30s", "5s").Should(ContainSubstring(appName)) |
| 40 | + }) |
| 41 | + |
| 42 | + It("app show includes app name", func() { |
| 43 | + appShowOutput, err := cli.AppShow(appName) |
| 44 | + Expect(err).NotTo(HaveOccurred()) |
| 45 | + Expect(appShowOutput.Name).To(Equal(appName)) |
| 46 | + Expect(appShowOutput.URI).To(BeEmpty()) |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + Context("when creating a new environment", func() { |
| 51 | + var ( |
| 52 | + testEnvInitErr error |
| 53 | + ) |
| 54 | + BeforeAll(func() { |
| 55 | + _, testEnvInitErr = cli.EnvInit(&client.EnvInitRequest{ |
| 56 | + AppName: appName, |
| 57 | + EnvName: envName, |
| 58 | + Profile: "default", |
| 59 | + Prod: false, |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + It("env init should succeed", func() { |
| 64 | + Expect(testEnvInitErr).NotTo(HaveOccurred()) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + Context("when adding a svc", func() { |
| 69 | + var ( |
| 70 | + svcInitErr error |
| 71 | + ) |
| 72 | + BeforeAll(func() { |
| 73 | + _, svcInitErr = cli.SvcInit(&client.SvcInitRequest{ |
| 74 | + Name: svcName, |
| 75 | + SvcType: "Load Balanced Web Service", |
| 76 | + Dockerfile: "./hello/Dockerfile", |
| 77 | + SvcPort: "80", |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + It("svc init should succeed", func() { |
| 82 | + Expect(svcInitErr).NotTo(HaveOccurred()) |
| 83 | + }) |
| 84 | + |
| 85 | + It("svc init should create svc manifests", func() { |
| 86 | + Expect("./copilot/hello/manifest.yml").Should(BeAnExistingFile()) |
| 87 | + }) |
| 88 | + |
| 89 | + It("svc ls should list the service", func() { |
| 90 | + svcList, svcListError := cli.SvcList(appName) |
| 91 | + Expect(svcListError).NotTo(HaveOccurred()) |
| 92 | + Expect(len(svcList.Services)).To(Equal(1)) |
| 93 | + |
| 94 | + svcsByName := map[string]client.WkldDescription{} |
| 95 | + for _, svc := range svcList.Services { |
| 96 | + svcsByName[svc.Name] = svc |
| 97 | + } |
| 98 | + |
| 99 | + for _, svc := range []string{svcName} { |
| 100 | + Expect(svcsByName[svc].AppName).To(Equal(appName)) |
| 101 | + Expect(svcsByName[svc].Name).To(Equal(svc)) |
| 102 | + } |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + Context("when deploying svc", func() { |
| 107 | + const newContent = "HELP I AM TRAPPED INSIDE A SHELL" |
| 108 | + var ( |
| 109 | + appDeployErr error |
| 110 | + taskID1 string |
| 111 | + taskID2 string |
| 112 | + ) |
| 113 | + BeforeAll(func() { |
| 114 | + _, appDeployErr = cli.SvcDeploy(&client.SvcDeployInput{ |
| 115 | + Name: svcName, |
| 116 | + EnvName: envName, |
| 117 | + ImageTag: "gallopinggurdey", |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + It("svc deploy should succeed", func() { |
| 122 | + Expect(appDeployErr).NotTo(HaveOccurred()) |
| 123 | + }) |
| 124 | + |
| 125 | + It("svc status should show two tasks running", func() { |
| 126 | + svc, svcStatusErr := cli.SvcStatus(&client.SvcStatusRequest{ |
| 127 | + AppName: appName, |
| 128 | + Name: svcName, |
| 129 | + EnvName: envName, |
| 130 | + }) |
| 131 | + Expect(svcStatusErr).NotTo(HaveOccurred()) |
| 132 | + // Service should be active. |
| 133 | + Expect(svc.Service.Status).To(Equal("ACTIVE")) |
| 134 | + // Should have correct number of running tasks. |
| 135 | + Expect(len(svc.Tasks)).To(Equal(2)) |
| 136 | + taskID1 = svc.Tasks[0].ID |
| 137 | + taskID2 = svc.Tasks[1].ID |
| 138 | + }) |
| 139 | + |
| 140 | + It("svc show should include a valid URL and description for test env", func() { |
| 141 | + svc, err := cli.SvcShow(&client.SvcShowRequest{ |
| 142 | + AppName: appName, |
| 143 | + Name: svcName, |
| 144 | + }) |
| 145 | + Expect(err).NotTo(HaveOccurred()) |
| 146 | + Expect(len(svc.Routes)).To(Equal(1)) |
| 147 | + |
| 148 | + route := svc.Routes[0] |
| 149 | + Expect(route.Environment).To(Equal(envName)) |
| 150 | + var resp *http.Response |
| 151 | + var fetchErr error |
| 152 | + Eventually(func() (int, error) { |
| 153 | + resp, fetchErr = http.Get(route.URL) |
| 154 | + return resp.StatusCode, fetchErr |
| 155 | + }, "60s", "1s").Should(Equal(200)) |
| 156 | + // Read the response - our deployed service should return a body with its |
| 157 | + // name as the value. |
| 158 | + bodyBytes, err := ioutil.ReadAll(resp.Body) |
| 159 | + Expect(err).NotTo(HaveOccurred()) |
| 160 | + Expect(string(bodyBytes)).To(Equal(svcName)) |
| 161 | + }) |
| 162 | + |
| 163 | + It("svc exec should be able to modify the content of the website", func() { |
| 164 | + _, err := cli.SvcExec(&client.SvcExecRequest{ |
| 165 | + Name: svcName, |
| 166 | + AppName: appName, |
| 167 | + TaskID: taskID1, |
| 168 | + Command: fmt.Sprintf(`/bin/sh -c "echo '%s' > /usr/share/nginx/html/index.html"`, newContent), |
| 169 | + EnvName: envName, |
| 170 | + }) |
| 171 | + Expect(err).NotTo(HaveOccurred()) |
| 172 | + _, err = cli.SvcExec(&client.SvcExecRequest{ |
| 173 | + Name: svcName, |
| 174 | + AppName: appName, |
| 175 | + TaskID: taskID2, |
| 176 | + Command: fmt.Sprintf(`/bin/sh -c "echo '%s' > /usr/share/nginx/html/index.html"`, newContent), |
| 177 | + EnvName: envName, |
| 178 | + }) |
| 179 | + Expect(err).NotTo(HaveOccurred()) |
| 180 | + }) |
| 181 | + |
| 182 | + It("website content should be modified", func() { |
| 183 | + svc, err := cli.SvcShow(&client.SvcShowRequest{ |
| 184 | + AppName: appName, |
| 185 | + Name: svcName, |
| 186 | + }) |
| 187 | + Expect(err).NotTo(HaveOccurred()) |
| 188 | + Expect(len(svc.Routes)).To(Equal(1)) |
| 189 | + route := svc.Routes[0] |
| 190 | + |
| 191 | + for i := 0; i < 5; i++ { |
| 192 | + var resp *http.Response |
| 193 | + var fetchErr error |
| 194 | + Eventually(func() (int, error) { |
| 195 | + resp, fetchErr = http.Get(route.URL) |
| 196 | + return resp.StatusCode, fetchErr |
| 197 | + }, "60s", "1s").Should(Equal(200)) |
| 198 | + // Our deployed service should return a body with the new content |
| 199 | + // as the value. |
| 200 | + bodyBytes, err := ioutil.ReadAll(resp.Body) |
| 201 | + Expect(err).NotTo(HaveOccurred()) |
| 202 | + Expect(strings.TrimSpace(string(bodyBytes))).To(Equal(newContent)) |
| 203 | + time.Sleep(3 * time.Second) |
| 204 | + } |
| 205 | + }) |
| 206 | + |
| 207 | + It("svc logs should include exec logs", func() { |
| 208 | + var validTaskExecLogsCount int |
| 209 | + for i := 0; i < 10; i++ { |
| 210 | + var svcLogs []client.SvcLogsOutput |
| 211 | + var svcLogsErr error |
| 212 | + Eventually(func() ([]client.SvcLogsOutput, error) { |
| 213 | + svcLogs, svcLogsErr = cli.SvcLogs(&client.SvcLogsRequest{ |
| 214 | + AppName: appName, |
| 215 | + Name: svcName, |
| 216 | + EnvName: envName, |
| 217 | + Since: "1m", |
| 218 | + }) |
| 219 | + return svcLogs, svcLogsErr |
| 220 | + }, "60s", "10s").ShouldNot(BeEmpty()) |
| 221 | + var prevExecLogStreamName string |
| 222 | + for _, logLine := range svcLogs { |
| 223 | + Expect(logLine.Message).NotTo(Equal("")) |
| 224 | + Expect(logLine.LogStreamName).NotTo(Equal("")) |
| 225 | + Expect(logLine.Timestamp).NotTo(Equal(0)) |
| 226 | + Expect(logLine.IngestionTime).NotTo(Equal(0)) |
| 227 | + if strings.Contains(logLine.LogStreamName, "ecs-execute-command") && |
| 228 | + logLine.LogStreamName != prevExecLogStreamName { |
| 229 | + validTaskExecLogsCount++ |
| 230 | + prevExecLogStreamName = logLine.LogStreamName |
| 231 | + } |
| 232 | + } |
| 233 | + if validTaskExecLogsCount == 2 { |
| 234 | + break |
| 235 | + } |
| 236 | + validTaskExecLogsCount = 0 |
| 237 | + time.Sleep(5 * time.Second) |
| 238 | + } |
| 239 | + Expect(validTaskExecLogsCount).To(Equal(2)) |
| 240 | + }) |
| 241 | + }) |
| 242 | + |
| 243 | + Context("when running a one-off task", func() { |
| 244 | + var ( |
| 245 | + taskRunErr error |
| 246 | + ) |
| 247 | + BeforeAll(func() { |
| 248 | + _, taskRunErr = cli.TaskRun(&client.TaskRunInput{ |
| 249 | + GroupName: groupName, |
| 250 | + Dockerfile: "./DockerfileTask", |
| 251 | + AppName: appName, |
| 252 | + Env: envName, |
| 253 | + }) |
| 254 | + }) |
| 255 | + |
| 256 | + It("should succeed", func() { |
| 257 | + Expect(taskRunErr).NotTo(HaveOccurred()) |
| 258 | + }) |
| 259 | + |
| 260 | + It("task exec should work", func() { |
| 261 | + var resp string |
| 262 | + var err error |
| 263 | + Eventually(func() (string, error) { |
| 264 | + resp, err = cli.TaskExec(&client.TaskExecRequest{ |
| 265 | + Name: groupName, |
| 266 | + AppName: appName, |
| 267 | + Command: "ls", |
| 268 | + EnvName: envName, |
| 269 | + }) |
| 270 | + return resp, err |
| 271 | + }, "60s", "10s").ShouldNot(BeEmpty()) |
| 272 | + |
| 273 | + Expect(err).NotTo(HaveOccurred()) |
| 274 | + Expect(resp).To(ContainSubstring("hello")) |
| 275 | + }) |
| 276 | + }) |
| 277 | +}) |
0 commit comments