示例#1
0
func (p *FakeNetworkPool) Acquire() (*network.Network, error) {
	if p.AcquireError != nil {
		return nil, p.AcquireError
	}

	_, ipNet, err := net.ParseCIDR(p.nextNetwork.String() + "/30")
	if err != nil {
		return nil, err
	}

	inc(p.nextNetwork)
	inc(p.nextNetwork)
	inc(p.nextNetwork)
	inc(p.nextNetwork)

	return network.New(ipNet), nil
}
示例#2
0
func New(ipNet *net.IPNet) *RealNetworkPool {
	pool := []*network.Network{}

	_, startNet, err := net.ParseCIDR(ipNet.IP.String() + "/30")
	if err != nil {
		panic(err)
	}

	for subnet := startNet; ipNet.Contains(subnet.IP); subnet = nextSubnet(subnet) {
		pool = append(pool, network.New(subnet))
	}

	return &RealNetworkPool{
		ipNet: ipNet,

		pool:            pool,
		poolMutex:       new(sync.Mutex),
		initialPoolSize: len(pool),
	}
}
示例#3
0
					_, err := pool.Acquire()
					Expect(err).ToNot(HaveOccurred())
				}

				_, err := pool.Acquire()
				Expect(err).To(HaveOccurred())
			})
		})
	})

	Describe("removing", func() {
		It("acquires a specific network from the pool", func() {
			_, ipNet, err := net.ParseCIDR("10.254.0.0/30")
			Expect(err).ToNot(HaveOccurred())

			err = pool.Remove(network.New(ipNet))
			Expect(err).ToNot(HaveOccurred())

			for i := 0; i < (256 - 1); i++ {
				network, err := pool.Acquire()
				Expect(err).ToNot(HaveOccurred())
				Expect(network.String()).ToNot(Equal("10.254.0.0/30"))
			}

			_, err = pool.Acquire()
			Expect(err).To(HaveOccurred())
		})

		Context("when the resource is already acquired", func() {
			It("returns a PortTakenError", func() {
				network, err := pool.Acquire()
示例#4
0
	})

	Describe("restoring", func() {
		var snapshot io.Reader

		var restoredNetwork *network.Network

		BeforeEach(func() {
			buf := new(bytes.Buffer)

			snapshot = buf

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

			restoredNetwork = network.New(ipNet)

			err = json.NewEncoder(buf).Encode(
				linux_backend.ContainerSnapshot{
					ID:     "some-restored-id",
					Handle: "some-restored-handle",

					GraceTime: 1 * time.Second,

					State: "some-restored-state",
					Events: []string{
						"some-restored-event",
						"some-other-restored-event",
					},

					Resources: linux_backend.ResourcesSnapshot{