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

Code before:
	addr2 := te2.iface.addr
	if addr2.IP.To4() == nil {
		t.Fatal("No Ipv4 address assigned to the endpoint:  ep2")
	}

	sbOptions = make(map[string]interface{})
	sbOptions[netlabel.GenericData] = options.Generic{
		"ChildEndpoints": []string{"ep1"},
	}

	err = d.Join(context.Background(), "net1", "ep2", "", te2, nil, sbOptions)
	if err != nil {
		t.Fatal("Failed to link ep1 and ep2")
	}

	err = d.ProgramExternalConnectivity(context.Background(), "net1", "ep2", sbOptions)
	if err != nil {
		t.Fatalf("Failed to program external connectivity: %v", err)
	}

	checkLink := func(expExists bool) {
		t.Helper()
		dnw, ok := d.networks["net1"]
		assert.Assert(t, ok)
		fnw, ok := dnw.firewallerNetwork.(*firewaller.StubFirewallerNetwork)
		assert.Assert(t, ok)
		parentAddr, ok := netip.AddrFromSlice(te2.iface.addr.IP)
		assert.Assert(t, ok)
		childAddr, ok := netip.AddrFromSlice(te1.iface.addr.IP)
		assert.Assert(t, ok)
		exists := fnw.LinkExists(parentAddr, childAddr, exposedPorts)
		assert.Check(t, is.Equal(exists, expExists))
	}
	checkLink(true)

	err = d.RevokeExternalConnectivity("net1", "ep2")
	if err != nil {
		t.Fatalf("Failed to revoke external connectivity: %v", err)
	}
	err = d.Leave("net1", "ep2")
	if err != nil {
		t.Fatal("Failed to unlink ep1 and ep2")
	}
	checkLink(false)

	// Error condition test with an invalid endpoint-id "ep4"
	sbOptions = make(map[string]interface{})
	sbOptions[netlabel.GenericData] = options.Generic{
		"ChildEndpoints": []string{"ep1", "ep4"},
	}


Review comment (suggestion):
```suggestion
		fnw := dnw.firewallerNetwork.(*firewaller.StubFirewallerNetwork)
```

Code after:
	addr2 := te2.iface.addr
	if addr2.IP.To4() == nil {
		t.Fatal("No Ipv4 address assigned to the endpoint:  ep2")
	}

	sbOptions = make(map[string]interface{})
	sbOptions[netlabel.GenericData] = options.Generic{
		"ChildEndpoints": []string{"ep1"},
	}

	err = d.Join(context.Background(), "net1", "ep2", "", te2, nil, sbOptions)
	if err != nil {
		t.Fatal("Failed to link ep1 and ep2")
	}

	err = d.ProgramExternalConnectivity(context.Background(), "net1", "ep2", sbOptions)
	if err != nil {
		t.Fatalf("Failed to program external connectivity: %v", err)
	}

	checkLink := func(expExists bool) {
		t.Helper()
		dnw, ok := d.networks["net1"]
		assert.Assert(t, ok)
		fnw := dnw.firewallerNetwork.(*firewaller.StubFirewallerNetwork)
		parentAddr, ok := netip.AddrFromSlice(te2.iface.addr.IP)
		assert.Assert(t, ok)
		childAddr, ok := netip.AddrFromSlice(te1.iface.addr.IP)
		assert.Assert(t, ok)
		exists := fnw.LinkExists(parentAddr, childAddr, exposedPorts)
		assert.Check(t, is.Equal(exists, expExists))
	}
	checkLink(true)

	err = d.RevokeExternalConnectivity("net1", "ep2")
	if err != nil {
		t.Fatalf("Failed to revoke external connectivity: %v", err)
	}
	err = d.Leave("net1", "ep2")
	if err != nil {
		t.Fatal("Failed to unlink ep1 and ep2")
	}
	checkLink(false)

	// Error condition test with an invalid endpoint-id "ep4"
	sbOptions = make(map[string]interface{})
	sbOptions[netlabel.GenericData] = options.Generic{
		"ChildEndpoints": []string{"ep1", "ep4"},
	}

	err = d.Join(context.Background(), "net1", "ep2", "", te2, nil, sbOptions)