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

Code before:
				ch <- fmt.Errorf("failed to create %s", name)
				return
			}
			if err := d.CreateNetwork(context.Background(), name, genericOption, nil, ipV4Data, nil); err == nil {
				ch <- fmt.Errorf("failed was able to create overlap %s", name)
				return
			}
			ch <- nil
		})
	}
	// wait for the go routines
	var success int
	for i := 0; i < 100; i++ {
		val := <-ch
		if val == nil {
			success++
		}
	}
	if success != 1 {
		t.Fatalf("Success should be 1 instead: %d", success)
	}
}

func useStubFirewaller() func() {
	origNewFirewaller := newFirewaller
	newFirewaller = func(_ context.Context, config firewaller.Config) (firewaller.Firewaller, error) {
		return firewaller.NewStubFirewaller(config), nil
	}
	return func() {
		newFirewaller = origNewFirewaller
	}
}

// Regression test for https://github.com/moby/moby/issues/46445
func TestSetupIP6TablesWithHostIPv4(t *testing.T) {
	defer netnsutils.SetupTestOSContext(t)()
	d := newDriver(storeutils.NewTempStore(t))
	dc := &configuration{
		EnableIPTables:  true,
		EnableIP6Tables: true,
	}
	if err := d.configure(map[string]interface{}{netlabel.GenericData: dc}); err != nil {
		t.Fatal(err)
	}
	nc := &networkConfiguration{
		BridgeName:         DefaultBridgeName,
		AddressIPv4:        &net.IPNet{IP: net.ParseIP("192.168.42.1"), Mask: net.CIDRMask(16, 32)},
		EnableIPMasquerade: true,
		EnableIPv4:         true,
		EnableIPv6:         true,
		AddressIPv6:        &net.IPNet{IP: net.ParseIP("2001:db8::1"), Mask: net.CIDRMask(64, 128)},

Review comment (suggestion):
Use `t.Cleanup()` to save on boilerplate at the call sites?
```suggestion
func useStubFirewaller(t testing.TB) {
	origNewFirewaller := newFirewaller
	newFirewaller = func(_ context.Context, config firewaller.Config) (firewaller.Firewaller, error) {
		return firewaller.NewStubFirewaller(config), nil
	}
	t.Cleanup(func() {
		newFirewaller = origNewFirewaller
	})
}
```

Code after:
				ch <- fmt.Errorf("failed was able to create overlap %s", name)
				return
			}
			ch <- nil
		})
	}
	// wait for the go routines
	var success int
	for i := 0; i < 100; i++ {
		val := <-ch
		if val == nil {
			success++
		}
	}
	if success != 1 {
		t.Fatalf("Success should be 1 instead: %d", success)
	}
}

func useStubFirewaller(t *testing.T) {
	origNewFirewaller := newFirewaller
	newFirewaller = func(_ context.Context, config firewaller.Config) (firewaller.Firewaller, error) {
		return firewaller.NewStubFirewaller(config), nil
	}
	t.Cleanup(func() { newFirewaller = origNewFirewaller })
}

// Regression test for https://github.com/moby/moby/issues/46445
func TestSetupIP6TablesWithHostIPv4(t *testing.T) {
	defer netnsutils.SetupTestOSContext(t)()
	d := newDriver(storeutils.NewTempStore(t))
	dc := &configuration{
		EnableIPTables:  true,
		EnableIP6Tables: true,
	}
	if err := d.configure(map[string]interface{}{netlabel.GenericData: dc}); err != nil {
		t.Fatal(err)
	}
	nc := &networkConfiguration{
		BridgeName:         DefaultBridgeName,
		AddressIPv4:        &net.IPNet{IP: net.ParseIP("192.168.42.1"), Mask: net.CIDRMask(16, 32)},
		EnableIPMasquerade: true,
		EnableIPv4:         true,
		EnableIPv6:         true,
		AddressIPv6:        &net.IPNet{IP: net.ParseIP("2001:db8::1"), Mask: net.CIDRMask(64, 128)},
		HostIPv4:           net.ParseIP("192.0.2.2"),
	}

	// Create test bridge.
	nh, err := nlwrap.NewHandle()
	if err != nil {