Repository: moby/moby
File: libnetwork/drivers/bridge/network_linux_test.go (Go)

Code before:

func TestLinkCreateNoEnableIPv6(t *testing.T) {
	defer netnsutils.SetupTestOSContext(t)()
	d := newDriver(storeutils.NewTempStore(t))
	err := d.configure(nil)
	assert.NilError(t, err)

	option := map[string]interface{}{
		netlabel.GenericData: &networkConfiguration{
			BridgeName: DefaultBridgeName,
			EnableIPv4: true,
		},
	}

	ipdList := getIPv4Data(t)
	err = d.CreateNetwork("dummy", option, nil, ipdList, getIPv6Data(t))
	assert.NilError(t, err, "Failed to create bridge")

	te := newTestEndpoint(ipdList[0].Pool, 30)
	err = d.CreateEndpoint(context.Background(), "dummy", "ep", te.Interface(), nil)
	assert.NilError(t, err)

	if !assert.Check(t, is.Nil(te.iface.addrv6), "Expected IPv6 address to be nil when IPv6 is not enabled") {
		assert.Check(t, is.Nil(te.iface.addrv6.IP.To16()), "Expected IPv6 address to be nil when IPv6 is not enabled. got: %s", te.iface.addrv6.String())
	}
	assert.Check(t, is.Nil(te.gw6.To16()), "Expected GatewayIPv6 to be nil when IPv6 is not enabled. Got GatewayIPv6 = %s", te.gw6.String())
}

func TestLinkDelete(t *testing.T) {
	defer netnsutils.SetupTestOSContext(t)()
	d := newDriver(storeutils.NewTempStore(t))

	if err := d.configure(nil); err != nil {
		t.Fatalf("Failed to setup driver config: %v", err)
	}

	option := map[string]interface{}{
		netlabel.GenericData: &networkConfiguration{
			BridgeName: DefaultBridgeName,
			EnableIPv4: true,
			EnableIPv6: true,
		},
	}

	ipdList := getIPv4Data(t)
	err := d.CreateNetwork("dummy", option, nil, ipdList, getIPv6Data(t))
	if err != nil {
		t.Fatalf("Failed to create bridge: %v", err)
	}

	te := newTestEndpoint(ipdList[0].Pool, 30)

Review comment (suggestion):
This is doing what it did before, but I'm not sure why the `To16` stuff is needed ... `addrv6` and `gw6` should both just be nil if IPv6 is disabled. So it's fine as it is, or can be simplified to ...

```suggestion
	assert.Check(t, is.Nil(te.iface.addrv6), "Expected IPv6 address to be nil when IPv6 is not enabled, got %s", te.iface.addrv6)
	assert.Check(t, is.Nil(te.gw6), "Expected GatewayIPv6 to be nil when IPv6 is not enabled, got %s", te.gw6)
```

Code after:

func TestLinkCreateNoEnableIPv6(t *testing.T) {
	defer netnsutils.SetupTestOSContext(t)()
	d := newDriver(storeutils.NewTempStore(t))
	err := d.configure(nil)
	assert.NilError(t, err)

	option := map[string]interface{}{
		netlabel.GenericData: &networkConfiguration{
			BridgeName: DefaultBridgeName,
			EnableIPv4: true,
		},
	}

	ipdList := getIPv4Data(t)
	err = d.CreateNetwork("dummy", option, nil, ipdList, getIPv6Data(t))
	assert.NilError(t, err, "Failed to create bridge")

	te := newTestEndpoint(ipdList[0].Pool, 30)
	err = d.CreateEndpoint(context.Background(), "dummy", "ep", te.Interface(), nil)
	assert.NilError(t, err)

	assert.Check(t, is.Nil(te.iface.addrv6), "Expected IPv6 address to be nil when IPv6 is not enabled, got %s", te.iface.addrv6)
	assert.Check(t, is.Nil(te.gw6), "Expected GatewayIPv6 to be nil when IPv6 is not enabled, got %s", te.gw6)
}

func TestLinkDelete(t *testing.T) {
	defer netnsutils.SetupTestOSContext(t)()
	d := newDriver(storeutils.NewTempStore(t))
	err := d.configure(nil)
	assert.NilError(t, err)

	option := map[string]interface{}{
		netlabel.GenericData: &networkConfiguration{
			BridgeName: DefaultBridgeName,
			EnableIPv4: true,
			EnableIPv6: true,
		},
	}

	ipdList := getIPv4Data(t)
	err = d.CreateNetwork("dummy", option, nil, ipdList, getIPv6Data(t))
	assert.NilError(t, err, "Failed to create bridge")

	te := newTestEndpoint(ipdList[0].Pool, 30)
	err = d.CreateEndpoint(context.Background(), "dummy", "ep1", te.Interface(), nil)
	assert.NilError(t, err)

	err = d.DeleteEndpoint("dummy", "")
	assert.Check(t, is.ErrorType(err, errdefs.IsInvalidParameter))
	assert.Assert(t, is.Error(err, "invalid endpoint id: "))