

package e2e

import (
	"fmt"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"k8s.io/kubernetes/pkg/api"
)

var _ = Describe("Initial Resources", func() {
	f := NewFramework("initial-resources")

	It("[Skipped] should set initial resources based on historical data", func() {
		cpu := 100
		mem := 200
		for i := 0; i < 10; i++ {
			rc := NewStaticResourceConsumer(fmt.Sprintf("ir-%d", i), 1, cpu, mem, int64(2*cpu), int64(2*mem), f)
			defer rc.CleanUp()
		}
		// Wait some time to make sure usage data is gathered.
		time.Sleep(10 * time.Minute)

		pod := runPod(f, "ir-test", resourceConsumerImage)
		r := pod.Spec.Containers[0].Resources.Requests
		Expect(r.Cpu().MilliValue()).Should(BeNumerically("~", cpu, 10))
		Expect(r.Memory().Value()).Should(BeNumerically("~", mem*1024*1024, 20*1024*1024))
	})
})

func runPod(f *Framework, name, image string) *api.Pod {
	pod := &api.Pod{
		ObjectMeta: api.ObjectMeta{
			Name: name,
		},
		Spec: api.PodSpec{
			Containers: []api.Container{
				{
					Name:  name,
					Image: image,
				},
			},
		},
	}
	createdPod, err := f.Client.Pods(f.Namespace.Name).Create(pod)
	expectNoError(err)
	expectNoError(waitForPodRunningInNamespace(f.Client, name, f.Namespace.Name))
	return createdPod
}
