e2e-aci_test.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "io/ioutil"
  19. "math/rand"
  20. "net/http"
  21. "net/url"
  22. "os"
  23. "path/filepath"
  24. "runtime"
  25. "strconv"
  26. "strings"
  27. "syscall"
  28. "testing"
  29. "time"
  30. "gotest.tools/v3/assert"
  31. is "gotest.tools/v3/assert/cmp"
  32. "gotest.tools/v3/icmd"
  33. "gotest.tools/v3/poll"
  34. "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/resources/mgmt/resources"
  35. "github.com/Azure/azure-storage-file-go/azfile"
  36. "github.com/Azure/go-autorest/autorest/to"
  37. "github.com/prometheus/tsdb/fileutil"
  38. "github.com/docker/compose-cli/aci"
  39. "github.com/docker/compose-cli/aci/convert"
  40. "github.com/docker/compose-cli/aci/login"
  41. "github.com/docker/compose-cli/api/containers"
  42. "github.com/docker/compose-cli/context/store"
  43. "github.com/docker/compose-cli/errdefs"
  44. . "github.com/docker/compose-cli/tests/framework"
  45. )
  46. const (
  47. contextName = "aci-test"
  48. )
  49. var (
  50. binDir string
  51. location = []string{"eastus2"}
  52. )
  53. func TestMain(m *testing.M) {
  54. p, cleanup, err := SetupExistingCLI()
  55. if err != nil {
  56. fmt.Println(err)
  57. os.Exit(1)
  58. }
  59. binDir = p
  60. exitCode := m.Run()
  61. cleanup()
  62. os.Exit(exitCode)
  63. }
  64. // Cannot be parallelized as login/logout is global.
  65. func TestLoginLogout(t *testing.T) {
  66. startTime := strconv.Itoa(int(time.Now().UnixNano()))
  67. c := NewE2eCLI(t, binDir)
  68. rg := "E2E-" + startTime
  69. t.Run("login", func(t *testing.T) {
  70. azureLogin(t, c)
  71. })
  72. t.Run("create context", func(t *testing.T) {
  73. sID := getSubscriptionID(t)
  74. location := getTestLocation()
  75. err := createResourceGroup(t, sID, rg, location)
  76. assert.Check(t, is.Nil(err))
  77. t.Cleanup(func() {
  78. _ = deleteResourceGroup(t, rg)
  79. })
  80. c.RunDockerCmd("context", "create", "aci", contextName, "--subscription-id", sID, "--resource-group", rg, "--location", location)
  81. res := c.RunDockerCmd("context", "use", contextName)
  82. res.Assert(t, icmd.Expected{Out: contextName})
  83. res = c.RunDockerCmd("context", "ls")
  84. res.Assert(t, icmd.Expected{Out: contextName + " *"})
  85. })
  86. t.Run("delete context", func(t *testing.T) {
  87. res := c.RunDockerCmd("context", "use", "default")
  88. res.Assert(t, icmd.Expected{Out: "default"})
  89. res = c.RunDockerCmd("context", "rm", contextName)
  90. res.Assert(t, icmd.Expected{Out: contextName})
  91. })
  92. t.Run("logout", func(t *testing.T) {
  93. _, err := os.Stat(login.GetTokenStorePath())
  94. assert.NilError(t, err)
  95. res := c.RunDockerCmd("logout", "azure")
  96. res.Assert(t, icmd.Expected{Out: "Removing login credentials for Azure"})
  97. _, err = os.Stat(login.GetTokenStorePath())
  98. errMsg := "no such file or directory"
  99. if runtime.GOOS == "windows" {
  100. errMsg = "The system cannot find the file specified"
  101. }
  102. assert.ErrorContains(t, err, errMsg)
  103. })
  104. t.Run("create context fail", func(t *testing.T) {
  105. res := c.RunDockerOrExitError("context", "create", "aci", "fail-context")
  106. res.Assert(t, icmd.Expected{
  107. ExitCode: errdefs.ExitCodeLoginRequired,
  108. Err: `not logged in to azure, you need to run "docker login azure" first`,
  109. })
  110. })
  111. }
  112. func getTestLocation() string {
  113. rand.Seed(time.Now().Unix())
  114. n := rand.Intn(len(location))
  115. return location[n]
  116. }
  117. func uploadTestFile(t *testing.T, aciContext store.AciContext, accountName string, fileshareName string, testFileName string, testFileContent string) {
  118. storageLogin := login.StorageLoginImpl{AciContext: aciContext}
  119. key, err := storageLogin.GetAzureStorageAccountKey(context.TODO(), accountName)
  120. assert.NilError(t, err)
  121. cred, err := azfile.NewSharedKeyCredential(accountName, key)
  122. assert.NilError(t, err)
  123. u, _ := url.Parse(fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, fileshareName))
  124. uploadFile(t, *cred, u.String(), testFileName, testFileContent)
  125. }
  126. const (
  127. fileshareName = "dockertestshare"
  128. fileshareName2 = "dockertestshare2"
  129. )
  130. func TestRunVolume(t *testing.T) {
  131. const (
  132. testFileContent = "Volume mounted successfully!"
  133. testFileName = "index.html"
  134. )
  135. c := NewParallelE2eCLI(t, binDir)
  136. sID, rg, location := setupTestResourceGroup(t, c)
  137. // Bootstrap volume
  138. aciContext := store.AciContext{
  139. SubscriptionID: sID,
  140. Location: location,
  141. ResourceGroup: rg,
  142. }
  143. // Used in subtests
  144. var (
  145. container string
  146. hostIP string
  147. endpoint string
  148. volumeID string
  149. accountName = "e2e" + strconv.Itoa(int(time.Now().UnixNano()))
  150. )
  151. t.Run("check empty volume name validity", func(t *testing.T) {
  152. invalidName := ""
  153. res := c.RunDockerOrExitError("volume", "create", "--storage-account", invalidName, fileshareName)
  154. res.Assert(t, icmd.Expected{
  155. ExitCode: 1,
  156. Err: `parameter=accountName constraint=MinLength value="" details: value length must be greater than or equal to 3`,
  157. })
  158. })
  159. t.Run("check volume name validity", func(t *testing.T) {
  160. invalidName := "some-storage-123"
  161. res := c.RunDockerOrExitError("volume", "create", "--storage-account", invalidName, fileshareName)
  162. res.Assert(t, icmd.Expected{
  163. ExitCode: 1,
  164. Err: "some-storage-123 is not a valid storage account name. Storage account name must be between 3 and 24 characters in length and use numbers and lower-case letters only.",
  165. })
  166. })
  167. t.Run("create volumes", func(t *testing.T) {
  168. c.RunDockerCmd("volume", "create", "--storage-account", accountName, fileshareName)
  169. })
  170. volumeID = accountName + "/" + fileshareName
  171. t.Cleanup(func() {
  172. c.RunDockerCmd("volume", "rm", volumeID)
  173. res := c.RunDockerCmd("volume", "ls")
  174. lines := lines(res.Stdout())
  175. assert.Equal(t, len(lines), 1)
  176. })
  177. t.Run("create second fileshare", func(t *testing.T) {
  178. c.RunDockerCmd("volume", "create", "--storage-account", accountName, fileshareName2)
  179. })
  180. volumeID2 := accountName + "/" + fileshareName2
  181. t.Run("list volumes", func(t *testing.T) {
  182. res := c.RunDockerCmd("volume", "ls", "--quiet")
  183. l := lines(res.Stdout())
  184. assert.Equal(t, len(l), 2)
  185. assert.Equal(t, l[0], volumeID)
  186. assert.Equal(t, l[1], volumeID2)
  187. res = c.RunDockerCmd("volume", "ls")
  188. l = lines(res.Stdout())
  189. assert.Equal(t, len(l), 3)
  190. firstAccount := l[1]
  191. fields := strings.Fields(firstAccount)
  192. assert.Equal(t, fields[0], volumeID)
  193. secondAccount := l[2]
  194. fields = strings.Fields(secondAccount)
  195. assert.Equal(t, fields[0], volumeID2)
  196. })
  197. t.Run("inspect volumes", func(t *testing.T) {
  198. res := c.RunDockerCmd("volume", "inspect", volumeID)
  199. assert.Equal(t, res.Stdout(), fmt.Sprintf(`{
  200. "ID": %q,
  201. "Description": "Fileshare %s in %s storage account"
  202. }
  203. `, volumeID, fileshareName, accountName))
  204. res = c.RunDockerCmd("volume", "inspect", volumeID2)
  205. assert.Equal(t, res.Stdout(), fmt.Sprintf(`{
  206. "ID": %q,
  207. "Description": "Fileshare %s in %s storage account"
  208. }
  209. `, volumeID2, fileshareName2, accountName))
  210. })
  211. t.Run("delete only fileshare", func(t *testing.T) {
  212. c.RunDockerCmd("volume", "rm", volumeID2)
  213. res := c.RunDockerCmd("volume", "ls")
  214. lines := lines(res.Stdout())
  215. assert.Equal(t, len(lines), 2)
  216. assert.Assert(t, !strings.Contains(res.Stdout(), fileshareName2), "second fileshare still visible after rm")
  217. })
  218. t.Run("upload file", func(t *testing.T) {
  219. uploadTestFile(t, aciContext, accountName, fileshareName, testFileName, testFileContent)
  220. })
  221. t.Run("run", func(t *testing.T) {
  222. mountTarget := "/usr/share/nginx/html"
  223. res := c.RunDockerCmd(
  224. "run", "-d",
  225. "-v", fmt.Sprintf("%s:%s", volumeID, mountTarget),
  226. "-p", "80:80",
  227. "nginx",
  228. )
  229. container = getContainerName(res.Stdout())
  230. })
  231. t.Run("inspect", func(t *testing.T) {
  232. res := c.RunDockerCmd("inspect", container)
  233. containerInspect, err := ParseContainerInspect(res.Stdout())
  234. assert.NilError(t, err)
  235. assert.Equal(t, containerInspect.Platform, "Linux")
  236. assert.Equal(t, containerInspect.HostConfig.CPULimit, 1.0)
  237. assert.Equal(t, containerInspect.HostConfig.CPUReservation, 1.0)
  238. assert.Equal(t, containerInspect.HostConfig.RestartPolicy, containers.RestartPolicyNone)
  239. assert.Assert(t, is.Len(containerInspect.Ports, 1))
  240. hostIP = containerInspect.Ports[0].HostIP
  241. endpoint = fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
  242. })
  243. t.Run("ps", func(t *testing.T) {
  244. res := c.RunDockerCmd("ps")
  245. out := lines(res.Stdout())
  246. l := out[len(out)-1]
  247. assert.Assert(t, strings.Contains(l, container), "Looking for %q in line: %s", container, l)
  248. assert.Assert(t, strings.Contains(l, "nginx"))
  249. assert.Assert(t, strings.Contains(l, "Running"))
  250. assert.Assert(t, strings.Contains(l, hostIP+":80->80/tcp"))
  251. })
  252. t.Run("http get", func(t *testing.T) {
  253. output := HTTPGetWithRetry(t, endpoint, http.StatusOK, 2*time.Second, 20*time.Second)
  254. assert.Assert(t, strings.Contains(output, testFileContent), "Actual content: "+output)
  255. })
  256. t.Run("logs", func(t *testing.T) {
  257. res := c.RunDockerCmd("logs", container)
  258. res.Assert(t, icmd.Expected{Out: "GET"})
  259. })
  260. t.Run("exec", func(t *testing.T) {
  261. res := c.RunDockerOrExitError("exec", container, "pwd")
  262. assert.Assert(t, strings.Contains(res.Stdout(), "/"))
  263. res = c.RunDockerOrExitError("exec", container, "echo", "fail_with_argument")
  264. res.Assert(t, icmd.Expected{
  265. ExitCode: 1,
  266. Err: "ACI exec command does not accept arguments to the command. Only the binary should be specified",
  267. })
  268. })
  269. t.Run("logs follow", func(t *testing.T) {
  270. cmd := c.NewDockerCmd("logs", "--follow", container)
  271. res := icmd.StartCmd(cmd)
  272. checkUp := func(t poll.LogT) poll.Result {
  273. r, _ := http.Get(endpoint + "/is_up")
  274. if r != nil && r.StatusCode == http.StatusNotFound {
  275. return poll.Success()
  276. }
  277. return poll.Continue("waiting for container to serve request")
  278. }
  279. poll.WaitOn(t, checkUp, poll.WithDelay(1*time.Second), poll.WithTimeout(60*time.Second))
  280. assert.Assert(t, !strings.Contains(res.Stdout(), "/test"))
  281. checkLogs := func(t poll.LogT) poll.Result {
  282. if strings.Contains(res.Stdout(), "/test") {
  283. return poll.Success()
  284. }
  285. return poll.Continue("waiting for logs to contain /test")
  286. }
  287. // Do request on /test
  288. go func() {
  289. time.Sleep(3 * time.Second)
  290. _, _ = http.Get(endpoint + "/test")
  291. }()
  292. poll.WaitOn(t, checkLogs, poll.WithDelay(3*time.Second), poll.WithTimeout(20*time.Second))
  293. if runtime.GOOS == "windows" {
  294. err := res.Cmd.Process.Kill()
  295. assert.NilError(t, err)
  296. } else {
  297. err := res.Cmd.Process.Signal(syscall.SIGTERM)
  298. assert.NilError(t, err)
  299. }
  300. })
  301. t.Run("rm a running container", func(t *testing.T) {
  302. res := c.RunDockerOrExitError("rm", container)
  303. res.Assert(t, icmd.Expected{
  304. Err: fmt.Sprintf("Error: you cannot remove a running container %s. Stop the container before attempting removal or force remove", container),
  305. ExitCode: 1,
  306. })
  307. })
  308. t.Run("force rm", func(t *testing.T) {
  309. res := c.RunDockerCmd("rm", "-f", container)
  310. res.Assert(t, icmd.Expected{Out: container})
  311. checkStopped := func(t poll.LogT) poll.Result {
  312. res := c.RunDockerOrExitError("inspect", container)
  313. if res.ExitCode == 1 {
  314. return poll.Success()
  315. }
  316. return poll.Continue("waiting for container to stop")
  317. }
  318. poll.WaitOn(t, checkStopped, poll.WithDelay(5*time.Second), poll.WithTimeout(60*time.Second))
  319. })
  320. }
  321. func lines(output string) []string {
  322. return strings.Split(strings.TrimSpace(output), "\n")
  323. }
  324. func TestContainerRunAttached(t *testing.T) {
  325. c := NewParallelE2eCLI(t, binDir)
  326. _, groupID, location := setupTestResourceGroup(t, c)
  327. // Used in subtests
  328. var (
  329. container string = "test-container"
  330. endpoint string
  331. followLogsProcess *icmd.Result
  332. )
  333. t.Run("run attached limits", func(t *testing.T) {
  334. dnsLabelName := "nginx-" + groupID
  335. fqdn := dnsLabelName + "." + location + ".azurecontainer.io"
  336. cmd := c.NewDockerCmd(
  337. "run",
  338. "--name", container,
  339. "--restart", "on-failure",
  340. "--memory", "0.1G", "--cpus", "0.1",
  341. "-p", "80:80",
  342. "--domainname",
  343. dnsLabelName,
  344. "nginx",
  345. )
  346. followLogsProcess = icmd.StartCmd(cmd)
  347. checkRunning := func(t poll.LogT) poll.Result {
  348. res := c.RunDockerOrExitError("inspect", container)
  349. if res.ExitCode == 0 && strings.Contains(res.Stdout(), `"Status": "Running"`) && !strings.Contains(res.Stdout(), `"HostIP": ""`) {
  350. return poll.Success()
  351. }
  352. return poll.Continue("waiting for container to be running, current inspect result: \n%s", res.Combined())
  353. }
  354. poll.WaitOn(t, checkRunning, poll.WithDelay(5*time.Second), poll.WithTimeout(90*time.Second))
  355. inspectRes := c.RunDockerCmd("inspect", container)
  356. containerInspect, err := ParseContainerInspect(inspectRes.Stdout())
  357. assert.NilError(t, err)
  358. assert.Equal(t, containerInspect.Platform, "Linux")
  359. assert.Equal(t, containerInspect.HostConfig.CPULimit, 0.1)
  360. assert.Equal(t, containerInspect.HostConfig.MemoryLimit, uint64(107374182))
  361. assert.Equal(t, containerInspect.HostConfig.CPUReservation, 0.1)
  362. assert.Equal(t, containerInspect.HostConfig.MemoryReservation, uint64(107374182))
  363. assert.Equal(t, containerInspect.HostConfig.RestartPolicy, containers.RestartPolicyOnFailure)
  364. assert.Assert(t, is.Len(containerInspect.Ports, 1))
  365. port := containerInspect.Ports[0]
  366. assert.Assert(t, port.HostIP != "", "empty hostIP, inspect: \n"+inspectRes.Stdout())
  367. assert.Equal(t, port.ContainerPort, uint32(80))
  368. assert.Equal(t, port.HostPort, uint32(80))
  369. assert.Equal(t, containerInspect.Config.FQDN, fqdn)
  370. endpoint = fmt.Sprintf("http://%s:%d", fqdn, port.HostPort)
  371. assert.Assert(t, !strings.Contains(followLogsProcess.Stdout(), "/test"))
  372. HTTPGetWithRetry(t, endpoint+"/test", http.StatusNotFound, 2*time.Second, 60*time.Second)
  373. checkLog := func(t poll.LogT) poll.Result {
  374. if strings.Contains(followLogsProcess.Stdout(), "/test") {
  375. return poll.Success()
  376. }
  377. return poll.Continue("waiting for logs to contain /test")
  378. }
  379. poll.WaitOn(t, checkLog, poll.WithDelay(1*time.Second), poll.WithTimeout(20*time.Second))
  380. })
  381. t.Run("stop wrong container", func(t *testing.T) {
  382. res := c.RunDockerOrExitError("stop", "unknown-container")
  383. res.Assert(t, icmd.Expected{
  384. Err: "Error: container unknown-container not found",
  385. ExitCode: 1,
  386. })
  387. })
  388. t.Run("stop container", func(t *testing.T) {
  389. res := c.RunDockerCmd("stop", container)
  390. res.Assert(t, icmd.Expected{Out: container})
  391. waitForStatus(t, c, container, "Terminated", "Node Stopped")
  392. })
  393. t.Run("check we stoppped following logs", func(t *testing.T) {
  394. // nolint errcheck
  395. followLogsStopped := waitWithTimeout(func() { followLogsProcess.Cmd.Process.Wait() }, 10*time.Second)
  396. assert.NilError(t, followLogsStopped, "Follow logs process did not stop after container is stopped")
  397. })
  398. t.Run("ps stopped container with --all", func(t *testing.T) {
  399. res := c.RunDockerCmd("ps", container)
  400. out := lines(res.Stdout())
  401. assert.Assert(t, is.Len(out, 1))
  402. res = c.RunDockerCmd("ps", "--all", container)
  403. out = lines(res.Stdout())
  404. assert.Assert(t, is.Len(out, 2))
  405. })
  406. t.Run("restart container", func(t *testing.T) {
  407. res := c.RunDockerCmd("start", container)
  408. res.Assert(t, icmd.Expected{Out: container})
  409. waitForStatus(t, c, container, convert.StatusRunning)
  410. })
  411. t.Run("prune dry run", func(t *testing.T) {
  412. res := c.RunDockerCmd("prune", "--dry-run")
  413. assert.Equal(t, "Resources that would be deleted:\nTotal CPUs reclaimed: 0.00, total memory reclaimed: 0.00 GB\n", res.Stdout())
  414. res = c.RunDockerCmd("prune", "--dry-run", "--force")
  415. assert.Equal(t, "Resources that would be deleted:\n"+container+"\nTotal CPUs reclaimed: 0.10, total memory reclaimed: 0.10 GB\n", res.Stdout())
  416. })
  417. t.Run("prune", func(t *testing.T) {
  418. res := c.RunDockerCmd("prune")
  419. assert.Equal(t, "Deleted resources:\nTotal CPUs reclaimed: 0.00, total memory reclaimed: 0.00 GB\n", res.Stdout())
  420. res = c.RunDockerCmd("ps")
  421. l := lines(res.Stdout())
  422. assert.Equal(t, 2, len(l))
  423. res = c.RunDockerCmd("prune", "--force")
  424. assert.Equal(t, "Deleted resources:\n"+container+"\nTotal CPUs reclaimed: 0.10, total memory reclaimed: 0.10 GB\n", res.Stdout())
  425. res = c.RunDockerCmd("ps", "--all")
  426. l = lines(res.Stdout())
  427. assert.Equal(t, 1, len(l))
  428. })
  429. }
  430. func overwriteFileStorageAccount(t *testing.T, absComposefileName string, storageAccount string) {
  431. data, err := ioutil.ReadFile(absComposefileName)
  432. assert.NilError(t, err)
  433. override := strings.Replace(string(data), "dockertestvolumeaccount", storageAccount, 1)
  434. err = ioutil.WriteFile(absComposefileName, []byte(override), 0644)
  435. assert.NilError(t, err)
  436. }
  437. func TestUpResources(t *testing.T) {
  438. const (
  439. composeProjectName = "testresources"
  440. serverContainer = composeProjectName + "_web"
  441. wordsContainer = composeProjectName + "_words"
  442. )
  443. c := NewParallelE2eCLI(t, binDir)
  444. setupTestResourceGroup(t, c)
  445. t.Run("compose up", func(t *testing.T) {
  446. c.RunDockerCmd("compose", "up", "-f", "../composefiles/aci-demo/aci_demo_port_resources.yaml", "--project-name", composeProjectName)
  447. res := c.RunDockerCmd("inspect", serverContainer)
  448. webInspect, err := ParseContainerInspect(res.Stdout())
  449. assert.NilError(t, err)
  450. assert.Equal(t, webInspect.HostConfig.CPULimit, 0.7)
  451. assert.Equal(t, webInspect.HostConfig.MemoryLimit, uint64(1073741824))
  452. assert.Equal(t, webInspect.HostConfig.CPUReservation, 0.5)
  453. assert.Equal(t, webInspect.HostConfig.MemoryReservation, uint64(536870912))
  454. res = c.RunDockerCmd("inspect", wordsContainer)
  455. wordsInspect, err := ParseContainerInspect(res.Stdout())
  456. assert.NilError(t, err)
  457. assert.Equal(t, wordsInspect.HostConfig.CPULimit, 0.5)
  458. assert.Equal(t, wordsInspect.HostConfig.MemoryLimit, uint64(751619276))
  459. assert.Equal(t, wordsInspect.HostConfig.CPUReservation, 0.5)
  460. assert.Equal(t, wordsInspect.HostConfig.MemoryReservation, uint64(751619276))
  461. })
  462. }
  463. func TestUpSecrets(t *testing.T) {
  464. const (
  465. composeProjectName = "aci_secrets"
  466. serverContainer = composeProjectName + "_web"
  467. secret1Name = "mytarget1"
  468. secret1Value = "myPassword1\n"
  469. secret2Name = "mysecret2"
  470. secret2Value = "another_password\n"
  471. )
  472. var (
  473. basefilePath = filepath.Join("..", "composefiles", composeProjectName)
  474. composefilePath = filepath.Join(basefilePath, "compose.yml")
  475. )
  476. c := NewParallelE2eCLI(t, binDir)
  477. _, _, _ = setupTestResourceGroup(t, c)
  478. t.Run("compose up", func(t *testing.T) {
  479. c.RunDockerCmd("compose", "up", "-f", composefilePath, "--project-name", composeProjectName)
  480. res := c.RunDockerCmd("ps")
  481. out := lines(res.Stdout())
  482. // Check one container running
  483. assert.Assert(t, is.Len(out, 2))
  484. webRunning := false
  485. for _, l := range out {
  486. if strings.Contains(l, serverContainer) {
  487. webRunning = true
  488. strings.Contains(l, ":80->80/tcp")
  489. }
  490. }
  491. assert.Assert(t, webRunning, "web container not running ; ps:\n"+res.Stdout())
  492. res = c.RunDockerCmd("inspect", serverContainer)
  493. containerInspect, err := ParseContainerInspect(res.Stdout())
  494. assert.NilError(t, err)
  495. assert.Assert(t, is.Len(containerInspect.Ports, 1))
  496. endpoint := fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
  497. output := HTTPGetWithRetry(t, endpoint+"/"+secret1Name, http.StatusOK, 2*time.Second, 20*time.Second)
  498. // replace windows carriage return
  499. output = strings.ReplaceAll(output, "\r", "")
  500. assert.Equal(t, output, secret1Value)
  501. output = HTTPGetWithRetry(t, endpoint+"/"+secret2Name, http.StatusOK, 2*time.Second, 20*time.Second)
  502. output = strings.ReplaceAll(output, "\r", "")
  503. assert.Equal(t, output, secret2Value)
  504. t.Cleanup(func() {
  505. c.RunDockerCmd("compose", "down", "--project-name", composeProjectName)
  506. res := c.RunDockerCmd("ps")
  507. out := lines(res.Stdout())
  508. assert.Equal(t, len(out), 1)
  509. })
  510. })
  511. }
  512. func TestUpUpdate(t *testing.T) {
  513. const (
  514. composeProjectName = "acidemo"
  515. serverContainer = composeProjectName + "_web"
  516. wordsContainer = composeProjectName + "_words"
  517. dbContainer = composeProjectName + "_db"
  518. )
  519. var (
  520. singlePortVolumesComposefile = "aci_demo_port_volumes.yaml"
  521. multiPortComposefile = "aci_demo_multi_port.yaml"
  522. )
  523. c := NewParallelE2eCLI(t, binDir)
  524. sID, groupID, location := setupTestResourceGroup(t, c)
  525. composeAccountName := groupID + "-sa"
  526. composeAccountName = strings.ReplaceAll(composeAccountName, "-", "")
  527. composeAccountName = strings.ToLower(composeAccountName)
  528. dstDir := filepath.Join(os.TempDir(), "e2e-aci-volume-"+composeAccountName)
  529. srcDir := filepath.Join("..", "composefiles", "aci-demo")
  530. err := fileutil.CopyDirs(srcDir, dstDir)
  531. assert.NilError(t, err)
  532. t.Cleanup(func() {
  533. assert.NilError(t, os.RemoveAll(dstDir))
  534. })
  535. singlePortVolumesComposefile = filepath.Join(dstDir, singlePortVolumesComposefile)
  536. overwriteFileStorageAccount(t, singlePortVolumesComposefile, composeAccountName)
  537. multiPortComposefile = filepath.Join(dstDir, multiPortComposefile)
  538. volumeID := composeAccountName + "/" + fileshareName
  539. t.Run("compose up", func(t *testing.T) {
  540. const (
  541. testFileName = "msg.txt"
  542. testFileContent = "VOLUME_OK"
  543. projectName = "acidemo"
  544. )
  545. c.RunDockerCmd("volume", "create", "--storage-account", composeAccountName, fileshareName)
  546. // Bootstrap volume
  547. aciContext := store.AciContext{
  548. SubscriptionID: sID,
  549. Location: location,
  550. ResourceGroup: groupID,
  551. }
  552. uploadTestFile(t, aciContext, composeAccountName, fileshareName, testFileName, testFileContent)
  553. dnsLabelName := "nginx-" + groupID
  554. fqdn := dnsLabelName + "." + location + ".azurecontainer.io"
  555. // Name of Compose project is taken from current folder "acie2e"
  556. c.RunDockerCmd("compose", "up", "-f", singlePortVolumesComposefile, "--domainname", dnsLabelName, "--project-name", projectName)
  557. res := c.RunDockerCmd("ps")
  558. out := lines(res.Stdout())
  559. // Check three containers are running
  560. assert.Assert(t, is.Len(out, 4))
  561. webRunning := false
  562. for _, l := range out {
  563. if strings.Contains(l, serverContainer) {
  564. webRunning = true
  565. strings.Contains(l, ":80->80/tcp")
  566. }
  567. }
  568. assert.Assert(t, webRunning, "web container not running ; ps:\n"+res.Stdout())
  569. res = c.RunDockerCmd("inspect", serverContainer)
  570. containerInspect, err := ParseContainerInspect(res.Stdout())
  571. assert.NilError(t, err)
  572. assert.Assert(t, is.Len(containerInspect.Ports, 1))
  573. endpoint := fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
  574. output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
  575. assert.Assert(t, strings.Contains(output, `"word":`))
  576. endpoint = fmt.Sprintf("http://%s:%d", fqdn, containerInspect.Ports[0].HostPort)
  577. HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
  578. body := HTTPGetWithRetry(t, endpoint+"/volume_test/"+testFileName, http.StatusOK, 2*time.Second, 20*time.Second)
  579. assert.Assert(t, strings.Contains(body, testFileContent))
  580. // Try to remove the volume while it's still in use
  581. res = c.RunDockerOrExitError("volume", "rm", volumeID)
  582. res.Assert(t, icmd.Expected{
  583. ExitCode: 1,
  584. Err: fmt.Sprintf(`Error: volume "%s/%s" is used in container group %q`,
  585. composeAccountName, fileshareName, projectName),
  586. })
  587. })
  588. t.Cleanup(func() {
  589. c.RunDockerCmd("volume", "rm", volumeID)
  590. })
  591. t.Run("compose ps", func(t *testing.T) {
  592. res := c.RunDockerCmd("compose", "ps", "--project-name", composeProjectName, "--quiet")
  593. l := lines(res.Stdout())
  594. assert.Assert(t, is.Len(l, 3))
  595. res = c.RunDockerCmd("compose", "ps", "--project-name", composeProjectName)
  596. l = lines(res.Stdout())
  597. assert.Assert(t, is.Len(l, 4))
  598. var wordsDisplayed, webDisplayed, dbDisplayed bool
  599. for _, line := range l {
  600. fields := strings.Fields(line)
  601. containerID := fields[0]
  602. switch containerID {
  603. case wordsContainer:
  604. wordsDisplayed = true
  605. assert.DeepEqual(t, fields, []string{containerID, "words", "1/1"})
  606. case dbContainer:
  607. dbDisplayed = true
  608. assert.DeepEqual(t, fields, []string{containerID, "db", "1/1"})
  609. case serverContainer:
  610. webDisplayed = true
  611. assert.Equal(t, fields[1], "web")
  612. assert.Check(t, strings.Contains(fields[3], ":80->80/tcp"))
  613. }
  614. }
  615. assert.Check(t, webDisplayed && wordsDisplayed && dbDisplayed, "\n%s\n", res.Stdout())
  616. })
  617. t.Run("compose ls", func(t *testing.T) {
  618. res := c.RunDockerCmd("compose", "ls", "--quiet")
  619. l := lines(res.Stdout())
  620. assert.Assert(t, is.Len(l, 1))
  621. res = c.RunDockerCmd("compose", "ls")
  622. l = lines(res.Stdout())
  623. assert.Equal(t, 2, len(l))
  624. fields := strings.Fields(l[1])
  625. assert.Equal(t, 2, len(fields))
  626. assert.Equal(t, fields[0], composeProjectName)
  627. assert.Equal(t, "Running", fields[1])
  628. })
  629. t.Run("logs web", func(t *testing.T) {
  630. res := c.RunDockerCmd("logs", serverContainer)
  631. res.Assert(t, icmd.Expected{Out: "Listening on port 80"})
  632. })
  633. t.Run("update", func(t *testing.T) {
  634. c.RunDockerCmd("compose", "up", "-f", multiPortComposefile, "--project-name", composeProjectName)
  635. res := c.RunDockerCmd("ps")
  636. out := lines(res.Stdout())
  637. // Check three containers are running
  638. assert.Assert(t, is.Len(out, 4))
  639. for _, cName := range []string{serverContainer, wordsContainer} {
  640. res = c.RunDockerCmd("inspect", cName)
  641. containerInspect, err := ParseContainerInspect(res.Stdout())
  642. assert.NilError(t, err)
  643. assert.Assert(t, is.Len(containerInspect.Ports, 1))
  644. endpoint := fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
  645. var route string
  646. switch cName {
  647. case serverContainer:
  648. route = "/words/noun"
  649. assert.Equal(t, containerInspect.Ports[0].HostPort, uint32(80))
  650. assert.Equal(t, containerInspect.Ports[0].ContainerPort, uint32(80))
  651. case wordsContainer:
  652. route = "/noun"
  653. assert.Equal(t, containerInspect.Ports[0].HostPort, uint32(8080))
  654. assert.Equal(t, containerInspect.Ports[0].ContainerPort, uint32(8080))
  655. }
  656. HTTPGetWithRetry(t, endpoint+route, http.StatusOK, 1*time.Second, 60*time.Second)
  657. res = c.RunDockerCmd("ps")
  658. p := containerInspect.Ports[0]
  659. res.Assert(t, icmd.Expected{
  660. Out: fmt.Sprintf("%s:%d->%d/tcp", p.HostIP, p.HostPort, p.ContainerPort),
  661. })
  662. }
  663. })
  664. t.Run("down", func(t *testing.T) {
  665. c.RunDockerCmd("compose", "down", "--project-name", composeProjectName)
  666. res := c.RunDockerCmd("ps")
  667. out := lines(res.Stdout())
  668. assert.Equal(t, len(out), 1)
  669. })
  670. }
  671. /*
  672. func TestRunEnvVars(t *testing.T) {
  673. c := NewParallelE2eCLI(t, binDir)
  674. _, _, _ = setupTestResourceGroup(t, c)
  675. t.Run("run", func(t *testing.T) {
  676. cmd := c.NewDockerCmd(
  677. "run", "-d",
  678. "-e", "MYSQL_ROOT_PASSWORD=rootpwd",
  679. "-e", "MYSQL_DATABASE=mytestdb",
  680. "-e", "MYSQL_USER",
  681. "-e", "MYSQL_PASSWORD=userpwd",
  682. "-e", "DATASOURCE_URL=jdbc:mysql://mydb.mysql.database.azure.com/db1?useSSL=true&requireSSL=false&serverTimezone=America/Recife",
  683. "mysql:5.7",
  684. )
  685. cmd.Env = append(cmd.Env, "MYSQL_USER=user1")
  686. res := icmd.RunCmd(cmd)
  687. res.Assert(t, icmd.Success)
  688. out := lines(res.Stdout())
  689. container := strings.TrimSpace(out[len(out)-1])
  690. res = c.RunDockerCmd("inspect", container)
  691. containerInspect, err := ParseContainerInspect(res.Stdout())
  692. assert.NilError(t, err)
  693. assert.Assert(t, containerInspect.Config != nil, "nil container config")
  694. assert.Assert(t, containerInspect.Config.Env != nil, "nil container env variables")
  695. assert.Equal(t, containerInspect.Image, "mysql:5.7")
  696. envVars := containerInspect.Config.Env
  697. assert.Equal(t, len(envVars), 5)
  698. assert.Equal(t, envVars["MYSQL_ROOT_PASSWORD"], "rootpwd")
  699. assert.Equal(t, envVars["MYSQL_DATABASE"], "mytestdb")
  700. assert.Equal(t, envVars["MYSQL_USER"], "user1")
  701. assert.Equal(t, envVars["MYSQL_PASSWORD"], "userpwd")
  702. assert.Equal(t, envVars["DATASOURCE_URL"], "jdbc:mysql://mydb.mysql.database.azure.com/db1?useSSL=true&requireSSL=false&serverTimezone=America/Recife")
  703. check := func(t poll.LogT) poll.Result {
  704. res := c.RunDockerOrExitError("logs", container)
  705. if strings.Contains(res.Stdout(), "Giving user user1 access to schema mytestdb") {
  706. return poll.Success()
  707. }
  708. return poll.Continue("waiting for DB container to be up\n%s", res.Combined())
  709. }
  710. poll.WaitOn(t, check, poll.WithDelay(5*time.Second), poll.WithTimeout(90*time.Second))
  711. })
  712. }
  713. */
  714. func setupTestResourceGroup(t *testing.T, c *E2eCLI) (string, string, string) {
  715. startTime := strconv.Itoa(int(time.Now().Unix()))
  716. rg := "E2E-" + t.Name() + "-" + startTime[5:]
  717. azureLogin(t, c)
  718. sID := getSubscriptionID(t)
  719. location := getTestLocation()
  720. err := createResourceGroup(t, sID, rg, location)
  721. assert.Check(t, is.Nil(err))
  722. t.Cleanup(func() {
  723. if err := deleteResourceGroup(t, rg); err != nil {
  724. t.Error(err)
  725. }
  726. })
  727. createAciContextAndUseIt(t, c, sID, rg, location)
  728. // Check nothing is running
  729. res := c.RunDockerCmd("ps")
  730. assert.Assert(t, is.Len(lines(res.Stdout()), 1))
  731. return sID, rg, location
  732. }
  733. func deleteResourceGroup(t *testing.T, rgName string) error {
  734. fmt.Printf(" [%s] deleting resource group %s\n", t.Name(), rgName)
  735. ctx := context.TODO()
  736. helper := aci.NewACIResourceGroupHelper()
  737. models, err := helper.GetSubscriptionIDs(ctx)
  738. if err != nil {
  739. return err
  740. }
  741. if len(models) == 0 {
  742. return errors.New("unable to delete resource group: no models")
  743. }
  744. return helper.DeleteAsync(ctx, *models[0].SubscriptionID, rgName)
  745. }
  746. func azureLogin(t *testing.T, c *E2eCLI) {
  747. // in order to create new service principal and get these 3 values : `az ad sp create-for-rbac --name 'TestServicePrincipal' --sdk-auth`
  748. clientID := os.Getenv("AZURE_CLIENT_ID")
  749. clientSecret := os.Getenv("AZURE_CLIENT_SECRET")
  750. tenantID := os.Getenv("AZURE_TENANT_ID")
  751. assert.Check(t, clientID != "", "AZURE_CLIENT_ID must not be empty")
  752. assert.Check(t, clientSecret != "", "AZURE_CLIENT_SECRET must not be empty")
  753. assert.Check(t, tenantID != "", "AZURE_TENANT_ID must not be empty")
  754. c.RunDockerCmd("login", "azure", "--client-id", clientID, "--client-secret", clientSecret, "--tenant-id", tenantID)
  755. }
  756. func getSubscriptionID(t *testing.T) string {
  757. ctx := context.TODO()
  758. helper := aci.NewACIResourceGroupHelper()
  759. models, err := helper.GetSubscriptionIDs(ctx)
  760. assert.Check(t, is.Nil(err))
  761. assert.Check(t, len(models) == 1)
  762. return *models[0].SubscriptionID
  763. }
  764. func createResourceGroup(t *testing.T, sID, rgName string, location string) error {
  765. fmt.Printf(" [%s] creating resource group %s\n", t.Name(), rgName)
  766. helper := aci.NewACIResourceGroupHelper()
  767. _, err := helper.CreateOrUpdate(context.TODO(), sID, rgName, resources.Group{Location: to.StringPtr(location)})
  768. return err
  769. }
  770. func createAciContextAndUseIt(t *testing.T, c *E2eCLI, sID, rgName string, location string) {
  771. res := c.RunDockerCmd("context", "create", "aci", contextName, "--subscription-id", sID, "--resource-group", rgName, "--location", location)
  772. res.Assert(t, icmd.Expected{Out: "Successfully created aci context \"" + contextName + "\""})
  773. res = c.RunDockerCmd("context", "use", contextName)
  774. res.Assert(t, icmd.Expected{Out: contextName})
  775. res = c.RunDockerCmd("context", "ls")
  776. res.Assert(t, icmd.Expected{Out: contextName + " *"})
  777. }
  778. func uploadFile(t *testing.T, cred azfile.SharedKeyCredential, baseURL, fileName, content string) {
  779. fURL, err := url.Parse(baseURL + "/" + fileName)
  780. assert.NilError(t, err)
  781. fileURL := azfile.NewFileURL(*fURL, azfile.NewPipeline(&cred, azfile.PipelineOptions{}))
  782. err = azfile.UploadBufferToAzureFile(context.TODO(), []byte(content), fileURL, azfile.UploadToAzureFileOptions{})
  783. assert.NilError(t, err)
  784. }
  785. func getContainerName(stdout string) string {
  786. out := lines(stdout)
  787. return strings.TrimSpace(out[len(out)-1])
  788. }
  789. func waitForStatus(t *testing.T, c *E2eCLI, containerID string, statuses ...string) {
  790. checkStopped := func(logt poll.LogT) poll.Result {
  791. res := c.RunDockerCmd("inspect", containerID)
  792. containerInspect, err := ParseContainerInspect(res.Stdout())
  793. assert.NilError(t, err)
  794. for _, status := range statuses {
  795. if containerInspect.Status == status {
  796. return poll.Success()
  797. }
  798. }
  799. return poll.Continue("Status %s != %s (expected) for container %s", containerInspect.Status, statuses, containerID)
  800. }
  801. poll.WaitOn(t, checkStopped, poll.WithDelay(5*time.Second), poll.WithTimeout(90*time.Second))
  802. }
  803. func waitWithTimeout(blockingCall func(), timeout time.Duration) error {
  804. c := make(chan struct{})
  805. go func() {
  806. defer close(c)
  807. blockingCall()
  808. }()
  809. select {
  810. case <-c:
  811. return nil
  812. case <-time.After(timeout):
  813. return fmt.Errorf("Timed out after %s", timeout)
  814. }
  815. }