var _ = Describe("ExecuteInNamespace", func() {
	var (
		context   *fakes.Context
		namespace *fakes.Namespace
		command   *fakes.Command

		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,
		}
	})
var _ = Describe("Reloader", func() {
	var (
		monitorReloader *reloader.Reloader
		watcher         *fakes.MissWatcher
		ns              *fakes.Namespace
	)

	BeforeEach(func() {
		watcher = &fakes.MissWatcher{}
		monitorReloader = &reloader.Reloader{
			Watcher: watcher,
		}
		ns = &fakes.Namespace{}

		ns.NameReturns("/some/sbox/path/vni-some-sandbox")
	})

	Describe("Callback", func() {
		It("restart the monitor for the given namespace", func() {
			err := monitorReloader.Callback(ns)
			Expect(err).NotTo(HaveOccurred())

			Expect(watcher.StartMonitorCallCount()).To(Equal(1))
			calledNS, vxlanDev := watcher.StartMonitorArgsForCall(0)
			Expect(calledNS).To(Equal(ns))
			Expect(vxlanDev).To(Equal("vxlansome-sandbox"))
		})

		Context("failure cases", func() {
			It("returns an error when the sandbox name is not valid", func() {
		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())
		})

		It("logs entry and exit", func() {
			missWatcher.StartMonitor(ns, vxlanLinkName)
							SandboxName:   "some-sandbox-name",
							ListenAddress: "some-dns-address",
						},
					),
				}))
		})
	})

	Describe("IdempotentlyCreateVxlan", func() {
		var sandboxNS *fakes.Namespace

		It("should return a command group that idempotently creates the vxlan device", func() {
			hostNamespace := &fakes.Namespace{NameStub: func() string { return "host namespace sentinel" }}

			sandboxNS = &fakes.Namespace{}
			sandboxNS.NameReturns("/sandbox/repo/some-sandbox-name")

			missWatcher := &fakes.MissWatcher{}
			b := container.CommandBuilder{
				HostNamespace: hostNamespace,
				MissWatcher:   missWatcher,
			}

			cmd := b.IdempotentlyCreateVxlan("some-vxlan-name", "some-sandbox-name", sandboxNS)

			Expect(cmd).To(Equal(
				commands.All(
					commands.InNamespace{
						Namespace: sandboxNS,
						Command: commands.SetLinkUp{
							LinkName: "some-vxlan-name",
		sandboxNS             *fakes.Namespace
		linkFactory           *fakes.LinkFactory
		cleanupSandboxCommand commands.CleanupSandbox
		missWatcher           *fakes.MissWatcher
		namespaceRepository   *fakes.Repository
		sandboxRepo           *fakes.SandboxRepository
	)

	BeforeEach(func() {
		context = &fakes.Context{}

		logger = lagertest.NewTestLogger("test")
		context.LoggerReturns(logger)

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

		sbox = &fakes.Sandbox{}
		sbox.NamespaceReturns(sandboxNS)

		linkFactory = &fakes.LinkFactory{}
		context.LinkFactoryReturns(linkFactory)
		missWatcher = &fakes.MissWatcher{}
		namespaceRepository = &fakes.Repository{}

		sandboxRepo = &fakes.SandboxRepository{}
		sandboxRepo.GetStub = func(key string) (sandbox.Sandbox, error) {
			if key == "sandbox-name" {
				return sbox, nil
			}
			return nil, sandbox.NotFoundError
			Invoker:        invoker,
			LinkFactory:    linkFactory,
			Watcher:        missWatcher,
			SandboxFactory: sandboxFactory,
			Sandboxes:      map[string]sandbox.Sandbox{},
		}
	})

	Describe("ForEach", func() {
		BeforeEach(func() {
			sbox, err := sandboxRepo.Create("some-sandbox-name")
			Expect(err).NotTo(HaveOccurred())
			Expect(sbox).NotTo(BeNil())

			sandboxCallback.CallbackReturns(nil)
			sboxNamespace.NameReturns("some-sandbox-name")
		})

		It("executes the callback for each sandbox in the repo", func() {
			err := sandboxRepo.ForEach(sandboxCallback)
			Expect(err).NotTo(HaveOccurred())

			Expect(sandboxCallback.CallbackCallCount()).To(Equal(1))
			ns := sandboxCallback.CallbackArgsForCall(0)
			Expect(ns).To(Equal(sboxNamespace))
		})

		It("locks and unlocks", func() {
			Expect(locker.LockCallCount()).To(Equal(1))
			Expect(locker.UnlockCallCount()).To(Equal(1))