github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/api/server/router/swarm/helpers_test.go (about)

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