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