123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479 |
- package main
- import (
- "bufio"
- "bytes"
- "crypto/tls"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "io/ioutil"
- "net"
- "net/http"
- "net/http/httptest"
- "net/http/httputil"
- "net/url"
- "os"
- "os/exec"
- "path"
- "path/filepath"
- "strconv"
- "strings"
- "time"
- "github.com/docker/docker/opts"
- "github.com/docker/docker/pkg/httputils"
- "github.com/docker/docker/pkg/integration"
- "github.com/docker/docker/pkg/ioutils"
- "github.com/docker/docker/pkg/stringutils"
- "github.com/docker/engine-api/types"
- "github.com/docker/go-connections/tlsconfig"
- "github.com/docker/go-units"
- "github.com/go-check/check"
- )
- func init() {
- cmd := exec.Command(dockerBinary, "images")
- cmd.Env = appendBaseEnv(true)
- out, err := cmd.CombinedOutput()
- if err != nil {
- panic(fmt.Errorf("err=%v\nout=%s\n", err, out))
- }
- lines := strings.Split(string(out), "\n")[1:]
- for _, l := range lines {
- if l == "" {
- continue
- }
- fields := strings.Fields(l)
- imgTag := fields[0] + ":" + fields[1]
- // just for case if we have dangling images in tested daemon
- if imgTag != "<none>:<none>" {
- protectedImages[imgTag] = struct{}{}
- }
- }
- // Obtain the daemon platform so that it can be used by tests to make
- // intelligent decisions about how to configure themselves, and validate
- // that the target platform is valid.
- res, _, err := sockRequestRaw("GET", "/version", nil, "application/json")
- if err != nil || res == nil || (res != nil && res.StatusCode != http.StatusOK) {
- panic(fmt.Errorf("Init failed to get version: %v. Res=%v", err.Error(), res))
- }
- svrHeader, _ := httputils.ParseServerHeader(res.Header.Get("Server"))
- daemonPlatform = svrHeader.OS
- if daemonPlatform != "linux" && daemonPlatform != "windows" {
- panic("Cannot run tests against platform: " + daemonPlatform)
- }
- // On Windows, extract out the version as we need to make selective
- // decisions during integration testing as and when features are implemented.
- if daemonPlatform == "windows" {
- if body, err := ioutil.ReadAll(res.Body); err == nil {
- var server types.Version
- if err := json.Unmarshal(body, &server); err == nil {
- // eg in "10.0 10550 (10550.1000.amd64fre.branch.date-time)" we want 10550
- windowsDaemonKV, _ = strconv.Atoi(strings.Split(server.KernelVersion, " ")[1])
- }
- }
- }
- // Now we know the daemon platform, can set paths used by tests.
- _, body, err := sockRequest("GET", "/info", nil)
- if err != nil {
- panic(err)
- }
- var info types.Info
- err = json.Unmarshal(body, &info)
- dockerBasePath = info.DockerRootDir
- volumesConfigPath = filepath.Join(dockerBasePath, "volumes")
- containerStoragePath = filepath.Join(dockerBasePath, "containers")
- // Make sure in context of daemon, not the local platform. Note we can't
- // use filepath.FromSlash or ToSlash here as they are a no-op on Unix.
- if daemonPlatform == "windows" {
- volumesConfigPath = strings.Replace(volumesConfigPath, `/`, `\`, -1)
- containerStoragePath = strings.Replace(containerStoragePath, `/`, `\`, -1)
- } else {
- volumesConfigPath = strings.Replace(volumesConfigPath, `\`, `/`, -1)
- containerStoragePath = strings.Replace(containerStoragePath, `\`, `/`, -1)
- }
- }
- func convertBasesize(basesizeBytes int64) (int64, error) {
- basesize := units.HumanSize(float64(basesizeBytes))
- basesize = strings.Trim(basesize, " ")[:len(basesize)-3]
- basesizeFloat, err := strconv.ParseFloat(strings.Trim(basesize, " "), 64)
- if err != nil {
- return 0, err
- }
- return int64(basesizeFloat) * 1024 * 1024 * 1024, nil
- }
- func daemonHost() string {
- daemonURLStr := "unix://" + opts.DefaultUnixSocket
- if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" {
- daemonURLStr = daemonHostVar
- }
- return daemonURLStr
- }
- func getTLSConfig() (*tls.Config, error) {
- dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
- if dockerCertPath == "" {
- return nil, fmt.Errorf("DOCKER_TLS_VERIFY specified, but no DOCKER_CERT_PATH environment variable")
- }
- option := &tlsconfig.Options{
- CAFile: filepath.Join(dockerCertPath, "ca.pem"),
- CertFile: filepath.Join(dockerCertPath, "cert.pem"),
- KeyFile: filepath.Join(dockerCertPath, "key.pem"),
- }
- tlsConfig, err := tlsconfig.Client(*option)
- if err != nil {
- return nil, err
- }
- return tlsConfig, nil
- }
- func sockConn(timeout time.Duration) (net.Conn, error) {
- daemon := daemonHost()
- daemonURL, err := url.Parse(daemon)
- if err != nil {
- return nil, fmt.Errorf("could not parse url %q: %v", daemon, err)
- }
- var c net.Conn
- switch daemonURL.Scheme {
- case "npipe":
- return npipeDial(daemonURL.Path, timeout)
- case "unix":
- return net.DialTimeout(daemonURL.Scheme, daemonURL.Path, timeout)
- case "tcp":
- if os.Getenv("DOCKER_TLS_VERIFY") != "" {
- // Setup the socket TLS configuration.
- tlsConfig, err := getTLSConfig()
- if err != nil {
- return nil, err
- }
- dialer := &net.Dialer{Timeout: timeout}
- return tls.DialWithDialer(dialer, daemonURL.Scheme, daemonURL.Host, tlsConfig)
- }
- return net.DialTimeout(daemonURL.Scheme, daemonURL.Host, timeout)
- default:
- return c, fmt.Errorf("unknown scheme %v (%s)", daemonURL.Scheme, daemon)
- }
- }
- func sockRequest(method, endpoint string, data interface{}) (int, []byte, error) {
- jsonData := bytes.NewBuffer(nil)
- if err := json.NewEncoder(jsonData).Encode(data); err != nil {
- return -1, nil, err
- }
- res, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json")
- if err != nil {
- return -1, nil, err
- }
- b, err := readBody(body)
- return res.StatusCode, b, err
- }
- func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
- req, client, err := newRequestClient(method, endpoint, data, ct)
- if err != nil {
- return nil, nil, err
- }
- resp, err := client.Do(req)
- if err != nil {
- client.Close()
- return nil, nil, err
- }
- body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
- defer resp.Body.Close()
- return client.Close()
- })
- return resp, body, nil
- }
- func sockRequestHijack(method, endpoint string, data io.Reader, ct string) (net.Conn, *bufio.Reader, error) {
- req, client, err := newRequestClient(method, endpoint, data, ct)
- if err != nil {
- return nil, nil, err
- }
- client.Do(req)
- conn, br := client.Hijack()
- return conn, br, nil
- }
- func newRequestClient(method, endpoint string, data io.Reader, ct string) (*http.Request, *httputil.ClientConn, error) {
- c, err := sockConn(time.Duration(10 * time.Second))
- if err != nil {
- return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
- }
- client := httputil.NewClientConn(c, nil)
- req, err := http.NewRequest(method, endpoint, data)
- if err != nil {
- client.Close()
- return nil, nil, fmt.Errorf("could not create new request: %v", err)
- }
- if ct != "" {
- req.Header.Set("Content-Type", ct)
- }
- return req, client, nil
- }
- func readBody(b io.ReadCloser) ([]byte, error) {
- defer b.Close()
- return ioutil.ReadAll(b)
- }
- func deleteContainer(container string) error {
- container = strings.TrimSpace(strings.Replace(container, "\n", " ", -1))
- rmArgs := strings.Split(fmt.Sprintf("rm -fv %v", container), " ")
- exitCode, err := runCommand(exec.Command(dockerBinary, rmArgs...))
- // set error manually if not set
- if exitCode != 0 && err == nil {
- err = fmt.Errorf("failed to remove container: `docker rm` exit is non-zero")
- }
- return err
- }
- func getAllContainers() (string, error) {
- getContainersCmd := exec.Command(dockerBinary, "ps", "-q", "-a")
- out, exitCode, err := runCommandWithOutput(getContainersCmd)
- if exitCode != 0 && err == nil {
- err = fmt.Errorf("failed to get a list of containers: %v\n", out)
- }
- return out, err
- }
- func deleteAllContainers() error {
- containers, err := getAllContainers()
- if err != nil {
- fmt.Println(containers)
- return err
- }
- if containers != "" {
- if err = deleteContainer(containers); err != nil {
- return err
- }
- }
- return nil
- }
- func deleteAllNetworks() error {
- networks, err := getAllNetworks()
- if err != nil {
- return err
- }
- var errors []string
- for _, n := range networks {
- if n.Name == "bridge" || n.Name == "none" || n.Name == "host" {
- continue
- }
- status, b, err := sockRequest("DELETE", "/networks/"+n.Name, nil)
- if err != nil {
- errors = append(errors, err.Error())
- continue
- }
- if status != http.StatusNoContent {
- errors = append(errors, fmt.Sprintf("error deleting network %s: %s", n.Name, string(b)))
- }
- }
- if len(errors) > 0 {
- return fmt.Errorf(strings.Join(errors, "\n"))
- }
- return nil
- }
- func getAllNetworks() ([]types.NetworkResource, error) {
- var networks []types.NetworkResource
- _, b, err := sockRequest("GET", "/networks", nil)
- if err != nil {
- return nil, err
- }
- if err := json.Unmarshal(b, &networks); err != nil {
- return nil, err
- }
- return networks, nil
- }
- func deleteAllVolumes() error {
- volumes, err := getAllVolumes()
- if err != nil {
- return err
- }
- var errors []string
- for _, v := range volumes {
- status, b, err := sockRequest("DELETE", "/volumes/"+v.Name, nil)
- if err != nil {
- errors = append(errors, err.Error())
- continue
- }
- if status != http.StatusNoContent {
- errors = append(errors, fmt.Sprintf("error deleting volume %s: %s", v.Name, string(b)))
- }
- }
- if len(errors) > 0 {
- return fmt.Errorf(strings.Join(errors, "\n"))
- }
- return nil
- }
- func getAllVolumes() ([]*types.Volume, error) {
- var volumes types.VolumesListResponse
- _, b, err := sockRequest("GET", "/volumes", nil)
- if err != nil {
- return nil, err
- }
- if err := json.Unmarshal(b, &volumes); err != nil {
- return nil, err
- }
- return volumes.Volumes, nil
- }
- var protectedImages = map[string]struct{}{}
- func deleteAllImages() error {
- cmd := exec.Command(dockerBinary, "images")
- cmd.Env = appendBaseEnv(true)
- out, err := cmd.CombinedOutput()
- if err != nil {
- return err
- }
- lines := strings.Split(string(out), "\n")[1:]
- var imgs []string
- for _, l := range lines {
- if l == "" {
- continue
- }
- fields := strings.Fields(l)
- imgTag := fields[0] + ":" + fields[1]
- if _, ok := protectedImages[imgTag]; !ok {
- if fields[0] == "<none>" {
- imgs = append(imgs, fields[2])
- continue
- }
- imgs = append(imgs, imgTag)
- }
- }
- if len(imgs) == 0 {
- return nil
- }
- args := append([]string{"rmi", "-f"}, imgs...)
- if err := exec.Command(dockerBinary, args...).Run(); err != nil {
- return err
- }
- return nil
- }
- func getPausedContainers() (string, error) {
- getPausedContainersCmd := exec.Command(dockerBinary, "ps", "-f", "status=paused", "-q", "-a")
- out, exitCode, err := runCommandWithOutput(getPausedContainersCmd)
- if exitCode != 0 && err == nil {
- err = fmt.Errorf("failed to get a list of paused containers: %v\n", out)
- }
- return out, err
- }
- func getSliceOfPausedContainers() ([]string, error) {
- out, err := getPausedContainers()
- if err == nil {
- if len(out) == 0 {
- return nil, err
- }
- slice := strings.Split(strings.TrimSpace(out), "\n")
- return slice, err
- }
- return []string{out}, err
- }
- func unpauseContainer(container string) error {
- unpauseCmd := exec.Command(dockerBinary, "unpause", container)
- exitCode, err := runCommand(unpauseCmd)
- if exitCode != 0 && err == nil {
- err = fmt.Errorf("failed to unpause container")
- }
- return nil
- }
- func unpauseAllContainers() error {
- containers, err := getPausedContainers()
- if err != nil {
- fmt.Println(containers)
- return err
- }
- containers = strings.Replace(containers, "\n", " ", -1)
- containers = strings.Trim(containers, " ")
- containerList := strings.Split(containers, " ")
- for _, value := range containerList {
- if err = unpauseContainer(value); err != nil {
- return err
- }
- }
- return nil
- }
- func deleteImages(images ...string) error {
- args := []string{"rmi", "-f"}
- args = append(args, images...)
- rmiCmd := exec.Command(dockerBinary, args...)
- exitCode, err := runCommand(rmiCmd)
- // set error manually if not set
- if exitCode != 0 && err == nil {
- err = fmt.Errorf("failed to remove image: `docker rmi` exit is non-zero")
- }
- return err
- }
- func imageExists(image string) error {
- inspectCmd := exec.Command(dockerBinary, "inspect", image)
- exitCode, err := runCommand(inspectCmd)
- if exitCode != 0 && err == nil {
- err = fmt.Errorf("couldn't find image %q", image)
- }
- return err
- }
- func pullImageIfNotExist(image string) error {
- if err := imageExists(image); err != nil {
- pullCmd := exec.Command(dockerBinary, "pull", image)
- _, exitCode, err := runCommandWithOutput(pullCmd)
- if err != nil || exitCode != 0 {
- return fmt.Errorf("image %q wasn't found locally and it couldn't be pulled: %s", image, err)
- }
- }
- return nil
- }
- func dockerCmdWithError(args ...string) (string, int, error) {
- if err := validateArgs(args...); err != nil {
- return "", 0, err
- }
- return integration.DockerCmdWithError(dockerBinary, args...)
- }
- func dockerCmdWithStdoutStderr(c *check.C, args ...string) (string, string, int) {
- if err := validateArgs(args...); err != nil {
- c.Fatalf(err.Error())
- }
- return integration.DockerCmdWithStdoutStderr(dockerBinary, c, args...)
- }
- func dockerCmd(c *check.C, args ...string) (string, int) {
- if err := validateArgs(args...); err != nil {
- c.Fatalf(err.Error())
- }
- return integration.DockerCmd(dockerBinary, c, args...)
- }
- // execute a docker command with a timeout
- func dockerCmdWithTimeout(timeout time.Duration, args ...string) (string, int, error) {
- if err := validateArgs(args...); err != nil {
- return "", 0, err
- }
- return integration.DockerCmdWithTimeout(dockerBinary, timeout, args...)
- }
- // execute a docker command in a directory
- func dockerCmdInDir(c *check.C, path string, args ...string) (string, int, error) {
- if err := validateArgs(args...); err != nil {
- c.Fatalf(err.Error())
- }
- return integration.DockerCmdInDir(dockerBinary, path, args...)
- }
- // execute a docker command in a directory with a timeout
- func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) (string, int, error) {
- if err := validateArgs(args...); err != nil {
- return "", 0, err
- }
- return integration.DockerCmdInDirWithTimeout(dockerBinary, timeout, path, args...)
- }
- // validateArgs is a checker to ensure tests are not running commands which are
- // not supported on platforms. Specifically on Windows this is 'busybox top'.
- func validateArgs(args ...string) error {
- if daemonPlatform != "windows" {
- return nil
- }
- foundBusybox := -1
- for key, value := range args {
- if strings.ToLower(value) == "busybox" {
- foundBusybox = key
- }
- if (foundBusybox != -1) && (key == foundBusybox+1) && (strings.ToLower(value) == "top") {
- return errors.New("Cannot use 'busybox top' in tests on Windows. Use runSleepingContainer()")
- }
- }
- return nil
- }
- // find the State.ExitCode in container metadata
- func findContainerExitCode(c *check.C, name string, vargs ...string) string {
- args := append(vargs, "inspect", "--format='{{ .State.ExitCode }} {{ .State.Error }}'", name)
- cmd := exec.Command(dockerBinary, args...)
- out, _, err := runCommandWithOutput(cmd)
- if err != nil {
- c.Fatal(err, out)
- }
- return out
- }
- func findContainerIP(c *check.C, id string, network string) string {
- out, _ := dockerCmd(c, "inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.%s.IPAddress }}'", network), id)
- return strings.Trim(out, " \r\n'")
- }
- func getContainerCount() (int, error) {
- const containers = "Containers:"
- cmd := exec.Command(dockerBinary, "info")
- out, _, err := runCommandWithOutput(cmd)
- if err != nil {
- return 0, err
- }
- lines := strings.Split(out, "\n")
- for _, line := range lines {
- if strings.Contains(line, containers) {
- output := strings.TrimSpace(line)
- output = strings.TrimLeft(output, containers)
- output = strings.Trim(output, " ")
- containerCount, err := strconv.Atoi(output)
- if err != nil {
- return 0, err
- }
- return containerCount, nil
- }
- }
- return 0, fmt.Errorf("couldn't find the Container count in the output")
- }
- // FakeContext creates directories that can be used as a build context
- type FakeContext struct {
- Dir string
- }
- // Add a file at a path, creating directories where necessary
- func (f *FakeContext) Add(file, content string) error {
- return f.addFile(file, []byte(content))
- }
- func (f *FakeContext) addFile(file string, content []byte) error {
- filepath := path.Join(f.Dir, file)
- dirpath := path.Dir(filepath)
- if dirpath != "." {
- if err := os.MkdirAll(dirpath, 0755); err != nil {
- return err
- }
- }
- return ioutil.WriteFile(filepath, content, 0644)
- }
- // Delete a file at a path
- func (f *FakeContext) Delete(file string) error {
- filepath := path.Join(f.Dir, file)
- return os.RemoveAll(filepath)
- }
- // Close deletes the context
- func (f *FakeContext) Close() error {
- return os.RemoveAll(f.Dir)
- }
- func fakeContextFromNewTempDir() (*FakeContext, error) {
- tmp, err := ioutil.TempDir("", "fake-context")
- if err != nil {
- return nil, err
- }
- if err := os.Chmod(tmp, 0755); err != nil {
- return nil, err
- }
- return fakeContextFromDir(tmp), nil
- }
- func fakeContextFromDir(dir string) *FakeContext {
- return &FakeContext{dir}
- }
- func fakeContextWithFiles(files map[string]string) (*FakeContext, error) {
- ctx, err := fakeContextFromNewTempDir()
- if err != nil {
- return nil, err
- }
- for file, content := range files {
- if err := ctx.Add(file, content); err != nil {
- ctx.Close()
- return nil, err
- }
- }
- return ctx, nil
- }
- func fakeContextAddDockerfile(ctx *FakeContext, dockerfile string) error {
- if err := ctx.Add("Dockerfile", dockerfile); err != nil {
- ctx.Close()
- return err
- }
- return nil
- }
- func fakeContext(dockerfile string, files map[string]string) (*FakeContext, error) {
- ctx, err := fakeContextWithFiles(files)
- if err != nil {
- return nil, err
- }
- if err := fakeContextAddDockerfile(ctx, dockerfile); err != nil {
- return nil, err
- }
- return ctx, nil
- }
- // FakeStorage is a static file server. It might be running locally or remotely
- // on test host.
- type FakeStorage interface {
- Close() error
- URL() string
- CtxDir() string
- }
- func fakeBinaryStorage(archives map[string]*bytes.Buffer) (FakeStorage, error) {
- ctx, err := fakeContextFromNewTempDir()
- if err != nil {
- return nil, err
- }
- for name, content := range archives {
- if err := ctx.addFile(name, content.Bytes()); err != nil {
- return nil, err
- }
- }
- return fakeStorageWithContext(ctx)
- }
- // fakeStorage returns either a local or remote (at daemon machine) file server
- func fakeStorage(files map[string]string) (FakeStorage, error) {
- ctx, err := fakeContextWithFiles(files)
- if err != nil {
- return nil, err
- }
- return fakeStorageWithContext(ctx)
- }
- // fakeStorageWithContext returns either a local or remote (at daemon machine) file server
- func fakeStorageWithContext(ctx *FakeContext) (FakeStorage, error) {
- if isLocalDaemon {
- return newLocalFakeStorage(ctx)
- }
- return newRemoteFileServer(ctx)
- }
- // localFileStorage is a file storage on the running machine
- type localFileStorage struct {
- *FakeContext
- *httptest.Server
- }
- func (s *localFileStorage) URL() string {
- return s.Server.URL
- }
- func (s *localFileStorage) CtxDir() string {
- return s.FakeContext.Dir
- }
- func (s *localFileStorage) Close() error {
- defer s.Server.Close()
- return s.FakeContext.Close()
- }
- func newLocalFakeStorage(ctx *FakeContext) (*localFileStorage, error) {
- handler := http.FileServer(http.Dir(ctx.Dir))
- server := httptest.NewServer(handler)
- return &localFileStorage{
- FakeContext: ctx,
- Server: server,
- }, nil
- }
- // remoteFileServer is a containerized static file server started on the remote
- // testing machine to be used in URL-accepting docker build functionality.
- type remoteFileServer struct {
- host string // hostname/port web server is listening to on docker host e.g. 0.0.0.0:43712
- container string
- image string
- ctx *FakeContext
- }
- func (f *remoteFileServer) URL() string {
- u := url.URL{
- Scheme: "http",
- Host: f.host}
- return u.String()
- }
- func (f *remoteFileServer) CtxDir() string {
- return f.ctx.Dir
- }
- func (f *remoteFileServer) Close() error {
- defer func() {
- if f.ctx != nil {
- f.ctx.Close()
- }
- if f.image != "" {
- deleteImages(f.image)
- }
- }()
- if f.container == "" {
- return nil
- }
- return deleteContainer(f.container)
- }
- func newRemoteFileServer(ctx *FakeContext) (*remoteFileServer, error) {
- var (
- image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
- container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
- )
- // Build the image
- if err := fakeContextAddDockerfile(ctx, `FROM httpserver
- COPY . /static`); err != nil {
- return nil, fmt.Errorf("Cannot add Dockerfile to context: %v", err)
- }
- if _, err := buildImageFromContext(image, ctx, false); err != nil {
- return nil, fmt.Errorf("failed building file storage container image: %v", err)
- }
- // Start the container
- runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--name", container, image)
- if out, ec, err := runCommandWithOutput(runCmd); err != nil {
- return nil, fmt.Errorf("failed to start file storage container. ec=%v\nout=%s\nerr=%v", ec, out, err)
- }
- // Find out the system assigned port
- out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "port", container, "80/tcp"))
- if err != nil {
- return nil, fmt.Errorf("failed to find container port: err=%v\nout=%s", err, out)
- }
- fileserverHostPort := strings.Trim(out, "\n")
- _, port, err := net.SplitHostPort(fileserverHostPort)
- if err != nil {
- return nil, fmt.Errorf("unable to parse file server host:port: %v", err)
- }
- dockerHostURL, err := url.Parse(daemonHost())
- if err != nil {
- return nil, fmt.Errorf("unable to parse daemon host URL: %v", err)
- }
- host, _, err := net.SplitHostPort(dockerHostURL.Host)
- if err != nil {
- return nil, fmt.Errorf("unable to parse docker daemon host:port: %v", err)
- }
- return &remoteFileServer{
- container: container,
- image: image,
- host: fmt.Sprintf("%s:%s", host, port),
- ctx: ctx}, nil
- }
- func inspectFieldAndMarshall(c *check.C, name, field string, output interface{}) {
- str := inspectFieldJSON(c, name, field)
- err := json.Unmarshal([]byte(str), output)
- if c != nil {
- c.Assert(err, check.IsNil, check.Commentf("failed to unmarshal: %v", err))
- }
- }
- func inspectFilter(name, filter string) (string, error) {
- format := fmt.Sprintf("{{%s}}", filter)
- inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
- out, exitCode, err := runCommandWithOutput(inspectCmd)
- if err != nil || exitCode != 0 {
- return "", fmt.Errorf("failed to inspect %s: %s", name, out)
- }
- return strings.TrimSpace(out), nil
- }
- func inspectFieldWithError(name, field string) (string, error) {
- return inspectFilter(name, fmt.Sprintf(".%s", field))
- }
- func inspectField(c *check.C, name, field string) string {
- out, err := inspectFilter(name, fmt.Sprintf(".%s", field))
- if c != nil {
- c.Assert(err, check.IsNil)
- }
- return out
- }
- func inspectFieldJSON(c *check.C, name, field string) string {
- out, err := inspectFilter(name, fmt.Sprintf("json .%s", field))
- if c != nil {
- c.Assert(err, check.IsNil)
- }
- return out
- }
- func inspectFieldMap(c *check.C, name, path, field string) string {
- out, err := inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
- if c != nil {
- c.Assert(err, check.IsNil)
- }
- return out
- }
- func inspectMountSourceField(name, destination string) (string, error) {
- m, err := inspectMountPoint(name, destination)
- if err != nil {
- return "", err
- }
- return m.Source, nil
- }
- func inspectMountPoint(name, destination string) (types.MountPoint, error) {
- out, err := inspectFilter(name, "json .Mounts")
- if err != nil {
- return types.MountPoint{}, err
- }
- return inspectMountPointJSON(out, destination)
- }
- var errMountNotFound = errors.New("mount point not found")
- func inspectMountPointJSON(j, destination string) (types.MountPoint, error) {
- var mp []types.MountPoint
- if err := unmarshalJSON([]byte(j), &mp); err != nil {
- return types.MountPoint{}, err
- }
- var m *types.MountPoint
- for _, c := range mp {
- if c.Destination == destination {
- m = &c
- break
- }
- }
- if m == nil {
- return types.MountPoint{}, errMountNotFound
- }
- return *m, nil
- }
- func getIDByName(name string) (string, error) {
- return inspectFieldWithError(name, "Id")
- }
- // getContainerState returns the exit code of the container
- // and true if it's running
- // the exit code should be ignored if it's running
- func getContainerState(c *check.C, id string) (int, bool, error) {
- var (
- exitStatus int
- running bool
- )
- out, exitCode := dockerCmd(c, "inspect", "--format={{.State.Running}} {{.State.ExitCode}}", id)
- if exitCode != 0 {
- return 0, false, fmt.Errorf("%q doesn't exist: %s", id, out)
- }
- out = strings.Trim(out, "\n")
- splitOutput := strings.Split(out, " ")
- if len(splitOutput) != 2 {
- return 0, false, fmt.Errorf("failed to get container state: output is broken")
- }
- if splitOutput[0] == "true" {
- running = true
- }
- if n, err := strconv.Atoi(splitOutput[1]); err == nil {
- exitStatus = n
- } else {
- return 0, false, fmt.Errorf("failed to get container state: couldn't parse integer")
- }
- return exitStatus, running, nil
- }
- func buildImageCmd(name, dockerfile string, useCache bool, buildFlags ...string) *exec.Cmd {
- args := []string{"build", "-t", name}
- if !useCache {
- args = append(args, "--no-cache")
- }
- args = append(args, buildFlags...)
- args = append(args, "-")
- buildCmd := exec.Command(dockerBinary, args...)
- buildCmd.Stdin = strings.NewReader(dockerfile)
- return buildCmd
- }
- func buildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, string, error) {
- buildCmd := buildImageCmd(name, dockerfile, useCache, buildFlags...)
- out, exitCode, err := runCommandWithOutput(buildCmd)
- if err != nil || exitCode != 0 {
- return "", out, fmt.Errorf("failed to build the image: %s", out)
- }
- id, err := getIDByName(name)
- if err != nil {
- return "", out, err
- }
- return id, out, nil
- }
- func buildImageWithStdoutStderr(name, dockerfile string, useCache bool, buildFlags ...string) (string, string, string, error) {
- buildCmd := buildImageCmd(name, dockerfile, useCache, buildFlags...)
- stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
- if err != nil || exitCode != 0 {
- return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
- }
- id, err := getIDByName(name)
- if err != nil {
- return "", stdout, stderr, err
- }
- return id, stdout, stderr, nil
- }
- func buildImage(name, dockerfile string, useCache bool, buildFlags ...string) (string, error) {
- id, _, err := buildImageWithOut(name, dockerfile, useCache, buildFlags...)
- return id, err
- }
- func buildImageFromContext(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, error) {
- id, _, err := buildImageFromContextWithOut(name, ctx, useCache, buildFlags...)
- if err != nil {
- return "", err
- }
- return id, nil
- }
- func buildImageFromContextWithOut(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, string, error) {
- args := []string{"build", "-t", name}
- if !useCache {
- args = append(args, "--no-cache")
- }
- args = append(args, buildFlags...)
- args = append(args, ".")
- buildCmd := exec.Command(dockerBinary, args...)
- buildCmd.Dir = ctx.Dir
- out, exitCode, err := runCommandWithOutput(buildCmd)
- if err != nil || exitCode != 0 {
- return "", "", fmt.Errorf("failed to build the image: %s", out)
- }
- id, err := getIDByName(name)
- if err != nil {
- return "", "", err
- }
- return id, out, nil
- }
- func buildImageFromContextWithStdoutStderr(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, string, string, error) {
- args := []string{"build", "-t", name}
- if !useCache {
- args = append(args, "--no-cache")
- }
- args = append(args, buildFlags...)
- args = append(args, ".")
- buildCmd := exec.Command(dockerBinary, args...)
- buildCmd.Dir = ctx.Dir
- stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
- if err != nil || exitCode != 0 {
- return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
- }
- id, err := getIDByName(name)
- if err != nil {
- return "", stdout, stderr, err
- }
- return id, stdout, stderr, nil
- }
- func buildImageFromGitWithStdoutStderr(name string, ctx *fakeGit, useCache bool, buildFlags ...string) (string, string, string, error) {
- args := []string{"build", "-t", name}
- if !useCache {
- args = append(args, "--no-cache")
- }
- args = append(args, buildFlags...)
- args = append(args, ctx.RepoURL)
- buildCmd := exec.Command(dockerBinary, args...)
- stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
- if err != nil || exitCode != 0 {
- return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
- }
- id, err := getIDByName(name)
- if err != nil {
- return "", stdout, stderr, err
- }
- return id, stdout, stderr, nil
- }
- func buildImageFromPath(name, path string, useCache bool, buildFlags ...string) (string, error) {
- args := []string{"build", "-t", name}
- if !useCache {
- args = append(args, "--no-cache")
- }
- args = append(args, buildFlags...)
- args = append(args, path)
- buildCmd := exec.Command(dockerBinary, args...)
- out, exitCode, err := runCommandWithOutput(buildCmd)
- if err != nil || exitCode != 0 {
- return "", fmt.Errorf("failed to build the image: %s", out)
- }
- return getIDByName(name)
- }
- type gitServer interface {
- URL() string
- Close() error
- }
- type localGitServer struct {
- *httptest.Server
- }
- func (r *localGitServer) Close() error {
- r.Server.Close()
- return nil
- }
- func (r *localGitServer) URL() string {
- return r.Server.URL
- }
- type fakeGit struct {
- root string
- server gitServer
- RepoURL string
- }
- func (g *fakeGit) Close() {
- g.server.Close()
- os.RemoveAll(g.root)
- }
- func newFakeGit(name string, files map[string]string, enforceLocalServer bool) (*fakeGit, error) {
- ctx, err := fakeContextWithFiles(files)
- if err != nil {
- return nil, err
- }
- defer ctx.Close()
- curdir, err := os.Getwd()
- if err != nil {
- return nil, err
- }
- defer os.Chdir(curdir)
- if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil {
- return nil, fmt.Errorf("error trying to init repo: %s (%s)", err, output)
- }
- err = os.Chdir(ctx.Dir)
- if err != nil {
- return nil, err
- }
- if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil {
- return nil, fmt.Errorf("error trying to set 'user.name': %s (%s)", err, output)
- }
- if output, err := exec.Command("git", "config", "user.email", "fake.user@example.com").CombinedOutput(); err != nil {
- return nil, fmt.Errorf("error trying to set 'user.email': %s (%s)", err, output)
- }
- if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
- return nil, fmt.Errorf("error trying to add files to repo: %s (%s)", err, output)
- }
- if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil {
- return nil, fmt.Errorf("error trying to commit to repo: %s (%s)", err, output)
- }
- root, err := ioutil.TempDir("", "docker-test-git-repo")
- if err != nil {
- return nil, err
- }
- repoPath := filepath.Join(root, name+".git")
- if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil {
- os.RemoveAll(root)
- return nil, fmt.Errorf("error trying to clone --bare: %s (%s)", err, output)
- }
- err = os.Chdir(repoPath)
- if err != nil {
- os.RemoveAll(root)
- return nil, err
- }
- if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil {
- os.RemoveAll(root)
- return nil, fmt.Errorf("error trying to git update-server-info: %s (%s)", err, output)
- }
- err = os.Chdir(curdir)
- if err != nil {
- os.RemoveAll(root)
- return nil, err
- }
- var server gitServer
- if !enforceLocalServer {
- // use fakeStorage server, which might be local or remote (at test daemon)
- server, err = fakeStorageWithContext(fakeContextFromDir(root))
- if err != nil {
- return nil, fmt.Errorf("cannot start fake storage: %v", err)
- }
- } else {
- // always start a local http server on CLI test machine
- httpServer := httptest.NewServer(http.FileServer(http.Dir(root)))
- server = &localGitServer{httpServer}
- }
- return &fakeGit{
- root: root,
- server: server,
- RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name),
- }, nil
- }
- // Write `content` to the file at path `dst`, creating it if necessary,
- // as well as any missing directories.
- // The file is truncated if it already exists.
- // Fail the test when error occurs.
- func writeFile(dst, content string, c *check.C) {
- // Create subdirectories if necessary
- c.Assert(os.MkdirAll(path.Dir(dst), 0700), check.IsNil)
- f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
- c.Assert(err, check.IsNil)
- defer f.Close()
- // Write content (truncate if it exists)
- _, err = io.Copy(f, strings.NewReader(content))
- c.Assert(err, check.IsNil)
- }
- // Return the contents of file at path `src`.
- // Fail the test when error occurs.
- func readFile(src string, c *check.C) (content string) {
- data, err := ioutil.ReadFile(src)
- c.Assert(err, check.IsNil)
- return string(data)
- }
- func containerStorageFile(containerID, basename string) string {
- return filepath.Join(containerStoragePath, containerID, basename)
- }
- // docker commands that use this function must be run with the '-d' switch.
- func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, error) {
- out, _, err := runCommandWithOutput(cmd)
- if err != nil {
- return nil, fmt.Errorf("%v: %q", err, out)
- }
- contID := strings.TrimSpace(out)
- if err := waitRun(contID); err != nil {
- return nil, fmt.Errorf("%v: %q", contID, err)
- }
- return readContainerFile(contID, filename)
- }
- func readContainerFile(containerID, filename string) ([]byte, error) {
- f, err := os.Open(containerStorageFile(containerID, filename))
- if err != nil {
- return nil, err
- }
- defer f.Close()
- content, err := ioutil.ReadAll(f)
- if err != nil {
- return nil, err
- }
- return content, nil
- }
- func readContainerFileWithExec(containerID, filename string) ([]byte, error) {
- out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "exec", containerID, "cat", filename))
- return []byte(out), err
- }
- // daemonTime provides the current time on the daemon host
- func daemonTime(c *check.C) time.Time {
- if isLocalDaemon {
- return time.Now()
- }
- status, body, err := sockRequest("GET", "/info", nil)
- c.Assert(err, check.IsNil)
- c.Assert(status, check.Equals, http.StatusOK)
- type infoJSON struct {
- SystemTime string
- }
- var info infoJSON
- err = json.Unmarshal(body, &info)
- c.Assert(err, check.IsNil, check.Commentf("unable to unmarshal GET /info response"))
- dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
- c.Assert(err, check.IsNil, check.Commentf("invalid time format in GET /info response"))
- return dt
- }
- func setupRegistry(c *check.C, schema1, auth bool) *testRegistryV2 {
- reg, err := newTestRegistryV2(c, schema1, auth)
- c.Assert(err, check.IsNil)
- // Wait for registry to be ready to serve requests.
- for i := 0; i != 50; i++ {
- if err = reg.Ping(); err == nil {
- break
- }
- time.Sleep(100 * time.Millisecond)
- }
- c.Assert(err, check.IsNil, check.Commentf("Timeout waiting for test registry to become available: %v", err))
- return reg
- }
- func setupNotary(c *check.C) *testNotary {
- ts, err := newTestNotary(c)
- c.Assert(err, check.IsNil)
- return ts
- }
- // appendBaseEnv appends the minimum set of environment variables to exec the
- // docker cli binary for testing with correct configuration to the given env
- // list.
- func appendBaseEnv(isTLS bool, env ...string) []string {
- preserveList := []string{
- // preserve remote test host
- "DOCKER_HOST",
- // windows: requires preserving SystemRoot, otherwise dial tcp fails
- // with "GetAddrInfoW: A non-recoverable error occurred during a database lookup."
- "SystemRoot",
- }
- if isTLS {
- preserveList = append(preserveList, "DOCKER_TLS_VERIFY", "DOCKER_CERT_PATH")
- }
- for _, key := range preserveList {
- if val := os.Getenv(key); val != "" {
- env = append(env, fmt.Sprintf("%s=%s", key, val))
- }
- }
- return env
- }
- func createTmpFile(c *check.C, content string) string {
- f, err := ioutil.TempFile("", "testfile")
- c.Assert(err, check.IsNil)
- filename := f.Name()
- err = ioutil.WriteFile(filename, []byte(content), 0644)
- c.Assert(err, check.IsNil)
- return filename
- }
- func buildImageWithOutInDamon(socket string, name, dockerfile string, useCache bool) (string, error) {
- args := []string{"--host", socket}
- buildCmd := buildImageCmdArgs(args, name, dockerfile, useCache)
- out, exitCode, err := runCommandWithOutput(buildCmd)
- if err != nil || exitCode != 0 {
- return out, fmt.Errorf("failed to build the image: %s, error: %v", out, err)
- }
- return out, nil
- }
- func buildImageCmdArgs(args []string, name, dockerfile string, useCache bool) *exec.Cmd {
- args = append(args, []string{"-D", "build", "-t", name}...)
- if !useCache {
- args = append(args, "--no-cache")
- }
- args = append(args, "-")
- buildCmd := exec.Command(dockerBinary, args...)
- buildCmd.Stdin = strings.NewReader(dockerfile)
- return buildCmd
- }
- func waitForContainer(contID string, args ...string) error {
- args = append([]string{"run", "--name", contID}, args...)
- cmd := exec.Command(dockerBinary, args...)
- if _, err := runCommand(cmd); err != nil {
- return err
- }
- if err := waitRun(contID); err != nil {
- return err
- }
- return nil
- }
- // waitRun will wait for the specified container to be running, maximum 5 seconds.
- func waitRun(contID string) error {
- return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)
- }
- // waitExited will wait for the specified container to state exit, subject
- // to a maximum time limit in seconds supplied by the caller
- func waitExited(contID string, duration time.Duration) error {
- return waitInspect(contID, "{{.State.Status}}", "exited", duration)
- }
- // waitInspect will wait for the specified container to have the specified string
- // in the inspect output. It will wait until the specified timeout (in seconds)
- // is reached.
- func waitInspect(name, expr, expected string, timeout time.Duration) error {
- return waitInspectWithArgs(name, expr, expected, timeout)
- }
- func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg ...string) error {
- after := time.After(timeout)
- args := append(arg, "inspect", "-f", expr, name)
- for {
- cmd := exec.Command(dockerBinary, args...)
- out, _, err := runCommandWithOutput(cmd)
- if err != nil {
- if !strings.Contains(out, "No such") {
- return fmt.Errorf("error executing docker inspect: %v\n%s", err, out)
- }
- select {
- case <-after:
- return err
- default:
- time.Sleep(10 * time.Millisecond)
- continue
- }
- }
- out = strings.TrimSpace(out)
- if out == expected {
- break
- }
- select {
- case <-after:
- return fmt.Errorf("condition \"%q == %q\" not true in time", out, expected)
- default:
- }
- time.Sleep(100 * time.Millisecond)
- }
- return nil
- }
- func getInspectBody(c *check.C, version, id string) []byte {
- endpoint := fmt.Sprintf("/%s/containers/%s/json", version, id)
- status, body, err := sockRequest("GET", endpoint, nil)
- c.Assert(err, check.IsNil)
- c.Assert(status, check.Equals, http.StatusOK)
- return body
- }
- // Run a long running idle task in a background container using the
- // system-specific default image and command.
- func runSleepingContainer(c *check.C, extraArgs ...string) (string, int) {
- return runSleepingContainerInImage(c, defaultSleepImage, extraArgs...)
- }
- // Run a long running idle task in a background container using the specified
- // image and the system-specific command.
- func runSleepingContainerInImage(c *check.C, image string, extraArgs ...string) (string, int) {
- args := []string{"run", "-d"}
- args = append(args, extraArgs...)
- args = append(args, image)
- args = append(args, defaultSleepCommand...)
- return dockerCmd(c, args...)
- }
- func getRootUIDGID() (int, int, error) {
- uidgid := strings.Split(filepath.Base(dockerBasePath), ".")
- if len(uidgid) == 1 {
- //user namespace remapping is not turned on; return 0
- return 0, 0, nil
- }
- uid, err := strconv.Atoi(uidgid[0])
- if err != nil {
- return 0, 0, err
- }
- gid, err := strconv.Atoi(uidgid[1])
- if err != nil {
- return 0, 0, err
- }
- return uid, gid, nil
- }
- // minimalBaseImage returns the name of the minimal base image for the current
- // daemon platform.
- func minimalBaseImage() string {
- if daemonPlatform == "windows" {
- return WindowsBaseImage
- }
- return "scratch"
- }
- func getGoroutineNumber() (int, error) {
- i := struct {
- NGoroutines int
- }{}
- status, b, err := sockRequest("GET", "/info", nil)
- if err != nil {
- return 0, err
- }
- if status != http.StatusOK {
- return 0, fmt.Errorf("http status code: %d", status)
- }
- if err := json.Unmarshal(b, &i); err != nil {
- return 0, err
- }
- return i.NGoroutines, nil
- }
- func waitForGoroutines(expected int) error {
- t := time.After(30 * time.Second)
- for {
- select {
- case <-t:
- n, err := getGoroutineNumber()
- if err != nil {
- return err
- }
- if n > expected {
- return fmt.Errorf("leaked goroutines: expected less than or equal to %d, got: %d", expected, n)
- }
- default:
- n, err := getGoroutineNumber()
- if err != nil {
- return err
- }
- if n <= expected {
- return nil
- }
- time.Sleep(200 * time.Millisecond)
- }
- }
- }
|