github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration-cli/daemon/daemon.go (about)

     1  package daemon // import "github.com/Prakhar-Agarwal-byte/moby/integration-cli/daemon"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/Prakhar-Agarwal-byte/moby/testutil/daemon"
    11  	"github.com/pkg/errors"
    12  	"gotest.tools/v3/assert"
    13  	"gotest.tools/v3/icmd"
    14  )
    15  
    16  // Daemon represents a Docker daemon for the testing framework.
    17  type Daemon struct {
    18  	*daemon.Daemon
    19  	dockerBinary string
    20  }
    21  
    22  // New returns a Daemon instance to be used for testing.
    23  // This will create a directory such as d123456789 in the folder specified by $DOCKER_INTEGRATION_DAEMON_DEST or $DEST.
    24  // The daemon will not automatically start.
    25  func New(t testing.TB, dockerBinary string, dockerdBinary string, ops ...daemon.Option) *Daemon {
    26  	t.Helper()
    27  	ops = append(ops, daemon.WithDockerdBinary(dockerdBinary))
    28  	d := daemon.New(t, ops...)
    29  	return &Daemon{
    30  		Daemon:       d,
    31  		dockerBinary: dockerBinary,
    32  	}
    33  }
    34  
    35  // Cmd executes a docker CLI command against this daemon.
    36  // Example: d.Cmd("version") will run docker -H unix://path/to/unix.sock version
    37  func (d *Daemon) Cmd(args ...string) (string, error) {
    38  	result := icmd.RunCmd(d.Command(args...))
    39  	return result.Combined(), result.Error
    40  }
    41  
    42  // Command creates a docker CLI command against this daemon, to be executed later.
    43  // Example: d.Command("version") creates a command to run "docker -H unix://path/to/unix.sock version"
    44  func (d *Daemon) Command(args ...string) icmd.Cmd {
    45  	return icmd.Command(d.dockerBinary, d.PrependHostArg(args)...)
    46  }
    47  
    48  // PrependHostArg prepend the specified arguments by the daemon host flags
    49  func (d *Daemon) PrependHostArg(args []string) []string {
    50  	for _, arg := range args {
    51  		if arg == "--host" || arg == "-H" {
    52  			return args
    53  		}
    54  	}
    55  	return append([]string{"--host", d.Sock()}, args...)
    56  }
    57  
    58  // GetIDByName returns the ID of an object (container, volume, …) given its name
    59  func (d *Daemon) GetIDByName(name string) (string, error) {
    60  	return d.inspectFieldWithError(name, "Id")
    61  }
    62  
    63  // InspectField returns the field filter by 'filter'
    64  func (d *Daemon) InspectField(name, filter string) (string, error) {
    65  	return d.inspectFilter(name, filter)
    66  }
    67  
    68  func (d *Daemon) inspectFilter(name, filter string) (string, error) {
    69  	format := fmt.Sprintf("{{%s}}", filter)
    70  	out, err := d.Cmd("inspect", "-f", format, name)
    71  	if err != nil {
    72  		return "", errors.Errorf("failed to inspect %s: %s", name, out)
    73  	}
    74  	return strings.TrimSpace(out), nil
    75  }
    76  
    77  func (d *Daemon) inspectFieldWithError(name, field string) (string, error) {
    78  	return d.inspectFilter(name, "."+field)
    79  }
    80  
    81  // CheckActiveContainerCount returns the number of active containers
    82  // FIXME(vdemeester) should re-use ActivateContainers in some way
    83  func (d *Daemon) CheckActiveContainerCount(ctx context.Context) func(t *testing.T) (interface{}, string) {
    84  	return func(t *testing.T) (interface{}, string) {
    85  		t.Helper()
    86  		out, err := d.Cmd("ps", "-q")
    87  		assert.NilError(t, err)
    88  		if len(strings.TrimSpace(out)) == 0 {
    89  			return 0, ""
    90  		}
    91  		return len(strings.Split(strings.TrimSpace(out), "\n")), fmt.Sprintf("output: %q", out)
    92  	}
    93  }
    94  
    95  // WaitRun waits for a container to be running for 10s
    96  func (d *Daemon) WaitRun(contID string) error {
    97  	args := []string{"--host", d.Sock()}
    98  	return WaitInspectWithArgs(d.dockerBinary, contID, "{{.State.Running}}", "true", 10*time.Second, args...)
    99  }
   100  
   101  // WaitInspectWithArgs waits for the specified expression to be equals to the specified expected string in the given time.
   102  // Deprecated: use cli.WaitCmd instead
   103  func WaitInspectWithArgs(dockerBinary, name, expr, expected string, timeout time.Duration, arg ...string) error {
   104  	after := time.After(timeout)
   105  
   106  	args := append(arg, "inspect", "-f", expr, name)
   107  	for {
   108  		result := icmd.RunCommand(dockerBinary, args...)
   109  		if result.Error != nil {
   110  			if !strings.Contains(strings.ToLower(result.Stderr()), "no such") {
   111  				return errors.Errorf("error executing docker inspect: %v\n%s",
   112  					result.Stderr(), result.Stdout())
   113  			}
   114  			select {
   115  			case <-after:
   116  				return result.Error
   117  			default:
   118  				time.Sleep(10 * time.Millisecond)
   119  				continue
   120  			}
   121  		}
   122  
   123  		out := strings.TrimSpace(result.Stdout())
   124  		if out == expected {
   125  			break
   126  		}
   127  
   128  		select {
   129  		case <-after:
   130  			return errors.Errorf("condition \"%q == %q\" not true in time (%v)", out, expected, timeout)
   131  		default:
   132  		}
   133  
   134  		time.Sleep(100 * time.Millisecond)
   135  	}
   136  	return nil
   137  }