import (
	"errors"

	"github.com/cloudfoundry-incubator/garden-linux/layercake"
	"github.com/cloudfoundry-incubator/garden-linux/layercake/fake_cake"
	"github.com/cloudfoundry-incubator/garden-linux/layercake/fake_retainer"
	"github.com/docker/docker/image"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/pivotal-golang/lager/lagertest"
)

// Pended until #102455044 is delivered
var _ = PDescribe("Oven cleaner", func() {
	var gc *layercake.OvenCleaner
	var fakeCake *fake_cake.FakeCake
	var fakeRetainer *fake_retainer.FakeRetainer
	var child2parent map[layercake.ID]layercake.ID // child -> parent

	BeforeEach(func() {
		fakeCake = new(fake_cake.FakeCake)
		fakeRetainer = new(fake_retainer.FakeRetainer)
		gc = &layercake.OvenCleaner{
			Cake:     fakeCake,
			Retainer: fakeRetainer,
			Logger:   lagertest.NewTestLogger("test"),
		}

		child2parent = make(map[layercake.ID]layercake.ID)
		fakeCake.GetStub = func(id layercake.ID) (*image.Image, error) {
			if parent, ok := child2parent[id]; ok {
	})

	It("returns a different ID if the path changes", func() {
		Expect(idp.ProvideID(path1)).NotTo(Equal(idp.ProvideID(path2)))
	})

	It("returns a different ID if the modification time changes", func() {
		beforeID := idp.ProvideID(path1)
		Expect(os.Chtimes(path1, accessTime, modifiedTime.Add(time.Second*1))).To(Succeed())
		Expect(idp.ProvideID(path1)).NotTo(Equal(beforeID))
	})
})

var _ = Describe("Local", func() {
	var fetcher *repository_fetcher.Local
	var fakeCake *fake_cake.FakeCake
	var defaultRootFSPath string
	var logger lager.Logger

	BeforeEach(func() {
		logger = lagertest.NewTestLogger("local")
		fakeCake = new(fake_cake.FakeCake)
		defaultRootFSPath = ""

		// default to not containing an image
		fakeCake.GetReturns(nil, errors.New("no image"))
	})

	JustBeforeEach(func() {
		fetcher = &repository_fetcher.Local{
			Cake:              fakeCake,
	RootPath string
	Volume   string
}

func (f *FakeVolumeCreator) Create(path, v string) error {
	f.Created = append(f.Created, RootAndVolume{path, v})
	return f.CreateError
}

var _ = Describe("DockerRootFSProvider", func() {
	var (
		fakeRepositoryFetcher *fake_repository_fetcher.FakeRepositoryFetcher
		fakeCake              *fake_cake.FakeCake
		fakeNamespacer        *fake_namespacer.FakeNamespacer
		fakeVolumeCreator     *FakeVolumeCreator
		fakeClock             *fakeclock.FakeClock
		name                  string

		provider RootFSProvider

		logger *lagertest.TestLogger
	)

	BeforeEach(func() {
		fakeRepositoryFetcher = fake_repository_fetcher.New()
		fakeCake = new(fake_cake.FakeCake)
		fakeVolumeCreator = &FakeVolumeCreator{}
		fakeNamespacer = &fake_namespacer.FakeNamespacer{}
		fakeClock = fakeclock.NewFakeClock(time.Now())
		name = "some-name"

		var err error
	"github.com/pivotal-golang/lager/lagertest"

	"math"

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

var _ = Describe("RemoteV2", func() {
	var (
		fetcher      *RemoteV2Fetcher
		server       *ghttp.Server
		cake         *fake_cake.FakeCake
		lock         *fake_lock.FakeLock
		logger       *lagertest.TestLogger
		fetchRequest *FetchRequest
		retainer     *fake_retainer.FakeRetainer

		registryAddr string
	)

	BeforeEach(func() {
		cake = new(fake_cake.FakeCake)
		lock = new(fake_lock.FakeLock)

		logger = lagertest.NewTestLogger("test")

		server = ghttp.NewServer()
		server.RouteToHandler(
			"GET", "/v2/",
	"github.com/cloudfoundry-incubator/garden-linux/layercake/fake_cake"
	"github.com/cloudfoundry/gunk/command_runner/fake_command_runner"
	. "github.com/cloudfoundry/gunk/command_runner/fake_command_runner/matchers"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/pivotal-golang/lager/lagertest"
)

var _ = Describe("BtrfsCleaningCake", func() {
	var (
		cleaner              *layercake.BtrfsCleaningCake
		runner               *fake_command_runner.FakeCommandRunner
		fakeCake             *fake_cake.FakeCake
		listSubvolumesOutput string
		layerId              = layercake.DockerImageID("the-layer")
		btrfsMountPoint      = "/absolute/btrfs_mount"

		listSubVolumeErr error
		graphDriverErr   error

		removedDirectories []string
	)

	BeforeEach(func() {
		graphDriverErr = nil
		listSubVolumeErr = nil
		removedDirectories = []string{}

		runner = fake_command_runner.New()
		fakeCake = new(fake_cake.FakeCake)
		cleaner = &layercake.BtrfsCleaningCake{
	"github.com/docker/docker/runconfig"
	"github.com/pivotal-golang/lager/lagertest"

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

var _ = Describe("RemoteV1", func() {
	var (
		fetcher         *RemoteV1Fetcher
		server          *ghttp.Server
		endpoint1Server *ghttp.Server
		endpoint2Server *ghttp.Server
		cake            *fake_cake.FakeCake
		lock            *fake_lock.FakeLock
		logger          *lagertest.TestLogger
		fetchRequest    *FetchRequest
		retainer        *fake_retainer.FakeRetainer

		registryAddr string
	)

	BeforeEach(func() {
		cake = new(fake_cake.FakeCake)
		lock = new(fake_lock.FakeLock)
		logger = lagertest.NewTestLogger("test")
		server = ghttp.NewServer()

		server.RouteToHandler(
			"GET", "/v1/_ping", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {