github.com/moby/docker@v26.1.3+incompatible/api/types/container/hostconfig_unix_test.go (about)

     1  //go:build !windows
     2  
     3  package container
     4  
     5  import (
     6  	"testing"
     7  
     8  	"gotest.tools/v3/assert"
     9  	is "gotest.tools/v3/assert/cmp"
    10  )
    11  
    12  func TestCgroupnsMode(t *testing.T) {
    13  	modes := map[CgroupnsMode]struct{ valid, private, host, empty bool }{
    14  		"":                {valid: true, empty: true},
    15  		":":               {valid: false},
    16  		"something":       {valid: false},
    17  		"something:":      {valid: false},
    18  		"something:weird": {valid: false},
    19  		":weird":          {valid: false},
    20  		"host":            {valid: true, host: true},
    21  		"host:":           {valid: false},
    22  		"host:name":       {valid: false},
    23  		"private":         {valid: true, private: true},
    24  		"private:name":    {valid: false, private: false},
    25  	}
    26  	for mode, expected := range modes {
    27  		t.Run("mode="+string(mode), func(t *testing.T) {
    28  			assert.Check(t, is.Equal(mode.IsPrivate(), expected.private))
    29  			assert.Check(t, is.Equal(mode.IsHost(), expected.host))
    30  			assert.Check(t, is.Equal(mode.IsEmpty(), expected.empty))
    31  			assert.Check(t, is.Equal(mode.Valid(), expected.valid))
    32  		})
    33  	}
    34  }
    35  
    36  func TestCgroupSpec(t *testing.T) {
    37  	modes := map[CgroupSpec]struct {
    38  		valid     bool
    39  		private   bool
    40  		host      bool
    41  		container bool
    42  		shareable bool
    43  		ctrName   string
    44  	}{
    45  		"":                      {valid: true},
    46  		":":                     {valid: false},
    47  		"something":             {valid: false},
    48  		"something:":            {valid: false},
    49  		"something:weird":       {valid: false},
    50  		":weird":                {valid: false},
    51  		"container":             {valid: false},
    52  		"container:":            {valid: true, container: true, ctrName: ""},
    53  		"container:name":        {valid: true, container: true, ctrName: "name"},
    54  		"container:name1:name2": {valid: true, container: true, ctrName: "name1:name2"},
    55  	}
    56  
    57  	for mode, expected := range modes {
    58  		t.Run("mode="+string(mode), func(t *testing.T) {
    59  			assert.Check(t, is.Equal(mode.Valid(), expected.valid))
    60  			assert.Check(t, is.Equal(mode.IsContainer(), expected.container))
    61  			assert.Check(t, is.Equal(mode.Container(), expected.ctrName))
    62  		})
    63  	}
    64  }
    65  
    66  // TODO Windows: This will need addressing for a Windows daemon.
    67  func TestNetworkMode(t *testing.T) {
    68  	// TODO(thaJeztah): we should consider the cases with a colon (":") in the network name to be invalid.
    69  	modes := map[NetworkMode]struct {
    70  		private, bridge, host, container, none, isDefault bool
    71  		name, ctrName                                     string
    72  	}{
    73  		"":                      {private: true, name: ""},
    74  		":":                     {private: true, name: ":"},
    75  		"something":             {private: true, name: "something"},
    76  		"something:":            {private: true, name: "something:"},
    77  		"something:weird":       {private: true, name: "something:weird"},
    78  		":weird":                {private: true, name: ":weird"},
    79  		"bridge":                {private: true, bridge: true, name: "bridge"},
    80  		"host":                  {private: false, host: true, name: "host"},
    81  		"none":                  {private: true, none: true, name: "none"},
    82  		"default":               {private: true, isDefault: true, name: "default"},
    83  		"container":             {private: true, container: false, name: "container", ctrName: ""},
    84  		"container:":            {private: false, container: true, name: "container", ctrName: ""},
    85  		"container:name":        {private: false, container: true, name: "container", ctrName: "name"},
    86  		"container:name1:name2": {private: false, container: true, name: "container", ctrName: "name1:name2"},
    87  	}
    88  	for mode, expected := range modes {
    89  		t.Run("mode="+string(mode), func(t *testing.T) {
    90  			assert.Check(t, is.Equal(mode.IsPrivate(), expected.private))
    91  			assert.Check(t, is.Equal(mode.IsBridge(), expected.bridge))
    92  			assert.Check(t, is.Equal(mode.IsHost(), expected.host))
    93  			assert.Check(t, is.Equal(mode.IsContainer(), expected.container))
    94  			assert.Check(t, is.Equal(mode.IsNone(), expected.none))
    95  			assert.Check(t, is.Equal(mode.IsDefault(), expected.isDefault))
    96  			assert.Check(t, is.Equal(mode.NetworkName(), expected.name))
    97  			assert.Check(t, is.Equal(mode.ConnectedContainer(), expected.ctrName))
    98  		})
    99  	}
   100  }
   101  
   102  func TestIpcMode(t *testing.T) {
   103  	ipcModes := map[IpcMode]struct {
   104  		valid     bool
   105  		private   bool
   106  		host      bool
   107  		container bool
   108  		shareable bool
   109  		ctrName   string
   110  	}{
   111  		"":                      {valid: true},
   112  		":":                     {valid: false},
   113  		"something":             {valid: false},
   114  		"something:":            {valid: false},
   115  		"something:weird":       {valid: false},
   116  		":weird":                {valid: false},
   117  		"private":               {valid: true, private: true},
   118  		"host":                  {valid: true, host: true},
   119  		"host:":                 {valid: false},
   120  		"host:name":             {valid: false},
   121  		"container":             {valid: false},
   122  		"container:":            {valid: true, container: true, ctrName: ""},
   123  		"container:name":        {valid: true, container: true, ctrName: "name"},
   124  		"container:name1:name2": {valid: true, container: true, ctrName: "name1:name2"},
   125  		"shareable":             {valid: true, shareable: true},
   126  	}
   127  
   128  	for mode, expected := range ipcModes {
   129  		t.Run("mode="+string(mode), func(t *testing.T) {
   130  			assert.Check(t, is.Equal(mode.Valid(), expected.valid))
   131  			assert.Check(t, is.Equal(mode.IsPrivate(), expected.private))
   132  			assert.Check(t, is.Equal(mode.IsHost(), expected.host))
   133  			assert.Check(t, is.Equal(mode.IsContainer(), expected.container))
   134  			assert.Check(t, is.Equal(mode.IsShareable(), expected.shareable))
   135  			assert.Check(t, is.Equal(mode.Container(), expected.ctrName))
   136  		})
   137  	}
   138  }
   139  
   140  func TestUTSMode(t *testing.T) {
   141  	modes := map[UTSMode]struct{ valid, private, host bool }{
   142  		"":                {valid: true, private: true},
   143  		":":               {valid: false, private: true},
   144  		"something":       {valid: false, private: true},
   145  		"something:":      {valid: false, private: true},
   146  		"something:weird": {valid: false, private: true},
   147  		":weird":          {valid: false, private: true},
   148  		"host":            {valid: true, private: false, host: true},
   149  		"host:":           {valid: false, private: true},
   150  		"host:name":       {valid: false, private: true},
   151  	}
   152  	for mode, expected := range modes {
   153  		t.Run("mode="+string(mode), func(t *testing.T) {
   154  			assert.Check(t, is.Equal(mode.IsPrivate(), expected.private))
   155  			assert.Check(t, is.Equal(mode.IsHost(), expected.host))
   156  			assert.Check(t, is.Equal(mode.Valid(), expected.valid))
   157  		})
   158  	}
   159  }
   160  
   161  func TestUsernsMode(t *testing.T) {
   162  	modes := map[UsernsMode]struct{ valid, private, host bool }{
   163  		"":                {valid: true, private: true},
   164  		":":               {valid: false, private: true},
   165  		"something":       {valid: false, private: true},
   166  		"something:":      {valid: false, private: true},
   167  		"something:weird": {valid: false, private: true},
   168  		":weird":          {valid: false, private: true},
   169  		"host":            {valid: true, private: false, host: true},
   170  		"host:":           {valid: false, private: true},
   171  		"host:name":       {valid: false, private: true},
   172  	}
   173  	for mode, expected := range modes {
   174  		t.Run("mode="+string(mode), func(t *testing.T) {
   175  			assert.Check(t, is.Equal(mode.Valid(), expected.valid))
   176  			assert.Check(t, is.Equal(mode.IsPrivate(), expected.private))
   177  			assert.Check(t, is.Equal(mode.IsHost(), expected.host))
   178  		})
   179  	}
   180  }
   181  
   182  func TestPidMode(t *testing.T) {
   183  	modes := map[PidMode]struct {
   184  		valid     bool
   185  		private   bool
   186  		host      bool
   187  		container bool
   188  		ctrName   string
   189  	}{
   190  		"":                      {valid: true, private: true},
   191  		":":                     {valid: false, private: true},
   192  		"something":             {valid: false, private: true},
   193  		"something:":            {valid: false, private: true},
   194  		"something:weird":       {valid: false, private: true},
   195  		":weird":                {valid: false, private: true},
   196  		"host":                  {valid: true, private: false, host: true},
   197  		"host:":                 {valid: false, private: true},
   198  		"host:name":             {valid: false, private: true},
   199  		"container":             {valid: false, private: true},
   200  		"container:":            {valid: false, private: false, container: true, ctrName: ""},
   201  		"container:name":        {valid: true, private: false, container: true, ctrName: "name"},
   202  		"container:name1:name2": {valid: true, private: false, container: true, ctrName: "name1:name2"},
   203  	}
   204  	for mode, expected := range modes {
   205  		t.Run("mode="+string(mode), func(t *testing.T) {
   206  			assert.Check(t, is.Equal(mode.Valid(), expected.valid))
   207  			assert.Check(t, is.Equal(mode.IsPrivate(), expected.private))
   208  			assert.Check(t, is.Equal(mode.IsHost(), expected.host))
   209  			assert.Check(t, is.Equal(mode.IsContainer(), expected.container))
   210  			assert.Check(t, is.Equal(mode.Container(), expected.ctrName))
   211  		})
   212  	}
   213  }
   214  
   215  func TestRestartPolicy(t *testing.T) {
   216  	policies := map[RestartPolicy]struct{ none, always, onFailure bool }{
   217  		{Name: "", MaximumRetryCount: 0}:           {none: true, always: false, onFailure: false},
   218  		{Name: "something", MaximumRetryCount: 0}:  {none: false, always: false, onFailure: false},
   219  		{Name: "no", MaximumRetryCount: 0}:         {none: true, always: false, onFailure: false},
   220  		{Name: "always", MaximumRetryCount: 0}:     {none: false, always: true, onFailure: false},
   221  		{Name: "on-failure", MaximumRetryCount: 0}: {none: false, always: false, onFailure: true},
   222  	}
   223  	for policy, expected := range policies {
   224  		t.Run("policy="+string(policy.Name), func(t *testing.T) {
   225  			assert.Check(t, is.Equal(policy.IsNone(), expected.none))
   226  			assert.Check(t, is.Equal(policy.IsAlways(), expected.always))
   227  			assert.Check(t, is.Equal(policy.IsOnFailure(), expected.onFailure))
   228  		})
   229  	}
   230  }