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

Code before:
			ctx := log.WithLogger(context.Background(), &log.Entry{Logger: logger})
			t.Cleanup(func() {
				if t.Failed() {
					t.Logf("Daemon logs:\n%s", sb.String())
				}
			})

			pbs, err := n.addPortMappings(ctx, tc.epAddrV4, tc.epAddrV6, tc.cfg, tc.defHostIP, tc.noProxy6To4)
			if tc.expErr != "" {
				assert.ErrorContains(t, err, tc.expErr)
				return
			}
			assert.NilError(t, err)
			for _, expLog := range tc.expLogs {
				assert.Check(t, is.Contains(sb.String(), expLog))
			}
			assert.Assert(t, is.Len(pbs, len(tc.expPBs)))

			fw, ok := n.driver.firewaller.(*firewaller.StubFirewaller)
			assert.Assert(t, ok)
			assert.Check(t, is.Equal(fw.Hairpin, tc.proxyPath == ""))
			assert.Check(t, fw.IPv4)
			assert.Check(t, fw.IPv6)

			fnw, ok := n.firewallerNetwork.(*firewaller.StubFirewallerNetwork)
			assert.Assert(t, ok)
			assert.Check(t, !fnw.Internal)
			assert.Check(t, !fnw.ICC)
			assert.Check(t, !fnw.Masquerade)

			if n.config.HostIPv4 == nil {
				assert.Check(t, !fnw.Config4.HostIP.IsValid())
			} else {
				assert.Check(t, is.Equal(fnw.Config4.HostIP.String(), n.config.HostIPv4.String()))
			}
			assert.Check(t, is.Equal(fnw.Config4.Routed, tc.gwMode4.routed()))
			assert.Check(t, !fnw.Config4.Unprotected)

			if n.config.HostIPv6 == nil {
				assert.Check(t, !fnw.Config6.HostIP.IsValid())
			} else {
				assert.Check(t, is.Equal(fnw.Config6.HostIP.String(), n.config.HostIPv6.String()))
			}
			assert.Check(t, is.Equal(fnw.Config6.Routed, tc.gwMode6.routed()))
			assert.Check(t, !fnw.Config6.Unprotected)

			assert.Check(t, is.Len(fnw.Ports, len(tc.expPBs)))
			for _, expPB := range tc.expPBs {
				expPBCopy := expPB
				expPBCopy.HostPortEnd = expPB.HostPort
				expPBCopy.HostIP = expChildIP(expPB.HostIP)

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

Code after:
			ctx := log.WithLogger(context.Background(), &log.Entry{Logger: logger})
			t.Cleanup(func() {
				if t.Failed() {
					t.Logf("Daemon logs:\n%s", sb.String())
				}
			})

			pbs, err := n.addPortMappings(ctx, tc.epAddrV4, tc.epAddrV6, tc.cfg, tc.defHostIP, tc.noProxy6To4)
			if tc.expErr != "" {
				assert.ErrorContains(t, err, tc.expErr)
				return
			}
			assert.NilError(t, err)
			for _, expLog := range tc.expLogs {
				assert.Check(t, is.Contains(sb.String(), expLog))
			}
			assert.Assert(t, is.Len(pbs, len(tc.expPBs)))

			fw := n.driver.firewaller.(*firewaller.StubFirewaller)
			assert.Check(t, is.Equal(fw.Hairpin, tc.proxyPath == ""))
			assert.Check(t, fw.IPv4)
			assert.Check(t, fw.IPv6)

			fnw := n.firewallerNetwork.(*firewaller.StubFirewallerNetwork)
			assert.Check(t, !fnw.Internal)
			assert.Check(t, !fnw.ICC)
			assert.Check(t, !fnw.Masquerade)

			if n.config.HostIPv4 == nil {
				assert.Check(t, !fnw.Config4.HostIP.IsValid())
			} else {
				assert.Check(t, is.Equal(fnw.Config4.HostIP.String(), n.config.HostIPv4.String()))
			}
			assert.Check(t, is.Equal(fnw.Config4.Routed, tc.gwMode4.routed()))
			assert.Check(t, !fnw.Config4.Unprotected)

			if n.config.HostIPv6 == nil {
				assert.Check(t, !fnw.Config6.HostIP.IsValid())
			} else {
				assert.Check(t, is.Equal(fnw.Config6.HostIP.String(), n.config.HostIPv6.String()))
			}
			assert.Check(t, is.Equal(fnw.Config6.Routed, tc.gwMode6.routed()))
			assert.Check(t, !fnw.Config6.Unprotected)

			assert.Check(t, is.Len(fnw.Ports, len(tc.expPBs)))
			for _, expPB := range tc.expPBs {
				expPBCopy := expPB
				expPBCopy.HostPortEnd = expPB.HostPort
				expPBCopy.HostIP = expChildIP(expPB.HostIP)
				assert.Check(t, fnw.PortExists(expPBCopy),
					"expected port mapping %v (%v)", expPBCopy, expChildIP(expPB.HostIP))