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