github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/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 ) 9 10 func TestAdjustForAPIVersion(t *testing.T) { 11 var ( 12 expectedSysctls = map[string]string{"foo": "bar"} 13 ) 14 // testing the negative -- does this leave everything else alone? -- is 15 // prohibitively time-consuming to write, because it would need an object 16 // with literally every field filled in. 17 spec := &swarm.ServiceSpec{ 18 TaskTemplate: swarm.TaskSpec{ 19 ContainerSpec: &swarm.ContainerSpec{ 20 Sysctls: expectedSysctls, 21 Privileges: &swarm.Privileges{ 22 CredentialSpec: &swarm.CredentialSpec{ 23 Config: "someconfig", 24 }, 25 }, 26 Configs: []*swarm.ConfigReference{ 27 { 28 File: &swarm.ConfigReferenceFileTarget{ 29 Name: "foo", 30 UID: "bar", 31 GID: "baz", 32 }, 33 ConfigID: "configFile", 34 ConfigName: "configFile", 35 }, 36 { 37 Runtime: &swarm.ConfigReferenceRuntimeTarget{}, 38 ConfigID: "configRuntime", 39 ConfigName: "configRuntime", 40 }, 41 }, 42 }, 43 Placement: &swarm.Placement{ 44 MaxReplicas: 222, 45 }, 46 }, 47 } 48 49 // first, does calling this with a later version correctly NOT strip 50 // fields? do the later version first, so we can reuse this spec in the 51 // next test. 52 adjustForAPIVersion("1.40", spec) 53 if !reflect.DeepEqual(spec.TaskTemplate.ContainerSpec.Sysctls, expectedSysctls) { 54 t.Error("Sysctls was stripped from spec") 55 } 56 57 if spec.TaskTemplate.ContainerSpec.Privileges.CredentialSpec.Config != "someconfig" { 58 t.Error("CredentialSpec.Config field was stripped from spec") 59 } 60 61 if spec.TaskTemplate.ContainerSpec.Configs[1].Runtime == nil { 62 t.Error("ConfigReferenceRuntimeTarget was stripped from spec") 63 } 64 65 if spec.TaskTemplate.Placement.MaxReplicas != 222 { 66 t.Error("MaxReplicas was stripped from spec") 67 } 68 69 // next, does calling this with an earlier version correctly strip fields? 70 adjustForAPIVersion("1.29", spec) 71 if spec.TaskTemplate.ContainerSpec.Sysctls != nil { 72 t.Error("Sysctls was not stripped from spec") 73 } 74 75 if spec.TaskTemplate.ContainerSpec.Privileges.CredentialSpec.Config != "" { 76 t.Error("CredentialSpec.Config field was not stripped from spec") 77 } 78 79 if spec.TaskTemplate.ContainerSpec.Configs[1].Runtime != nil { 80 t.Error("ConfigReferenceRuntimeTarget was not stripped from spec") 81 } 82 83 if spec.TaskTemplate.Placement.MaxReplicas != 0 { 84 t.Error("MaxReplicas was not stripped from spec") 85 } 86 87 }