package commands_test

import (
	"errors"

	"github.com/cloudfoundry-incubator/ducati-daemon/executor/commands"
	"github.com/cloudfoundry-incubator/ducati-daemon/fakes"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("MoveLink", func() {
	var (
		context           *fakes.Context
		linkFactory       *fakes.LinkFactory
		sandboxRepository *fakes.SandboxRepository
		sbox              *fakes.Sandbox
		moveLink          commands.MoveLink
	)

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

		linkFactory = &fakes.LinkFactory{}
		context.LinkFactoryReturns(linkFactory)

		sandboxRepository = &fakes.SandboxRepository{}
		context.SandboxRepositoryReturns(sandboxRepository)

		sbox = &fakes.Sandbox{}
		sandboxRepository.GetReturns(sbox, nil)
	"github.com/cloudfoundry-incubator/ducati-daemon/executor/commands"
	"github.com/cloudfoundry-incubator/ducati-daemon/fakes"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Start DNS Server", func() {
	var (
		ns               *fakes.Namespace
		context          *fakes.Context
		listenerFactory  *fakes.ListenerFactory
		linkFactory      *fakes.LinkFactory
		addressManager   *fakes.AddressManager
		dnsServerFactory *fakes.DNSServerFactory
		returnedListener *net.UDPConn

		sandboxRepo *fakes.SandboxRepository
		sbox        *fakes.Sandbox
		dnsServer   *fakes.Runner

		startDNS commands.StartDNSServer
	)

	BeforeEach(func() {
		listenerFactory = &fakes.ListenerFactory{}
		linkFactory = &fakes.LinkFactory{}
		addressManager = &fakes.AddressManager{}
		dnsServerFactory = &fakes.DNSServerFactory{}

		ns = &fakes.Namespace{}
			Expect(locker.LockCallCount()).To(Equal(1))
			Expect(locker.UnlockCallCount()).To(Equal(1))
		})

		Context("when namespace repo fails to get sandbox", func() {
			It("returns an error", func() {
				namespaceRepo.GetReturns(nil, errors.New("potato"))

				err := sandboxRepo.Load(sboxNamespaceDir)
				Expect(err).To(MatchError(ContainSubstring("loading sandbox repo: potato")))
			})
		})
	})

	Describe("Create", func() {
		var fakeSandbox *fakes.Sandbox

		BeforeEach(func() {
			fakeSandbox = &fakes.Sandbox{}
			sandboxFactory.NewReturns(fakeSandbox)
		})

		It("returns the created sandbox", func() {
			sbox, err := sandboxRepo.Create("some-sandbox-name")
			Expect(err).NotTo(HaveOccurred())
			Expect(sbox).NotTo(BeNil())
		})

		It("logs entry and exit", func() {
			_, err := sandboxRepo.Create("some-sandbox-name")
			Expect(err).NotTo(HaveOccurred())
	"github.com/cloudfoundry-incubator/ducati-daemon/executor/commands"
	"github.com/cloudfoundry-incubator/ducati-daemon/fakes"
	"github.com/cloudfoundry-incubator/ducati-daemon/sandbox"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	"github.com/pivotal-golang/lager/lagertest"
)

var _ = Describe("CleanupSandbox", func() {
	var (
		context               *fakes.Context
		logger                *lagertest.TestLogger
		sbox                  *fakes.Sandbox
		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")
	"github.com/cloudfoundry-incubator/ducati-daemon/fakes"
	"github.com/cloudfoundry-incubator/ducati-daemon/models"
	"github.com/cloudfoundry-incubator/ducati-daemon/watcher"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Creator", func() {
	var (
		creator         container.Creator
		ex              *fakes.Executor
		containerMAC    net.HardwareAddr
		containerNS     *fakes.Namespace
		ipamResult      *types.Result
		config          container.CreatorConfig
		sandbox         *fakes.Sandbox
		sandboxRepo     *fakes.SandboxRepository
		sandboxNS       *fakes.Namespace
		missWatcher     watcher.MissWatcher
		commandBuilder  *fakes.CommandBuilder
		namespaceOpener *fakes.Opener
	)

	BeforeEach(func() {
		ex = &fakes.Executor{}
		sandbox = &fakes.Sandbox{}
		sandboxRepo = &fakes.SandboxRepository{}
		missWatcher = &fakes.MissWatcher{}
		commandBuilder = &fakes.CommandBuilder{}
		containerNS = &fakes.Namespace{NameStub: func() string { return "container ns sentinel" }}
		namespaceOpener = &fakes.Opener{}