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

Code before:

	netconfig := &networkConfiguration{BridgeName: "dummy0", DefaultBridge: true}
	genericOption := make(map[string]interface{})
	genericOption[netlabel.GenericData] = netconfig

	if err := d.CreateNetwork(context.Background(), "dummy", genericOption, nil, getIPv4Data(t), nil); err == nil {
		t.Fatal("Bridge creation was expected to fail")
	}
}

func TestCreateMultipleNetworks(t *testing.T) {
	defer netnsutils.SetupTestOSContext(t)()
	defer useStubFirewaller()()

	d := newDriver(storeutils.NewTempStore(t))

	checkFirewallerNetworks := func() {
		t.Helper()
		fw, ok := d.firewaller.(*firewaller.StubFirewaller)
		assert.Assert(t, ok)
		got := maps.Clone(fw.Networks)
		for _, wantNw := range d.networks {
			if gotNw, ok := got[wantNw.config.BridgeName]; ok {
				assert.Check(t, !gotNw.Deleted,
					"Rules for bridge %s (nid:%s) have been deleted", wantNw.config.BridgeName, wantNw.id)
			}
			delete(got, wantNw.config.BridgeName)
		}
		for gotNwId, gotNw := range got {
			assert.Check(t, gotNw.Deleted, "Rules for bridge %s have not been deleted", gotNwId)
		}
	}

	config := &configuration{
		EnableIPTables: true,
	}
	genericOption := make(map[string]interface{})
	genericOption[netlabel.GenericData] = config

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

	config1 := &networkConfiguration{BridgeName: "net_test_1", EnableIPv4: true}
	genericOption = make(map[string]interface{})
	genericOption[netlabel.GenericData] = config1
	if err := d.CreateNetwork(context.Background(), "1", genericOption, nil, getIPv4Data(t), nil); err != nil {
		t.Fatalf("Failed to create bridge: %v", err)
	}
	checkFirewallerNetworks()


Review comment (suggestion):
It strikes me as odd that the test asserts that:
- the rules for a network were added and not deleted, OR
- the rules for a network were never added in the first place.

Is that the correct expectation? It smells suspicious that the test would pass when `fw.Networks` is an empty map.

Code after:
	}

	netconfig := &networkConfiguration{BridgeName: "dummy0", DefaultBridge: true}
	genericOption := make(map[string]interface{})
	genericOption[netlabel.GenericData] = netconfig

	if err := d.CreateNetwork(context.Background(), "dummy", genericOption, nil, getIPv4Data(t), nil); err == nil {
		t.Fatal("Bridge creation was expected to fail")
	}
}

func TestCreateMultipleNetworks(t *testing.T) {
	defer netnsutils.SetupTestOSContext(t)()
	useStubFirewaller(t)

	d := newDriver(storeutils.NewTempStore(t))

	checkFirewallerNetworks := func() {
		t.Helper()
		fw := d.firewaller.(*firewaller.StubFirewaller)
		got := maps.Clone(fw.Networks)
		for _, wantNw := range d.networks {
			_, ok := got[wantNw.config.BridgeName]
			assert.Check(t, ok, "Rules for bridge %s (nid:%s) have been deleted", wantNw.config.BridgeName, wantNw.id)
			delete(got, wantNw.config.BridgeName)
		}
		assert.Check(t, is.Len(slices.Collect(maps.Keys(got)), 0), "Rules for bridges have not been deleted")
	}

	config := &configuration{
		EnableIPTables: true,
	}
	genericOption := make(map[string]interface{})
	genericOption[netlabel.GenericData] = config

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

	config1 := &networkConfiguration{BridgeName: "net_test_1", EnableIPv4: true}
	genericOption = make(map[string]interface{})
	genericOption[netlabel.GenericData] = config1
	if err := d.CreateNetwork(context.Background(), "1", genericOption, nil, getIPv4Data(t), nil); err != nil {
		t.Fatalf("Failed to create bridge: %v", err)
	}
	checkFirewallerNetworks()

	config2 := &networkConfiguration{BridgeName: "net_test_2", EnableIPv4: true}
	genericOption[netlabel.GenericData] = config2
	if err := d.CreateNetwork(context.Background(), "2", genericOption, nil, getIPv4Data(t), nil); err != nil {
		t.Fatalf("Failed to create bridge: %v", err)