github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+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  )
     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  			Resources: &swarm.ResourceRequirements{
    47  				Limits: &swarm.Limit{
    48  					Pids: 300,
    49  				},
    50  			},
    51  		},
    52  	}
    53  
    54  	// first, does calling this with a later version correctly NOT strip
    55  	// fields? do the later version first, so we can reuse this spec in the
    56  	// next test.
    57  	adjustForAPIVersion("1.41", spec)
    58  	if !reflect.DeepEqual(spec.TaskTemplate.ContainerSpec.Sysctls, expectedSysctls) {
    59  		t.Error("Sysctls was stripped from spec")
    60  	}
    61  
    62  	if spec.TaskTemplate.Resources.Limits.Pids == 0 {
    63  		t.Error("PidsLimit was stripped from spec")
    64  	}
    65  	if spec.TaskTemplate.Resources.Limits.Pids != 300 {
    66  		t.Error("PidsLimit did not preserve the value from spec")
    67  	}
    68  
    69  	if spec.TaskTemplate.ContainerSpec.Privileges.CredentialSpec.Config != "someconfig" {
    70  		t.Error("CredentialSpec.Config field was stripped from spec")
    71  	}
    72  
    73  	if spec.TaskTemplate.ContainerSpec.Configs[1].Runtime == nil {
    74  		t.Error("ConfigReferenceRuntimeTarget was stripped from spec")
    75  	}
    76  
    77  	if spec.TaskTemplate.Placement.MaxReplicas != 222 {
    78  		t.Error("MaxReplicas was stripped from spec")
    79  	}
    80  
    81  	// next, does calling this with an earlier version correctly strip fields?
    82  	adjustForAPIVersion("1.29", spec)
    83  	if spec.TaskTemplate.ContainerSpec.Sysctls != nil {
    84  		t.Error("Sysctls was not stripped from spec")
    85  	}
    86  
    87  	if spec.TaskTemplate.Resources.Limits.Pids != 0 {
    88  		t.Error("PidsLimit was not stripped from spec")
    89  	}
    90  
    91  	if spec.TaskTemplate.ContainerSpec.Privileges.CredentialSpec.Config != "" {
    92  		t.Error("CredentialSpec.Config field was not stripped from spec")
    93  	}
    94  
    95  	if spec.TaskTemplate.ContainerSpec.Configs[1].Runtime != nil {
    96  		t.Error("ConfigReferenceRuntimeTarget was not stripped from spec")
    97  	}
    98  
    99  	if spec.TaskTemplate.Placement.MaxReplicas != 0 {
   100  		t.Error("MaxReplicas was not stripped from spec")
   101  	}
   102  
   103  }