Exemple #1
0
func TestUpdateFreeResources(t *testing.T) {
	r := func(cpu, mem, disk int) resource.ResourceTuple {
		return resource.ResourceTuple{cpu, mem, disk}
	}
	for i, tt := range []struct {
		total resource.ResourceTuple
		res   map[string]resource.ResourceTuple
		free  resource.ResourceTuple // before accounting for resource.HostResources
	}{
		{
			resource.ResourceTuple{},
			nil,
			resource.ResourceTuple{},
		},
		{
			r(1000, 4096, 1024),
			map[string]resource.ResourceTuple{"job1.service": r(750, 3072, 768)},
			r(250, 1024, 256),
		},
	} {
		ms := MachineState{TotalResources: tt.total}
		got := UpdateFreeResources(ms, tt.res)
		want := MachineState{
			TotalResources: tt.total,
			FreeResources:  resource.Sub(tt.free, resource.HostResources),
		}
		if !reflect.DeepEqual(got, want) {
			t.Errorf("case %d: got %v, want %v", i, got, want)
		}
	}
}
Exemple #2
0
// UpdateFreeResources populates the FreeResources of a MachineState, given a
// map of units to resource reservations, using the following formula:
// FreeResources = TotalResources - (sum(unit resource reservations) + HostResources)
func UpdateFreeResources(ms MachineState, reservations map[string]resource.ResourceTuple) MachineState {
	all := []resource.ResourceTuple{resource.HostResources}
	for _, res := range reservations {
		all = append(all, res)
	}
	reserved := resource.Sum(all...)
	// TODO(jonboulle): check for negatives!
	ms.FreeResources = resource.Sub(ms.TotalResources, reserved)
	return ms
}