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

     1  package network
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  	is "gotest.tools/v3/assert/cmp"
     8  )
     9  
    10  func TestNetworkWithInvalidIPAM(t *testing.T) {
    11  	testcases := []struct {
    12  		name           string
    13  		ipam           IPAM
    14  		ipv6           bool
    15  		expectedErrors []string
    16  	}{
    17  		{
    18  			name: "IP version mismatch",
    19  			ipam: IPAM{
    20  				Config: []IPAMConfig{{
    21  					Subnet:     "10.10.10.0/24",
    22  					IPRange:    "2001:db8::/32",
    23  					Gateway:    "2001:db8::1",
    24  					AuxAddress: map[string]string{"DefaultGatewayIPv4": "2001:db8::1"},
    25  				}},
    26  			},
    27  			expectedErrors: []string{
    28  				"invalid ip-range 2001:db8::/32: parent subnet is an IPv4 block",
    29  				"invalid gateway 2001:db8::1: parent subnet is an IPv4 block",
    30  				"invalid auxiliary address DefaultGatewayIPv4: parent subnet is an IPv4 block",
    31  			},
    32  		},
    33  		{
    34  			// Regression test for https://github.com/moby/moby/issues/47202
    35  			name: "IPv6 subnet is discarded with no error when IPv6 is disabled",
    36  			ipam: IPAM{Config: []IPAMConfig{{Subnet: "2001:db8::/32"}}},
    37  			ipv6: false,
    38  		},
    39  		{
    40  			name: "Invalid data - Subnet",
    41  			ipam: IPAM{Config: []IPAMConfig{{Subnet: "foobar"}}},
    42  			expectedErrors: []string{
    43  				`invalid subnet foobar: invalid CIDR block notation`,
    44  			},
    45  		},
    46  		{
    47  			name: "Invalid data",
    48  			ipam: IPAM{
    49  				Config: []IPAMConfig{{
    50  					Subnet:     "10.10.10.0/24",
    51  					IPRange:    "foobar",
    52  					Gateway:    "1001.10.5.3",
    53  					AuxAddress: map[string]string{"DefaultGatewayIPv4": "dummy"},
    54  				}},
    55  			},
    56  			expectedErrors: []string{
    57  				"invalid ip-range foobar: invalid CIDR block notation",
    58  				"invalid gateway 1001.10.5.3: invalid address",
    59  				"invalid auxiliary address DefaultGatewayIPv4: invalid address",
    60  			},
    61  		},
    62  		{
    63  			name: "IPRange bigger than its subnet",
    64  			ipam: IPAM{
    65  				Config: []IPAMConfig{
    66  					{Subnet: "10.10.10.0/24", IPRange: "10.0.0.0/8"},
    67  				},
    68  			},
    69  			expectedErrors: []string{
    70  				"invalid ip-range 10.0.0.0/8: CIDR block is bigger than its parent subnet 10.10.10.0/24",
    71  			},
    72  		},
    73  		{
    74  			name: "Out of range prefix & addresses",
    75  			ipam: IPAM{
    76  				Config: []IPAMConfig{{
    77  					Subnet:     "10.0.0.0/8",
    78  					IPRange:    "192.168.0.1/24",
    79  					Gateway:    "192.168.0.1",
    80  					AuxAddress: map[string]string{"DefaultGatewayIPv4": "192.168.0.1"},
    81  				}},
    82  			},
    83  			expectedErrors: []string{
    84  				"invalid ip-range 192.168.0.1/24: it should be 192.168.0.0/24",
    85  				"invalid ip-range 192.168.0.1/24: parent subnet 10.0.0.0/8 doesn't contain ip-range",
    86  				"invalid gateway 192.168.0.1: parent subnet 10.0.0.0/8 doesn't contain this address",
    87  				"invalid auxiliary address DefaultGatewayIPv4: parent subnet 10.0.0.0/8 doesn't contain this address",
    88  			},
    89  		},
    90  		{
    91  			name: "Subnet with host fragment set",
    92  			ipam: IPAM{
    93  				Config: []IPAMConfig{{
    94  					Subnet: "10.10.10.0/8",
    95  				}},
    96  			},
    97  			expectedErrors: []string{"invalid subnet 10.10.10.0/8: it should be 10.0.0.0/8"},
    98  		},
    99  		{
   100  			name: "IPRange with host fragment set",
   101  			ipam: IPAM{
   102  				Config: []IPAMConfig{{
   103  					Subnet:  "10.0.0.0/8",
   104  					IPRange: "10.10.10.0/16",
   105  				}},
   106  			},
   107  			expectedErrors: []string{"invalid ip-range 10.10.10.0/16: it should be 10.10.0.0/16"},
   108  		},
   109  		{
   110  			name: "Empty IPAM is valid",
   111  			ipam: IPAM{},
   112  		},
   113  		{
   114  			name: "Valid IPAM",
   115  			ipam: IPAM{
   116  				Config: []IPAMConfig{{
   117  					Subnet:     "10.0.0.0/8",
   118  					IPRange:    "10.10.0.0/16",
   119  					Gateway:    "10.10.0.1",
   120  					AuxAddress: map[string]string{"DefaultGatewayIPv4": "10.10.0.1"},
   121  				}},
   122  			},
   123  		},
   124  	}
   125  
   126  	for _, tc := range testcases {
   127  		tc := tc
   128  		t.Run(tc.name, func(t *testing.T) {
   129  			t.Parallel()
   130  
   131  			errs := ValidateIPAM(&tc.ipam, tc.ipv6)
   132  			if tc.expectedErrors == nil {
   133  				assert.NilError(t, errs)
   134  				return
   135  			}
   136  
   137  			assert.Check(t, is.ErrorContains(errs, "invalid network config"))
   138  			for _, expected := range tc.expectedErrors {
   139  				assert.Check(t, is.ErrorContains(errs, expected))
   140  			}
   141  		})
   142  	}
   143  }