Example #1
0
func TestDockerInitScheduler(t *testing.T) {
	tests := []struct {
		pullImages bool
	}{
		{
			pullImages: true,
		},
		{
			pullImages: false,
		},
	}

	for _, test := range tests {
		d := NewScheduler(test.pullImages, "unix:///var/run/docker.sock")
		log.Infof("Should I pull images? %v", test.pullImages)
		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			log.Infof("Received something %v", r)

			// TODO!! Check that we're receiving what we expect
		}))

		log.Infof("Test server at %s", server.URL)
		d.client, _ = docker.NewClient(server.URL)
		log.Debugf("Docker client %v", d.client)

		var task demand.Task
		task.Demand = 5
		task.Image = "microscaling/priority-1:latest"

		d.InitScheduler(&task)
		d.startTask(&task)
	}
}
Example #2
0
func TestDockerScheduler(t *testing.T) {
	d := NewScheduler(true, "unix:///var/run/docker.sock")
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	}))

	d.client, _ = docker.NewClient(server.URL)

	var task demand.Task
	task.Demand = 5
	task.Image = "microscaling/priority-1:latest"

	d.InitScheduler(&task)

	d.startTask(&task)
	// TODO! Some Docker tests that mock out the Docker client

	var tasks demand.Tasks
	tasks.Tasks = make([]*demand.Task, 1)
	tasks.Tasks = append(tasks.Tasks, &task)
	d.CountAllTasks(&tasks)
}
Example #3
0
func appsFromResponse(b []byte) (tasks []*demand.Task, maxContainers int, err error) {
	var appsMessage AppsMessage

	err = json.Unmarshal(b, &appsMessage)
	if err != nil {
		log.Debugf("Error unmarshalling from %s", string(b[:]))
	}

	maxContainers = appsMessage.MaxContainers

	for _, a := range appsMessage.Apps {
		task := demand.Task{
			Name:          a.Name,
			Image:         a.Config.Image,
			Command:       a.Config.Command,
			Priority:      a.Priority,
			MinContainers: a.MinContainers,
			MaxContainers: a.MaxContainers,
			MaxDelta:      (a.MaxContainers - a.MinContainers),
			IsScalable:    true,

			// TODO!! Settings that need to be made configurable via the API.
			// Default PublishAllPorts to true.
			PublishAllPorts: true,
			// Set Network mode to host only. This won't work for load balancer metrics.
			NetworkMode: "host",
		}

		switch a.RuleType {
		case "Queue":
			task.Target = target.NewQueueLengthTarget(a.Config.QueueLength)
			switch a.MetricType {
			case "AzureQueue":
				task.Metric = metric.NewAzureQueueMetric(a.Config.QueueName)
			case "NSQ":
				task.Metric = metric.NewNSQMetric(a.Config.TopicName, a.Config.ChannelName)
			default:
				log.Errorf("Unexpected queue metricType %s", a.MetricType)
			}
		default:
			task.Target = target.NewRemainderTarget(a.MaxContainers)
			task.Metric = metric.NewNullMetric()
		}

		tasks = append(tasks, &task)
	}

	if err != nil {
		log.Debugf("Apps message: %v", appsMessage)
	}

	return
}
Example #4
0
// stopStartTask updates the number of running tasks using the Marathon API.
func (m *MarathonScheduler) stopStartTask(task *demand.Task) (blocked bool, err error) {

	// Scale app using the Marathon REST API.
	status, err := updateApp(m.baseMarathonURL, task.Name, task.Demand)
	if err != nil {
		return blocked, err
	}

	switch status {
	case 200:
		// Update was successful
		task.Requested = task.Demand
	case 409:
		// Deployment is locked and we need to back off
		log.Debugf("Deployment locked")
		blocked = true
	default:
		err = fmt.Errorf("Error response code %d from Marathon API", status)
	}

	return blocked, err
}