| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684 |
- /*
- Copyright 2020 Docker, Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package main
- import (
- "context"
- "errors"
- "fmt"
- "io/ioutil"
- "net/http"
- "net/url"
- "os"
- "runtime"
- "strconv"
- "strings"
- "syscall"
- "testing"
- "time"
- "gotest.tools/v3/assert"
- is "gotest.tools/v3/assert/cmp"
- "gotest.tools/v3/icmd"
- "gotest.tools/v3/poll"
- "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/resources/mgmt/resources"
- azure_storage "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/storage/mgmt/storage"
- "github.com/Azure/azure-storage-file-go/azfile"
- "github.com/Azure/go-autorest/autorest/to"
- "github.com/docker/api/aci"
- "github.com/docker/api/aci/login"
- "github.com/docker/api/containers"
- "github.com/docker/api/context/store"
- "github.com/docker/api/errdefs"
- "github.com/docker/api/tests/aci-e2e/storage"
- . "github.com/docker/api/tests/framework"
- )
- const (
- contextName = "aci-test"
- location = "eastus2"
- )
- var binDir string
- func TestMain(m *testing.M) {
- p, cleanup, err := SetupExistingCLI()
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- binDir = p
- exitCode := m.Run()
- cleanup()
- os.Exit(exitCode)
- }
- // Cannot be parallelized as login/logout is global.
- func TestLoginLogout(t *testing.T) {
- startTime := strconv.Itoa(int(time.Now().UnixNano()))
- c := NewE2eCLI(t, binDir)
- rg := "E2E-" + startTime
- t.Run("login", func(t *testing.T) {
- azureLogin(t, c)
- })
- t.Run("create context", func(t *testing.T) {
- sID := getSubscriptionID(t)
- err := createResourceGroup(sID, rg)
- assert.Check(t, is.Nil(err))
- t.Cleanup(func() {
- _ = deleteResourceGroup(rg)
- })
- res := c.RunDockerCmd("context", "create", "aci", contextName, "--subscription-id", sID, "--resource-group", rg, "--location", location)
- res.Assert(t, icmd.Success)
- res = c.RunDockerCmd("context", "use", contextName)
- res.Assert(t, icmd.Expected{Out: contextName})
- res = c.RunDockerCmd("context", "ls")
- res.Assert(t, icmd.Expected{Out: contextName + " *"})
- })
- t.Run("delete context", func(t *testing.T) {
- res := c.RunDockerCmd("context", "use", "default")
- res.Assert(t, icmd.Expected{Out: "default"})
- res = c.RunDockerCmd("context", "rm", contextName)
- res.Assert(t, icmd.Expected{Out: contextName})
- })
- t.Run("logout", func(t *testing.T) {
- _, err := os.Stat(login.GetTokenStorePath())
- assert.NilError(t, err)
- res := c.RunDockerCmd("logout", "azure")
- res.Assert(t, icmd.Expected{Out: "Removing login credentials for Azure"})
- _, err = os.Stat(login.GetTokenStorePath())
- errMsg := "no such file or directory"
- if runtime.GOOS == "windows" {
- errMsg = "The system cannot find the file specified"
- }
- assert.ErrorContains(t, err, errMsg)
- })
- t.Run("create context fail", func(t *testing.T) {
- res := c.RunDockerCmd("context", "create", "aci", "fail-context")
- res.Assert(t, icmd.Expected{
- ExitCode: errdefs.ExitCodeLoginRequired,
- Err: `not logged in to azure, you need to run "docker login azure" first`,
- })
- })
- }
- func TestContainerRun(t *testing.T) {
- c := NewParallelE2eCLI(t, binDir)
- sID, rg := setupTestResourceGroup(t, c, "run")
- const (
- testShareName = "dockertestshare"
- testFileContent = "Volume mounted successfully!"
- testFileName = "index.html"
- )
- // Bootstrap volume
- aciContext := store.AciContext{
- SubscriptionID: sID,
- Location: location,
- ResourceGroup: rg,
- }
- saName := "e2e" + strconv.Itoa(int(time.Now().UnixNano()))
- _, cleanupSa := createStorageAccount(t, aciContext, saName)
- t.Cleanup(func() {
- if err := cleanupSa(); err != nil {
- t.Error(err)
- }
- })
- keys := getStorageKeys(t, aciContext, saName)
- assert.Assert(t, len(keys) > 0)
- k := *keys[0].Value
- cred, u := createFileShare(t, k, testShareName, saName)
- uploadFile(t, *cred, u.String(), testFileName, testFileContent)
- // Used in subtests
- var (
- container string
- hostIP string
- endpoint string
- )
- t.Run("run", func(t *testing.T) {
- mountTarget := "/usr/share/nginx/html"
- res := c.RunDockerCmd(
- "run", "-d",
- "-v", fmt.Sprintf("%s@%s:%s", saName, testShareName, mountTarget),
- "-p", "80:80",
- "nginx",
- )
- res.Assert(t, icmd.Success)
- container = getContainerName(res.Stdout())
- t.Logf("Container name: %q", container)
- })
- t.Run("inspect", func(t *testing.T) {
- res := c.RunDockerCmd("inspect", container)
- res.Assert(t, icmd.Success)
- containerInspect, err := ParseContainerInspect(res.Stdout())
- assert.NilError(t, err)
- assert.Equal(t, containerInspect.Platform, "Linux")
- assert.Equal(t, containerInspect.CPULimit, 1.0)
- assert.Equal(t, containerInspect.RestartPolicyCondition, containers.RestartPolicyNone)
- assert.Assert(t, is.Len(containerInspect.Ports, 1))
- hostIP = containerInspect.Ports[0].HostIP
- endpoint = fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
- t.Logf("Endpoint: %s", endpoint)
- })
- t.Run("ps", func(t *testing.T) {
- res := c.RunDockerCmd("ps")
- res.Assert(t, icmd.Success)
- out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
- l := out[len(out)-1]
- assert.Assert(t, strings.Contains(l, container), "Looking for %q in line: %s", container, l)
- assert.Assert(t, strings.Contains(l, "nginx"))
- assert.Assert(t, strings.Contains(l, "Running"))
- assert.Assert(t, strings.Contains(l, hostIP+":80->80/tcp"))
- })
- t.Run("http get", func(t *testing.T) {
- r, err := http.Get(endpoint)
- assert.NilError(t, err)
- assert.Equal(t, r.StatusCode, http.StatusOK)
- b, err := ioutil.ReadAll(r.Body)
- assert.NilError(t, err)
- assert.Assert(t, strings.Contains(string(b), testFileContent), "Actual content: "+string(b))
- })
- t.Run("logs", func(t *testing.T) {
- res := c.RunDockerCmd("logs", container)
- res.Assert(t, icmd.Expected{Out: "GET"})
- })
- t.Run("exec", func(t *testing.T) {
- res := c.RunDockerCmd("exec", container, "pwd")
- res.Assert(t, icmd.Expected{Out: "/"})
- res = c.RunDockerCmd("exec", container, "echo", "fail_with_argument")
- res.Assert(t, icmd.Expected{
- ExitCode: 1,
- Err: "ACI exec command does not accept arguments to the command. Only the binary should be specified",
- })
- })
- t.Run("logs follow", func(t *testing.T) {
- cmd := c.NewDockerCmd("logs", "--follow", container)
- res := icmd.StartCmd(cmd)
- checkUp := func(t poll.LogT) poll.Result {
- r, _ := http.Get(endpoint + "/is_up")
- if r != nil && r.StatusCode == http.StatusNotFound {
- return poll.Success()
- }
- return poll.Continue("waiting for container to serve request")
- }
- poll.WaitOn(t, checkUp, poll.WithDelay(1*time.Second), poll.WithTimeout(60*time.Second))
- assert.Assert(t, !strings.Contains(res.Stdout(), "/test"))
- checkLogs := func(t poll.LogT) poll.Result {
- if strings.Contains(res.Stdout(), "/test") {
- return poll.Success()
- }
- return poll.Continue("waiting for logs to contain /test")
- }
- // Do request on /test
- go func() {
- time.Sleep(3 * time.Second)
- _, _ = http.Get(endpoint + "/test")
- }()
- poll.WaitOn(t, checkLogs, poll.WithDelay(3*time.Second), poll.WithTimeout(20*time.Second))
- if runtime.GOOS == "windows" {
- err := res.Cmd.Process.Kill()
- assert.NilError(t, err)
- } else {
- err := res.Cmd.Process.Signal(syscall.SIGTERM)
- assert.NilError(t, err)
- }
- })
- t.Run("rm a running container", func(t *testing.T) {
- res := c.RunDockerCmd("rm", container)
- res.Assert(t, icmd.Expected{
- Err: fmt.Sprintf("Error: you cannot remove a running container %s. Stop the container before attempting removal or force remove", container),
- ExitCode: 1,
- })
- })
- t.Run("force rm", func(t *testing.T) {
- res := c.RunDockerCmd("rm", "-f", container)
- res.Assert(t, icmd.Expected{
- Out: container,
- ExitCode: 0,
- })
- checkStopped := func(t poll.LogT) poll.Result {
- res := c.RunDockerCmd("inspect", container)
- if res.ExitCode == 1 {
- return poll.Success()
- }
- return poll.Continue("waiting for container to stop")
- }
- poll.WaitOn(t, checkStopped, poll.WithDelay(5*time.Second), poll.WithTimeout(60*time.Second))
- })
- }
- func TestContainerRunAttached(t *testing.T) {
- c := NewParallelE2eCLI(t, binDir)
- _, _ = setupTestResourceGroup(t, c, "runA")
- // Used in subtests
- var (
- container string
- endpoint string
- )
- container = "test-container"
- t.Run("run attached limits", func(t *testing.T) {
- cmd := c.NewDockerCmd(
- "run",
- "--name", container,
- "--restart", "on-failure",
- "--memory", "0.1G", "--cpus", "0.1",
- "-p", "80:80",
- "nginx",
- )
- runRes := icmd.StartCmd(cmd)
- checkRunning := func(t poll.LogT) poll.Result {
- res := c.RunDockerCmd("inspect", container)
- if res.ExitCode == 0 {
- return poll.Success()
- }
- return poll.Continue("waiting for container to be running")
- }
- poll.WaitOn(t, checkRunning, poll.WithDelay(5*time.Second), poll.WithTimeout(60*time.Second))
- inspectRes := c.RunDockerCmd("inspect", container)
- inspectRes.Assert(t, icmd.Success)
- containerInspect, err := ParseContainerInspect(inspectRes.Stdout())
- assert.NilError(t, err)
- assert.Equal(t, containerInspect.Platform, "Linux")
- assert.Equal(t, containerInspect.CPULimit, 0.1)
- assert.Equal(t, containerInspect.MemoryLimit, uint64(107374182))
- assert.Equal(t, containerInspect.RestartPolicyCondition, containers.RestartPolicyOnFailure)
- assert.Assert(t, is.Len(containerInspect.Ports, 1))
- port := containerInspect.Ports[0]
- assert.Assert(t, len(port.HostIP) > 0)
- assert.Equal(t, port.ContainerPort, uint32(80))
- assert.Equal(t, port.HostPort, uint32(80))
- endpoint = fmt.Sprintf("http://%s:%d", port.HostIP, port.HostPort)
- t.Logf("Endpoint: %s", endpoint)
- assert.Assert(t, !strings.Contains(runRes.Stdout(), "/test"))
- checkRequest := func(t poll.LogT) poll.Result {
- r, _ := http.Get(endpoint + "/test")
- if r != nil && r.StatusCode == http.StatusNotFound {
- return poll.Success()
- }
- return poll.Continue("waiting for container to serve request")
- }
- poll.WaitOn(t, checkRequest, poll.WithDelay(1*time.Second), poll.WithTimeout(60*time.Second))
- checkLog := func(t poll.LogT) poll.Result {
- if strings.Contains(runRes.Stdout(), "/test") {
- return poll.Success()
- }
- return poll.Continue("waiting for logs to contain /test")
- }
- poll.WaitOn(t, checkLog, poll.WithDelay(1*time.Second), poll.WithTimeout(20*time.Second))
- })
- t.Run("stop wrong container", func(t *testing.T) {
- res := c.RunDockerCmd("stop", "unknown-container")
- res.Assert(t, icmd.Expected{
- Err: "Error: container unknown-container not found",
- ExitCode: 1,
- })
- })
- t.Run("stop container", func(t *testing.T) {
- res := c.RunDockerCmd("stop", container)
- res.Assert(t, icmd.Expected{Out: container})
- })
- t.Run("ps stopped container with --all", func(t *testing.T) {
- res := c.RunDockerCmd("ps", container)
- res.Assert(t, icmd.Success)
- out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
- assert.Assert(t, is.Len(out, 1))
- res = c.RunDockerCmd("ps", "--all", container)
- res.Assert(t, icmd.Success)
- out = strings.Split(strings.TrimSpace(res.Stdout()), "\n")
- assert.Assert(t, is.Len(out, 2))
- })
- t.Run("start container", func(t *testing.T) {
- res := c.RunDockerCmd("start", container)
- res.Assert(t, icmd.Expected{Out: container})
- waitForStatus(t, c, container, "Running")
- })
- t.Run("rm stopped container", func(t *testing.T) {
- res := c.RunDockerCmd("stop", container)
- res.Assert(t, icmd.Expected{Out: container})
- waitForStatus(t, c, container, "Terminated", "Node Stopped")
- res = c.RunDockerCmd("rm", container)
- res.Assert(t, icmd.Expected{Out: container})
- })
- }
- func TestCompose(t *testing.T) {
- c := NewParallelE2eCLI(t, binDir)
- _, _ = setupTestResourceGroup(t, c, "compose")
- const (
- composeFile = "../composefiles/aci-demo/aci_demo_port.yaml"
- composeFileMultiplePorts = "../composefiles/aci-demo/aci_demo_multi_port.yaml"
- composeProjectName = "acidemo"
- serverContainer = composeProjectName + "_web"
- wordsContainer = composeProjectName + "_words"
- )
- t.Run("compose up", func(t *testing.T) {
- // Name of Compose project is taken from current folder "acie2e"
- res := c.RunDockerCmd("compose", "up", "-f", composeFile)
- res.Assert(t, icmd.Success)
- res = c.RunDockerCmd("ps")
- res.Assert(t, icmd.Success)
- out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
- // Check three containers are running
- assert.Assert(t, is.Len(out, 4))
- webRunning := false
- for _, l := range out {
- if strings.Contains(l, serverContainer) {
- webRunning = true
- strings.Contains(l, ":80->80/tcp")
- }
- }
- assert.Assert(t, webRunning, "web container not running")
- res = c.RunDockerCmd("inspect", serverContainer)
- res.Assert(t, icmd.Success)
- containerInspect, err := ParseContainerInspect(res.Stdout())
- assert.NilError(t, err)
- assert.Assert(t, is.Len(containerInspect.Ports, 1))
- endpoint := fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
- t.Logf("Endpoint: %s", endpoint)
- r, err := http.Get(endpoint + "/words/noun")
- assert.NilError(t, err)
- assert.Equal(t, r.StatusCode, http.StatusOK)
- b, err := ioutil.ReadAll(r.Body)
- assert.NilError(t, err)
- assert.Assert(t, strings.Contains(string(b), `"word":`))
- })
- t.Run("logs web", func(t *testing.T) {
- res := c.RunDockerCmd("logs", serverContainer)
- res.Assert(t, icmd.Expected{Out: "Listening on port 80"})
- })
- t.Run("update", func(t *testing.T) {
- res := c.RunDockerCmd("compose", "up", "-f", composeFileMultiplePorts, "--project-name", composeProjectName)
- res.Assert(t, icmd.Success)
- res = c.RunDockerCmd("ps")
- res.Assert(t, icmd.Success)
- out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
- // Check three containers are running
- assert.Assert(t, is.Len(out, 4))
- for _, cName := range []string{serverContainer, wordsContainer} {
- res = c.RunDockerCmd("inspect", cName)
- res.Assert(t, icmd.Success)
- containerInspect, err := ParseContainerInspect(res.Stdout())
- assert.NilError(t, err)
- assert.Assert(t, is.Len(containerInspect.Ports, 1))
- endpoint := fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
- t.Logf("Endpoint: %s", endpoint)
- var route string
- switch cName {
- case serverContainer:
- route = "/words/noun"
- assert.Equal(t, containerInspect.Ports[0].HostPort, uint32(80))
- assert.Equal(t, containerInspect.Ports[0].ContainerPort, uint32(80))
- case wordsContainer:
- route = "/noun"
- assert.Equal(t, containerInspect.Ports[0].HostPort, uint32(8080))
- assert.Equal(t, containerInspect.Ports[0].ContainerPort, uint32(8080))
- }
- checkUp := func(t poll.LogT) poll.Result {
- r, _ := http.Get(endpoint + route)
- if r != nil && r.StatusCode == http.StatusOK {
- return poll.Success()
- }
- return poll.Continue("Waiting for container to serve request")
- }
- poll.WaitOn(t, checkUp, poll.WithDelay(1*time.Second), poll.WithTimeout(60*time.Second))
- res = c.RunDockerCmd("ps")
- p := containerInspect.Ports[0]
- res.Assert(t, icmd.Expected{
- Out: fmt.Sprintf("%s:%d->%d/tcp", p.HostIP, p.HostPort, p.ContainerPort),
- })
- }
- })
- t.Run("down", func(t *testing.T) {
- res := c.RunDockerCmd("compose", "down", "--project-name", composeProjectName)
- res.Assert(t, icmd.Success)
- res = c.RunDockerCmd("ps")
- res.Assert(t, icmd.Success)
- out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
- assert.Equal(t, len(out), 1)
- })
- }
- func TestRunEnvVars(t *testing.T) {
- c := NewParallelE2eCLI(t, binDir)
- _, _ = setupTestResourceGroup(t, c, "runEnv")
- t.Run("run", func(t *testing.T) {
- cmd := c.NewDockerCmd(
- "run", "-d",
- "-e", "MYSQL_ROOT_PASSWORD=rootpwd",
- "-e", "MYSQL_DATABASE=mytestdb",
- "-e", "MYSQL_USER",
- "-e", "MYSQL_PASSWORD=userpwd",
- "mysql:5.7",
- )
- cmd.Env = append(cmd.Env, "MYSQL_USER=user1")
- res := icmd.RunCmd(cmd)
- res.Assert(t, icmd.Success)
- out := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
- container := strings.TrimSpace(out[len(out)-1])
- t.Logf("Container name: %q", container)
- res = c.RunDockerCmd("inspect", container)
- res.Assert(t, icmd.Success)
- containerInspect, err := ParseContainerInspect(res.Stdout())
- assert.NilError(t, err)
- assert.Equal(t, containerInspect.Image, "mysql:5.7")
- check := func(t poll.LogT) poll.Result {
- res := c.RunDockerCmd("logs", container)
- if strings.Contains(res.Stdout(), "Giving user user1 access to schema mytestdb") {
- return poll.Success()
- }
- return poll.Continue("waiting for DB container to be up")
- }
- poll.WaitOn(t, check, poll.WithDelay(5*time.Second), poll.WithTimeout(60*time.Second))
- })
- }
- func setupTestResourceGroup(t *testing.T, c *E2eCLI, tName string) (string, string) {
- startTime := strconv.Itoa(int(time.Now().Unix()))
- rg := "E2E-" + tName + "-" + startTime
- azureLogin(t, c)
- sID := getSubscriptionID(t)
- t.Logf("Create resource group %q", rg)
- err := createResourceGroup(sID, rg)
- assert.Check(t, is.Nil(err))
- t.Cleanup(func() {
- if err := deleteResourceGroup(rg); err != nil {
- t.Error(err)
- }
- })
- createAciContextAndUseIt(t, c, sID, rg)
- // Check nothing is running
- res := c.RunDockerCmd("ps")
- res.Assert(t, icmd.Success)
- assert.Assert(t, is.Len(strings.Split(strings.TrimSpace(res.Stdout()), "\n"), 1))
- return sID, rg
- }
- func deleteResourceGroup(rgName string) error {
- ctx := context.TODO()
- helper := aci.NewACIResourceGroupHelper()
- models, err := helper.GetSubscriptionIDs(ctx)
- if err != nil {
- return err
- }
- if len(models) == 0 {
- return errors.New("unable to delete resource group: no models")
- }
- return helper.DeleteAsync(ctx, *models[0].SubscriptionID, rgName)
- }
- func azureLogin(t *testing.T, c *E2eCLI) {
- t.Log("Log in to Azure")
- // in order to create new service principal and get these 3 values : `az ad sp create-for-rbac --name 'TestServicePrincipal' --sdk-auth`
- clientID := os.Getenv("AZURE_CLIENT_ID")
- clientSecret := os.Getenv("AZURE_CLIENT_SECRET")
- tenantID := os.Getenv("AZURE_TENANT_ID")
- assert.Check(t, clientID != "", "AZURE_CLIENT_ID must not be empty")
- assert.Check(t, clientSecret != "", "AZURE_CLIENT_SECRET must not be empty")
- assert.Check(t, tenantID != "", "AZURE_TENANT_ID must not be empty")
- res := c.RunDockerCmd("login", "azure", "--client-id", clientID, "--client-secret", clientSecret, "--tenant-id", tenantID)
- res.Assert(t, icmd.Success)
- }
- func getSubscriptionID(t *testing.T) string {
- ctx := context.TODO()
- helper := aci.NewACIResourceGroupHelper()
- models, err := helper.GetSubscriptionIDs(ctx)
- assert.Check(t, is.Nil(err))
- assert.Check(t, len(models) == 1)
- return *models[0].SubscriptionID
- }
- func createResourceGroup(sID, rgName string) error {
- helper := aci.NewACIResourceGroupHelper()
- _, err := helper.CreateOrUpdate(context.TODO(), sID, rgName, resources.Group{Location: to.StringPtr(location)})
- return err
- }
- func createAciContextAndUseIt(t *testing.T, c *E2eCLI, sID, rgName string) {
- t.Log("Create ACI context")
- res := c.RunDockerCmd("context", "create", "aci", contextName, "--subscription-id", sID, "--resource-group", rgName, "--location", location)
- res.Assert(t, icmd.Success)
- res = c.RunDockerCmd("context", "use", contextName)
- res.Assert(t, icmd.Expected{Out: contextName})
- res = c.RunDockerCmd("context", "ls")
- res.Assert(t, icmd.Expected{Out: contextName + " *"})
- }
- func createStorageAccount(t *testing.T, aciContext store.AciContext, name string) (azure_storage.Account, func() error) {
- t.Logf("Create storage account %q", name)
- account, err := storage.CreateStorageAccount(context.TODO(), aciContext, name)
- assert.Check(t, is.Nil(err))
- assert.Check(t, is.Equal(*(account.Name), name))
- return account, func() error { return deleteStorageAccount(aciContext, name) }
- }
- func deleteStorageAccount(aciContext store.AciContext, name string) error {
- _, err := storage.DeleteStorageAccount(context.TODO(), aciContext, name)
- return err
- }
- func getStorageKeys(t *testing.T, aciContext store.AciContext, saName string) []azure_storage.AccountKey {
- l, err := storage.ListKeys(context.TODO(), aciContext, saName)
- assert.NilError(t, err)
- assert.Assert(t, l.Keys != nil)
- return *l.Keys
- }
- func createFileShare(t *testing.T, key, share, storageAccount string) (*azfile.SharedKeyCredential, *url.URL) {
- // Create a ShareURL object that wraps a soon-to-be-created share's URL and a default pipeline.
- u, _ := url.Parse(fmt.Sprintf("https://%s.file.core.windows.net/%s", storageAccount, share))
- cred, err := azfile.NewSharedKeyCredential(storageAccount, key)
- assert.NilError(t, err)
- shareURL := azfile.NewShareURL(*u, azfile.NewPipeline(cred, azfile.PipelineOptions{}))
- _, err = shareURL.Create(context.TODO(), azfile.Metadata{}, 0)
- assert.NilError(t, err)
- return cred, u
- }
- func uploadFile(t *testing.T, cred azfile.SharedKeyCredential, baseURL, fileName, content string) {
- fURL, err := url.Parse(baseURL + "/" + fileName)
- assert.NilError(t, err)
- fileURL := azfile.NewFileURL(*fURL, azfile.NewPipeline(&cred, azfile.PipelineOptions{}))
- err = azfile.UploadBufferToAzureFile(context.TODO(), []byte(content), fileURL, azfile.UploadToAzureFileOptions{})
- assert.NilError(t, err)
- }
- func getContainerName(stdout string) string {
- out := strings.Split(strings.TrimSpace(stdout), "\n")
- return strings.TrimSpace(out[len(out)-1])
- }
- func waitForStatus(t *testing.T, c *E2eCLI, containerID string, statuses ...string) {
- checkStopped := func(logt poll.LogT) poll.Result {
- res := c.RunDockerCmd("inspect", containerID)
- containerInspect, err := ParseContainerInspect(res.Stdout())
- assert.NilError(t, err)
- for _, status := range statuses {
- if containerInspect.Status == status {
- return poll.Success()
- }
- }
- return poll.Continue("Status %s != %s (expected) for container %s", containerInspect.Status, statuses, containerID)
- }
- poll.WaitOn(t, checkStopped, poll.WithDelay(5*time.Second), poll.WithTimeout(90*time.Second))
- }
|