e2e-aci_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. "net/http"
  20. "net/url"
  21. "os"
  22. "runtime"
  23. "strconv"
  24. "strings"
  25. "syscall"
  26. "testing"
  27. "time"
  28. "gotest.tools/v3/assert"
  29. is "gotest.tools/v3/assert/cmp"
  30. "gotest.tools/v3/icmd"
  31. "gotest.tools/v3/poll"
  32. "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/resources/mgmt/resources"
  33. azure_storage "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/storage/mgmt/storage"
  34. "github.com/Azure/azure-storage-file-go/azfile"
  35. "github.com/Azure/go-autorest/autorest/to"
  36. "github.com/docker/api/aci"
  37. "github.com/docker/api/aci/login"
  38. "github.com/docker/api/containers"
  39. "github.com/docker/api/context/store"
  40. "github.com/docker/api/errdefs"
  41. "github.com/docker/api/tests/aci-e2e/storage"
  42. . "github.com/docker/api/tests/framework"
  43. )
  44. const (
  45. contextName = "aci-test"
  46. location = "eastus2"
  47. )
  48. var binDir string
  49. func TestMain(m *testing.M) {
  50. p, cleanup, err := SetupExistingCLI()
  51. if err != nil {
  52. fmt.Println(err)
  53. os.Exit(1)
  54. }
  55. binDir = p
  56. exitCode := m.Run()
  57. cleanup()
  58. os.Exit(exitCode)
  59. }
  60. // Cannot be parallelized as login/logout is global.
  61. func TestLoginLogout(t *testing.T) {
  62. startTime := strconv.Itoa(int(time.Now().UnixNano()))
  63. c := NewE2eCLI(t, binDir)
  64. rg := "E2E-" + startTime
  65. t.Run("login", func(t *testing.T) {
  66. azureLogin(t, c)
  67. })
  68. t.Run("create context", func(t *testing.T) {
  69. sID := getSubscriptionID(t)
  70. err := createResourceGroup(sID, rg)
  71. assert.Check(t, is.Nil(err))
  72. t.Cleanup(func() {
  73. _ = deleteResourceGroup(rg)
  74. })
  75. res := c.RunDockerCmd("context", "create", "aci", contextName, "--subscription-id", sID, "--resource-group", rg, "--location", location)
  76. res.Assert(t, icmd.Success)
  77. res = c.RunDockerCmd("context", "use", contextName)
  78. res.Assert(t, icmd.Expected{Out: contextName})
  79. res = c.RunDockerCmd("context", "ls")
  80. res.Assert(t, icmd.Expected{Out: contextName + " *"})
  81. })
  82. t.Run("delete context", func(t *testing.T) {
  83. res := c.RunDockerCmd("context", "use", "default")
  84. res.Assert(t, icmd.Expected{Out: "default"})
  85. res = c.RunDockerCmd("context", "rm", contextName)
  86. res.Assert(t, icmd.Expected{Out: contextName})
  87. })
  88. t.Run("logout", func(t *testing.T) {
  89. _, err := os.Stat(login.GetTokenStorePath())
  90. assert.NilError(t, err)
  91. res := c.RunDockerCmd("logout", "azure")
  92. res.Assert(t, icmd.Expected{Out: "Removing login credentials for Azure"})
  93. _, err = os.Stat(login.GetTokenStorePath())
  94. errMsg := "no such file or directory"
  95. if runtime.GOOS == "windows" {
  96. errMsg = "The system cannot find the file specified"
  97. }
  98. assert.ErrorContains(t, err, errMsg)
  99. })
  100. t.Run("create context fail", func(t *testing.T) {
  101. res := c.RunDockerCmd("context", "create", "aci", "fail-context")
  102. res.Assert(t, icmd.Expected{
  103. ExitCode: errdefs.ExitCodeLoginRequired,
  104. Err: `not logged in to azure, you need to run "docker login azure" first`,
  105. })
  106. })
  107. }
  108. func TestContainerRun(t *testing.T) {
  109. c := NewParallelE2eCLI(t, binDir)
  110. sID, rg := setupTestResourceGroup(t, c, "run")
  111. const (
  112. testShareName = "dockertestshare"
  113. testFileContent = "Volume mounted successfully!"
  114. testFileName = "index.html"
  115. )
  116. // Bootstrap volume
  117. aciContext := store.AciContext{
  118. SubscriptionID: sID,
  119. Location: location,
  120. ResourceGroup: rg,
  121. }
  122. saName := "e2e" + strconv.Itoa(int(time.Now().UnixNano()))
  123. _, cleanupSa := createStorageAccount(t, aciContext, saName)
  124. t.Cleanup(func() {
  125. if err := cleanupSa(); err != nil {
  126. t.Error(err)
  127. }
  128. })
  129. keys := getStorageKeys(t, aciContext, saName)
  130. assert.Assert(t, len(keys) > 0)
  131. k := *keys[0].Value
  132. cred, u := createFileShare(t, k, testShareName, saName)
  133. uploadFile(t, *cred, u.String(), testFileName, testFileContent)
  134. // Used in subtests
  135. var (
  136. container string
  137. hostIP string
  138. endpoint string
  139. )
  140. t.Run("run", func(t *testing.T) {
  141. mountTarget := "/usr/share/nginx/html"
  142. res := c.RunDockerCmd(
  143. "run", "-d",
  144. "-v", fmt.Sprintf("%s@%s:%s", saName, testShareName, mountTarget),
  145. "-p", "80:80",
  146. "nginx",
  147. )
  148. res.Assert(t, icmd.Success)
  149. container = getContainerName(res.Stdout())
  150. t.Logf("Container name: %q", container)
  151. })
  152. t.Run("inspect", func(t *testing.T) {
  153. res := c.RunDockerCmd("inspect", container)
  154. res.Assert(t, icmd.Success)
  155. containerInspect, err := ParseContainerInspect(res.Stdout())
  156. assert.NilError(t, err)
  157. assert.Equal(t, containerInspect.Platform, "Linux")
  158. assert.Equal(t, containerInspect.CPULimit, 1.0)
  159. assert.Equal(t, containerInspect.RestartPolicyCondition, containers.RestartPolicyNone)
  160. assert.Assert(t, is.Len(containerInspect.Ports, 1))
  161. hostIP = containerInspect.Ports[0].HostIP
  162. endpoint = fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
  163. t.Logf("Endpoint: %s", endpoint)
  164. })
  165. t.Run("ps", func(t *testing.T) {
  166. res := c.RunDockerCmd("ps")
  167. res.Assert(t, icmd.Success)
  168. out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  169. l := out[len(out)-1]
  170. assert.Assert(t, strings.Contains(l, container), "Looking for %q in line: %s", container, l)
  171. assert.Assert(t, strings.Contains(l, "nginx"))
  172. assert.Assert(t, strings.Contains(l, "Running"))
  173. assert.Assert(t, strings.Contains(l, hostIP+":80->80/tcp"))
  174. })
  175. t.Run("http get", func(t *testing.T) {
  176. r, err := http.Get(endpoint)
  177. assert.NilError(t, err)
  178. assert.Equal(t, r.StatusCode, http.StatusOK)
  179. b, err := ioutil.ReadAll(r.Body)
  180. assert.NilError(t, err)
  181. assert.Assert(t, strings.Contains(string(b), testFileContent), "Actual content: "+string(b))
  182. })
  183. t.Run("logs", func(t *testing.T) {
  184. res := c.RunDockerCmd("logs", container)
  185. res.Assert(t, icmd.Expected{Out: "GET"})
  186. })
  187. t.Run("exec", func(t *testing.T) {
  188. res := c.RunDockerCmd("exec", container, "pwd")
  189. res.Assert(t, icmd.Expected{Out: "/"})
  190. res = c.RunDockerCmd("exec", container, "echo", "fail_with_argument")
  191. res.Assert(t, icmd.Expected{
  192. ExitCode: 1,
  193. Err: "ACI exec command does not accept arguments to the command. Only the binary should be specified",
  194. })
  195. })
  196. t.Run("logs follow", func(t *testing.T) {
  197. cmd := c.NewDockerCmd("logs", "--follow", container)
  198. res := icmd.StartCmd(cmd)
  199. checkUp := func(t poll.LogT) poll.Result {
  200. r, _ := http.Get(endpoint + "/is_up")
  201. if r != nil && r.StatusCode == http.StatusNotFound {
  202. return poll.Success()
  203. }
  204. return poll.Continue("waiting for container to serve request")
  205. }
  206. poll.WaitOn(t, checkUp, poll.WithDelay(1*time.Second), poll.WithTimeout(60*time.Second))
  207. assert.Assert(t, !strings.Contains(res.Stdout(), "/test"))
  208. checkLogs := func(t poll.LogT) poll.Result {
  209. if strings.Contains(res.Stdout(), "/test") {
  210. return poll.Success()
  211. }
  212. return poll.Continue("waiting for logs to contain /test")
  213. }
  214. // Do request on /test
  215. go func() {
  216. time.Sleep(3 * time.Second)
  217. _, _ = http.Get(endpoint + "/test")
  218. }()
  219. poll.WaitOn(t, checkLogs, poll.WithDelay(3*time.Second), poll.WithTimeout(20*time.Second))
  220. if runtime.GOOS == "windows" {
  221. err := res.Cmd.Process.Kill()
  222. assert.NilError(t, err)
  223. } else {
  224. err := res.Cmd.Process.Signal(syscall.SIGTERM)
  225. assert.NilError(t, err)
  226. }
  227. })
  228. t.Run("rm a running container", func(t *testing.T) {
  229. res := c.RunDockerCmd("rm", container)
  230. res.Assert(t, icmd.Expected{
  231. Err: fmt.Sprintf("Error: you cannot remove a running container %s. Stop the container before attempting removal or force remove", container),
  232. ExitCode: 1,
  233. })
  234. })
  235. t.Run("force rm", func(t *testing.T) {
  236. res := c.RunDockerCmd("rm", "-f", container)
  237. res.Assert(t, icmd.Expected{
  238. Out: container,
  239. ExitCode: 0,
  240. })
  241. checkStopped := func(t poll.LogT) poll.Result {
  242. res := c.RunDockerCmd("inspect", container)
  243. if res.ExitCode == 1 {
  244. return poll.Success()
  245. }
  246. return poll.Continue("waiting for container to stop")
  247. }
  248. poll.WaitOn(t, checkStopped, poll.WithDelay(5*time.Second), poll.WithTimeout(60*time.Second))
  249. })
  250. }
  251. func TestContainerRunAttached(t *testing.T) {
  252. c := NewParallelE2eCLI(t, binDir)
  253. _, _ = setupTestResourceGroup(t, c, "runA")
  254. // Used in subtests
  255. var (
  256. container string
  257. endpoint string
  258. )
  259. container = "test-container"
  260. t.Run("run attached limits", func(t *testing.T) {
  261. cmd := c.NewDockerCmd(
  262. "run",
  263. "--name", container,
  264. "--restart", "on-failure",
  265. "--memory", "0.1G", "--cpus", "0.1",
  266. "-p", "80:80",
  267. "nginx",
  268. )
  269. runRes := icmd.StartCmd(cmd)
  270. checkRunning := func(t poll.LogT) poll.Result {
  271. res := c.RunDockerCmd("inspect", container)
  272. if res.ExitCode == 0 {
  273. return poll.Success()
  274. }
  275. return poll.Continue("waiting for container to be running")
  276. }
  277. poll.WaitOn(t, checkRunning, poll.WithDelay(5*time.Second), poll.WithTimeout(60*time.Second))
  278. inspectRes := c.RunDockerCmd("inspect", container)
  279. inspectRes.Assert(t, icmd.Success)
  280. containerInspect, err := ParseContainerInspect(inspectRes.Stdout())
  281. assert.NilError(t, err)
  282. assert.Equal(t, containerInspect.Platform, "Linux")
  283. assert.Equal(t, containerInspect.CPULimit, 0.1)
  284. assert.Equal(t, containerInspect.MemoryLimit, uint64(107374182))
  285. assert.Equal(t, containerInspect.RestartPolicyCondition, containers.RestartPolicyOnFailure)
  286. assert.Assert(t, is.Len(containerInspect.Ports, 1))
  287. port := containerInspect.Ports[0]
  288. assert.Assert(t, len(port.HostIP) > 0)
  289. assert.Equal(t, port.ContainerPort, uint32(80))
  290. assert.Equal(t, port.HostPort, uint32(80))
  291. endpoint = fmt.Sprintf("http://%s:%d", port.HostIP, port.HostPort)
  292. t.Logf("Endpoint: %s", endpoint)
  293. assert.Assert(t, !strings.Contains(runRes.Stdout(), "/test"))
  294. checkRequest := func(t poll.LogT) poll.Result {
  295. r, _ := http.Get(endpoint + "/test")
  296. if r != nil && r.StatusCode == http.StatusNotFound {
  297. return poll.Success()
  298. }
  299. return poll.Continue("waiting for container to serve request")
  300. }
  301. poll.WaitOn(t, checkRequest, poll.WithDelay(1*time.Second), poll.WithTimeout(60*time.Second))
  302. checkLog := func(t poll.LogT) poll.Result {
  303. if strings.Contains(runRes.Stdout(), "/test") {
  304. return poll.Success()
  305. }
  306. return poll.Continue("waiting for logs to contain /test")
  307. }
  308. poll.WaitOn(t, checkLog, poll.WithDelay(1*time.Second), poll.WithTimeout(20*time.Second))
  309. })
  310. t.Run("stop wrong container", func(t *testing.T) {
  311. res := c.RunDockerCmd("stop", "unknown-container")
  312. res.Assert(t, icmd.Expected{
  313. Err: "Error: container unknown-container not found",
  314. ExitCode: 1,
  315. })
  316. })
  317. t.Run("stop container", func(t *testing.T) {
  318. res := c.RunDockerCmd("stop", container)
  319. res.Assert(t, icmd.Expected{Out: container})
  320. })
  321. t.Run("ps stopped container with --all", func(t *testing.T) {
  322. res := c.RunDockerCmd("ps", container)
  323. res.Assert(t, icmd.Success)
  324. out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  325. assert.Assert(t, is.Len(out, 1))
  326. res = c.RunDockerCmd("ps", "--all", container)
  327. res.Assert(t, icmd.Success)
  328. out = strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  329. assert.Assert(t, is.Len(out, 2))
  330. })
  331. t.Run("start container", func(t *testing.T) {
  332. res := c.RunDockerCmd("start", container)
  333. res.Assert(t, icmd.Expected{Out: container})
  334. waitForStatus(t, c, container, "Running")
  335. })
  336. t.Run("rm stopped container", func(t *testing.T) {
  337. res := c.RunDockerCmd("stop", container)
  338. res.Assert(t, icmd.Expected{Out: container})
  339. waitForStatus(t, c, container, "Terminated", "Node Stopped")
  340. res = c.RunDockerCmd("rm", container)
  341. res.Assert(t, icmd.Expected{Out: container})
  342. })
  343. }
  344. func TestCompose(t *testing.T) {
  345. c := NewParallelE2eCLI(t, binDir)
  346. _, _ = setupTestResourceGroup(t, c, "compose")
  347. const (
  348. composeFile = "../composefiles/aci-demo/aci_demo_port.yaml"
  349. composeFileMultiplePorts = "../composefiles/aci-demo/aci_demo_multi_port.yaml"
  350. composeProjectName = "acidemo"
  351. serverContainer = composeProjectName + "_web"
  352. wordsContainer = composeProjectName + "_words"
  353. )
  354. t.Run("compose up", func(t *testing.T) {
  355. // Name of Compose project is taken from current folder "acie2e"
  356. res := c.RunDockerCmd("compose", "up", "-f", composeFile)
  357. res.Assert(t, icmd.Success)
  358. res = c.RunDockerCmd("ps")
  359. res.Assert(t, icmd.Success)
  360. out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  361. // Check three containers are running
  362. assert.Assert(t, is.Len(out, 4))
  363. webRunning := false
  364. for _, l := range out {
  365. if strings.Contains(l, serverContainer) {
  366. webRunning = true
  367. strings.Contains(l, ":80->80/tcp")
  368. }
  369. }
  370. assert.Assert(t, webRunning, "web container not running")
  371. res = c.RunDockerCmd("inspect", serverContainer)
  372. res.Assert(t, icmd.Success)
  373. containerInspect, err := ParseContainerInspect(res.Stdout())
  374. assert.NilError(t, err)
  375. assert.Assert(t, is.Len(containerInspect.Ports, 1))
  376. endpoint := fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
  377. t.Logf("Endpoint: %s", endpoint)
  378. r, err := http.Get(endpoint + "/words/noun")
  379. assert.NilError(t, err)
  380. assert.Equal(t, r.StatusCode, http.StatusOK)
  381. b, err := ioutil.ReadAll(r.Body)
  382. assert.NilError(t, err)
  383. assert.Assert(t, strings.Contains(string(b), `"word":`))
  384. })
  385. t.Run("logs web", func(t *testing.T) {
  386. res := c.RunDockerCmd("logs", serverContainer)
  387. res.Assert(t, icmd.Expected{Out: "Listening on port 80"})
  388. })
  389. t.Run("update", func(t *testing.T) {
  390. res := c.RunDockerCmd("compose", "up", "-f", composeFileMultiplePorts, "--project-name", composeProjectName)
  391. res.Assert(t, icmd.Success)
  392. res = c.RunDockerCmd("ps")
  393. res.Assert(t, icmd.Success)
  394. out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  395. // Check three containers are running
  396. assert.Assert(t, is.Len(out, 4))
  397. for _, cName := range []string{serverContainer, wordsContainer} {
  398. res = c.RunDockerCmd("inspect", cName)
  399. res.Assert(t, icmd.Success)
  400. containerInspect, err := ParseContainerInspect(res.Stdout())
  401. assert.NilError(t, err)
  402. assert.Assert(t, is.Len(containerInspect.Ports, 1))
  403. endpoint := fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
  404. t.Logf("Endpoint: %s", endpoint)
  405. var route string
  406. switch cName {
  407. case serverContainer:
  408. route = "/words/noun"
  409. assert.Equal(t, containerInspect.Ports[0].HostPort, uint32(80))
  410. assert.Equal(t, containerInspect.Ports[0].ContainerPort, uint32(80))
  411. case wordsContainer:
  412. route = "/noun"
  413. assert.Equal(t, containerInspect.Ports[0].HostPort, uint32(8080))
  414. assert.Equal(t, containerInspect.Ports[0].ContainerPort, uint32(8080))
  415. }
  416. checkUp := func(t poll.LogT) poll.Result {
  417. r, _ := http.Get(endpoint + route)
  418. if r != nil && r.StatusCode == http.StatusOK {
  419. return poll.Success()
  420. }
  421. return poll.Continue("Waiting for container to serve request")
  422. }
  423. poll.WaitOn(t, checkUp, poll.WithDelay(1*time.Second), poll.WithTimeout(60*time.Second))
  424. res = c.RunDockerCmd("ps")
  425. p := containerInspect.Ports[0]
  426. res.Assert(t, icmd.Expected{
  427. Out: fmt.Sprintf("%s:%d->%d/tcp", p.HostIP, p.HostPort, p.ContainerPort),
  428. })
  429. }
  430. })
  431. t.Run("down", func(t *testing.T) {
  432. res := c.RunDockerCmd("compose", "down", "--project-name", composeProjectName)
  433. res.Assert(t, icmd.Success)
  434. res = c.RunDockerCmd("ps")
  435. res.Assert(t, icmd.Success)
  436. out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  437. assert.Equal(t, len(out), 1)
  438. })
  439. }
  440. func TestRunEnvVars(t *testing.T) {
  441. c := NewParallelE2eCLI(t, binDir)
  442. _, _ = setupTestResourceGroup(t, c, "runEnv")
  443. t.Run("run", func(t *testing.T) {
  444. cmd := c.NewDockerCmd(
  445. "run", "-d",
  446. "-e", "MYSQL_ROOT_PASSWORD=rootpwd",
  447. "-e", "MYSQL_DATABASE=mytestdb",
  448. "-e", "MYSQL_USER",
  449. "-e", "MYSQL_PASSWORD=userpwd",
  450. "mysql:5.7",
  451. )
  452. cmd.Env = append(cmd.Env, "MYSQL_USER=user1")
  453. res := icmd.RunCmd(cmd)
  454. res.Assert(t, icmd.Success)
  455. out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  456. container := strings.TrimSpace(out[len(out)-1])
  457. t.Logf("Container name: %q", container)
  458. res = c.RunDockerCmd("inspect", container)
  459. res.Assert(t, icmd.Success)
  460. containerInspect, err := ParseContainerInspect(res.Stdout())
  461. assert.NilError(t, err)
  462. assert.Equal(t, containerInspect.Image, "mysql:5.7")
  463. check := func(t poll.LogT) poll.Result {
  464. res := c.RunDockerCmd("logs", container)
  465. if strings.Contains(res.Stdout(), "Giving user user1 access to schema mytestdb") {
  466. return poll.Success()
  467. }
  468. return poll.Continue("waiting for DB container to be up")
  469. }
  470. poll.WaitOn(t, check, poll.WithDelay(5*time.Second), poll.WithTimeout(60*time.Second))
  471. })
  472. }
  473. func setupTestResourceGroup(t *testing.T, c *E2eCLI, tName string) (string, string) {
  474. startTime := strconv.Itoa(int(time.Now().Unix()))
  475. rg := "E2E-" + tName + "-" + startTime
  476. azureLogin(t, c)
  477. sID := getSubscriptionID(t)
  478. t.Logf("Create resource group %q", rg)
  479. err := createResourceGroup(sID, rg)
  480. assert.Check(t, is.Nil(err))
  481. t.Cleanup(func() {
  482. if err := deleteResourceGroup(rg); err != nil {
  483. t.Error(err)
  484. }
  485. })
  486. createAciContextAndUseIt(t, c, sID, rg)
  487. // Check nothing is running
  488. res := c.RunDockerCmd("ps")
  489. res.Assert(t, icmd.Success)
  490. assert.Assert(t, is.Len(strings.Split(strings.TrimSpace(res.Stdout()), "\n"), 1))
  491. return sID, rg
  492. }
  493. func deleteResourceGroup(rgName string) error {
  494. ctx := context.TODO()
  495. helper := aci.NewACIResourceGroupHelper()
  496. models, err := helper.GetSubscriptionIDs(ctx)
  497. if err != nil {
  498. return err
  499. }
  500. if len(models) == 0 {
  501. return errors.New("unable to delete resource group: no models")
  502. }
  503. return helper.DeleteAsync(ctx, *models[0].SubscriptionID, rgName)
  504. }
  505. func azureLogin(t *testing.T, c *E2eCLI) {
  506. t.Log("Log in to Azure")
  507. // in order to create new service principal and get these 3 values : `az ad sp create-for-rbac --name 'TestServicePrincipal' --sdk-auth`
  508. clientID := os.Getenv("AZURE_CLIENT_ID")
  509. clientSecret := os.Getenv("AZURE_CLIENT_SECRET")
  510. tenantID := os.Getenv("AZURE_TENANT_ID")
  511. assert.Check(t, clientID != "", "AZURE_CLIENT_ID must not be empty")
  512. assert.Check(t, clientSecret != "", "AZURE_CLIENT_SECRET must not be empty")
  513. assert.Check(t, tenantID != "", "AZURE_TENANT_ID must not be empty")
  514. res := c.RunDockerCmd("login", "azure", "--client-id", clientID, "--client-secret", clientSecret, "--tenant-id", tenantID)
  515. res.Assert(t, icmd.Success)
  516. }
  517. func getSubscriptionID(t *testing.T) string {
  518. ctx := context.TODO()
  519. helper := aci.NewACIResourceGroupHelper()
  520. models, err := helper.GetSubscriptionIDs(ctx)
  521. assert.Check(t, is.Nil(err))
  522. assert.Check(t, len(models) == 1)
  523. return *models[0].SubscriptionID
  524. }
  525. func createResourceGroup(sID, rgName string) error {
  526. helper := aci.NewACIResourceGroupHelper()
  527. _, err := helper.CreateOrUpdate(context.TODO(), sID, rgName, resources.Group{Location: to.StringPtr(location)})
  528. return err
  529. }
  530. func createAciContextAndUseIt(t *testing.T, c *E2eCLI, sID, rgName string) {
  531. t.Log("Create ACI context")
  532. res := c.RunDockerCmd("context", "create", "aci", contextName, "--subscription-id", sID, "--resource-group", rgName, "--location", location)
  533. res.Assert(t, icmd.Success)
  534. res = c.RunDockerCmd("context", "use", contextName)
  535. res.Assert(t, icmd.Expected{Out: contextName})
  536. res = c.RunDockerCmd("context", "ls")
  537. res.Assert(t, icmd.Expected{Out: contextName + " *"})
  538. }
  539. func createStorageAccount(t *testing.T, aciContext store.AciContext, name string) (azure_storage.Account, func() error) {
  540. t.Logf("Create storage account %q", name)
  541. account, err := storage.CreateStorageAccount(context.TODO(), aciContext, name)
  542. assert.Check(t, is.Nil(err))
  543. assert.Check(t, is.Equal(*(account.Name), name))
  544. return account, func() error { return deleteStorageAccount(aciContext, name) }
  545. }
  546. func deleteStorageAccount(aciContext store.AciContext, name string) error {
  547. _, err := storage.DeleteStorageAccount(context.TODO(), aciContext, name)
  548. return err
  549. }
  550. func getStorageKeys(t *testing.T, aciContext store.AciContext, saName string) []azure_storage.AccountKey {
  551. l, err := storage.ListKeys(context.TODO(), aciContext, saName)
  552. assert.NilError(t, err)
  553. assert.Assert(t, l.Keys != nil)
  554. return *l.Keys
  555. }
  556. func createFileShare(t *testing.T, key, share, storageAccount string) (*azfile.SharedKeyCredential, *url.URL) {
  557. // Create a ShareURL object that wraps a soon-to-be-created share's URL and a default pipeline.
  558. u, _ := url.Parse(fmt.Sprintf("https://%s.file.core.windows.net/%s", storageAccount, share))
  559. cred, err := azfile.NewSharedKeyCredential(storageAccount, key)
  560. assert.NilError(t, err)
  561. shareURL := azfile.NewShareURL(*u, azfile.NewPipeline(cred, azfile.PipelineOptions{}))
  562. _, err = shareURL.Create(context.TODO(), azfile.Metadata{}, 0)
  563. assert.NilError(t, err)
  564. return cred, u
  565. }
  566. func uploadFile(t *testing.T, cred azfile.SharedKeyCredential, baseURL, fileName, content string) {
  567. fURL, err := url.Parse(baseURL + "/" + fileName)
  568. assert.NilError(t, err)
  569. fileURL := azfile.NewFileURL(*fURL, azfile.NewPipeline(&cred, azfile.PipelineOptions{}))
  570. err = azfile.UploadBufferToAzureFile(context.TODO(), []byte(content), fileURL, azfile.UploadToAzureFileOptions{})
  571. assert.NilError(t, err)
  572. }
  573. func getContainerName(stdout string) string {
  574. out := strings.Split(strings.TrimSpace(stdout), "\n")
  575. return strings.TrimSpace(out[len(out)-1])
  576. }
  577. func waitForStatus(t *testing.T, c *E2eCLI, containerID string, statuses ...string) {
  578. checkStopped := func(logt poll.LogT) poll.Result {
  579. res := c.RunDockerCmd("inspect", containerID)
  580. containerInspect, err := ParseContainerInspect(res.Stdout())
  581. assert.NilError(t, err)
  582. for _, status := range statuses {
  583. if containerInspect.Status == status {
  584. return poll.Success()
  585. }
  586. }
  587. return poll.Continue("Status %s != %s (expected) for container %s", containerInspect.Status, statuses, containerID)
  588. }
  589. poll.WaitOn(t, checkStopped, poll.WithDelay(5*time.Second), poll.WithTimeout(90*time.Second))
  590. }