github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/api/server/router/swarm/helpers_test.go (about) 1 package swarm // import "github.com/docker/docker/api/server/router/swarm" 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/docker/docker/api/types/swarm" 8 "github.com/docker/go-units" 9 ) 10 11 func TestAdjustForAPIVersion(t *testing.T) { 12 var ( 13 expectedSysctls = map[string]string{"foo": "bar"} 14 ) 15 // testing the negative -- does this leave everything else alone? -- is 16 // prohibitively time-consuming to write, because it would need an object 17 // with literally every field filled in. 18 spec := &swarm.ServiceSpec{ 19 TaskTemplate: swarm.TaskSpec{ 20 ContainerSpec: &swarm.ContainerSpec{ 21 Sysctls: expectedSysctls, 22 Privileges: &swarm.Privileges{ 23 CredentialSpec: &swarm.CredentialSpec{ 24 Config: "someconfig", 25 }, 26 }, 27 Configs: []*swarm.ConfigReference{ 28 { 29 File: &swarm.ConfigReferenceFileTarget{ 30 Name: "foo", 31 UID: "bar", 32 GID: "baz", 33 }, 34 ConfigID: "configFile", 35 ConfigName: "configFile", 36 }, 37 { 38 Runtime: &swarm.ConfigReferenceRuntimeTarget{}, 39 ConfigID: "configRuntime", 40 ConfigName: "configRuntime", 41 }, 42 }, 43 Ulimits: []*units.Ulimit{ 44 { 45 Name: "nofile", 46 Soft: 100, 47 Hard: 200, 48 }, 49 }, 50 }, 51 Placement: &swarm.Placement{ 52 MaxReplicas: 222, 53 }, 54 Resources: &swarm.ResourceRequirements{ 55 Limits: &swarm.Limit{ 56 Pids: 300, 57 }, 58 }, 59 }, 60 } 61 62 // first, does calling this with a later version correctly NOT strip 63 // fields? do the later version first, so we can reuse this spec in the 64 // next test. 65 adjustForAPIVersion("1.41", spec) 66 if !reflect.DeepEqual(spec.TaskTemplate.ContainerSpec.Sysctls, expectedSysctls) { 67 t.Error("Sysctls was stripped from spec") 68 } 69 70 if spec.TaskTemplate.Resources.Limits.Pids == 0 { 71 t.Error("PidsLimit was stripped from spec") 72 } 73 if spec.TaskTemplate.Resources.Limits.Pids != 300 { 74 t.Error("PidsLimit did not preserve the value from spec") 75 } 76 77 if spec.TaskTemplate.ContainerSpec.Privileges.CredentialSpec.Config != "someconfig" { 78 t.Error("CredentialSpec.Config field was stripped from spec") 79 } 80 81 if spec.TaskTemplate.ContainerSpec.Configs[1].Runtime == nil { 82 t.Error("ConfigReferenceRuntimeTarget was stripped from spec") 83 } 84 85 if spec.TaskTemplate.Placement.MaxReplicas != 222 { 86 t.Error("MaxReplicas was stripped from spec") 87 } 88 89 if len(spec.TaskTemplate.ContainerSpec.Ulimits) == 0 { 90 t.Error("Ulimits were stripped from spec") 91 } 92 93 // next, does calling this with an earlier version correctly strip fields? 94 adjustForAPIVersion("1.29", spec) 95 if spec.TaskTemplate.ContainerSpec.Sysctls != nil { 96 t.Error("Sysctls was not stripped from spec") 97 } 98 99 if spec.TaskTemplate.Resources.Limits.Pids != 0 { 100 t.Error("PidsLimit was not stripped from spec") 101 } 102 103 if spec.TaskTemplate.ContainerSpec.Privileges.CredentialSpec.Config != "" { 104 t.Error("CredentialSpec.Config field was not stripped from spec") 105 } 106 107 if spec.TaskTemplate.ContainerSpec.Configs[1].Runtime != nil { 108 t.Error("ConfigReferenceRuntimeTarget was not stripped from spec") 109 } 110 111 if spec.TaskTemplate.Placement.MaxReplicas != 0 { 112 t.Error("MaxReplicas was not stripped from spec") 113 } 114 115 if len(spec.TaskTemplate.ContainerSpec.Ulimits) != 0 { 116 t.Error("Ulimits were not stripped from spec") 117 } 118 119 }