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

	"github.com/pivotal-cf-experimental/garden/linux_backend/network"
	"github.com/pivotal-cf-experimental/garden/linux_backend/network_pool"
)

var _ = Describe("Network Pool", func() {
	var pool *network_pool.RealNetworkPool

	BeforeEach(func() {
		_, ipNet, err := net.ParseCIDR("10.254.0.0/22")
		Expect(err).ToNot(HaveOccurred())

		pool = network_pool.New(ipNet)
	})

	Describe("acquiring", func() {
		It("takes the next network in the pool", func() {
			network1, err := pool.Acquire()
			Expect(err).ToNot(HaveOccurred())

			Expect(network1.String()).To(Equal("10.254.0.0/30"))

			network2, err := pool.Acquire()
			Expect(err).ToNot(HaveOccurred())

			Expect(network2.String()).To(Equal("10.254.0.4/30"))
		})
Esempio n. 2
0
func main() {
	flag.Parse()

	maxProcs := runtime.NumCPU()
	prevMaxProcs := runtime.GOMAXPROCS(maxProcs)

	log.Println("set GOMAXPROCS to", maxProcs, "was", prevMaxProcs)

	var backend backend.Backend

	switch *backendName {
	case "linux":
		if *rootPath == "" {
			log.Fatalln("must specify -root with linux backend")
		}

		if *depotPath == "" {
			log.Fatalln("must specify -depot with linux backend")
		}

		if *rootFSPath == "" {
			log.Fatalln("must specify -rootfs with linux backend")
		}

		uidPool := uid_pool.New(10000, 256)

		_, ipNet, err := net.ParseCIDR("10.254.0.0/22")
		if err != nil {
			log.Fatalln("error parsing CIDR:", err)
		}

		networkPool := network_pool.New(ipNet)

		// TODO: base on ephemeral port range
		portPool := port_pool.New(61000, 6501)

		var runner command_runner.CommandRunner

		runner = command_runner.New(*debug)

		quotaManager, err := quota_manager.New(*depotPath, *rootPath, runner)
		if err != nil {
			log.Fatalln("error creating quota manager:", err)
		}

		if *disableQuotas {
			quotaManager.Disable()
		}

		pool := container_pool.New(
			path.Join(*rootPath, "linux"),
			*depotPath,
			*rootFSPath,
			uidPool,
			networkPool,
			portPool,
			runner,
			quotaManager,
		)

		backend = linux_backend.New(pool, *snapshotsPath)
	case "fake":
		backend = fake_backend.New()
	}

	log.Println("setting up backend")

	err := backend.Setup()
	if err != nil {
		log.Fatalln("failed to set up backend:", err)
	}

	log.Println("starting server; listening with", *listenNetwork, "on", *listenAddr)

	graceTime := time.Duration(*containerGraceTime) * time.Second

	wardenServer := server.New(*listenNetwork, *listenAddr, graceTime, backend)

	err = wardenServer.Start()
	if err != nil {
		log.Fatalln("failed to start:", err)
	}

	signals := make(chan os.Signal, 1)

	go func() {
		<-signals
		log.Println("stopping...")
		wardenServer.Stop()
		os.Exit(0)
	}()

	signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)

	select {}
}
var _ = Describe("Linux containers", func() {
	BeforeEach(func() {
		fakeRunner = fake_command_runner.New()

		fakeCgroups = fake_cgroups_manager.New("/cgroups", "some-id")

		fakeQuotaManager = fake_quota_manager.New()
		fakeBandwidthManager = fake_bandwidth_manager.New()

		_, ipNet, err := net.ParseCIDR("10.254.0.0/24")
		Expect(err).ToNot(HaveOccurred())

		fakePortPool = fake_port_pool.New(1000)

		networkPool := network_pool.New(ipNet)

		network, err := networkPool.Acquire()
		Expect(err).ToNot(HaveOccurred())

		containerResources = linux_backend.NewResources(
			1234,
			network,
			[]uint32{},
		)

		container = linux_backend.NewLinuxContainer(
			"some-id",
			"some-handle",
			"/depot/some-id",
			1*time.Second,