github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/daemon/cluster/executor/container/container_test.go (about)

     1  package container // import "github.com/Prakhar-Agarwal-byte/moby/daemon/cluster/executor/container"
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Prakhar-Agarwal-byte/moby/api/types/container"
     7  	swarmapi "github.com/moby/swarmkit/v2/api"
     8  	"gotest.tools/v3/assert"
     9  )
    10  
    11  func TestIsolationConversion(t *testing.T) {
    12  	cases := []struct {
    13  		name string
    14  		from swarmapi.ContainerSpec_Isolation
    15  		to   container.Isolation
    16  	}{
    17  		{name: "default", from: swarmapi.ContainerIsolationDefault, to: container.IsolationDefault},
    18  		{name: "process", from: swarmapi.ContainerIsolationProcess, to: container.IsolationProcess},
    19  		{name: "hyperv", from: swarmapi.ContainerIsolationHyperV, to: container.IsolationHyperV},
    20  	}
    21  	for _, c := range cases {
    22  		t.Run(c.name, func(t *testing.T) {
    23  			task := swarmapi.Task{
    24  				Spec: swarmapi.TaskSpec{
    25  					Runtime: &swarmapi.TaskSpec_Container{
    26  						Container: &swarmapi.ContainerSpec{
    27  							Image:     "alpine:latest",
    28  							Isolation: c.from,
    29  						},
    30  					},
    31  				},
    32  			}
    33  			config := containerConfig{task: &task}
    34  			// NOTE(dperny): you shouldn't ever pass nil outside of testing,
    35  			// because if there are CSI volumes, the code will panic. However,
    36  			// in testing. this is acceptable.
    37  			assert.Equal(t, c.to, config.hostConfig(nil).Isolation)
    38  		})
    39  	}
    40  }
    41  
    42  func TestContainerLabels(t *testing.T) {
    43  	c := &containerConfig{
    44  		task: &swarmapi.Task{
    45  			ID: "real-task.id",
    46  			Spec: swarmapi.TaskSpec{
    47  				Runtime: &swarmapi.TaskSpec_Container{
    48  					Container: &swarmapi.ContainerSpec{
    49  						Labels: map[string]string{
    50  							"com.docker.swarm.task":         "user-specified-task",
    51  							"com.docker.swarm.task.id":      "user-specified-task.id",
    52  							"com.docker.swarm.task.name":    "user-specified-task.name",
    53  							"com.docker.swarm.node.id":      "user-specified-node.id",
    54  							"com.docker.swarm.service.id":   "user-specified-service.id",
    55  							"com.docker.swarm.service.name": "user-specified-service.name",
    56  							"this-is-a-user-label":          "this is a user label's value",
    57  						},
    58  					},
    59  				},
    60  			},
    61  			ServiceID: "real-service.id",
    62  			Slot:      123,
    63  			NodeID:    "real-node.id",
    64  			Annotations: swarmapi.Annotations{
    65  				Name: "real-service.name.123.real-task.id",
    66  			},
    67  			ServiceAnnotations: swarmapi.Annotations{
    68  				Name: "real-service.name",
    69  			},
    70  		},
    71  	}
    72  
    73  	expected := map[string]string{
    74  		"com.docker.swarm.task":         "",
    75  		"com.docker.swarm.task.id":      "real-task.id",
    76  		"com.docker.swarm.task.name":    "real-service.name.123.real-task.id",
    77  		"com.docker.swarm.node.id":      "real-node.id",
    78  		"com.docker.swarm.service.id":   "real-service.id",
    79  		"com.docker.swarm.service.name": "real-service.name",
    80  		"this-is-a-user-label":          "this is a user label's value",
    81  	}
    82  
    83  	labels := c.labels()
    84  	assert.DeepEqual(t, expected, labels)
    85  }
    86  
    87  func TestCredentialSpecConversion(t *testing.T) {
    88  	cases := []struct {
    89  		name string
    90  		from swarmapi.Privileges_CredentialSpec
    91  		to   []string
    92  	}{
    93  		{
    94  			name: "none",
    95  			from: swarmapi.Privileges_CredentialSpec{},
    96  			to:   nil,
    97  		},
    98  		{
    99  			name: "config",
   100  			from: swarmapi.Privileges_CredentialSpec{
   101  				Source: &swarmapi.Privileges_CredentialSpec_Config{Config: "0bt9dmxjvjiqermk6xrop3ekq"},
   102  			},
   103  			to: []string{"credentialspec=config://0bt9dmxjvjiqermk6xrop3ekq"},
   104  		},
   105  		{
   106  			name: "file",
   107  			from: swarmapi.Privileges_CredentialSpec{
   108  				Source: &swarmapi.Privileges_CredentialSpec_File{File: "foo.json"},
   109  			},
   110  			to: []string{"credentialspec=file://foo.json"},
   111  		},
   112  		{
   113  			name: "registry",
   114  			from: swarmapi.Privileges_CredentialSpec{
   115  				Source: &swarmapi.Privileges_CredentialSpec_Registry{Registry: "testing"},
   116  			},
   117  			to: []string{"credentialspec=registry://testing"},
   118  		},
   119  	}
   120  	for _, c := range cases {
   121  		c := c
   122  		t.Run(c.name, func(t *testing.T) {
   123  			task := swarmapi.Task{
   124  				Spec: swarmapi.TaskSpec{
   125  					Runtime: &swarmapi.TaskSpec_Container{
   126  						Container: &swarmapi.ContainerSpec{
   127  							Privileges: &swarmapi.Privileges{
   128  								CredentialSpec: &c.from,
   129  							},
   130  						},
   131  					},
   132  				},
   133  			}
   134  			config := containerConfig{task: &task}
   135  			// NOTE(dperny): you shouldn't ever pass nil outside of testing,
   136  			// because if there are CSI volumes, the code will panic. However,
   137  			// in testing. this is acceptable.
   138  			assert.DeepEqual(t, c.to, config.hostConfig(nil).SecurityOpt)
   139  		})
   140  	}
   141  }