inNamespace commands.InNamespace
	)

	BeforeEach(func() {
		context = &fakes.Context{}
		command = &fakes.Command{}
		command.StringReturns("some-command")

		namespace = &fakes.Namespace{}
		namespace.NameReturns("namespace-name")

		namespace.ExecuteStub = func(callback func(*os.File) error) error {
			Expect(command.ExecuteCallCount()).To(Equal(0))

			err := callback(nil)

			Expect(command.ExecuteCallCount()).To(Equal(1))
			return err
		}

		inNamespace = commands.InNamespace{
			Namespace: namespace,
			Command:   command,
		}
	})

	It("executes the command in the specified namespace", func() {
		err := inNamespace.Execute(context)
		Expect(err).NotTo(HaveOccurred())

		Expect(namespace.ExecuteCallCount()).To(Equal(1))
		dnsServer = &fakes.Runner{}
		dnsServerFactory.NewReturns(dnsServer)

		context = &fakes.Context{}
		context.ListenerFactoryReturns(listenerFactory)
		context.LinkFactoryReturns(linkFactory)
		context.AddressManagerReturns(addressManager)
		context.DNSServerFactoryReturns(dnsServerFactory)
		context.SandboxRepositoryReturns(sandboxRepo)
		sandboxRepo.GetReturns(sbox, nil)

		returnedListener = &net.UDPConn{}
		listenerFactory.ListenUDPReturns(returnedListener, nil)

		ns.ExecuteStub = func(callback func(*os.File) error) error {
			return callback(nil)
		}

		startDNS = commands.StartDNSServer{
			ListenAddress: "10.10.10.10:53",
			SandboxName:   "some-sandbox-name",
		}
	})

	It("gets the namespace from the sandbox", func() {
		err := startDNS.Execute(context)
		Expect(err).NotTo(HaveOccurred())

		Expect(sandboxRepo.GetCallCount()).To(Equal(1))
		Expect(sandboxRepo.GetArgsForCall(0)).To(Equal("some-sandbox-name"))
		targetNS      *fakes.Namespace
		mySubscriber  *subscriber.Subscriber
		neighChan     chan *watcher.Neigh
		doneChan      chan struct{}
		logger        *lagertest.TestLogger
	)

	BeforeEach(func() {
		fakeNetlinker = &nlfakes.Netlinker{}
		fakeSocket = &nlfakes.NLSocket{}
		neighChan = make(chan *watcher.Neigh, 100)
		doneChan = make(chan struct{})
		logger = lagertest.NewTestLogger("test")
		targetNS = &fakes.Namespace{}
		targetNS.ExecuteStub = func(callback func(*os.File) error) error {
			return callback(nil)
		}

		mySubscriber = &subscriber.Subscriber{
			Netlinker: fakeNetlinker,
			Logger:    logger,
		}

		fakeNetlinker.SubscribeReturns(fakeSocket, nil)
		fakeSocket.ReceiveReturns([]syscall.NetlinkMessage{{Data: []byte("something")}}, nil)
		fakeNetlinker.NeighDeserializeReturns(&netlink.Neigh{}, nil)
	})

	It("subscribes in the sandbox namespace", func() {
		targetNS.ExecuteStub = func(callback func(*os.File) error) error {
			defer GinkgoRecover()
	"github.com/onsi/gomega/gbytes"
)

var _ = Describe("NamespaceWriter", func() {
	var (
		nsWriter *executor.NamespaceWriter
		ns       *fakes.Namespace
		writer   *fakes.Writer
		logger   *lagertest.TestLogger
	)

	BeforeEach(func() {
		ns = &fakes.Namespace{}
		ns.MarshalJSONReturns([]byte(`{ "namespace": "my-namespace", "inode": "some-inode" }`), nil)
		ns.ExecuteStub = func(callback func(*os.File) error) error {
			return callback(nil)
		}

		logger = lagertest.NewTestLogger("test")

		writer = &fakes.Writer{}
		nsWriter = &executor.NamespaceWriter{
			Logger:    logger,
			Namespace: ns,
			Writer:    writer,
		}
	})

	It("writes to the wrapped writer in the associated namespace", func() {
		ns.ExecuteStub = func(callback func(*os.File) error) error {
			Expect(writer.WriteCallCount()).To(Equal(0))
		vxlanLink *netlink.Vxlan
		logger    *lagertest.TestLogger
	)

	BeforeEach(func() {
		ns = &fakes.Namespace{}
		netlinker = &nl_fakes.Netlinker{}
		logger = lagertest.NewTestLogger("test")
		vxlanLink = &netlink.Vxlan{
			LinkAttrs: netlink.LinkAttrs{
				Index: 9876,
			},
		}

		ns.ExecuteStub = func(callback func(ns *os.File) error) error {
			return callback(nil)
		}

		netlinker.LinkByNameStub = func(linkName string) (netlink.Link, error) {
			return vxlanLink, nil
		}

		inserter = &neigh.ARPInserter{
			Logger:    logger,
			Netlinker: netlinker,
		}
	})

	Describe("HandleResolvedNeighbors", func() {
		var (
			neigh    watcher.Neigh
		sandboxNS = &fakes.Namespace{}
		sandboxRepository.GetReturns(sandboxNS, nil)

		hostNS = &fakes.Namespace{}

		fakeWatcher = &fakes.MissWatcher{}

		startMonitor = commands.StartMonitor{
			HostNamespace: hostNS,
			Watcher:       fakeWatcher,
			SandboxName:   "some-sandbox",
			VxlanLinkName: "some-vxlan-name",
		}

		hostNS.ExecuteStub = func(callback func(_ *os.File) error) error {
			return callback(nil)
		}
	})

	Describe("Execute", func() {
		It("it calls StartMonitor inside the HostNamespace", func() {

			hostNS.ExecuteStub = func(callback func(_ *os.File) error) error {
				Expect(fakeWatcher.StartMonitorCallCount()).To(Equal(0))
				callback(nil)
				Expect(fakeWatcher.StartMonitorCallCount()).To(Equal(1))
				return nil
			}

			err := startMonitor.Execute(context)
			Expect(err).NotTo(HaveOccurred())
		ns.MarshalJSONReturns([]byte("{}"), nil)

		vxlanLinkName = "some-vxlan-name"
		resolver = &fakes.Resolver{}

		arpInserter = &fakes.ARPInserter{}
		arpInserter.HandleResolvedNeighborsStub = func(ready chan error, _ namespace.Namespace, _ string, _ <-chan watcher.Neighbor) {
			close(ready)
		}

		missWatcher = watcher.New(logger, sub, locker, resolver, arpInserter)

		ns.ExecuteStub = func(callback func(ns *os.File) error) error {
			err := callback(nil)
			if err != nil {
				return fmt.Errorf("callback failed: %s", err)
			}
			return nil
		}
		ns.NameReturns("some-namespace")
	})

	Describe("StartMonitor", func() {
		It("subscribes to sandbox l3 misses", func() {
			missWatcher.StartMonitor(ns, vxlanLinkName)

			Expect(sub.SubscribeCallCount()).To(Equal(1))

			targetNS, _, doneCh := sub.SubscribeArgsForCall(0)
			Expect(targetNS).To(Equal(ns))
			Consistently(doneCh).ShouldNot(BeClosed())
		sb          sandbox.Sandbox
		logger      *lagertest.TestLogger
		sbNamespace *fakes.Namespace
		invoker     *fakes.Invoker
		watcher     *fakes.MissWatcher
		linkFactory *fakes.LinkFactory
	)

	BeforeEach(func() {
		logger = lagertest.NewTestLogger("test")
		invoker = &fakes.Invoker{}
		watcher = &fakes.MissWatcher{}
		linkFactory = &fakes.LinkFactory{}
		sbNamespace = &fakes.Namespace{}
		sbNamespace.ExecuteStub = func(callback func(*os.File) error) error {
			return callback(nil)
		}

		sb = sandbox.New(logger, sbNamespace, invoker, linkFactory, watcher)
	})

	Describe("Setup", func() {
		It("brings up the loopback adapter in the sandbox namespace", func() {
			sbNamespace.ExecuteStub = func(callback func(*os.File) error) error {
				Expect(linkFactory.SetUpCallCount()).To(Equal(0))
				err := callback(nil)
				Expect(linkFactory.SetUpCallCount()).To(Equal(1))
				return err
			}

			err := sb.Setup()
			}
			return nil, sandbox.NotFoundError
		}

		context.SandboxRepositoryReturns(sandboxRepo)
		context.SandboxNamespaceRepositoryReturns(namespaceRepository)

		cleanupSandboxCommand = commands.CleanupSandbox{
			SandboxName:     "sandbox-name",
			VxlanDeviceName: "some-vxlan",
		}

		sandboxNS.ExecuteStub = func(callback func(ns *os.File) error) error {
			err := callback(nil)
			if err != nil {
				return fmt.Errorf("callback failed: %s", err)
			}
			return nil
		}
	})

	It("gets the sandbox by name", func() {
		err := cleanupSandboxCommand.Execute(context)
		Expect(err).NotTo(HaveOccurred())

		Expect(sandboxRepo.GetCallCount()).To(Equal(1))
		Expect(sandboxRepo.GetArgsForCall(0)).To(Equal("sandbox-name"))
	})

	Context("when sandbox doesn't exist", func() {
		It("returns without an error", func() {