github.com/rish1988/moby@v25.0.2+incompatible/integration/service/inspect_test.go (about)

     1  package service // import "github.com/docker/docker/integration/service"
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/api/types/container"
     9  	swarmtypes "github.com/docker/docker/api/types/swarm"
    10  	"github.com/docker/docker/integration/internal/swarm"
    11  	"github.com/google/go-cmp/cmp"
    12  	"gotest.tools/v3/assert"
    13  	is "gotest.tools/v3/assert/cmp"
    14  	"gotest.tools/v3/poll"
    15  	"gotest.tools/v3/skip"
    16  )
    17  
    18  func TestInspect(t *testing.T) {
    19  	skip.If(t, testEnv.IsRemoteDaemon)
    20  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    21  	ctx := setupTest(t)
    22  	d := swarm.NewSwarm(ctx, t, testEnv)
    23  	defer d.Stop(t)
    24  	client := d.NewClientT(t)
    25  	defer client.Close()
    26  
    27  	now := time.Now()
    28  	var instances uint64 = 2
    29  	serviceSpec := fullSwarmServiceSpec("test-service-inspect"+t.Name(), instances)
    30  
    31  	resp, err := client.ServiceCreate(ctx, serviceSpec, types.ServiceCreateOptions{
    32  		QueryRegistry: false,
    33  	})
    34  	assert.NilError(t, err)
    35  
    36  	id := resp.ID
    37  	poll.WaitOn(t, swarm.RunningTasksCount(ctx, client, id, instances))
    38  
    39  	service, _, err := client.ServiceInspectWithRaw(ctx, id, types.ServiceInspectOptions{})
    40  	assert.NilError(t, err)
    41  
    42  	expected := swarmtypes.Service{
    43  		ID:   id,
    44  		Spec: serviceSpec,
    45  		Meta: swarmtypes.Meta{
    46  			Version:   swarmtypes.Version{Index: uint64(11)},
    47  			CreatedAt: now,
    48  			UpdatedAt: now,
    49  		},
    50  	}
    51  	assert.Check(t, is.DeepEqual(service, expected, cmpServiceOpts()))
    52  }
    53  
    54  // TODO: use helpers from gotest.tools/assert/opt when available
    55  func cmpServiceOpts() cmp.Option {
    56  	const threshold = 20 * time.Second
    57  
    58  	metaTimeFields := func(path cmp.Path) bool {
    59  		switch path.String() {
    60  		case "Meta.CreatedAt", "Meta.UpdatedAt":
    61  			return true
    62  		}
    63  		return false
    64  	}
    65  	withinThreshold := cmp.Comparer(func(x, y time.Time) bool {
    66  		delta := x.Sub(y)
    67  		return delta < threshold && delta > -threshold
    68  	})
    69  
    70  	return cmp.FilterPath(metaTimeFields, withinThreshold)
    71  }
    72  
    73  func fullSwarmServiceSpec(name string, replicas uint64) swarmtypes.ServiceSpec {
    74  	restartDelay := 100 * time.Millisecond
    75  	maxAttempts := uint64(4)
    76  
    77  	return swarmtypes.ServiceSpec{
    78  		Annotations: swarmtypes.Annotations{
    79  			Name: name,
    80  			Labels: map[string]string{
    81  				"service-label": "service-label-value",
    82  			},
    83  		},
    84  		TaskTemplate: swarmtypes.TaskSpec{
    85  			ContainerSpec: &swarmtypes.ContainerSpec{
    86  				Image:           "busybox:latest",
    87  				Labels:          map[string]string{"container-label": "container-value"},
    88  				Command:         []string{"/bin/top"},
    89  				Args:            []string{"-d", "5"},
    90  				Hostname:        "hostname",
    91  				Env:             []string{"envvar=envvalue"},
    92  				Dir:             "/work",
    93  				User:            "root",
    94  				StopSignal:      "SIGINT",
    95  				StopGracePeriod: &restartDelay,
    96  				Hosts:           []string{"8.8.8.8  google"},
    97  				DNSConfig: &swarmtypes.DNSConfig{
    98  					Nameservers: []string{"8.8.8.8"},
    99  					Search:      []string{"somedomain"},
   100  				},
   101  				Isolation: container.IsolationDefault,
   102  			},
   103  			RestartPolicy: &swarmtypes.RestartPolicy{
   104  				Delay:       &restartDelay,
   105  				Condition:   swarmtypes.RestartPolicyConditionOnFailure,
   106  				MaxAttempts: &maxAttempts,
   107  			},
   108  			Runtime: swarmtypes.RuntimeContainer,
   109  		},
   110  		Mode: swarmtypes.ServiceMode{
   111  			Replicated: &swarmtypes.ReplicatedService{
   112  				Replicas: &replicas,
   113  			},
   114  		},
   115  		UpdateConfig: &swarmtypes.UpdateConfig{
   116  			Parallelism:     2,
   117  			Delay:           200 * time.Second,
   118  			FailureAction:   swarmtypes.UpdateFailureActionContinue,
   119  			Monitor:         2 * time.Second,
   120  			MaxFailureRatio: 0.2,
   121  			Order:           swarmtypes.UpdateOrderStopFirst,
   122  		},
   123  		RollbackConfig: &swarmtypes.UpdateConfig{
   124  			Parallelism:     3,
   125  			Delay:           300 * time.Second,
   126  			FailureAction:   swarmtypes.UpdateFailureActionPause,
   127  			Monitor:         3 * time.Second,
   128  			MaxFailureRatio: 0.3,
   129  			Order:           swarmtypes.UpdateOrderStartFirst,
   130  		},
   131  	}
   132  }