github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/opts/network_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  	is "gotest.tools/v3/assert/cmp"
     8  )
     9  
    10  func TestNetworkOptLegacySyntax(t *testing.T) {
    11  	testCases := []struct {
    12  		value    string
    13  		expected []NetworkAttachmentOpts
    14  	}{
    15  		{
    16  			value: "docknet1",
    17  			expected: []NetworkAttachmentOpts{
    18  				{
    19  					Target: "docknet1",
    20  				},
    21  			},
    22  		},
    23  	}
    24  	for _, tc := range testCases {
    25  		var network NetworkOpt
    26  		assert.NilError(t, network.Set(tc.value))
    27  		assert.Check(t, is.DeepEqual(tc.expected, network.Value()))
    28  	}
    29  }
    30  
    31  func TestNetworkOptAdvancedSyntax(t *testing.T) {
    32  	testCases := []struct {
    33  		value    string
    34  		expected []NetworkAttachmentOpts
    35  	}{
    36  		{
    37  			value: "name=docknet1,alias=web,driver-opt=field1=value1",
    38  			expected: []NetworkAttachmentOpts{
    39  				{
    40  					Target:  "docknet1",
    41  					Aliases: []string{"web"},
    42  					DriverOpts: map[string]string{
    43  						"field1": "value1",
    44  					},
    45  				},
    46  			},
    47  		},
    48  		{
    49  			value: "name=docknet1,alias=web1,alias=web2,driver-opt=field1=value1,driver-opt=field2=value2",
    50  			expected: []NetworkAttachmentOpts{
    51  				{
    52  					Target:  "docknet1",
    53  					Aliases: []string{"web1", "web2"},
    54  					DriverOpts: map[string]string{
    55  						"field1": "value1",
    56  						"field2": "value2",
    57  					},
    58  				},
    59  			},
    60  		},
    61  		{
    62  			value: "name=docknet1,ip=172.20.88.22,ip6=2001:db8::8822",
    63  			expected: []NetworkAttachmentOpts{
    64  				{
    65  					Target:      "docknet1",
    66  					Aliases:     []string{},
    67  					IPv4Address: "172.20.88.22",
    68  					IPv6Address: "2001:db8::8822",
    69  				},
    70  			},
    71  		},
    72  		{
    73  			value: "name=docknet1",
    74  			expected: []NetworkAttachmentOpts{
    75  				{
    76  					Target:  "docknet1",
    77  					Aliases: []string{},
    78  				},
    79  			},
    80  		},
    81  	}
    82  	for _, tc := range testCases {
    83  		tc := tc
    84  		t.Run(tc.value, func(t *testing.T) {
    85  			var network NetworkOpt
    86  			assert.NilError(t, network.Set(tc.value))
    87  			assert.Check(t, is.DeepEqual(tc.expected, network.Value()))
    88  		})
    89  	}
    90  }
    91  
    92  func TestNetworkOptAdvancedSyntaxInvalid(t *testing.T) {
    93  	testCases := []struct {
    94  		value         string
    95  		expectedError string
    96  	}{
    97  		{
    98  			value:         "invalidField=docknet1",
    99  			expectedError: "invalid field",
   100  		},
   101  		{
   102  			value:         "network=docknet1,invalid=web",
   103  			expectedError: "invalid field",
   104  		},
   105  		{
   106  			value:         "driver-opt=field1=value1,driver-opt=field2=value2",
   107  			expectedError: "network name/id is not specified",
   108  		},
   109  	}
   110  	for _, tc := range testCases {
   111  		tc := tc
   112  		t.Run(tc.value, func(t *testing.T) {
   113  			var network NetworkOpt
   114  			assert.ErrorContains(t, network.Set(tc.value), tc.expectedError)
   115  		})
   116  	}
   117  }