github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/service/inspect_test.go (about) 1 package service 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/docker/cli/cli/command/formatter" 11 "github.com/docker/docker/api/types" 12 "github.com/docker/docker/api/types/container" 13 "github.com/docker/docker/api/types/swarm" 14 "gotest.tools/v3/assert" 15 is "gotest.tools/v3/assert/cmp" 16 "gotest.tools/v3/golden" 17 ) 18 19 func formatServiceInspect(t *testing.T, format formatter.Format, now time.Time) string { 20 b := new(bytes.Buffer) 21 22 endpointSpec := &swarm.EndpointSpec{ 23 Mode: "vip", 24 Ports: []swarm.PortConfig{ 25 { 26 Protocol: swarm.PortConfigProtocolTCP, 27 TargetPort: 5000, 28 }, 29 }, 30 } 31 32 two := uint64(2) 33 34 s := swarm.Service{ 35 ID: "de179gar9d0o7ltdybungplod", 36 Meta: swarm.Meta{ 37 Version: swarm.Version{Index: 315}, 38 CreatedAt: now, 39 UpdatedAt: now, 40 }, 41 Spec: swarm.ServiceSpec{ 42 Annotations: swarm.Annotations{ 43 Name: "my_service", 44 Labels: map[string]string{"com.label": "foo"}, 45 }, 46 TaskTemplate: swarm.TaskSpec{ 47 LogDriver: &swarm.Driver{ 48 Name: "driver", 49 Options: map[string]string{ 50 "max-file": "5", 51 }, 52 }, 53 ContainerSpec: &swarm.ContainerSpec{ 54 Image: "foo/bar@sha256:this_is_a_test", 55 Configs: []*swarm.ConfigReference{ 56 { 57 ConfigID: "mtc3i44r1awdoziy2iceg73z8", 58 ConfigName: "configtest.conf", 59 File: &swarm.ConfigReferenceFileTarget{ 60 Name: "/configtest.conf", 61 }, 62 }, 63 }, 64 Secrets: []*swarm.SecretReference{ 65 { 66 SecretID: "3hv39ehbbb4hdozo7spod9ftn", 67 SecretName: "secrettest.conf", 68 File: &swarm.SecretReferenceFileTarget{ 69 Name: "/secrettest.conf", 70 }, 71 }, 72 }, 73 74 Healthcheck: &container.HealthConfig{ 75 Test: []string{"CMD-SHELL", "curl"}, 76 Interval: 4, 77 Retries: 3, 78 StartPeriod: 2, 79 Timeout: 1, 80 }, 81 }, 82 Resources: &swarm.ResourceRequirements{ 83 Limits: &swarm.Limit{ 84 NanoCPUs: 100000000000, 85 MemoryBytes: 10490000, 86 Pids: 20, 87 }, 88 }, 89 Networks: []swarm.NetworkAttachmentConfig{ 90 { 91 Target: "5vpyomhb6ievnk0i0o60gcnei", 92 Aliases: []string{"web"}, 93 }, 94 }, 95 }, 96 Mode: swarm.ServiceMode{ 97 Replicated: &swarm.ReplicatedService{ 98 Replicas: &two, 99 }, 100 }, 101 EndpointSpec: endpointSpec, 102 }, 103 Endpoint: swarm.Endpoint{ 104 Spec: *endpointSpec, 105 Ports: []swarm.PortConfig{ 106 { 107 Protocol: swarm.PortConfigProtocolTCP, 108 TargetPort: 5000, 109 PublishedPort: 30000, 110 }, 111 }, 112 VirtualIPs: []swarm.EndpointVirtualIP{ 113 { 114 NetworkID: "6o4107cj2jx9tihgb0jyts6pj", 115 Addr: "10.255.0.4/16", 116 }, 117 }, 118 }, 119 UpdateStatus: &swarm.UpdateStatus{ 120 StartedAt: &now, 121 CompletedAt: &now, 122 }, 123 } 124 125 ctx := formatter.Context{ 126 Output: b, 127 Format: format, 128 } 129 130 err := InspectFormatWrite(ctx, []string{"de179gar9d0o7ltdybungplod"}, 131 func(ref string) (interface{}, []byte, error) { 132 return s, nil, nil 133 }, 134 func(ref string) (interface{}, []byte, error) { 135 return types.NetworkResource{ 136 ID: "5vpyomhb6ievnk0i0o60gcnei", 137 Name: "mynetwork", 138 }, nil, nil 139 }, 140 ) 141 if err != nil { 142 t.Fatal(err) 143 } 144 return b.String() 145 } 146 147 func TestPrettyPrint(t *testing.T) { 148 s := formatServiceInspect(t, NewFormat("pretty"), time.Now()) 149 golden.Assert(t, s, "service-inspect-pretty.golden") 150 } 151 152 func TestPrettyPrintWithNoUpdateConfig(t *testing.T) { 153 s := formatServiceInspect(t, NewFormat("pretty"), time.Now()) 154 if strings.Contains(s, "UpdateStatus") { 155 t.Fatal("Pretty print failed before parsing UpdateStatus") 156 } 157 if !strings.Contains(s, "mynetwork") { 158 t.Fatal("network name not found in inspect output") 159 } 160 } 161 162 func TestJSONFormatWithNoUpdateConfig(t *testing.T) { 163 now := time.Now() 164 // s1: [{"ID":..}] 165 // s2: {"ID":..} 166 s1 := formatServiceInspect(t, NewFormat(""), now) 167 s2 := formatServiceInspect(t, NewFormat("{{json .}}"), now) 168 var m1Wrap []map[string]interface{} 169 if err := json.Unmarshal([]byte(s1), &m1Wrap); err != nil { 170 t.Fatal(err) 171 } 172 if len(m1Wrap) != 1 { 173 t.Fatalf("strange s1=%s", s1) 174 } 175 m1 := m1Wrap[0] 176 var m2 map[string]interface{} 177 if err := json.Unmarshal([]byte(s2), &m2); err != nil { 178 t.Fatal(err) 179 } 180 assert.Check(t, is.DeepEqual(m1, m2)) 181 } 182 183 func TestPrettyPrintWithConfigsAndSecrets(t *testing.T) { 184 s := formatServiceInspect(t, NewFormat("pretty"), time.Now()) 185 assert.Check(t, is.Contains(s, "Log Driver:"), "Pretty print missing Log Driver") 186 assert.Check(t, is.Contains(s, "Configs:"), "Pretty print missing configs") 187 assert.Check(t, is.Contains(s, "Secrets:"), "Pretty print missing secrets") 188 assert.Check(t, is.Contains(s, "Healthcheck:"), "Pretty print missing healthcheck") 189 }