github.com/kunnos/engine@v1.13.1/opts/port_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/docker/api/types/swarm"
     7  	"github.com/docker/docker/pkg/testutil/assert"
     8  )
     9  
    10  func TestPortOptValidSimpleSyntax(t *testing.T) {
    11  	testCases := []struct {
    12  		value    string
    13  		expected []swarm.PortConfig
    14  	}{
    15  		{
    16  			value: "80",
    17  			expected: []swarm.PortConfig{
    18  				{
    19  					Protocol:    "tcp",
    20  					TargetPort:  80,
    21  					PublishMode: swarm.PortConfigPublishModeIngress,
    22  				},
    23  			},
    24  		},
    25  		{
    26  			value: "80:8080",
    27  			expected: []swarm.PortConfig{
    28  				{
    29  					Protocol:      "tcp",
    30  					TargetPort:    8080,
    31  					PublishedPort: 80,
    32  					PublishMode:   swarm.PortConfigPublishModeIngress,
    33  				},
    34  			},
    35  		},
    36  		{
    37  			value: "8080:80/tcp",
    38  			expected: []swarm.PortConfig{
    39  				{
    40  					Protocol:      "tcp",
    41  					TargetPort:    80,
    42  					PublishedPort: 8080,
    43  					PublishMode:   swarm.PortConfigPublishModeIngress,
    44  				},
    45  			},
    46  		},
    47  		{
    48  			value: "80:8080/udp",
    49  			expected: []swarm.PortConfig{
    50  				{
    51  					Protocol:      "udp",
    52  					TargetPort:    8080,
    53  					PublishedPort: 80,
    54  					PublishMode:   swarm.PortConfigPublishModeIngress,
    55  				},
    56  			},
    57  		},
    58  		{
    59  			value: "80-81:8080-8081/tcp",
    60  			expected: []swarm.PortConfig{
    61  				{
    62  					Protocol:      "tcp",
    63  					TargetPort:    8080,
    64  					PublishedPort: 80,
    65  					PublishMode:   swarm.PortConfigPublishModeIngress,
    66  				},
    67  				{
    68  					Protocol:      "tcp",
    69  					TargetPort:    8081,
    70  					PublishedPort: 81,
    71  					PublishMode:   swarm.PortConfigPublishModeIngress,
    72  				},
    73  			},
    74  		},
    75  		{
    76  			value: "80-82:8080-8082/udp",
    77  			expected: []swarm.PortConfig{
    78  				{
    79  					Protocol:      "udp",
    80  					TargetPort:    8080,
    81  					PublishedPort: 80,
    82  					PublishMode:   swarm.PortConfigPublishModeIngress,
    83  				},
    84  				{
    85  					Protocol:      "udp",
    86  					TargetPort:    8081,
    87  					PublishedPort: 81,
    88  					PublishMode:   swarm.PortConfigPublishModeIngress,
    89  				},
    90  				{
    91  					Protocol:      "udp",
    92  					TargetPort:    8082,
    93  					PublishedPort: 82,
    94  					PublishMode:   swarm.PortConfigPublishModeIngress,
    95  				},
    96  			},
    97  		},
    98  	}
    99  	for _, tc := range testCases {
   100  		var port PortOpt
   101  		assert.NilError(t, port.Set(tc.value))
   102  		assert.Equal(t, len(port.Value()), len(tc.expected))
   103  		for _, expectedPortConfig := range tc.expected {
   104  			assertContains(t, port.Value(), expectedPortConfig)
   105  		}
   106  	}
   107  }
   108  
   109  func TestPortOptValidComplexSyntax(t *testing.T) {
   110  	testCases := []struct {
   111  		value    string
   112  		expected []swarm.PortConfig
   113  	}{
   114  		{
   115  			value: "target=80",
   116  			expected: []swarm.PortConfig{
   117  				{
   118  					TargetPort:  80,
   119  					Protocol:    "tcp",
   120  					PublishMode: swarm.PortConfigPublishModeIngress,
   121  				},
   122  			},
   123  		},
   124  		{
   125  			value: "target=80,protocol=tcp",
   126  			expected: []swarm.PortConfig{
   127  				{
   128  					Protocol:    "tcp",
   129  					TargetPort:  80,
   130  					PublishMode: swarm.PortConfigPublishModeIngress,
   131  				},
   132  			},
   133  		},
   134  		{
   135  			value: "target=80,published=8080,protocol=tcp",
   136  			expected: []swarm.PortConfig{
   137  				{
   138  					Protocol:      "tcp",
   139  					TargetPort:    80,
   140  					PublishedPort: 8080,
   141  					PublishMode:   swarm.PortConfigPublishModeIngress,
   142  				},
   143  			},
   144  		},
   145  		{
   146  			value: "published=80,target=8080,protocol=tcp",
   147  			expected: []swarm.PortConfig{
   148  				{
   149  					Protocol:      "tcp",
   150  					TargetPort:    8080,
   151  					PublishedPort: 80,
   152  					PublishMode:   swarm.PortConfigPublishModeIngress,
   153  				},
   154  			},
   155  		},
   156  		{
   157  			value: "target=80,published=8080,protocol=tcp,mode=host",
   158  			expected: []swarm.PortConfig{
   159  				{
   160  					Protocol:      "tcp",
   161  					TargetPort:    80,
   162  					PublishedPort: 8080,
   163  					PublishMode:   "host",
   164  				},
   165  			},
   166  		},
   167  		{
   168  			value: "target=80,published=8080,mode=host",
   169  			expected: []swarm.PortConfig{
   170  				{
   171  					TargetPort:    80,
   172  					PublishedPort: 8080,
   173  					PublishMode:   "host",
   174  					Protocol:      "tcp",
   175  				},
   176  			},
   177  		},
   178  		{
   179  			value: "target=80,published=8080,mode=ingress",
   180  			expected: []swarm.PortConfig{
   181  				{
   182  					TargetPort:    80,
   183  					PublishedPort: 8080,
   184  					PublishMode:   "ingress",
   185  					Protocol:      "tcp",
   186  				},
   187  			},
   188  		},
   189  	}
   190  	for _, tc := range testCases {
   191  		var port PortOpt
   192  		assert.NilError(t, port.Set(tc.value))
   193  		assert.Equal(t, len(port.Value()), len(tc.expected))
   194  		for _, expectedPortConfig := range tc.expected {
   195  			assertContains(t, port.Value(), expectedPortConfig)
   196  		}
   197  	}
   198  }
   199  
   200  func TestPortOptInvalidComplexSyntax(t *testing.T) {
   201  	testCases := []struct {
   202  		value         string
   203  		expectedError string
   204  	}{
   205  		{
   206  			value:         "invalid,target=80",
   207  			expectedError: "invalid field",
   208  		},
   209  		{
   210  			value:         "invalid=field",
   211  			expectedError: "invalid field",
   212  		},
   213  		{
   214  			value:         "protocol=invalid",
   215  			expectedError: "invalid protocol value",
   216  		},
   217  		{
   218  			value:         "target=invalid",
   219  			expectedError: "invalid syntax",
   220  		},
   221  		{
   222  			value:         "published=invalid",
   223  			expectedError: "invalid syntax",
   224  		},
   225  		{
   226  			value:         "mode=invalid",
   227  			expectedError: "invalid publish mode value",
   228  		},
   229  		{
   230  			value:         "published=8080,protocol=tcp,mode=ingress",
   231  			expectedError: "missing mandatory field",
   232  		},
   233  		{
   234  			value:         `target=80,protocol="tcp,mode=ingress"`,
   235  			expectedError: "non-quoted-field",
   236  		},
   237  		{
   238  			value:         `target=80,"protocol=tcp,mode=ingress"`,
   239  			expectedError: "invalid protocol value",
   240  		},
   241  	}
   242  	for _, tc := range testCases {
   243  		var port PortOpt
   244  		assert.Error(t, port.Set(tc.value), tc.expectedError)
   245  	}
   246  }
   247  
   248  func assertContains(t *testing.T, portConfigs []swarm.PortConfig, expected swarm.PortConfig) {
   249  	var contains = false
   250  	for _, portConfig := range portConfigs {
   251  		if portConfig == expected {
   252  			contains = true
   253  			break
   254  		}
   255  	}
   256  	if !contains {
   257  		t.Errorf("expected %v to contain %v, did not", portConfigs, expected)
   258  	}
   259  }