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

Code before:
			assert.NilError(t, err)
			defer nsh.Close()
			nlh, err := nlwrap.NewHandleAt(nsh)
			assert.NilError(t, err)
			defer nlh.Close()

			netnsPath := ""
			if tc.netnsName != "" {
				netnsPath = "/var/run/netns/" + tc.netnsName
			}
			if tc.createNetns {
				res := icmd.RunCommand("ip", "netns", "add", tc.netnsName)
				assert.Assert(t, is.Equal(res.ExitCode, 0))
				defer icmd.RunCommand("ip", "netns", "del", tc.netnsName)
			}

			const hostIfName = "vethtesth"
			const containerIfName = "vethtestc"
			defer func() {
				// Just in case anything ends up in the host's netns, make sure it doesn't hang around ...
				icmd.RunCommand("ip", "link", "del", hostIfName)
				icmd.RunCommand("ip", "link", "del", containerIfName)
			}()

			iface := &testInterface{netnsPath: netnsPath}
			nlhCtr, err := createVeth(context.TODO(), hostIfName, containerIfName, iface, nlh)
			assert.Check(t, err)

			assert.Check(t, is.Equal(iface.createdInContainer, tc.expCreatedInContainer))
			if tc.expCreatedInContainer {
				assert.Check(t, nlhCtr != nil)
				res := icmd.RunCommand("ip", "netns", "exec", hostNsName, "ip", "link", "show", hostIfName)
				assert.Check(t, is.Equal(res.ExitCode, 0))
				res = icmd.RunCommand("ip", "netns", "exec", hostNsName, "ip", "link", "show", containerIfName)
				assert.Check(t, is.Equal(res.ExitCode, 1))
				res = icmd.RunCommand("ip", "netns", "exec", tc.netnsName, "ip", "link", "show", containerIfName)
				assert.Check(t, is.Equal(res.ExitCode, 0))
			} else {
				assert.Check(t, nlhCtr == nil)
				res := icmd.RunCommand("ip", "netns", "exec", hostNsName, "ip", "link", "show", hostIfName)
				assert.Check(t, is.Equal(res.ExitCode, 0))
				res = icmd.RunCommand("ip", "netns", "exec", hostNsName, "ip", "link", "show", containerIfName)
				assert.Check(t, is.Equal(res.ExitCode, 0))
			}
		})
	}
}

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


Review comment (suggestion):
There's no plan to plumb OTel in unit tests, so there no need to mark this context as a TODO:

```suggestion
			nlhCtr, err := createVeth(context.Background(), hostIfName, containerIfName, iface, nlh)
```

Code after:
			assert.NilError(t, err)
			defer nsh.Close()
			nlh, err := nlwrap.NewHandleAt(nsh)
			assert.NilError(t, err)
			defer nlh.Close()

			netnsPath := ""
			if tc.netnsName != "" {
				netnsPath = "/var/run/netns/" + tc.netnsName
			}
			if tc.createNetns {
				res := icmd.RunCommand("ip", "netns", "add", tc.netnsName)
				assert.Assert(t, is.Equal(res.ExitCode, 0))
				defer icmd.RunCommand("ip", "netns", "del", tc.netnsName)
			}

			const hostIfName = "vethtesth"
			const containerIfName = "vethtestc"
			defer func() {
				// Just in case anything ends up in the host's netns, make sure it doesn't hang around ...
				icmd.RunCommand("ip", "link", "del", hostIfName)
				icmd.RunCommand("ip", "link", "del", containerIfName)
			}()

			iface := &testInterface{netnsPath: netnsPath}
			nlhCtr, err := createVeth(context.Background(), hostIfName, containerIfName, iface, nlh)
			assert.Check(t, err)

			assert.Check(t, is.Equal(iface.createdInContainer, tc.expCreatedInContainer))
			if tc.expCreatedInContainer {
				assert.Check(t, nlhCtr != nil)
				res := icmd.RunCommand("ip", "netns", "exec", hostNsName, "ip", "link", "show", hostIfName)
				assert.Check(t, is.Equal(res.ExitCode, 0))
				res = icmd.RunCommand("ip", "netns", "exec", hostNsName, "ip", "link", "show", containerIfName)
				assert.Check(t, is.Equal(res.ExitCode, 1))
				res = icmd.RunCommand("ip", "netns", "exec", tc.netnsName, "ip", "link", "show", containerIfName)
				assert.Check(t, is.Equal(res.ExitCode, 0))
			} else {
				assert.Check(t, nlhCtr == nil)
				res := icmd.RunCommand("ip", "netns", "exec", hostNsName, "ip", "link", "show", hostIfName)
				assert.Check(t, is.Equal(res.ExitCode, 0))
				res = icmd.RunCommand("ip", "netns", "exec", hostNsName, "ip", "link", "show", containerIfName)
				assert.Check(t, is.Equal(res.ExitCode, 0))
			}
		})
	}
}

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